Skip to content

Commit a5441c1

Browse files
author
Willem Wyndham
committed
Added Transformer class
1 parent 8729f06 commit a5441c1

File tree

15 files changed

+633
-674
lines changed

15 files changed

+633
-674
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ docs/
33
node_modules/
44
out/
55
raw/
6-
.history
6+
.history
7+
lib/visitor/dist/lib/
8+
9+
lib/visitor/dist/src/

cli/asc.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,27 @@ exports.main = function main(argv, options, callback) {
212212
// Set up transforms
213213
const transforms = [];
214214
if (args.transform) {
215-
args.transform.forEach(transform =>
216-
transforms.push(
217-
require(
218-
path.isAbsolute(transform = transform.trim())
219-
? transform
220-
: path.join(process.cwd(), transform)
221-
)
222-
)
223-
);
215+
args.transform.forEach(transform => {
216+
const transformer = require(path.isAbsolute(transform = transform.trim())
217+
? transform : path.join(process.cwd(), transform));
218+
debugger;
219+
if (transformer.default){
220+
transforms.push(transformer.default);
221+
} else {
222+
throw new Error("Transformer must have a default export.");
223+
}
224+
});
224225
}
225226
function applyTransform(name, ...args) {
226227
transforms.forEach(transform => {
227228
try {
228-
if (typeof transform[name] === "function") transform[name](...args);
229+
if (typeof transform === "function") {
230+
const transformer = new transform(...args);
231+
if (typeof transformer[name] !== "function") {
232+
throw new Error("Transformer missing " + name + " method.");
233+
}
234+
transformer[name]();
235+
}
229236
} catch (e) {
230237
callback(e);
231238
}
@@ -430,7 +437,7 @@ exports.main = function main(argv, options, callback) {
430437
}
431438

432439
// Call afterParse transform hook
433-
applyTransform("afterParse", parser, writeFile, baseDir);
440+
applyTransform("afterParse", parser, writeFile, baseDir, writeStdout);
434441

435442
// Parse additional files, if any
436443
{

lib/visitor/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
# Near Bindings
1+
# AST Transformer
22

3-
This AST transformer is added to the compiler fontend so that it is called automatically after all files have been parsed.
3+
After parsing the files imported by the entry files, the Abstract Syntax Tree (AST) is made. With the `--transform` argument, the compiler will load any js files provided and if the exports include a Transformer class.
44

5-
Any file that has `//@nearfile` as the first line, will be processed and a new source text will be generated.
5+
# Transformer class
66

7-
Currently any class defined in the file will have an `encode` and `decode` method for serializing to and from json. Furthermore, any functions that are exported by an entry function are wrapped so that their arguments are deserialized from storage and its return value is serailazed.
8-
9-
Furthermore `./src/preamble.ts` defines a string that can be added to the top of the source file. After each file is processed the new source text is parsed. This means any new imports added in the preamble will be parsed too.

lib/visitor/dist/ASTPrinter.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/visitor/dist/jsonbindings.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)