-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.js
60 lines (50 loc) · 1.37 KB
/
repl.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
var readline = require("readline");
var fs = require('fs');
var klisp = require("./klisp").klisp;
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: ">> "
});
var fileReader = function (input) {
console.log("Loading file " + input.substring(7, input.length-1) + "...");
// read file into an array containing each line
var lines = fs.readFileSync(input.substring(7, input.length-1), "utf8").split('\n');
console.log("Loaded file " + input.substring(7, input.length-1));
// iterate through each line and evaluate it
for (let i = 0; i < lines.length; i++) {
let out = klisp.output(klisp.parse(lines[i]));
handle_output(rl, out);
}
// if evaluating each line is successful, return T
return "T";
};
var handle_output = function (rl, out) {
if (out === "quit" || out === "QUIT") {
rl.close();
} else {
console.log(klisp.library.print(out, output = ""));
}
}
rl.prompt();
rl.on("line", (input) => {
klisp.setup();
switch(input.trim()) {
default:
if (input === "()") {
console.log("NIL");
break;
}
let out = undefined;
if (input.substring(1, 5) === 'load') {
out = fileReader(input);
} else {
out = klisp.output(klisp.parse(input));
}
handle_output(rl, out);
}
rl.prompt();
});
rl.on("close", function() {
process.exit(0);
});