-
Notifications
You must be signed in to change notification settings - Fork 25
/
compile-assigner.js
207 lines (187 loc) · 7.91 KB
/
compile-assigner.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var compileEvaluator = require("./compile-evaluator");
var solve = require("./algebra");
var Scope = require("./scope");
var valueSyntax = {type: "value"};
var trueScope = {type: "literal", value: true};
var falseScope = {type: "literal", value: false};
module.exports = compile;
function compile(syntax) {
return compile.semantics.compile(syntax);
}
compile.semantics = {
compile: function (syntax) {
var compilers = this.compilers;
if (syntax.type === "equals") {
var assignLeft = this.compile(syntax.args[0]);
var evaluateRight = this.compileEvaluator(syntax.args[1]);
return compilers.equals(assignLeft, evaluateRight);
} else if (syntax.type === "if") {
var evaluateCondition = this.compileEvaluator(syntax.args[0]);
var assignConsequent = this.compile(syntax.args[1]);
var assignAlternate = this.compile(syntax.args[2]);
return compilers["if"](evaluateCondition, assignConsequent, assignAlternate);
} else if (syntax.type === "and" || syntax.type === "or") {
var leftArgs = solve(syntax.args[0], valueSyntax);
var rightArgs = solve(syntax.args[1], valueSyntax);
var evaluateLeft = this.compileEvaluator(syntax.args[0]);
var evaluateRight = this.compileEvaluator(syntax.args[1]);
var evaluateLeftAssign = this.compileEvaluator(leftArgs[1]);
var evaluateRightAssign = this.compileEvaluator(rightArgs[1]);
var assignLeft = this.compile(leftArgs[0]);
var assignRight = this.compile(rightArgs[0]);
return compilers[syntax.type](
assignLeft,
assignRight,
evaluateLeft,
evaluateRight,
evaluateLeftAssign,
evaluateRightAssign
);
} else if (syntax.type === "everyBlock") {
var evaluateCollection = this.compileEvaluator(syntax.args[0]);
var args = solve(syntax.args[1], {type: "literal", value: true});
var assignCondition = this.compile(args[0]);
var evaluateValue = this.compileEvaluator(args[1]);
return compilers["everyBlock"](evaluateCollection, assignCondition, evaluateValue);
} else if (syntax.type === "parent") {
var assignParent = this.compile(syntax.args[0]);
return function (value, scope) {
return assignParent(value, scope.parent);
};
} else if (compilers.hasOwnProperty(syntax.type)) {
var argEvaluators = syntax.args.map(this.compileEvaluator, this.compileEvaluator.semantics);
if(argEvaluators.length === 1) {
return compilers[syntax.type].call(null, argEvaluators[0]);
}
else if(argEvaluators.length === 2) {
return compilers[syntax.type].call(null, argEvaluators[0], argEvaluators[1]);
}
else {
return compilers[syntax.type].apply(null, argEvaluators);
}
} else {
throw new Error("Can't compile assigner for " + JSON.stringify(syntax.type));
}
},
compileEvaluator: compileEvaluator,
compilers: {
property: function (evaluateObject, evaluateKey) {
return function (value, scope) {
var object = evaluateObject(scope);
if (!object) return;
var key = evaluateKey(scope);
if (key == null) return;
if (Array.isArray(object)) {
object.set(key, value);
} else {
object[key] = value;
}
};
},
get: function (evaluateCollection, evaluateKey) {
return function (value, scope) {
var collection = evaluateCollection(scope);
if (!collection) return;
var key = evaluateKey(scope);
if (key == null) return;
collection.set(key, value);
};
},
has: function (evaluateCollection, evaluateValue) {
return function (has, scope) {
var collection = evaluateCollection(scope);
if (!collection) return;
var value = evaluateValue(scope);
if (has == null) return;
if (has) {
if (!(collection.has || collection.contains).call(collection, value)) {
collection.add(value);
}
} else {
if ((collection.has || collection.contains).call(collection, value)) {
(collection.remove || collection["delete"]).call(collection, value);
}
}
};
},
equals: function (assignLeft, evaluateRight) {
return function (value, scope) {
if (value) {
return assignLeft(evaluateRight(scope), scope);
}
};
},
"if": function (evaluateCondition, assignConsequent, assignAlternate) {
return function (value, scope) {
var condition = evaluateCondition(scope);
if (condition == null) return;
if (condition) {
return assignConsequent(value, scope);
} else {
return assignAlternate(value, scope);
}
};
},
and: function (assignLeft, assignRight, evaluateLeft, evaluateRight, evaluateLeftAssign, evaluateRightAssign) {
return function (value, scope) {
if (value == null) return;
if (value) {
assignLeft(evaluateLeftAssign(trueScope), scope);
assignRight(evaluateRightAssign(trueScope), scope);
} else {
assignLeft(evaluateLeft(scope) && !evaluateRight(scope), scope);
}
}
},
or: function (assignLeft, assignRight, evaluateLeft, evaluateRight, evaluateLeftAssign, evaluateRightAssign) {
return function (value, scope) {
if (value == null) return;
if (!value) {
assignLeft(evaluateLeftAssign(falseScope), scope);
assignRight(evaluateRightAssign(falseScope), scope);
} else {
assignLeft(evaluateLeft(scope) || !evaluateRight(scope), scope);
}
}
},
rangeContent: function (evaluateTarget) {
return function (value, scope) {
var target = evaluateTarget(scope);
if (!target) return;
if (!value) {
target.clear();
} else {
target.swap(0, target.length, value);
}
};
},
mapContent: function (evaluateTarget) {
return function (value, scope) {
var target = evaluateTarget(scope);
if (!target) return;
target.clear();
if (scope.value) {
target.addEach(value);
}
};
},
reversed: function (evaluateTarget) {
return function (value, scope) {
var target = evaluateTarget(scope);
if (!target) return;
target.swap(0, target.length, value.reversed());
};
},
everyBlock: function (evaluateCollection, assignCondition, evaluateEffect) {
return function (value, scope) {
if (value) {
var collection = evaluateCollection(scope);
var effect = evaluateEffect(scope);
collection.forEach(function (content) {
assignCondition(effect, scope.nest(content));
});
}
};
}
}
}