generated from firleaf/devcontainer-alpine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.ts
36 lines (31 loc) · 858 Bytes
/
repl.ts
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
import { Temil, Implementation } from './lib/index';
const str: Implementation = (exec, ctx, args) => {
if (args.length !== 1) throw new Error();
return String(exec(args[0], ctx));
};
const num: Implementation = (exec, ctx, args) => {
if (args.length !== 1) throw new Error();
return Number(exec(args[0], ctx));
};
const add: Implementation = (exec, ctx, args) => {
if (args.length !== 2) throw new Error();
const a = exec(args[0], ctx);
const b = exec(args[1], ctx);
if (typeof a !== 'number') throw new Error();
if (typeof b !== 'number') throw new Error();
return a + b;
};
const ip = new Temil(
{
str,
num,
add,
},
[
[/^true$/, () => true],
[/^false$/, () => false],
[/^-?\d+$/, (v) => new Number(v).valueOf()],
],
);
console.write('>');
for await (const line of console) console.write(String(ip.eval(line, null)), '\n>');