-
Notifications
You must be signed in to change notification settings - Fork 2
/
repl.js
41 lines (32 loc) · 834 Bytes
/
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
const GLisp = require('./lib');
const readline = require('readline');
const chalk = require('chalk');
const env = Object.create(GLisp.RootEnv);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
main();
function main() {
return read().then(evaluate).then(print, printErrors).then(main);
}
function read() {
return ask(chalk.bold('> '));
}
function evaluate(input) {
return GLisp.evaluate(env, GLisp.parse(input));
}
function print(output) {
console.log(chalk.gray('; ') + chalk.green(serialize(output)));
}
function printErrors(output) {
console.error(output);
}
function serialize(object) {
return object && object.toString && object.toString() || String(object);
}
function ask(question) {
return new Promise((fulfill) => {
rl.question(question, fulfill);
});
}