Add a mechanism to hook into the compilation process with custom transforms, see #20

A custom transform is a node module that exports hooks called by the compiler on specific occasions, then being able to modify the intermediate results. Starting with 'afterParse' to modify the AST before compilation is performed.
This commit is contained in:
dcodeIO 2018-05-26 13:13:39 +02:00
parent 7ad13f9d65
commit 113925fa7e
5 changed files with 29 additions and 3 deletions

View File

@ -187,6 +187,26 @@ exports.main = function main(argv, options, callback) {
// Set up base directory
const baseDir = args.baseDir ? path.resolve(args.baseDir) : ".";
// Set up transforms
const transforms = [];
if (args.transform) {
if (typeof args.transform === "string") args.transform = args.transform.split(",");
args.transform.forEach(transform =>
transforms.push(
require(
path.isAbsolute(transform = transform.trim())
? transform
: path.join(process.cwd(), transform)
)
)
);
}
function applyTransform(name, ...args) {
transforms.forEach(transform => {
if (typeof transform[name] === "function") transform[name](...args);
});
}
// Begin parsing
var parser = null;
@ -340,6 +360,8 @@ exports.main = function main(argv, options, callback) {
}
}
applyTransform("afterParse", parser);
// Finish parsing
const program = assemblyscript.finishParsing(parser);

View File

@ -160,6 +160,10 @@
"type": "string",
"aliases": [ "feature" ]
},
"transform": {
"desc": "Specifies the path to a custom transform to 'require'.",
"type": "string"
},
"measure": {
"desc": "Prints measuring information on I/O and compile times.",
"type": "boolean"

2
dist/asc.js vendored

File diff suppressed because one or more lines are too long

2
dist/asc.js.map vendored

File diff suppressed because one or more lines are too long

2
std/assembly.d.ts vendored
View File

@ -267,7 +267,7 @@ declare function offsetof<T>(fieldName?: string): usize;
declare function changetype<T>(value: any): T;
/** Explicitly requests no bounds checks on the provided expression. Useful for array accesses. */
declare function unchecked<T>(value: T): T;
/** Creates a `call_indirect` instruction, calling the specified target by index with the specified arguments. */
/** Emits a `call_indirect` instruction, calling the specified function in the function table by index with the specified arguments. Does result in a runtime error if the arguments do not match the called function. */
declare function call_indirect<T>(target: Function | u32, ...args: any[]): T;
/** Tests if a 32-bit or 64-bit float is `NaN`. */
declare function isNaN<T = f32 | f64>(value: T): bool;