-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
56 lines (40 loc) · 1.35 KB
/
main.ts
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
import Parser from "./frontend/parser.ts";
import Environment, { createGlobalEnv } from "./runtime/environment.ts";
import { evaluate } from "./runtime/interpreter.ts";
import { readFileSync } from "node:fs";
import { transcribe } from "./utils/trans.ts";
import process from 'node:process';
import * as readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';
const rl = readline.createInterface({ input, output });
const file = process.argv[2];
if(file) {
run(file);
} else {
repl();
}
async function run(filename: string) {
const parser = new Parser();
const env = createGlobalEnv();
let input = readFileSync(filename, 'utf-8');
if (filename.endsWith('.sc')) input = await transcribe(input);
const program = parser.produceAST(input);
const result = evaluate(program, env);
console.log(result);
process.exit();
}
async function repl() {
const parser = new Parser();
const env = createGlobalEnv();
console.log("Repl v1.0 (schizo)");
while (true) {
const input = await rl.question("> ");
// check for no user input or exit keyword.
if (!input || input == "exit()") {
process.exit(1);
}
const program = parser.produceAST(input);
const result = evaluate(program, env);
console.log(result);
}
}