forked from aiscript-dev/aiscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.js
55 lines (49 loc) · 1.13 KB
/
console.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
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}`));
},
log(type, params) {
switch (type) {
case 'end': console.log(chalk.gray(`< ${valToString(params.val, true)}`)); break;
default: break;
}
}
});
let interpreter;
async function main(){
let a = await i.question('> ');
interpreter?.abort();
if (a === 'exit') return false;
try {
let ast = Parser.parse(a);
interpreter = getInterpreter();
await interpreter.exec(ast);
} catch(e) {
console.log(chalk.red(`${e}`));
}
return true;
};
while (await main());
i.close();