-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
67 lines (59 loc) · 1.3 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// To build hit up https://bundle-repl.amasad.repl.co/bundle/@amasad/pg-basic
const readline = require('readline');
const Basic = require('./basic');
const colors = {};
const keyQueue = ['a', 'b', 'c'];
const display = {
plot(x, y, color) {
console.log('plotting', x, y, color);
colors[`${x}${y}`] = color;
},
color(x, y) {
return colors[`${x}${y}`];
},
text(x, y, text, size, color) {
console.log(text, x, y, text, size, color);
},
clear() {
console.log('display cleared');
},
getChar() {
return keyQueue.pop();
},
};
const cnsle = {
write: s => process.stdout.write(s),
clear: () => console.log('console cleared'),
input: callback => {
setTimeout(() => callback('foo'), 1000);
},
};
const interp = new Basic({
console: cnsle,
display,
debugLevel: 99,
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function asyncInput(prompt) {
return new Promise(resolve => {
rl.question(prompt, resolve);
});
}
async function repl() {
while (true) {
let code = await asyncInput('pg-basic> ');
try {
await interp.run(code);
} catch (e) {
console.error(e.stack);
}
}
};
process.on('SIGINT', function() {
process.exit(1);
});
setTimeout(() => repl().catch(console.error), 0);
undefined;