-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathconsole.js
More file actions
61 lines (55 loc) · 1.27 KB
/
console.js
File metadata and controls
61 lines (55 loc) · 1.27 KB
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
import * as readline from 'readline/promises';
import chalk from 'chalk';
import { Parser, Interpreter, utils } from '@syuilo/aiscript';
const { valToString } = utils;
const i = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log(
`Welcome to AiScript!
https://github.com/syuilo/aiscript
Type 'exit' to end this session.`);
const getInterpreter = () => new Interpreter({}, {
in(q) {
return i.question(q + ': ');
},
out(value) {
if (value.type === 'str') {
console.log(chalk.magenta(value.value));
} else {
console.log(chalk.magenta(valToString(value)));
}
},
err(e) {
console.log(chalk.red(`${e}`));
interpreter = getInterpreter();
},
log(type, params) {
switch (type) {
case 'end': console.log(chalk.gray(`< ${valToString(params.val, true)}`)); break;
default: break;
}
}
});
let interpreter = getInterpreter();
async function main(){
let a = await i.question('> ');
if (a === 'exit') return false;
if (a === 'reset') {
interpreter.abort();
interpreter = getInterpreter();
return true;
}
try {
let ast = Parser.parse(a);
await interpreter.exec(ast);
} catch(e) {
console.log(chalk.red(`${e}`));
interpreter.abort();
interpreter = getInterpreter();
}
return true;
};
while (await main());
i.close();