yukigo 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.json +4 -0
- package/CHANGELOG.md +6 -0
- package/README.md +199 -0
- package/dist/analyzer/index.d.ts +71 -0
- package/dist/analyzer/index.js +110 -0
- package/dist/analyzer/inspections/functional.d.ts +46 -0
- package/dist/analyzer/inspections/functional.js +123 -0
- package/dist/analyzer/inspections/generic.d.ts +151 -0
- package/dist/analyzer/inspections/generic.js +427 -0
- package/dist/analyzer/inspections/imperative.d.ts +37 -0
- package/dist/analyzer/inspections/imperative.js +105 -0
- package/dist/analyzer/inspections/logic.d.ts +49 -0
- package/dist/analyzer/inspections/logic.js +140 -0
- package/dist/analyzer/inspections/object.d.ts +83 -0
- package/dist/analyzer/inspections/object.js +235 -0
- package/dist/analyzer/utils.d.ts +4 -0
- package/dist/analyzer/utils.js +16 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interpreter/components/EnvBuilder.d.ts +16 -0
- package/dist/interpreter/components/EnvBuilder.js +78 -0
- package/dist/interpreter/components/FunctionRuntime.d.ts +8 -0
- package/dist/interpreter/components/FunctionRuntime.js +52 -0
- package/dist/interpreter/components/LazyRuntime.d.ts +7 -0
- package/dist/interpreter/components/LazyRuntime.js +75 -0
- package/dist/interpreter/components/LogicEngine.d.ts +21 -0
- package/dist/interpreter/components/LogicEngine.js +152 -0
- package/dist/interpreter/components/LogicResolver.d.ts +11 -0
- package/dist/interpreter/components/LogicResolver.js +87 -0
- package/dist/interpreter/components/Operations.d.ts +14 -0
- package/dist/interpreter/components/Operations.js +69 -0
- package/dist/interpreter/components/PatternMatcher.d.ts +41 -0
- package/dist/interpreter/components/PatternMatcher.js +206 -0
- package/dist/interpreter/components/Visitor.d.ts +64 -0
- package/dist/interpreter/components/Visitor.js +299 -0
- package/dist/interpreter/errors.d.ts +19 -0
- package/dist/interpreter/errors.js +36 -0
- package/dist/interpreter/index.d.ts +32 -0
- package/dist/interpreter/index.js +44 -0
- package/dist/interpreter/utils.d.ts +14 -0
- package/dist/interpreter/utils.js +57 -0
- package/dist/utils/helpers.d.ts +14 -0
- package/dist/utils/helpers.js +51 -0
- package/package.json +30 -0
- package/src/analyzer/index.ts +132 -0
- package/src/analyzer/inspections/functional.ts +159 -0
- package/src/analyzer/inspections/generic.ts +499 -0
- package/src/analyzer/inspections/imperative.ts +129 -0
- package/src/analyzer/inspections/logic.ts +166 -0
- package/src/analyzer/inspections/object.ts +282 -0
- package/src/analyzer/utils.ts +26 -0
- package/src/index.ts +3 -0
- package/src/interpreter/components/EnvBuilder.ts +97 -0
- package/src/interpreter/components/FunctionRuntime.ts +79 -0
- package/src/interpreter/components/LazyRuntime.ts +97 -0
- package/src/interpreter/components/LogicEngine.ts +227 -0
- package/src/interpreter/components/LogicResolver.ts +130 -0
- package/src/interpreter/components/Operations.ts +81 -0
- package/src/interpreter/components/PatternMatcher.ts +254 -0
- package/src/interpreter/components/Visitor.ts +493 -0
- package/src/interpreter/errors.ts +47 -0
- package/src/interpreter/index.ts +59 -0
- package/src/interpreter/utils.ts +79 -0
- package/src/utils/helpers.ts +73 -0
- package/tests/analyzer/functional.spec.ts +221 -0
- package/tests/analyzer/generic.spec.ts +100 -0
- package/tests/analyzer/helpers.spec.ts +83 -0
- package/tests/analyzer/logic.spec.ts +292 -0
- package/tests/analyzer/oop.spec.ts +338 -0
- package/tests/interpreter/EnvBuilder.spec.ts +178 -0
- package/tests/interpreter/FunctionRuntime.spec.ts +234 -0
- package/tests/interpreter/LazyRuntime.spec.ts +190 -0
- package/tests/interpreter/LogicEngine.spec.ts +194 -0
- package/tests/interpreter/Operations.spec.ts +220 -0
- package/tests/interpreter/PatternSystem.spec.ts +189 -0
- package/tests/interpreter/interpreter.spec.ts +937 -0
- package/tsconfig.build.json +7 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Application,
|
|
3
|
+
ArithmeticBinaryOperation,
|
|
4
|
+
ArithmeticUnaryOperation,
|
|
5
|
+
BitwiseBinaryOperation,
|
|
6
|
+
BitwiseUnaryOperation,
|
|
7
|
+
BooleanPrimitive,
|
|
8
|
+
Call,
|
|
9
|
+
ComparisonOperation,
|
|
10
|
+
CompositionExpression,
|
|
11
|
+
Equation,
|
|
12
|
+
Fact,
|
|
13
|
+
Function,
|
|
14
|
+
FunctorPattern,
|
|
15
|
+
Goal,
|
|
16
|
+
isLazyList,
|
|
17
|
+
Lambda,
|
|
18
|
+
ListPrimitive,
|
|
19
|
+
ListUnaryOperation,
|
|
20
|
+
LiteralPattern,
|
|
21
|
+
LogicalBinaryOperation,
|
|
22
|
+
LogicalUnaryOperation,
|
|
23
|
+
LogicResult,
|
|
24
|
+
NumberPrimitive,
|
|
25
|
+
Pattern,
|
|
26
|
+
Query,
|
|
27
|
+
RangeExpression,
|
|
28
|
+
Return,
|
|
29
|
+
RuntimePredicate,
|
|
30
|
+
Sequence,
|
|
31
|
+
StringOperation,
|
|
32
|
+
StringPrimitive,
|
|
33
|
+
SymbolPrimitive,
|
|
34
|
+
UnguardedBody,
|
|
35
|
+
VariablePattern,
|
|
36
|
+
WildcardPattern,
|
|
37
|
+
} from "yukigo-ast";
|
|
38
|
+
import { Interpreter } from "../../src/interpreter/index.js";
|
|
39
|
+
import {
|
|
40
|
+
solveGoal,
|
|
41
|
+
unify,
|
|
42
|
+
} from "../../src/interpreter/components/LogicResolver.js";
|
|
43
|
+
import { assert } from "chai";
|
|
44
|
+
import {
|
|
45
|
+
createEnv,
|
|
46
|
+
createGlobalEnv,
|
|
47
|
+
pushEnv,
|
|
48
|
+
} from "../../src/interpreter/utils.js";
|
|
49
|
+
import { YukigoPrologParser } from "yukigo-prolog-parser";
|
|
50
|
+
|
|
51
|
+
describe("Interpreter Spec", () => {
|
|
52
|
+
let interpreter: Interpreter;
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
interpreter = new Interpreter([], { debug: true });
|
|
55
|
+
});
|
|
56
|
+
it("Evaluates ArithmeticBinaryOperation", () => {
|
|
57
|
+
assert.equal(
|
|
58
|
+
interpreter.evaluate(
|
|
59
|
+
new ArithmeticBinaryOperation(
|
|
60
|
+
"Plus",
|
|
61
|
+
new NumberPrimitive(3),
|
|
62
|
+
new NumberPrimitive(4)
|
|
63
|
+
)
|
|
64
|
+
),
|
|
65
|
+
7
|
|
66
|
+
);
|
|
67
|
+
assert.equal(
|
|
68
|
+
interpreter.evaluate(
|
|
69
|
+
new ArithmeticBinaryOperation(
|
|
70
|
+
"Minus",
|
|
71
|
+
new NumberPrimitive(3),
|
|
72
|
+
new NumberPrimitive(4)
|
|
73
|
+
)
|
|
74
|
+
),
|
|
75
|
+
-1
|
|
76
|
+
);
|
|
77
|
+
assert.equal(
|
|
78
|
+
interpreter.evaluate(
|
|
79
|
+
new ArithmeticBinaryOperation(
|
|
80
|
+
"Multiply",
|
|
81
|
+
new NumberPrimitive(3),
|
|
82
|
+
new NumberPrimitive(4)
|
|
83
|
+
)
|
|
84
|
+
),
|
|
85
|
+
12
|
|
86
|
+
);
|
|
87
|
+
assert.equal(
|
|
88
|
+
interpreter.evaluate(
|
|
89
|
+
new ArithmeticBinaryOperation(
|
|
90
|
+
"Divide",
|
|
91
|
+
new NumberPrimitive(3),
|
|
92
|
+
new NumberPrimitive(4)
|
|
93
|
+
)
|
|
94
|
+
),
|
|
95
|
+
0.75
|
|
96
|
+
);
|
|
97
|
+
assert.equal(
|
|
98
|
+
interpreter.evaluate(
|
|
99
|
+
new ArithmeticBinaryOperation(
|
|
100
|
+
"Modulo",
|
|
101
|
+
new NumberPrimitive(3),
|
|
102
|
+
new NumberPrimitive(4)
|
|
103
|
+
)
|
|
104
|
+
),
|
|
105
|
+
3
|
|
106
|
+
);
|
|
107
|
+
assert.equal(
|
|
108
|
+
interpreter.evaluate(
|
|
109
|
+
new ArithmeticBinaryOperation(
|
|
110
|
+
"Power",
|
|
111
|
+
new NumberPrimitive(3),
|
|
112
|
+
new NumberPrimitive(4)
|
|
113
|
+
)
|
|
114
|
+
),
|
|
115
|
+
81
|
|
116
|
+
);
|
|
117
|
+
assert.equal(
|
|
118
|
+
interpreter.evaluate(
|
|
119
|
+
new ArithmeticBinaryOperation(
|
|
120
|
+
"Min",
|
|
121
|
+
new NumberPrimitive(3),
|
|
122
|
+
new NumberPrimitive(4)
|
|
123
|
+
)
|
|
124
|
+
),
|
|
125
|
+
3
|
|
126
|
+
);
|
|
127
|
+
assert.equal(
|
|
128
|
+
interpreter.evaluate(
|
|
129
|
+
new ArithmeticBinaryOperation(
|
|
130
|
+
"Max",
|
|
131
|
+
new NumberPrimitive(3),
|
|
132
|
+
new NumberPrimitive(4)
|
|
133
|
+
)
|
|
134
|
+
),
|
|
135
|
+
4
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("Evaluates ArithmeticUnaryOperation", () => {
|
|
140
|
+
assert.equal(
|
|
141
|
+
interpreter.evaluate(
|
|
142
|
+
new ArithmeticUnaryOperation("Round", new NumberPrimitive(3.4))
|
|
143
|
+
),
|
|
144
|
+
3
|
|
145
|
+
);
|
|
146
|
+
assert.equal(
|
|
147
|
+
interpreter.evaluate(
|
|
148
|
+
new ArithmeticUnaryOperation("Round", new NumberPrimitive(3.5))
|
|
149
|
+
),
|
|
150
|
+
4
|
|
151
|
+
);
|
|
152
|
+
assert.equal(
|
|
153
|
+
interpreter.evaluate(
|
|
154
|
+
new ArithmeticUnaryOperation("Absolute", new NumberPrimitive(-3))
|
|
155
|
+
),
|
|
156
|
+
3
|
|
157
|
+
);
|
|
158
|
+
assert.equal(
|
|
159
|
+
interpreter.evaluate(
|
|
160
|
+
new ArithmeticUnaryOperation("Absolute", new NumberPrimitive(3))
|
|
161
|
+
),
|
|
162
|
+
3
|
|
163
|
+
);
|
|
164
|
+
assert.equal(
|
|
165
|
+
interpreter.evaluate(
|
|
166
|
+
new ArithmeticUnaryOperation("Ceil", new NumberPrimitive(3.3))
|
|
167
|
+
),
|
|
168
|
+
4
|
|
169
|
+
);
|
|
170
|
+
assert.equal(
|
|
171
|
+
interpreter.evaluate(
|
|
172
|
+
new ArithmeticUnaryOperation("Floor", new NumberPrimitive(3.3))
|
|
173
|
+
),
|
|
174
|
+
3
|
|
175
|
+
);
|
|
176
|
+
assert.equal(
|
|
177
|
+
interpreter.evaluate(
|
|
178
|
+
new ArithmeticUnaryOperation("Negation", new NumberPrimitive(3.3))
|
|
179
|
+
),
|
|
180
|
+
-3.3
|
|
181
|
+
);
|
|
182
|
+
assert.equal(
|
|
183
|
+
interpreter.evaluate(
|
|
184
|
+
new ArithmeticUnaryOperation("Sqrt", new NumberPrimitive(25))
|
|
185
|
+
),
|
|
186
|
+
5
|
|
187
|
+
);
|
|
188
|
+
});
|
|
189
|
+
describe("Evaluates ComparisonOperation", () => {
|
|
190
|
+
it("Equal Operator", () => {
|
|
191
|
+
assert.equal(
|
|
192
|
+
interpreter.evaluate(
|
|
193
|
+
new ComparisonOperation(
|
|
194
|
+
"Equal",
|
|
195
|
+
new NumberPrimitive(3.4),
|
|
196
|
+
new NumberPrimitive(3.4)
|
|
197
|
+
)
|
|
198
|
+
),
|
|
199
|
+
true
|
|
200
|
+
);
|
|
201
|
+
assert.equal(
|
|
202
|
+
interpreter.evaluate(
|
|
203
|
+
new ComparisonOperation(
|
|
204
|
+
"Equal",
|
|
205
|
+
new NumberPrimitive(3.4),
|
|
206
|
+
new NumberPrimitive(4)
|
|
207
|
+
)
|
|
208
|
+
),
|
|
209
|
+
false
|
|
210
|
+
);
|
|
211
|
+
assert.equal(
|
|
212
|
+
interpreter.evaluate(
|
|
213
|
+
new ComparisonOperation(
|
|
214
|
+
"Equal",
|
|
215
|
+
new NumberPrimitive(3),
|
|
216
|
+
new StringPrimitive("3")
|
|
217
|
+
)
|
|
218
|
+
),
|
|
219
|
+
true
|
|
220
|
+
);
|
|
221
|
+
assert.equal(
|
|
222
|
+
interpreter.evaluate(
|
|
223
|
+
new ComparisonOperation(
|
|
224
|
+
"Equal",
|
|
225
|
+
new NumberPrimitive(3),
|
|
226
|
+
new StringPrimitive("4")
|
|
227
|
+
)
|
|
228
|
+
),
|
|
229
|
+
false
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
it("NotEqual Operator", () => {
|
|
233
|
+
assert.equal(
|
|
234
|
+
interpreter.evaluate(
|
|
235
|
+
new ComparisonOperation(
|
|
236
|
+
"NotEqual",
|
|
237
|
+
new NumberPrimitive(3.4),
|
|
238
|
+
new NumberPrimitive(3.4)
|
|
239
|
+
)
|
|
240
|
+
),
|
|
241
|
+
false
|
|
242
|
+
);
|
|
243
|
+
assert.equal(
|
|
244
|
+
interpreter.evaluate(
|
|
245
|
+
new ComparisonOperation(
|
|
246
|
+
"NotEqual",
|
|
247
|
+
new NumberPrimitive(3.4),
|
|
248
|
+
new NumberPrimitive(4)
|
|
249
|
+
)
|
|
250
|
+
),
|
|
251
|
+
true
|
|
252
|
+
);
|
|
253
|
+
assert.equal(
|
|
254
|
+
interpreter.evaluate(
|
|
255
|
+
new ComparisonOperation(
|
|
256
|
+
"NotEqual",
|
|
257
|
+
new NumberPrimitive(3),
|
|
258
|
+
new StringPrimitive("3")
|
|
259
|
+
)
|
|
260
|
+
),
|
|
261
|
+
false
|
|
262
|
+
);
|
|
263
|
+
assert.equal(
|
|
264
|
+
interpreter.evaluate(
|
|
265
|
+
new ComparisonOperation(
|
|
266
|
+
"NotEqual",
|
|
267
|
+
new NumberPrimitive(3),
|
|
268
|
+
new StringPrimitive("4")
|
|
269
|
+
)
|
|
270
|
+
),
|
|
271
|
+
true
|
|
272
|
+
);
|
|
273
|
+
});
|
|
274
|
+
it("Same Operator", () => {
|
|
275
|
+
assert.equal(
|
|
276
|
+
interpreter.evaluate(
|
|
277
|
+
new ComparisonOperation(
|
|
278
|
+
"Same",
|
|
279
|
+
new NumberPrimitive(3.4),
|
|
280
|
+
new NumberPrimitive(3.4)
|
|
281
|
+
)
|
|
282
|
+
),
|
|
283
|
+
true
|
|
284
|
+
);
|
|
285
|
+
assert.equal(
|
|
286
|
+
interpreter.evaluate(
|
|
287
|
+
new ComparisonOperation(
|
|
288
|
+
"Same",
|
|
289
|
+
new NumberPrimitive(3.4),
|
|
290
|
+
new NumberPrimitive(4)
|
|
291
|
+
)
|
|
292
|
+
),
|
|
293
|
+
false
|
|
294
|
+
);
|
|
295
|
+
assert.equal(
|
|
296
|
+
interpreter.evaluate(
|
|
297
|
+
new ComparisonOperation(
|
|
298
|
+
"Same",
|
|
299
|
+
new NumberPrimitive(3),
|
|
300
|
+
new StringPrimitive("3")
|
|
301
|
+
)
|
|
302
|
+
),
|
|
303
|
+
false
|
|
304
|
+
);
|
|
305
|
+
assert.equal(
|
|
306
|
+
interpreter.evaluate(
|
|
307
|
+
new ComparisonOperation(
|
|
308
|
+
"Same",
|
|
309
|
+
new NumberPrimitive(3),
|
|
310
|
+
new StringPrimitive("4")
|
|
311
|
+
)
|
|
312
|
+
),
|
|
313
|
+
false
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
it("NotSame Operator", () => {
|
|
317
|
+
assert.equal(
|
|
318
|
+
interpreter.evaluate(
|
|
319
|
+
new ComparisonOperation(
|
|
320
|
+
"NotSame",
|
|
321
|
+
new NumberPrimitive(3.4),
|
|
322
|
+
new NumberPrimitive(3.4)
|
|
323
|
+
)
|
|
324
|
+
),
|
|
325
|
+
false
|
|
326
|
+
);
|
|
327
|
+
assert.equal(
|
|
328
|
+
interpreter.evaluate(
|
|
329
|
+
new ComparisonOperation(
|
|
330
|
+
"NotSame",
|
|
331
|
+
new NumberPrimitive(3.4),
|
|
332
|
+
new NumberPrimitive(4)
|
|
333
|
+
)
|
|
334
|
+
),
|
|
335
|
+
true
|
|
336
|
+
);
|
|
337
|
+
assert.equal(
|
|
338
|
+
interpreter.evaluate(
|
|
339
|
+
new ComparisonOperation(
|
|
340
|
+
"NotSame",
|
|
341
|
+
new NumberPrimitive(3),
|
|
342
|
+
new StringPrimitive("3")
|
|
343
|
+
)
|
|
344
|
+
),
|
|
345
|
+
true
|
|
346
|
+
);
|
|
347
|
+
assert.equal(
|
|
348
|
+
interpreter.evaluate(
|
|
349
|
+
new ComparisonOperation(
|
|
350
|
+
"NotSame",
|
|
351
|
+
new NumberPrimitive(3),
|
|
352
|
+
new StringPrimitive("4")
|
|
353
|
+
)
|
|
354
|
+
),
|
|
355
|
+
true
|
|
356
|
+
);
|
|
357
|
+
});
|
|
358
|
+
it("GreaterOrEqualThan Operator", () => {
|
|
359
|
+
assert.equal(
|
|
360
|
+
interpreter.evaluate(
|
|
361
|
+
new ComparisonOperation(
|
|
362
|
+
"GreaterOrEqualThan",
|
|
363
|
+
new NumberPrimitive(3.4),
|
|
364
|
+
new NumberPrimitive(3.4)
|
|
365
|
+
)
|
|
366
|
+
),
|
|
367
|
+
true
|
|
368
|
+
);
|
|
369
|
+
assert.equal(
|
|
370
|
+
interpreter.evaluate(
|
|
371
|
+
new ComparisonOperation(
|
|
372
|
+
"GreaterOrEqualThan",
|
|
373
|
+
new NumberPrimitive(3.4),
|
|
374
|
+
new NumberPrimitive(4)
|
|
375
|
+
)
|
|
376
|
+
),
|
|
377
|
+
false
|
|
378
|
+
);
|
|
379
|
+
assert.equal(
|
|
380
|
+
interpreter.evaluate(
|
|
381
|
+
new ComparisonOperation(
|
|
382
|
+
"GreaterOrEqualThan",
|
|
383
|
+
new NumberPrimitive(3),
|
|
384
|
+
new StringPrimitive("3")
|
|
385
|
+
)
|
|
386
|
+
),
|
|
387
|
+
true
|
|
388
|
+
);
|
|
389
|
+
assert.equal(
|
|
390
|
+
interpreter.evaluate(
|
|
391
|
+
new ComparisonOperation(
|
|
392
|
+
"GreaterOrEqualThan",
|
|
393
|
+
new NumberPrimitive(3),
|
|
394
|
+
new StringPrimitive("4")
|
|
395
|
+
)
|
|
396
|
+
),
|
|
397
|
+
false
|
|
398
|
+
);
|
|
399
|
+
});
|
|
400
|
+
it("GreaterThan Operator", () => {
|
|
401
|
+
assert.equal(
|
|
402
|
+
interpreter.evaluate(
|
|
403
|
+
new ComparisonOperation(
|
|
404
|
+
"GreaterThan",
|
|
405
|
+
new NumberPrimitive(3.4),
|
|
406
|
+
new NumberPrimitive(3.4)
|
|
407
|
+
)
|
|
408
|
+
),
|
|
409
|
+
false
|
|
410
|
+
);
|
|
411
|
+
assert.equal(
|
|
412
|
+
interpreter.evaluate(
|
|
413
|
+
new ComparisonOperation(
|
|
414
|
+
"GreaterThan",
|
|
415
|
+
new NumberPrimitive(3.4),
|
|
416
|
+
new NumberPrimitive(4)
|
|
417
|
+
)
|
|
418
|
+
),
|
|
419
|
+
false
|
|
420
|
+
);
|
|
421
|
+
assert.equal(
|
|
422
|
+
interpreter.evaluate(
|
|
423
|
+
new ComparisonOperation(
|
|
424
|
+
"GreaterThan",
|
|
425
|
+
new NumberPrimitive(3),
|
|
426
|
+
new StringPrimitive("3")
|
|
427
|
+
)
|
|
428
|
+
),
|
|
429
|
+
false
|
|
430
|
+
);
|
|
431
|
+
assert.equal(
|
|
432
|
+
interpreter.evaluate(
|
|
433
|
+
new ComparisonOperation(
|
|
434
|
+
"GreaterThan",
|
|
435
|
+
new NumberPrimitive(3),
|
|
436
|
+
new StringPrimitive("4")
|
|
437
|
+
)
|
|
438
|
+
),
|
|
439
|
+
false
|
|
440
|
+
);
|
|
441
|
+
});
|
|
442
|
+
it("LessOrEqualThan Operator", () => {
|
|
443
|
+
assert.equal(
|
|
444
|
+
interpreter.evaluate(
|
|
445
|
+
new ComparisonOperation(
|
|
446
|
+
"LessOrEqualThan",
|
|
447
|
+
new NumberPrimitive(3.4),
|
|
448
|
+
new NumberPrimitive(3.4)
|
|
449
|
+
)
|
|
450
|
+
),
|
|
451
|
+
true
|
|
452
|
+
);
|
|
453
|
+
assert.equal(
|
|
454
|
+
interpreter.evaluate(
|
|
455
|
+
new ComparisonOperation(
|
|
456
|
+
"LessOrEqualThan",
|
|
457
|
+
new NumberPrimitive(3.4),
|
|
458
|
+
new NumberPrimitive(4)
|
|
459
|
+
)
|
|
460
|
+
),
|
|
461
|
+
true
|
|
462
|
+
);
|
|
463
|
+
assert.equal(
|
|
464
|
+
interpreter.evaluate(
|
|
465
|
+
new ComparisonOperation(
|
|
466
|
+
"LessOrEqualThan",
|
|
467
|
+
new NumberPrimitive(3),
|
|
468
|
+
new StringPrimitive("3")
|
|
469
|
+
)
|
|
470
|
+
),
|
|
471
|
+
true
|
|
472
|
+
);
|
|
473
|
+
assert.equal(
|
|
474
|
+
interpreter.evaluate(
|
|
475
|
+
new ComparisonOperation(
|
|
476
|
+
"LessOrEqualThan",
|
|
477
|
+
new NumberPrimitive(3),
|
|
478
|
+
new StringPrimitive("4")
|
|
479
|
+
)
|
|
480
|
+
),
|
|
481
|
+
true
|
|
482
|
+
);
|
|
483
|
+
});
|
|
484
|
+
it("LessThan Operator", () => {
|
|
485
|
+
assert.equal(
|
|
486
|
+
interpreter.evaluate(
|
|
487
|
+
new ComparisonOperation(
|
|
488
|
+
"LessThan",
|
|
489
|
+
new NumberPrimitive(3.4),
|
|
490
|
+
new NumberPrimitive(3.4)
|
|
491
|
+
)
|
|
492
|
+
),
|
|
493
|
+
false
|
|
494
|
+
);
|
|
495
|
+
assert.equal(
|
|
496
|
+
interpreter.evaluate(
|
|
497
|
+
new ComparisonOperation(
|
|
498
|
+
"LessThan",
|
|
499
|
+
new NumberPrimitive(3.4),
|
|
500
|
+
new NumberPrimitive(4)
|
|
501
|
+
)
|
|
502
|
+
),
|
|
503
|
+
true
|
|
504
|
+
);
|
|
505
|
+
assert.equal(
|
|
506
|
+
interpreter.evaluate(
|
|
507
|
+
new ComparisonOperation(
|
|
508
|
+
"LessThan",
|
|
509
|
+
new NumberPrimitive(3),
|
|
510
|
+
new StringPrimitive("3")
|
|
511
|
+
)
|
|
512
|
+
),
|
|
513
|
+
false
|
|
514
|
+
);
|
|
515
|
+
assert.equal(
|
|
516
|
+
interpreter.evaluate(
|
|
517
|
+
new ComparisonOperation(
|
|
518
|
+
"LessThan",
|
|
519
|
+
new NumberPrimitive(3),
|
|
520
|
+
new StringPrimitive("4")
|
|
521
|
+
)
|
|
522
|
+
),
|
|
523
|
+
true
|
|
524
|
+
);
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
describe("Evaluates LogicalBinaryOperation", () => {
|
|
528
|
+
it("And Operator", () => {
|
|
529
|
+
assert.equal(
|
|
530
|
+
interpreter.evaluate(
|
|
531
|
+
new LogicalBinaryOperation(
|
|
532
|
+
"And",
|
|
533
|
+
new BooleanPrimitive(true),
|
|
534
|
+
new BooleanPrimitive(true)
|
|
535
|
+
)
|
|
536
|
+
),
|
|
537
|
+
true
|
|
538
|
+
);
|
|
539
|
+
assert.equal(
|
|
540
|
+
interpreter.evaluate(
|
|
541
|
+
new LogicalBinaryOperation(
|
|
542
|
+
"And",
|
|
543
|
+
new BooleanPrimitive(true),
|
|
544
|
+
new BooleanPrimitive(false)
|
|
545
|
+
)
|
|
546
|
+
),
|
|
547
|
+
false
|
|
548
|
+
);
|
|
549
|
+
assert.equal(
|
|
550
|
+
interpreter.evaluate(
|
|
551
|
+
new LogicalBinaryOperation(
|
|
552
|
+
"And",
|
|
553
|
+
new BooleanPrimitive(false),
|
|
554
|
+
new BooleanPrimitive(true)
|
|
555
|
+
)
|
|
556
|
+
),
|
|
557
|
+
false
|
|
558
|
+
);
|
|
559
|
+
assert.equal(
|
|
560
|
+
interpreter.evaluate(
|
|
561
|
+
new LogicalBinaryOperation(
|
|
562
|
+
"And",
|
|
563
|
+
new BooleanPrimitive(false),
|
|
564
|
+
new BooleanPrimitive(false)
|
|
565
|
+
)
|
|
566
|
+
),
|
|
567
|
+
false
|
|
568
|
+
);
|
|
569
|
+
});
|
|
570
|
+
it("Or Operator", () => {
|
|
571
|
+
assert.equal(
|
|
572
|
+
interpreter.evaluate(
|
|
573
|
+
new LogicalBinaryOperation(
|
|
574
|
+
"Or",
|
|
575
|
+
new BooleanPrimitive(true),
|
|
576
|
+
new BooleanPrimitive(true)
|
|
577
|
+
)
|
|
578
|
+
),
|
|
579
|
+
true
|
|
580
|
+
);
|
|
581
|
+
assert.equal(
|
|
582
|
+
interpreter.evaluate(
|
|
583
|
+
new LogicalBinaryOperation(
|
|
584
|
+
"Or",
|
|
585
|
+
new BooleanPrimitive(true),
|
|
586
|
+
new BooleanPrimitive(false)
|
|
587
|
+
)
|
|
588
|
+
),
|
|
589
|
+
true
|
|
590
|
+
);
|
|
591
|
+
assert.equal(
|
|
592
|
+
interpreter.evaluate(
|
|
593
|
+
new LogicalBinaryOperation(
|
|
594
|
+
"Or",
|
|
595
|
+
new BooleanPrimitive(false),
|
|
596
|
+
new BooleanPrimitive(true)
|
|
597
|
+
)
|
|
598
|
+
),
|
|
599
|
+
true
|
|
600
|
+
);
|
|
601
|
+
assert.equal(
|
|
602
|
+
interpreter.evaluate(
|
|
603
|
+
new LogicalBinaryOperation(
|
|
604
|
+
"Or",
|
|
605
|
+
new BooleanPrimitive(false),
|
|
606
|
+
new BooleanPrimitive(false)
|
|
607
|
+
)
|
|
608
|
+
),
|
|
609
|
+
false
|
|
610
|
+
);
|
|
611
|
+
});
|
|
612
|
+
});
|
|
613
|
+
describe("Evaluates LogicalUnaryOperation", () => {
|
|
614
|
+
it("Negation Operator", () => {
|
|
615
|
+
assert.equal(
|
|
616
|
+
interpreter.evaluate(
|
|
617
|
+
new LogicalUnaryOperation("Negation", new BooleanPrimitive(true))
|
|
618
|
+
),
|
|
619
|
+
false
|
|
620
|
+
);
|
|
621
|
+
assert.equal(
|
|
622
|
+
interpreter.evaluate(
|
|
623
|
+
new LogicalUnaryOperation("Negation", new BooleanPrimitive(false))
|
|
624
|
+
),
|
|
625
|
+
true
|
|
626
|
+
);
|
|
627
|
+
});
|
|
628
|
+
});
|
|
629
|
+
describe("Evaluates StringOperation", () => {
|
|
630
|
+
it("Concat Operator", () => {
|
|
631
|
+
assert.equal(
|
|
632
|
+
interpreter.evaluate(
|
|
633
|
+
new StringOperation(
|
|
634
|
+
"Concat",
|
|
635
|
+
new StringPrimitive("Hello"),
|
|
636
|
+
new StringPrimitive(" world!")
|
|
637
|
+
)
|
|
638
|
+
),
|
|
639
|
+
"Hello world!"
|
|
640
|
+
);
|
|
641
|
+
});
|
|
642
|
+
});
|
|
643
|
+
describe("Evaluates ListUnaryOperation", () => {
|
|
644
|
+
it("Size Operator", () => {
|
|
645
|
+
assert.equal(
|
|
646
|
+
interpreter.evaluate(
|
|
647
|
+
new ListUnaryOperation(
|
|
648
|
+
"Size",
|
|
649
|
+
new ListPrimitive([
|
|
650
|
+
new StringPrimitive("Hello"),
|
|
651
|
+
new StringPrimitive("world!"),
|
|
652
|
+
])
|
|
653
|
+
)
|
|
654
|
+
),
|
|
655
|
+
2
|
|
656
|
+
);
|
|
657
|
+
});
|
|
658
|
+
it("DetectMax Operator", () => {
|
|
659
|
+
assert.equal(
|
|
660
|
+
interpreter.evaluate(
|
|
661
|
+
new ListUnaryOperation(
|
|
662
|
+
"DetectMax",
|
|
663
|
+
new ListPrimitive([new NumberPrimitive(2), new NumberPrimitive(4)])
|
|
664
|
+
)
|
|
665
|
+
),
|
|
666
|
+
4
|
|
667
|
+
);
|
|
668
|
+
});
|
|
669
|
+
it("DetectMin Operator", () => {
|
|
670
|
+
assert.equal(
|
|
671
|
+
interpreter.evaluate(
|
|
672
|
+
new ListUnaryOperation(
|
|
673
|
+
"DetectMin",
|
|
674
|
+
new ListPrimitive([new NumberPrimitive(2), new NumberPrimitive(4)])
|
|
675
|
+
)
|
|
676
|
+
),
|
|
677
|
+
2
|
|
678
|
+
);
|
|
679
|
+
});
|
|
680
|
+
it("Flatten Operator", () => {
|
|
681
|
+
assert.deepEqual(
|
|
682
|
+
interpreter.evaluate(
|
|
683
|
+
new ListUnaryOperation(
|
|
684
|
+
"Flatten",
|
|
685
|
+
new ListPrimitive([
|
|
686
|
+
new ListPrimitive([new NumberPrimitive(2)]),
|
|
687
|
+
new ListPrimitive([
|
|
688
|
+
new NumberPrimitive(3),
|
|
689
|
+
new NumberPrimitive(8),
|
|
690
|
+
]),
|
|
691
|
+
])
|
|
692
|
+
)
|
|
693
|
+
),
|
|
694
|
+
[2, 3, 8]
|
|
695
|
+
);
|
|
696
|
+
});
|
|
697
|
+
});
|
|
698
|
+
describe("Evaluates BitwiseBinaryOperation", () => {
|
|
699
|
+
it("BitwiseOr Operator", () => {
|
|
700
|
+
assert.equal(
|
|
701
|
+
interpreter.evaluate(
|
|
702
|
+
new BitwiseBinaryOperation(
|
|
703
|
+
"BitwiseOr",
|
|
704
|
+
new NumberPrimitive(5),
|
|
705
|
+
new NumberPrimitive(1)
|
|
706
|
+
)
|
|
707
|
+
),
|
|
708
|
+
5
|
|
709
|
+
);
|
|
710
|
+
});
|
|
711
|
+
it("BitwiseAnd Operator", () => {
|
|
712
|
+
assert.equal(
|
|
713
|
+
interpreter.evaluate(
|
|
714
|
+
new BitwiseBinaryOperation(
|
|
715
|
+
"BitwiseAnd",
|
|
716
|
+
new NumberPrimitive(5),
|
|
717
|
+
new NumberPrimitive(1)
|
|
718
|
+
)
|
|
719
|
+
),
|
|
720
|
+
1
|
|
721
|
+
);
|
|
722
|
+
});
|
|
723
|
+
it("BitwiseLeftShift Operator", () => {
|
|
724
|
+
assert.equal(
|
|
725
|
+
interpreter.evaluate(
|
|
726
|
+
new BitwiseBinaryOperation(
|
|
727
|
+
"BitwiseLeftShift",
|
|
728
|
+
new NumberPrimitive(5),
|
|
729
|
+
new NumberPrimitive(1)
|
|
730
|
+
)
|
|
731
|
+
),
|
|
732
|
+
10
|
|
733
|
+
);
|
|
734
|
+
});
|
|
735
|
+
it("BitwiseRightShift Operator", () => {
|
|
736
|
+
assert.equal(
|
|
737
|
+
interpreter.evaluate(
|
|
738
|
+
new BitwiseBinaryOperation(
|
|
739
|
+
"BitwiseRightShift",
|
|
740
|
+
new NumberPrimitive(5),
|
|
741
|
+
new NumberPrimitive(1)
|
|
742
|
+
)
|
|
743
|
+
),
|
|
744
|
+
2
|
|
745
|
+
);
|
|
746
|
+
});
|
|
747
|
+
it("BitwiseUnsignedRightShift Operator", () => {
|
|
748
|
+
assert.equal(
|
|
749
|
+
interpreter.evaluate(
|
|
750
|
+
new BitwiseBinaryOperation(
|
|
751
|
+
"BitwiseUnsignedRightShift",
|
|
752
|
+
new NumberPrimitive(5),
|
|
753
|
+
new NumberPrimitive(1)
|
|
754
|
+
)
|
|
755
|
+
),
|
|
756
|
+
2
|
|
757
|
+
);
|
|
758
|
+
});
|
|
759
|
+
it("BitwiseXor Operator", () => {
|
|
760
|
+
assert.equal(
|
|
761
|
+
interpreter.evaluate(
|
|
762
|
+
new BitwiseBinaryOperation(
|
|
763
|
+
"BitwiseXor",
|
|
764
|
+
new NumberPrimitive(5),
|
|
765
|
+
new NumberPrimitive(1)
|
|
766
|
+
)
|
|
767
|
+
),
|
|
768
|
+
4
|
|
769
|
+
);
|
|
770
|
+
});
|
|
771
|
+
});
|
|
772
|
+
describe("Evaluates BitwiseUnaryOperator", () => {
|
|
773
|
+
it("BitwiseNot Operator", () => {
|
|
774
|
+
assert.equal(
|
|
775
|
+
interpreter.evaluate(
|
|
776
|
+
new BitwiseUnaryOperation("BitwiseNot", new NumberPrimitive(5))
|
|
777
|
+
),
|
|
778
|
+
-6
|
|
779
|
+
);
|
|
780
|
+
});
|
|
781
|
+
});
|
|
782
|
+
describe("Evaluates Application", () => {
|
|
783
|
+
beforeEach(() => {
|
|
784
|
+
interpreter = new Interpreter([
|
|
785
|
+
new Function(new SymbolPrimitive("f"), [
|
|
786
|
+
new Equation(
|
|
787
|
+
[new LiteralPattern(new NumberPrimitive(3))],
|
|
788
|
+
new UnguardedBody(
|
|
789
|
+
new Sequence([new Return(new NumberPrimitive(6))])
|
|
790
|
+
),
|
|
791
|
+
new Return(new NumberPrimitive(6))
|
|
792
|
+
),
|
|
793
|
+
new Equation(
|
|
794
|
+
[new VariablePattern(new SymbolPrimitive("y"))],
|
|
795
|
+
new UnguardedBody(
|
|
796
|
+
new Sequence([
|
|
797
|
+
new Return(
|
|
798
|
+
new ArithmeticBinaryOperation(
|
|
799
|
+
"Plus",
|
|
800
|
+
new SymbolPrimitive("y"),
|
|
801
|
+
new NumberPrimitive(3)
|
|
802
|
+
)
|
|
803
|
+
),
|
|
804
|
+
])
|
|
805
|
+
),
|
|
806
|
+
new Return(
|
|
807
|
+
new ArithmeticBinaryOperation(
|
|
808
|
+
"Plus",
|
|
809
|
+
new SymbolPrimitive("y"),
|
|
810
|
+
new NumberPrimitive(3)
|
|
811
|
+
)
|
|
812
|
+
)
|
|
813
|
+
),
|
|
814
|
+
]),
|
|
815
|
+
]);
|
|
816
|
+
});
|
|
817
|
+
it("Lambda + currified Application", () => {
|
|
818
|
+
const lambda = new Lambda(
|
|
819
|
+
[
|
|
820
|
+
new VariablePattern(new SymbolPrimitive("x")),
|
|
821
|
+
new VariablePattern(new SymbolPrimitive("y")),
|
|
822
|
+
new VariablePattern(new SymbolPrimitive("z")),
|
|
823
|
+
],
|
|
824
|
+
new ArithmeticBinaryOperation(
|
|
825
|
+
"Plus",
|
|
826
|
+
new ArithmeticBinaryOperation(
|
|
827
|
+
"Plus",
|
|
828
|
+
new SymbolPrimitive("x"),
|
|
829
|
+
new SymbolPrimitive("y")
|
|
830
|
+
),
|
|
831
|
+
new SymbolPrimitive("z")
|
|
832
|
+
)
|
|
833
|
+
);
|
|
834
|
+
const app1 = new Application(lambda, new NumberPrimitive(1));
|
|
835
|
+
const app2 = new Application(app1, new NumberPrimitive(2));
|
|
836
|
+
const app3 = new Application(app2, new NumberPrimitive(3));
|
|
837
|
+
assert.equal(interpreter.evaluate(app3), 6);
|
|
838
|
+
});
|
|
839
|
+
it("Application of Function", () => {
|
|
840
|
+
const app1 = new Application(
|
|
841
|
+
new SymbolPrimitive("f"),
|
|
842
|
+
new NumberPrimitive(3)
|
|
843
|
+
);
|
|
844
|
+
assert.equal(interpreter.evaluate(app1), 6);
|
|
845
|
+
});
|
|
846
|
+
});
|
|
847
|
+
describe("Evaluates Composition", () => {
|
|
848
|
+
beforeEach(() => {
|
|
849
|
+
interpreter = new Interpreter(
|
|
850
|
+
[
|
|
851
|
+
new Function(new SymbolPrimitive("doble"), [
|
|
852
|
+
new Equation(
|
|
853
|
+
[new VariablePattern(new SymbolPrimitive("x"))],
|
|
854
|
+
new UnguardedBody(
|
|
855
|
+
new Sequence([
|
|
856
|
+
new Return(
|
|
857
|
+
new ArithmeticBinaryOperation(
|
|
858
|
+
"Multiply",
|
|
859
|
+
new SymbolPrimitive("x"),
|
|
860
|
+
new NumberPrimitive(2)
|
|
861
|
+
)
|
|
862
|
+
),
|
|
863
|
+
])
|
|
864
|
+
),
|
|
865
|
+
new Return(
|
|
866
|
+
new ArithmeticBinaryOperation(
|
|
867
|
+
"Multiply",
|
|
868
|
+
new SymbolPrimitive("x"),
|
|
869
|
+
new NumberPrimitive(2)
|
|
870
|
+
)
|
|
871
|
+
)
|
|
872
|
+
),
|
|
873
|
+
]),
|
|
874
|
+
new Function(new SymbolPrimitive("cuadrado"), [
|
|
875
|
+
new Equation(
|
|
876
|
+
[new VariablePattern(new SymbolPrimitive("x"))],
|
|
877
|
+
new UnguardedBody(
|
|
878
|
+
new Sequence([
|
|
879
|
+
new Return(
|
|
880
|
+
new ArithmeticBinaryOperation(
|
|
881
|
+
"Power",
|
|
882
|
+
new SymbolPrimitive("x"),
|
|
883
|
+
new NumberPrimitive(2)
|
|
884
|
+
)
|
|
885
|
+
),
|
|
886
|
+
])
|
|
887
|
+
),
|
|
888
|
+
new Return(
|
|
889
|
+
new ArithmeticBinaryOperation(
|
|
890
|
+
"Power",
|
|
891
|
+
new SymbolPrimitive("x"),
|
|
892
|
+
new NumberPrimitive(2)
|
|
893
|
+
)
|
|
894
|
+
)
|
|
895
|
+
),
|
|
896
|
+
]),
|
|
897
|
+
],
|
|
898
|
+
{ debug: true }
|
|
899
|
+
);
|
|
900
|
+
});
|
|
901
|
+
it("Composition (cuadrado . doble) 2 should be 16", () => {
|
|
902
|
+
const fog = new CompositionExpression(
|
|
903
|
+
new SymbolPrimitive("cuadrado"),
|
|
904
|
+
new SymbolPrimitive("doble")
|
|
905
|
+
);
|
|
906
|
+
const app = new Application(fog, new NumberPrimitive(2));
|
|
907
|
+
assert.equal(interpreter.evaluate(app), 16);
|
|
908
|
+
});
|
|
909
|
+
});
|
|
910
|
+
describe("Evaluates Range Expression", () => {
|
|
911
|
+
it("Evaluates [1..5] to full range", () => {
|
|
912
|
+
const range = new RangeExpression(
|
|
913
|
+
new NumberPrimitive(1),
|
|
914
|
+
new NumberPrimitive(5)
|
|
915
|
+
);
|
|
916
|
+
assert.deepEqual(interpreter.evaluate(range), [1, 2, 3, 4, 5]);
|
|
917
|
+
});
|
|
918
|
+
it("Evaluates [0,0.5..2] to full range", () => {
|
|
919
|
+
const range = new RangeExpression(
|
|
920
|
+
new NumberPrimitive(0),
|
|
921
|
+
new NumberPrimitive(2),
|
|
922
|
+
new NumberPrimitive(0.5)
|
|
923
|
+
);
|
|
924
|
+
assert.deepEqual(interpreter.evaluate(range), [0, 0.5, 1, 1.5, 2]);
|
|
925
|
+
});
|
|
926
|
+
it("Evaluates [1..] lazily", () => {
|
|
927
|
+
interpreter = new Interpreter([], { lazyLoading: true });
|
|
928
|
+
const range = new RangeExpression(new NumberPrimitive(1));
|
|
929
|
+
const evaluatedList = interpreter.evaluate(range);
|
|
930
|
+
if (!isLazyList(evaluatedList))
|
|
931
|
+
assert.fail("Evaluated list should be LazyList");
|
|
932
|
+
const iter = evaluatedList.generator();
|
|
933
|
+
assert.equal(iter.next().value, 1);
|
|
934
|
+
assert.equal(iter.next().value, 2);
|
|
935
|
+
});
|
|
936
|
+
});
|
|
937
|
+
});
|