-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (50 loc) · 1.14 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
const wordRegistry = require('./src/WordRegistry.js');
const { KINDS, Token } = require('./src/Token.js');
const tokenize = require('./src/tokenize.js');
const parse = require('./src/parse.js');
const evaluate = require('./src/evaluate.js');
const standardWords = require('./src/standardWords.js');
standardWords(wordRegistry);
function run(code, scope = {}) {
let tokens;
let ast;
try {
tokens = tokenize(code);
} catch (err) {
throw new Error(`Failure to tokenize: ${err.message}
${err.stack}`);
}
try {
ast = parse(tokens);
} catch (err) {
throw new Error(`Failure to parse: ${err.message}
${err.stack}`);
}
try {
const r = evaluate(
new Token({
kind: KINDS.BLOCK,
value: ast,
line: 1,
pos: 1,
}),
scope,
);
return r;
} catch (err) {
throw err;
}
}
module.exports = {
wordRegistry,
Token,
KINDS,
tokenize,
parse,
evaluate,
run,
reset: () => {
wordRegistry.reset();
standardWords(wordRegistry);
},
};