yukigo 0.2.3 → 0.3.2
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 +3 -3
- package/CHANGELOG.md +105 -40
- package/README.md +193 -193
- package/dist/analyzer/GraphBuilder.d.ts +1 -0
- package/dist/analyzer/GraphBuilder.js +2 -1
- package/dist/analyzer/index.d.ts +0 -7
- package/dist/analyzer/index.js +3 -3
- package/dist/analyzer/inspections/functional/functional.d.ts +1 -1
- package/dist/analyzer/inspections/functional/functional.js +1 -1
- package/dist/analyzer/inspections/functional/smells.d.ts +3 -3
- package/dist/analyzer/inspections/functional/smells.js +27 -35
- package/dist/analyzer/inspections/generic/generic.d.ts +22 -14
- package/dist/analyzer/inspections/generic/generic.js +82 -60
- package/dist/analyzer/inspections/generic/smells.d.ts +2 -0
- package/dist/analyzer/inspections/generic/smells.js +55 -64
- package/dist/analyzer/inspections/imperative/imperative.d.ts +3 -3
- package/dist/analyzer/inspections/imperative/imperative.js +3 -3
- package/dist/analyzer/inspections/imperative/smells.d.ts +1 -0
- package/dist/analyzer/inspections/imperative/smells.js +8 -5
- package/dist/analyzer/inspections/logic/logic.d.ts +8 -8
- package/dist/analyzer/inspections/logic/logic.js +5 -5
- package/dist/analyzer/inspections/logic/smells.js +2 -2
- package/dist/analyzer/inspections/object/object.d.ts +5 -5
- package/dist/analyzer/inspections/object/object.js +22 -21
- package/dist/analyzer/inspections/object/smells.js +11 -23
- package/dist/analyzer/utils.d.ts +6 -2
- package/dist/analyzer/utils.js +6 -2
- package/dist/interpreter/components/EnvBuilder.d.ts +20 -1
- package/dist/interpreter/components/EnvBuilder.js +211 -66
- package/dist/interpreter/components/EqualityComparer.d.ts +15 -0
- package/dist/interpreter/components/EqualityComparer.js +69 -0
- package/dist/interpreter/components/Operations.d.ts +17 -14
- package/dist/interpreter/components/Operations.js +61 -62
- package/dist/interpreter/components/PatternMatcher.d.ts +19 -58
- package/dist/interpreter/components/PatternMatcher.js +185 -286
- package/dist/interpreter/components/RuntimeContext.d.ts +24 -15
- package/dist/interpreter/components/RuntimeContext.js +43 -4
- package/dist/interpreter/components/TestRunner.d.ts +15 -13
- package/dist/interpreter/components/TestRunner.js +86 -53
- package/dist/interpreter/components/evaluators/BaseEvaluator.d.ts +19 -0
- package/dist/interpreter/components/evaluators/BaseEvaluator.js +20 -0
- package/dist/interpreter/components/evaluators/ExpressionEvaluator.d.ts +34 -0
- package/dist/interpreter/components/evaluators/ExpressionEvaluator.js +298 -0
- package/dist/interpreter/components/evaluators/OperationEvaluator.d.ts +25 -0
- package/dist/interpreter/components/evaluators/OperationEvaluator.js +226 -0
- package/dist/interpreter/components/evaluators/PrimitiveEvaluator.d.ts +19 -0
- package/dist/interpreter/components/evaluators/PrimitiveEvaluator.js +50 -0
- package/dist/interpreter/components/evaluators/StatementEvaluator.d.ts +28 -0
- package/dist/interpreter/components/evaluators/StatementEvaluator.js +240 -0
- package/dist/interpreter/components/evaluators/TestingEvaluator.d.ts +15 -0
- package/dist/interpreter/components/evaluators/TestingEvaluator.js +14 -0
- package/dist/interpreter/components/evaluators/index.d.ts +110 -0
- package/dist/interpreter/components/evaluators/index.js +21 -0
- package/dist/interpreter/components/evaluators/utils.d.ts +7 -0
- package/dist/interpreter/components/evaluators/utils.js +50 -0
- package/dist/interpreter/components/hooks/YukigoHook.d.ts +27 -0
- package/dist/interpreter/components/hooks/YukigoHook.js +1 -0
- package/dist/interpreter/components/kernel/commands.d.ts +122 -0
- package/dist/interpreter/components/kernel/commands.js +203 -0
- package/dist/interpreter/components/kernel/index.d.ts +51 -0
- package/dist/interpreter/components/kernel/index.js +176 -0
- package/dist/interpreter/components/logic/LogicEngine.d.ts +26 -23
- package/dist/interpreter/components/logic/LogicEngine.js +234 -221
- package/dist/interpreter/components/logic/LogicResolver.d.ts +22 -51
- package/dist/interpreter/components/logic/LogicResolver.js +73 -440
- package/dist/interpreter/components/logic/LogicTerm.d.ts +104 -0
- package/dist/interpreter/components/logic/LogicTerm.js +423 -0
- package/dist/interpreter/components/logic/LogicTranslator.d.ts +25 -9
- package/dist/interpreter/components/logic/LogicTranslator.js +211 -74
- package/dist/interpreter/components/runtimes/FunctionRuntime.d.ts +6 -4
- package/dist/interpreter/components/runtimes/FunctionRuntime.js +82 -84
- package/dist/interpreter/components/runtimes/LazyRuntime.d.ts +13 -14
- package/dist/interpreter/components/runtimes/LazyRuntime.js +112 -239
- package/dist/interpreter/components/runtimes/ObjectRuntime.d.ts +17 -14
- package/dist/interpreter/components/runtimes/ObjectRuntime.js +105 -74
- package/dist/interpreter/errors.d.ts +10 -0
- package/dist/interpreter/errors.js +17 -0
- package/dist/interpreter/index.d.ts +9 -5
- package/dist/interpreter/index.js +11 -13
- package/dist/interpreter/primitives/YuValue.d.ts +21 -0
- package/dist/interpreter/primitives/YuValue.js +40 -0
- package/dist/interpreter/primitives/capabilities.d.ts +90 -0
- package/dist/interpreter/primitives/capabilities.js +1 -0
- package/dist/interpreter/primitives/entities/LazyList.d.ts +48 -0
- package/dist/interpreter/primitives/entities/LazyList.js +164 -0
- package/dist/interpreter/primitives/entities/LogicResult.d.ts +51 -0
- package/dist/interpreter/primitives/entities/LogicResult.js +101 -0
- package/dist/interpreter/primitives/entities/RuntimeClass.d.ts +24 -0
- package/dist/interpreter/primitives/entities/RuntimeClass.js +57 -0
- package/dist/interpreter/primitives/entities/RuntimeFunction.d.ts +40 -0
- package/dist/interpreter/primitives/entities/RuntimeFunction.js +73 -0
- package/dist/interpreter/primitives/entities/RuntimeObject.d.ts +27 -0
- package/dist/interpreter/primitives/entities/RuntimeObject.js +82 -0
- package/dist/interpreter/primitives/entities/RuntimePredicate.d.ts +19 -0
- package/dist/interpreter/primitives/entities/RuntimePredicate.js +59 -0
- package/dist/interpreter/primitives/index.d.ts +14 -0
- package/dist/interpreter/primitives/index.js +14 -0
- package/dist/interpreter/primitives/scalars/YuBoolean.d.ts +24 -0
- package/dist/interpreter/primitives/scalars/YuBoolean.js +69 -0
- package/dist/interpreter/primitives/scalars/YuChar.d.ts +28 -0
- package/dist/interpreter/primitives/scalars/YuChar.js +78 -0
- package/dist/interpreter/primitives/scalars/YuNil.d.ts +25 -0
- package/dist/interpreter/primitives/scalars/YuNil.js +73 -0
- package/dist/interpreter/primitives/scalars/YuNumber.d.ts +49 -0
- package/dist/interpreter/primitives/scalars/YuNumber.js +165 -0
- package/dist/interpreter/primitives/sequences/YuArray.d.ts +33 -0
- package/dist/interpreter/primitives/sequences/YuArray.js +150 -0
- package/dist/interpreter/primitives/sequences/YuString.d.ts +40 -0
- package/dist/interpreter/primitives/sequences/YuString.js +130 -0
- package/dist/interpreter/utils.d.ts +23 -10
- package/dist/interpreter/utils.js +25 -30
- package/dist/tester/index.d.ts +3 -3
- package/dist/tester/index.js +39 -28
- package/dist/utils/helpers.d.ts +13 -5
- package/dist/utils/helpers.js +38 -85
- package/package.json +3 -16
- package/src/analyzer/GraphBuilder.ts +143 -142
- package/src/analyzer/index.ts +185 -194
- package/src/analyzer/inspections/functional/functional.ts +121 -121
- package/src/analyzer/inspections/functional/smells.ts +88 -102
- package/src/analyzer/inspections/generic/generic.ts +610 -577
- package/src/analyzer/inspections/generic/smells.ts +355 -365
- package/src/analyzer/inspections/imperative/imperative.ts +104 -101
- package/src/analyzer/inspections/imperative/smells.ts +58 -54
- package/src/analyzer/inspections/logic/logic.ts +84 -90
- package/src/analyzer/inspections/logic/smells.ts +54 -54
- package/src/analyzer/inspections/object/object.ts +295 -264
- package/src/analyzer/inspections/object/smells.ts +130 -144
- package/src/analyzer/utils.ts +115 -109
- package/src/index.ts +3 -3
- package/src/interpreter/components/EnvBuilder.ts +405 -202
- package/src/interpreter/components/EqualityComparer.ts +107 -0
- package/src/interpreter/components/Operations.ts +126 -99
- package/src/interpreter/components/PatternMatcher.ts +375 -475
- package/src/interpreter/components/RuntimeContext.ts +181 -119
- package/src/interpreter/components/TestRunner.ts +223 -151
- package/src/interpreter/components/evaluators/BaseEvaluator.ts +36 -0
- package/src/interpreter/components/evaluators/ExpressionEvaluator.ts +475 -0
- package/src/interpreter/components/evaluators/OperationEvaluator.ts +477 -0
- package/src/interpreter/components/evaluators/PrimitiveEvaluator.ts +80 -0
- package/src/interpreter/components/evaluators/StatementEvaluator.ts +372 -0
- package/src/interpreter/components/evaluators/TestingEvaluator.ts +26 -0
- package/src/interpreter/components/evaluators/index.ts +34 -0
- package/src/interpreter/components/evaluators/utils.ts +64 -0
- package/src/interpreter/components/hooks/YukigoHook.ts +45 -0
- package/src/interpreter/components/kernel/commands.ts +255 -0
- package/src/interpreter/components/kernel/index.ts +222 -0
- package/src/interpreter/components/logic/LogicEngine.ts +509 -519
- package/src/interpreter/components/logic/LogicResolver.ts +209 -858
- package/src/interpreter/components/logic/LogicTerm.ts +477 -0
- package/src/interpreter/components/logic/LogicTranslator.ts +362 -149
- package/src/interpreter/components/runtimes/FunctionRuntime.ts +249 -227
- package/src/interpreter/components/runtimes/LazyRuntime.ts +226 -334
- package/src/interpreter/components/runtimes/ObjectRuntime.ts +262 -224
- package/src/interpreter/errors.ts +68 -47
- package/src/interpreter/index.ts +50 -52
- package/src/interpreter/primitives/YuValue.ts +64 -0
- package/src/interpreter/primitives/capabilities.ts +111 -0
- package/src/interpreter/primitives/entities/LazyList.ts +225 -0
- package/src/interpreter/primitives/entities/LogicResult.ts +133 -0
- package/src/interpreter/primitives/entities/RuntimeClass.ts +70 -0
- package/src/interpreter/primitives/entities/RuntimeFunction.ts +108 -0
- package/src/interpreter/primitives/entities/RuntimeObject.ts +110 -0
- package/src/interpreter/primitives/entities/RuntimePredicate.ts +77 -0
- package/src/interpreter/primitives/index.ts +17 -0
- package/src/interpreter/primitives/scalars/YuBoolean.ts +79 -0
- package/src/interpreter/primitives/scalars/YuChar.ts +94 -0
- package/src/interpreter/primitives/scalars/YuNil.ts +84 -0
- package/src/interpreter/primitives/scalars/YuNumber.ts +187 -0
- package/src/interpreter/primitives/sequences/YuArray.ts +178 -0
- package/src/interpreter/primitives/sequences/YuString.ts +165 -0
- package/src/interpreter/utils.ts +105 -84
- package/src/tester/index.ts +157 -128
- package/src/utils/helpers.ts +113 -169
- package/tests/analyzer/functional.spec.ts +221 -207
- package/tests/analyzer/generic.spec.ts +178 -178
- package/tests/analyzer/helpers.spec.ts +83 -89
- package/tests/analyzer/logic.spec.ts +237 -237
- package/tests/analyzer/oop.spec.ts +320 -323
- package/tests/analyzer/transitive.spec.ts +166 -166
- package/tests/interpreter/EnvBuilder.spec.ts +181 -183
- package/tests/interpreter/FunctionRuntime.spec.ts +224 -223
- package/tests/interpreter/LazyRuntime.spec.ts +227 -225
- package/tests/interpreter/LogicEngine.spec.ts +371 -327
- package/tests/interpreter/LogicSubstitution.spec.ts +90 -80
- package/tests/interpreter/ObjectRuntime.spec.ts +431 -606
- package/tests/interpreter/Operations.spec.ts +312 -220
- package/tests/interpreter/PatternSystem.spec.ts +254 -213
- package/tests/interpreter/Tests.spec.ts +122 -122
- package/tests/interpreter/interpreter.spec.ts +1143 -991
- package/tests/kernel/YukigoKernel.spec.ts +112 -0
- package/tests/tester/Tester.spec.ts +156 -152
- package/tsconfig.build.json +16 -15
- package/tsconfig.json +27 -25
- package/dist/interpreter/components/Visitor.d.ts +0 -70
- package/dist/interpreter/components/Visitor.js +0 -634
- package/dist/interpreter/entities.d.ts +0 -105
- package/dist/interpreter/entities.js +0 -96
- package/dist/interpreter/trampoline.d.ts +0 -17
- package/dist/interpreter/trampoline.js +0 -38
- package/dist/src/analyzer/GraphBuilder.d.ts +0 -30
- package/dist/src/analyzer/GraphBuilder.js +0 -100
- package/dist/src/analyzer/index.d.ts +0 -59
- package/dist/src/analyzer/index.js +0 -152
- package/dist/src/analyzer/inspections/functional/functional.d.ts +0 -44
- package/dist/src/analyzer/inspections/functional/functional.js +0 -149
- package/dist/src/analyzer/inspections/functional/smells.d.ts +0 -16
- package/dist/src/analyzer/inspections/functional/smells.js +0 -98
- package/dist/src/analyzer/inspections/generic/generic.d.ts +0 -178
- package/dist/src/analyzer/inspections/generic/generic.js +0 -604
- package/dist/src/analyzer/inspections/generic/smells.d.ts +0 -61
- package/dist/src/analyzer/inspections/generic/smells.js +0 -349
- package/dist/src/analyzer/inspections/imperative/imperative.d.ts +0 -35
- package/dist/src/analyzer/inspections/imperative/imperative.js +0 -109
- package/dist/src/analyzer/inspections/imperative/smells.d.ts +0 -16
- package/dist/src/analyzer/inspections/imperative/smells.js +0 -58
- package/dist/src/analyzer/inspections/logic/logic.d.ts +0 -32
- package/dist/src/analyzer/inspections/logic/logic.js +0 -96
- package/dist/src/analyzer/inspections/logic/smells.d.ts +0 -15
- package/dist/src/analyzer/inspections/logic/smells.js +0 -60
- package/dist/src/analyzer/inspections/object/object.d.ts +0 -90
- package/dist/src/analyzer/inspections/object/object.js +0 -321
- package/dist/src/analyzer/inspections/object/smells.d.ts +0 -30
- package/dist/src/analyzer/inspections/object/smells.js +0 -135
- package/dist/src/analyzer/utils.d.ts +0 -30
- package/dist/src/analyzer/utils.js +0 -78
- package/dist/src/index.d.ts +0 -4
- package/dist/src/index.js +0 -4
- package/dist/src/interpreter/components/EnvBuilder.d.ts +0 -21
- package/dist/src/interpreter/components/EnvBuilder.js +0 -155
- package/dist/src/interpreter/components/Operations.d.ts +0 -14
- package/dist/src/interpreter/components/Operations.js +0 -84
- package/dist/src/interpreter/components/PatternMatcher.d.ts +0 -73
- package/dist/src/interpreter/components/PatternMatcher.js +0 -358
- package/dist/src/interpreter/components/RuntimeContext.d.ts +0 -35
- package/dist/src/interpreter/components/RuntimeContext.js +0 -93
- package/dist/src/interpreter/components/TestRunner.d.ts +0 -19
- package/dist/src/interpreter/components/TestRunner.js +0 -110
- package/dist/src/interpreter/components/Visitor.d.ts +0 -71
- package/dist/src/interpreter/components/Visitor.js +0 -638
- package/dist/src/interpreter/components/logic/LogicEngine.d.ts +0 -29
- package/dist/src/interpreter/components/logic/LogicEngine.js +0 -259
- package/dist/src/interpreter/components/logic/LogicResolver.d.ts +0 -53
- package/dist/src/interpreter/components/logic/LogicResolver.js +0 -484
- package/dist/src/interpreter/components/logic/LogicTranslator.d.ts +0 -14
- package/dist/src/interpreter/components/logic/LogicTranslator.js +0 -99
- package/dist/src/interpreter/components/runtimes/FunctionRuntime.d.ts +0 -12
- package/dist/src/interpreter/components/runtimes/FunctionRuntime.js +0 -149
- package/dist/src/interpreter/components/runtimes/LazyRuntime.d.ts +0 -19
- package/dist/src/interpreter/components/runtimes/LazyRuntime.js +0 -269
- package/dist/src/interpreter/components/runtimes/ObjectRuntime.d.ts +0 -35
- package/dist/src/interpreter/components/runtimes/ObjectRuntime.js +0 -126
- package/dist/src/interpreter/errors.d.ts +0 -19
- package/dist/src/interpreter/errors.js +0 -36
- package/dist/src/interpreter/index.d.ts +0 -24
- package/dist/src/interpreter/index.js +0 -41
- package/dist/src/interpreter/trampoline.d.ts +0 -17
- package/dist/src/interpreter/trampoline.js +0 -38
- package/dist/src/interpreter/utils.d.ts +0 -11
- package/dist/src/interpreter/utils.js +0 -65
- package/dist/src/tester/index.d.ts +0 -25
- package/dist/src/tester/index.js +0 -113
- package/dist/src/utils/helpers.d.ts +0 -13
- package/dist/src/utils/helpers.js +0 -52
- package/src/interpreter/components/Visitor.ts +0 -1065
- package/src/interpreter/trampoline.ts +0 -71
|
@@ -1,269 +1,142 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { error, raise } from "../../utils.js";
|
|
2
|
+
import { StepCommand, BindCommand, } from "../kernel/commands.js";
|
|
3
|
+
import { YuValue } from "../../primitives/YuValue.js";
|
|
4
|
+
import { YuNil } from "../../primitives/scalars/YuNil.js";
|
|
5
|
+
import { YuNumber } from "../../primitives/scalars/YuNumber.js";
|
|
6
|
+
import { YuArray } from "../../primitives/sequences/YuArray.js";
|
|
7
|
+
import { YuString } from "../../primitives/sequences/YuString.js";
|
|
8
|
+
import { isLazyList, LazyList, LazyStepResult, } from "../../primitives/entities/LazyList.js";
|
|
4
9
|
export class LazyRuntime {
|
|
5
10
|
context;
|
|
6
11
|
constructor(context) {
|
|
7
12
|
this.context = context;
|
|
8
13
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (step.value === undefined)
|
|
22
|
-
throw new Error("LazyList yielded undefined");
|
|
23
|
-
result.push(step.value);
|
|
24
|
-
return () => next();
|
|
25
|
-
};
|
|
26
|
-
return next();
|
|
27
|
-
}
|
|
28
|
-
throw new Error(`Expected List or LazyList, got ${typeof val}`);
|
|
14
|
+
/**
|
|
15
|
+
* Realizes a list or lazy list into a YuArray.
|
|
16
|
+
*/
|
|
17
|
+
realizeList(val) {
|
|
18
|
+
if (this.context.config.debug)
|
|
19
|
+
console.log(`[LazyRuntime] realizeList: input`, val);
|
|
20
|
+
if (val instanceof YuNil)
|
|
21
|
+
return new StepCommand(new YuArray([]));
|
|
22
|
+
const seq = val.asSequence;
|
|
23
|
+
if (seq)
|
|
24
|
+
return seq.realize();
|
|
25
|
+
return raise(error("LazyRuntime.realizeList", `Expected Sequence, got ${val.getType()}`));
|
|
29
26
|
}
|
|
30
|
-
evaluateRange(node, evaluator
|
|
31
|
-
return evaluator.evaluate(node.start, (startVal) => {
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
evaluateRange(node, evaluator) {
|
|
28
|
+
return new BindCommand(evaluator.evaluate(node.start), (startVal) => {
|
|
29
|
+
const startNum = startVal.toJSON();
|
|
30
|
+
if (typeof startNum !== "number")
|
|
31
|
+
return raise(error("LazyRuntime.evaluateRange", "Range start must be a number"));
|
|
34
32
|
const hasEnd = node.end != null;
|
|
35
33
|
const finishWithStep = (step) => {
|
|
34
|
+
const cond = (c, end) => step > 0 ? c <= end : c >= end;
|
|
35
|
+
const createLazyRange = (val, end) => {
|
|
36
|
+
if (end !== undefined && !cond(val, end))
|
|
37
|
+
return null;
|
|
38
|
+
return new LazyList(() => {
|
|
39
|
+
const tail = createLazyRange(val + step, end);
|
|
40
|
+
return new StepCommand(new LazyStepResult(new YuNumber(val), tail));
|
|
41
|
+
}, `Range(${val})`);
|
|
42
|
+
};
|
|
43
|
+
// if the list is infinite
|
|
36
44
|
if (!hasEnd) {
|
|
37
|
-
return
|
|
38
|
-
let current = startVal;
|
|
39
|
-
while (true) {
|
|
40
|
-
yield current;
|
|
41
|
-
current += step;
|
|
42
|
-
}
|
|
43
|
-
}));
|
|
45
|
+
return new StepCommand(createLazyRange(startNum));
|
|
44
46
|
}
|
|
45
|
-
return evaluator.evaluate(node.end, (endVal) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
return new BindCommand(evaluator.evaluate(node.end), (endVal) => {
|
|
48
|
+
const endNum = endVal.toJSON();
|
|
49
|
+
if (typeof endNum !== "number")
|
|
50
|
+
return raise(error("LazyRuntime.evaluateRange", "Range end must be a number"));
|
|
49
51
|
if (this.context.config.lazyLoading) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
while (cond(current)) {
|
|
53
|
-
yield current;
|
|
54
|
-
current += step;
|
|
55
|
-
}
|
|
56
|
-
}));
|
|
52
|
+
const range = createLazyRange(startNum, endNum);
|
|
53
|
+
return new StepCommand(range || YuNil.getInstance());
|
|
57
54
|
}
|
|
58
55
|
const result = [];
|
|
59
|
-
let current =
|
|
60
|
-
while (cond(current)) {
|
|
61
|
-
result.push(current);
|
|
56
|
+
let current = startNum;
|
|
57
|
+
while (cond(current, endNum)) {
|
|
58
|
+
result.push(new YuNumber(current));
|
|
62
59
|
current += step;
|
|
63
60
|
}
|
|
64
|
-
return
|
|
61
|
+
return new StepCommand(new YuArray(result));
|
|
65
62
|
});
|
|
66
63
|
};
|
|
67
|
-
if (node.step)
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
64
|
+
if (!node.step)
|
|
65
|
+
return finishWithStep(1);
|
|
66
|
+
return new BindCommand(evaluator.evaluate(node.step), (secondVal) => {
|
|
67
|
+
const secondNum = secondVal.toJSON();
|
|
68
|
+
if (typeof secondNum !== "number")
|
|
69
|
+
return raise(error("LazyRuntime.evaluateRange", "Range step must be a number"));
|
|
70
|
+
const step = secondNum - startNum;
|
|
71
|
+
if (step === 0)
|
|
72
|
+
return raise(error("LazyRuntime.evaluateRange", "Range step cannot be zero"));
|
|
73
|
+
return finishWithStep(step);
|
|
74
|
+
});
|
|
78
75
|
});
|
|
79
76
|
}
|
|
80
|
-
evaluateCons(node, evaluator
|
|
77
|
+
evaluateCons(node, evaluator) {
|
|
81
78
|
const ctx = this.context;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const consList = {
|
|
93
|
-
type: "LazyList",
|
|
94
|
-
generator: function* () {
|
|
95
|
-
let current = consState;
|
|
96
|
-
while (current !== undefined && current !== null) {
|
|
97
|
-
if (current.tailExpr !== undefined) {
|
|
98
|
-
yield current.head;
|
|
99
|
-
if (current.realizedTail === undefined) {
|
|
100
|
-
const prevEnv = ctx.env;
|
|
101
|
-
ctx.setEnv(current.capturedEnv);
|
|
102
|
-
try {
|
|
103
|
-
current.realizedTail = trampoline(current.evaluator.evaluate(current.tailExpr, idContinuation));
|
|
104
|
-
}
|
|
105
|
-
finally {
|
|
106
|
-
ctx.setEnv(prevEnv);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
current = current.realizedTail;
|
|
110
|
-
}
|
|
111
|
-
else if (isLazyList(current)) {
|
|
112
|
-
const memoized = current;
|
|
113
|
-
if (isMemoizedList(memoized) && memoized._consState) {
|
|
114
|
-
current = memoized._consState;
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
const iter = current.generator();
|
|
118
|
-
let step = iter.next();
|
|
119
|
-
while (!step.done) {
|
|
120
|
-
yield step.value;
|
|
121
|
-
step = iter.next();
|
|
122
|
-
}
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
else if (Array.isArray(current) ||
|
|
127
|
-
typeof current === "string") {
|
|
128
|
-
for (const x of current)
|
|
129
|
-
yield x;
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
throw new Error(`Invalid tail type for Cons: ${typeof current}`);
|
|
134
|
-
}
|
|
79
|
+
if (ctx.config.lazyLoading) {
|
|
80
|
+
const capturedCtx = ctx.clone();
|
|
81
|
+
const conjoinedLazyList = new LazyList(() => {
|
|
82
|
+
if (!capturedCtx.evaluatorFactory)
|
|
83
|
+
return raise(error("LazyRuntime.evaluateRange", "EvaluatorFactory not initialized"));
|
|
84
|
+
const subEvaluator = capturedCtx.evaluatorFactory(capturedCtx);
|
|
85
|
+
return new BindCommand(subEvaluator.evaluate(node.head), (head) => {
|
|
86
|
+
return new BindCommand(subEvaluator.evaluate(node.tail), (tailRes) => {
|
|
87
|
+
if (tailRes instanceof YuNil) {
|
|
88
|
+
return new StepCommand(new LazyStepResult(head, null));
|
|
135
89
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
throw new Error("Expected Array in eager Cons");
|
|
148
|
-
return k([head, ...tail]);
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
evaluateConcat(left, right, k) {
|
|
153
|
-
if (this.context.config.lazyLoading) {
|
|
154
|
-
return k(createMemoizedStream(function* () {
|
|
155
|
-
if (Array.isArray(left))
|
|
156
|
-
yield* left;
|
|
157
|
-
else if (typeof left === "string")
|
|
158
|
-
yield* left.split("");
|
|
159
|
-
else if (isLazyList(left))
|
|
160
|
-
yield* left.generator();
|
|
161
|
-
else
|
|
162
|
-
throw new Error("Invalid left operand for lazy Concat");
|
|
163
|
-
if (Array.isArray(right))
|
|
164
|
-
yield* right;
|
|
165
|
-
else if (typeof right === "string")
|
|
166
|
-
yield* right.split("");
|
|
167
|
-
else if (isLazyList(right))
|
|
168
|
-
yield* right.generator();
|
|
169
|
-
else
|
|
170
|
-
throw new Error("Invalid right operand for lazy Concat");
|
|
171
|
-
}));
|
|
90
|
+
const tailSeq = tailRes.asSequence;
|
|
91
|
+
if (tailSeq) {
|
|
92
|
+
if (isLazyList(tailSeq))
|
|
93
|
+
return new StepCommand(new LazyStepResult(head, tailSeq));
|
|
94
|
+
return new StepCommand(new LazyStepResult(head, this.arrayToLazyList([...tailSeq])));
|
|
95
|
+
}
|
|
96
|
+
return raise(error("LazyRuntime.evaluateRange", `Invalid tail in cons: ${tailRes.getType()}`));
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}, "Cons", capturedCtx);
|
|
100
|
+
return new StepCommand(conjoinedLazyList);
|
|
172
101
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
102
|
+
// Eager behavior
|
|
103
|
+
return new BindCommand(evaluator.evaluate(node.head), (head) => {
|
|
104
|
+
return new BindCommand(evaluator.evaluate(node.tail), (tail) => {
|
|
105
|
+
if (tail instanceof YuString)
|
|
106
|
+
return new StepCommand(new YuString(head.toJSON() + tail.toJSON()));
|
|
107
|
+
const tailSeq = tail.asSequence;
|
|
108
|
+
if (tailSeq instanceof YuArray) {
|
|
109
|
+
return new StepCommand(new YuArray([head, ...tailSeq]));
|
|
110
|
+
}
|
|
111
|
+
return raise(error("LazyRuntime.evaluateRange", "Expected Array in eager Cons"));
|
|
178
112
|
});
|
|
179
113
|
});
|
|
180
114
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
return
|
|
186
|
-
if (Array.isArray(left))
|
|
187
|
-
yield* left;
|
|
188
|
-
else if (typeof left === "string")
|
|
189
|
-
yield* left.split("");
|
|
190
|
-
else if (isLazyList(left))
|
|
191
|
-
yield* left.generator();
|
|
192
|
-
else
|
|
193
|
-
throw new Error("Invalid left operand for lazy Concat");
|
|
194
|
-
// right evaluates lazily on demand
|
|
195
|
-
const prevEnv = ctx.env;
|
|
196
|
-
ctx.setEnv(capturedEnv);
|
|
197
|
-
let right;
|
|
198
|
-
try {
|
|
199
|
-
right = trampoline(evaluator.evaluate(node.right, idContinuation));
|
|
200
|
-
}
|
|
201
|
-
finally {
|
|
202
|
-
ctx.setEnv(prevEnv);
|
|
203
|
-
}
|
|
204
|
-
if (Array.isArray(right))
|
|
205
|
-
yield* right;
|
|
206
|
-
else if (typeof right === "string")
|
|
207
|
-
yield* right.split("");
|
|
208
|
-
else if (isLazyList(right))
|
|
209
|
-
yield* right.generator();
|
|
210
|
-
else
|
|
211
|
-
throw new Error("Invalid right operand for lazy Concat");
|
|
212
|
-
}));
|
|
115
|
+
arrayToLazyList(arr, index = 0) {
|
|
116
|
+
if (index >= arr.length)
|
|
117
|
+
return null;
|
|
118
|
+
return new LazyList(() => {
|
|
119
|
+
return new StepCommand(new LazyStepResult(arr[index], this.arrayToLazyList(arr, index + 1)));
|
|
213
120
|
});
|
|
214
121
|
}
|
|
215
|
-
|
|
216
|
-
if (
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
122
|
+
evaluateConcat(left, right) {
|
|
123
|
+
if (this.context.config.lazyLoading) {
|
|
124
|
+
const createConcatList = (L, R) => {
|
|
125
|
+
return new LazyList(() => {
|
|
126
|
+
return new BindCommand(L.step(), (stepRes) => {
|
|
127
|
+
if (stepRes.isNil)
|
|
128
|
+
return R.step();
|
|
129
|
+
const stepResult = stepRes.asStepResult;
|
|
130
|
+
if (!stepResult)
|
|
131
|
+
return raise(error("LazyRuntime.evaluateRange", "Concat: step did not return StepResult"));
|
|
132
|
+
return new StepCommand(new LazyStepResult(stepResult.head, stepResult.tail && stepResult.tail instanceof YuValue
|
|
133
|
+
? createConcatList(stepResult.tail, R)
|
|
134
|
+
: R));
|
|
135
|
+
});
|
|
136
|
+
}, "Concat");
|
|
137
|
+
};
|
|
138
|
+
return new StepCommand(createConcatList(left, right));
|
|
227
139
|
}
|
|
228
|
-
|
|
229
|
-
return k(false); // colección vs número → false
|
|
230
|
-
if (this.isPlainObject(a) && this.isPlainObject(b))
|
|
231
|
-
return this.deepEqualObject(a, b, k);
|
|
232
|
-
return k(a == b); // primitivos: number, boolean, string==number
|
|
233
|
-
}
|
|
234
|
-
isPlainObject(val) {
|
|
235
|
-
return val !== null && typeof val === "object"
|
|
236
|
-
&& !isLazyList(val) && !Array.isArray(val);
|
|
237
|
-
}
|
|
238
|
-
isCollection(val) {
|
|
239
|
-
return Array.isArray(val) || isLazyList(val);
|
|
240
|
-
}
|
|
241
|
-
isListLike(val) {
|
|
242
|
-
return Array.isArray(val) || isLazyList(val) || typeof val === "string";
|
|
243
|
-
}
|
|
244
|
-
deepEqualCollection(a, b, index, k) {
|
|
245
|
-
if (index >= a.length)
|
|
246
|
-
return k(true);
|
|
247
|
-
return this.deepEqual(a[index], b[index], (eq) => {
|
|
248
|
-
if (!eq)
|
|
249
|
-
return k(false);
|
|
250
|
-
return () => this.deepEqualCollection(a, b, index + 1, k);
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
deepEqualObject(a, b, k) {
|
|
254
|
-
const keys = Object.keys(a);
|
|
255
|
-
if (keys.length !== Object.keys(b).length)
|
|
256
|
-
return k(false);
|
|
257
|
-
const checkNext = (index) => {
|
|
258
|
-
if (index >= keys.length)
|
|
259
|
-
return k(true);
|
|
260
|
-
const key = keys[index];
|
|
261
|
-
return this.deepEqual(a[key], b[key], (eq) => {
|
|
262
|
-
if (!eq)
|
|
263
|
-
return k(false);
|
|
264
|
-
return () => checkNext(index + 1);
|
|
265
|
-
});
|
|
266
|
-
};
|
|
267
|
-
return checkNext(0);
|
|
140
|
+
return left.concat(right);
|
|
268
141
|
}
|
|
269
142
|
}
|
|
@@ -1,35 +1,38 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Continuation, Thunk } from "../../trampoline.js";
|
|
1
|
+
import { YuValue, RuntimeClass, RuntimeObject, RuntimeFunction } from "../../primitives/index.js";
|
|
3
2
|
import { RuntimeContext } from "../RuntimeContext.js";
|
|
3
|
+
import { ExecutionCommand } from "../kernel/commands.js";
|
|
4
|
+
type OOPEntity = RuntimeClass | RuntimeObject;
|
|
5
|
+
type OOPMatch = {
|
|
6
|
+
method: RuntimeFunction;
|
|
7
|
+
holder: RuntimeObject | RuntimeClass;
|
|
8
|
+
};
|
|
4
9
|
export declare class ObjectRuntime {
|
|
5
10
|
private context;
|
|
6
11
|
constructor(context: RuntimeContext);
|
|
7
|
-
/**
|
|
8
|
-
* Creates a new instance of an Object.
|
|
9
|
-
* Typically called by visitNew()
|
|
10
|
-
*/
|
|
11
|
-
instantiate(className: string, identifier: string, fieldDefinitions: Map<string, PrimitiveValue>, methodDefinitions: Map<string, RuntimeFunction>): RuntimeObject;
|
|
12
12
|
/**
|
|
13
13
|
* Handles Method Calls (Message Passing).
|
|
14
14
|
* Reuses FunctionRuntime to execute the method body.
|
|
15
15
|
*/
|
|
16
|
-
dispatch(receiver:
|
|
16
|
+
dispatch(receiver: YuValue, methodName: string, args: YuValue[]): ExecutionCommand;
|
|
17
17
|
/**
|
|
18
18
|
* Handles calls to super() or super.method()
|
|
19
19
|
*/
|
|
20
|
-
dispatchSuper(
|
|
21
|
-
|
|
22
|
-
private getResolutionChain;
|
|
20
|
+
dispatchSuper(methodName: string, args: YuValue[]): ExecutionCommand;
|
|
21
|
+
getResolutionChain(receiver: RuntimeObject): OOPEntity[];
|
|
23
22
|
private expandClassHierarchy;
|
|
24
|
-
private
|
|
23
|
+
private getClassDef;
|
|
24
|
+
findMethodInChain(chain: Array<OOPEntity>, methodName: string): OOPMatch | undefined;
|
|
25
25
|
/**
|
|
26
26
|
* Field Access (Get)
|
|
27
27
|
* e.g. self.myField
|
|
28
28
|
*/
|
|
29
|
-
getField(receiver:
|
|
29
|
+
getField(receiver: YuValue, fieldName: string): YuValue;
|
|
30
30
|
/**
|
|
31
31
|
* Field Mutation (Set)
|
|
32
32
|
* e.g. self.myField = 10
|
|
33
33
|
*/
|
|
34
|
-
setField(
|
|
34
|
+
setField(obj: YuValue, fieldName: string, value: YuValue): YuValue;
|
|
35
|
+
private getReceiver;
|
|
36
|
+
dispatchPrimitive(receiver: YuValue, methodName: string, args: YuValue[]): ExecutionCommand;
|
|
35
37
|
}
|
|
38
|
+
export {};
|