-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·68 lines (56 loc) · 1.66 KB
/
index.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
#!/usr/bin/env node
let fs = require("fs");
let resolve = require("path").resolve
let { transpile } = require("berlin-lang/transpiler");
let { lex } = require("berlin-lang/lexer");
let core = require("berlin-lang/core");
let vm = require("vm");
let prompt = require("prompt-sync")({
sigint: true,
history: require('prompt-sync-history')()
});
let setTitle = title => {
process.stdout.write(
String.fromCharCode(27) + "]0;" + title + String.fromCharCode(7)
);
};
// The command was simply "berlin". We want to run
// the repl.
if (process.argv.length <= 2) {
setTitle("Berlin REPL");
const context = core;
vm.createContext(context);
console.log("\nWelcome to Berlin. Type exit to leave.\n")
let command = "";
while (true) {
let input = prompt(command.length ? "... " : "berlin > ");
if (input === "exit" && !command.length) {
prompt.history.save();
break;
}
command += "\n" + input;
if (!lex(command).unclosedDelimiters.length) {
try {
let result = vm.runInContext(transpile(command), context);
console.log(result);
} catch (e) {
console.log("Exception:", e);
} finally {
command = "";
}
}
}
// The command was invoked with a source and target path.
// We want to transpile.
} else {
setTitle("Transpiling Berlin...");
let [sourcePath, targetPath] = process.argv.slice(2);
let sourceCode = fs.readFileSync(sourcePath, { encoding: "utf8" });
let transpilation = transpile(sourceCode);
fs.writeFile(targetPath, transpilation, err => {
if (err) {
throw err;
}
console.log(`Transpilation successfully written to ${resolve(targetPath)}`);
});
}