-
Notifications
You must be signed in to change notification settings - Fork 4
/
koshur.js
55 lines (44 loc) · 1.49 KB
/
koshur.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
/**
* Koshur - A simple programming language inspired by the Kashmiri language (Koshur)
*
* @author Afaan Bilal
* @link https://afaan.dev
* @link https://github.com/AfaanBilal/koshur-lang
* @license MIT
*/
import fs from "fs";
import { TokenStream } from "./koshur/lexer.js";
import { parse } from "./koshur/parser.js";
import { InputStream } from "./koshur/input.js";
import { Environment, evaluate } from "./koshur/evaluator.js";
var gE = new Environment();
gE.def("time", function (func) {
try {
console.time("time");
return func();
} finally {
console.timeEnd("time");
}
});
gE.def("wan", (...v) => console.log(...v));
const file = process.argv[process.argv.length - 1];
const printAst = process.argv.includes("--print-ast");
const writeAst = process.argv.includes("--write-ast");
const fromAst = process.argv.includes("--from-ast");
if (!file.endsWith(".k") && !(file.endsWith(".ast") && fromAst)) {
console.error("Usage: node koshur.js [--print-ast] [--write-ast] [--from-ast] <filename>");
process.exit(1);
}
fs.readFile(file, "utf8", function (err, code) {
if (err) {
console.error("Could not open file: %s", err); process.exit(1);
}
let ast = fromAst ? JSON.parse(code) : parse(TokenStream(InputStream(code)));
if (printAst) {
console.log(JSON.stringify(ast, null, 4));
}
if (writeAst) {
fs.writeFileSync(file.replace(".k", ".ast"), JSON.stringify(ast, null, 4));
}
evaluate(ast, gE);
});