-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.js
39 lines (35 loc) · 794 Bytes
/
ast.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
function Node(type) {
return function _Node(nodes, attrs) {
if(!(Array.isArray(nodes) && nodes.length)) {
var msg = 'Node "'+type+'" needs an array of at least one subnode.';
throw new Error(msg);
}
return {
type: type,
nodes: nodes,
attrs: attrs,
};
}
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
var types = [
'assignment',
'exponentiation',
'multiplication',
'division',
'addition',
'subtraction',
'negation',
'modulo',
'function',
'functiondec',
'variable',
'number',
];
var ast = types.reduce(function(obj, type) {
obj[capitalize(type)] = Node(type);
return obj;
}, {});
module.exports = ast;