-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.js
101 lines (100 loc) · 2.02 KB
/
lib.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// if (!module) module = {};
var lib = {
ChildrenToParent: function(parent, children) {
var self = this;
children.forEach(function(child) {
self.ChildToParent(parent, child);
});
return parent;
},
ChildToParent: function(parent, child) {
child.parent = parent;
return parent;
},
UnaryExpr: function(op, child) {
return this.ChildToParent({
type: "unary",
"class": "expression",
operator: op,
child: child,
children: [child]
}, child);
},
BinaryExpr: function(op, left, right) {
return this.ChildrenToParent({
type: "binary",
"class": "expression",
operator: op,
children: [left, right],
left: left,
right: right
},[left, right]);
},
Assignment: function(left, right) {
return this.ChildrenToParent({
type: left.type,
"class": "assignment",
left: left,
right: right,
children: [left, right]
}, [left, right]);
},
Variable: function(chain) {
return {
chain: chain,
base: chain[0],
rest: chain.slice(1, chain.length),
type: chain.length > 1 ? "fieldAccess" : "singleton",
"class": "variable"
}
},
String: function(text) {
return {
value: text.slice(1, text.length-1),
type: "string",
"class": "value"
}
},
HashEntry: function(key, value) {
return {
key: key,
value: value,
type: "object",
"class": "member"
}
},
Object: function(entries) {
return this.ChildrenToParent({
type: "object",
"class": "value",
children: entries
}, entries);
},
Constant: function(value) {
return {
value: parseInt(value),
type: "number",
"class": "value"
}
},
VariableDeclaration: function(assignment, isLocal) {
assignment.isLocal = isLocal;
return assignment;
},
Function: function(header, stmts) {
return this.ChildrenToParent({
name: header.name,
arguments: header.arguments,
children: stmts,
type: "function",
"class": "declaration"
}, stmts);
},
File: function(stmts) {
return this.ChildrenToParent({
children: stmts,
"class": "file",
type: "statementList"
}, stmts);
}
};