-
Notifications
You must be signed in to change notification settings - Fork 3
/
termlisp.js
45 lines (37 loc) · 1.3 KB
/
termlisp.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
const fs = require('node:fs');
const {formatExpression} = require('./interpreter/helpers')
const {execString, env } = require('./interpreter/interpreter')
const {typecheckString} = require('./typechecker/typechecker')
require('./tests')
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
const execInteractive = (program) => {
//let typingErrors = typecheckString(program, env())
//console.log(typingErrors.env.types)
let result = execString(program, env())
const repl = (env) => {
return prompt('> ')
.then((line) => {
env.history = []
const result = execString(line, env)
for (log of result.env.history) {
console.log(log)
}
if (typeof formatExpression(result) === 'string') {
console.log(formatExpression(result))
}
return result.env
})
.catch((err) => { console.log(err); return env})
.then(repl)}
return repl(result.env)
}
try {
const prelude = fs.readFileSync(__dirname + "/prelude.tls", 'utf8');
//const prelude = ''
const data = process.argv[2] ? fs.readFileSync( process.argv[2], 'utf8') : ''
execInteractive(prelude+data)
} catch (err) {
console.error(err);
}