-
Notifications
You must be signed in to change notification settings - Fork 18
/
bonsai-c.js
76 lines (64 loc) · 2.36 KB
/
bonsai-c.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var fs = require("fs");
var util = require('util');
var parser = require("./c_parser/parser").parser;
parser.yy = require("./c_parser/syntax_tree");
var abstractor = require("./abstractor/abstractor");
var asmJsExporter = require("./asm_js_exporter/asm_js_exporter");
var escodegen = require('escodegen');
var WasmModule = require("./wasm_exporter/wasm_module").WasmModule;
exports.compile = function(filename, outputFormat, stream) {
var cSource = fs.readFileSync(filename, "utf8");
var cTree = parser.parse(cSource);
var module = new abstractor.Module(cTree);
var wasmModule;
if (outputFormat == 'asmjs') {
var jsTree = asmJsExporter.compileModule(module);
return escodegen.generate(jsTree, {'verbatim': 'x-verbatim-property'});
} else if (outputFormat == 'wasm') {
wasmModule = WasmModule.fromAbstractModule(module);
return wasmModule.asBinary(stream);
} else if (outputFormat == 'wast') {
wasmModule = WasmModule.fromAbstractModule(module);
return wasmModule.asText();
} else {
throw util.format("Unrecognised output format: %s", outputFormat);
}
};
exports.main = function(argv) {
var outputFormat, wasmModule;
if (argv[2] == '--asmjs') {
outputFormat = 'asmjs';
} else if (argv[2] == '--wasm') {
outputFormat = 'wasm';
} else if (argv[2] == '--wast') {
outputFormat = 'wast';
} else {
throw "Output format must be specified (--asmjs, --wasm or --wast)";
}
var cSource = fs.readFileSync(argv[3], "utf8");
var cTree = parser.parse(cSource);
console.log(util.inspect(cTree, { depth: null }));
console.log("\n---------\n");
var module = new abstractor.Module(cTree);
console.log(util.inspect(module));
if (outputFormat == 'asmjs') {
console.log("\n---------\n");
var jsTree = asmJsExporter.compileModule(module);
console.log(util.inspect(jsTree, { depth: null }));
console.log("\n---------\n");
var out = escodegen.generate(jsTree, {'verbatim': 'x-verbatim-property'});
console.log(out);
} else if (outputFormat == 'wasm') {
wasmModule = WasmModule.fromAbstractModule(module);
var outStream = fs.createWriteStream(argv[4]);
wasmModule.asBinary(outStream);
outStream.end();
} else if (outputFormat == 'wast') {
console.log("\n---------\n");
wasmModule = WasmModule.fromAbstractModule(module);
console.log(wasmModule.asText());
}
};
if (typeof module !== 'undefined' && require.main === module) {
exports.main(process.argv);
}