forked from daynin/tiny-lisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (37 loc) · 803 Bytes
/
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
#! /usr/bin/env node
const fs = require('fs');
const rl = require("readline");
const prompts = rl.createInterface(process.stdin, process.stdout);
const fileName = process.argv[2];
const i = require('./lib/interpreter.js');
const repl = () => {
prompts.question('> ', code => {
try {
const result = i.exec(code);
if (result !== undefined) {
console.log(result);
}
} catch (err) {
console.error(err);
}
repl();
});
}
const execFile = () => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (!err) {
const result = i.exec(data);
if (result !== undefined) {
console.log(result);
}
prompts.close();
} else {
console.error(err.message);
}
});
}
if (fileName) {
execFile();
} else {
repl();
}