-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha-sharp.js
49 lines (40 loc) · 1.33 KB
/
a-sharp.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
const fs = require("fs");
const path = require("path");
const util = require("util");
const lexer = require("./lexer");
const parser = require("./parser");
const eval = require("./eval");
const generator = require("./generator");
const generateFiles = require("./util/generate-files");
const { argv } = process;
const relPath = argv[2];
const shouldEval = argv.includes("--eval");
const outDir = argv.includes("--out")
? argv[argv.findIndex(a => a === "--out") + 1]
: undefined;
const printTokens = argv.includes("--tokens");
const printAst = argv.includes("--ast");
const printSource = argv.includes("--source");
if (!relPath) {
console.error("No file specified");
process.exit(1);
}
const filePath = path.resolve(process.cwd(), relPath);
const cwd = path.dirname(filePath);
const source = fs.readFileSync(filePath, "utf8");
const print = (label, thing) =>
console.log(label, util.inspect(thing, false, null, true), "\n");
if (printSource) console.log("SOURCE") || console.log(source, "\n");
if (printTokens) print("TOKENS", lexer(source));
const ast = parser(source);
if (printAst) print("AST", ast);
if (shouldEval) {
eval(ast, { codeFrames: true, source, cwd });
} else {
if (outDir) {
generateFiles(ast, cwd, filePath, path.resolve(process.cwd(), outDir));
} else {
const { code } = generator(ast);
console.log(code);
}
}