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,358 +0,0 @@
|
|
|
1
|
-
import { ConstructorPattern, isLazyList, ListPrimitive, SimpleType, ListType, } from "yukigo-ast";
|
|
2
|
-
import { InterpreterVisitor } from "./Visitor.js";
|
|
3
|
-
import { getYukigoType } from "../utils.js";
|
|
4
|
-
import { UnexpectedNode } from "../../utils/helpers.js";
|
|
5
|
-
class SharedSequence {
|
|
6
|
-
cache = [];
|
|
7
|
-
source;
|
|
8
|
-
isDone = false;
|
|
9
|
-
constructor(generatorFactory) {
|
|
10
|
-
this.source = generatorFactory();
|
|
11
|
-
}
|
|
12
|
-
get(index) {
|
|
13
|
-
if (index < this.cache.length)
|
|
14
|
-
return { value: this.cache[index], done: false };
|
|
15
|
-
if (this.isDone)
|
|
16
|
-
return { value: null, done: true };
|
|
17
|
-
while (this.cache.length <= index) {
|
|
18
|
-
const next = this.source.next();
|
|
19
|
-
if (next.done) {
|
|
20
|
-
this.isDone = true;
|
|
21
|
-
return { value: null, done: true };
|
|
22
|
-
}
|
|
23
|
-
this.cache.push(next.value);
|
|
24
|
-
}
|
|
25
|
-
return { value: this.cache[index], done: false };
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
export function isMemoizedList(list) {
|
|
29
|
-
return (list !== null &&
|
|
30
|
-
typeof list === "object" &&
|
|
31
|
-
"type" in list &&
|
|
32
|
-
"_offset" in list &&
|
|
33
|
-
"_sequence" in list);
|
|
34
|
-
}
|
|
35
|
-
export function createMemoizedStream(genFactory, sequence, offset = 0) {
|
|
36
|
-
const seq = sequence ?? new SharedSequence(genFactory);
|
|
37
|
-
return {
|
|
38
|
-
type: "LazyList",
|
|
39
|
-
_sequence: seq,
|
|
40
|
-
_offset: offset,
|
|
41
|
-
generator: function* () {
|
|
42
|
-
let currentIdx = offset;
|
|
43
|
-
while (true) {
|
|
44
|
-
const res = seq.get(currentIdx);
|
|
45
|
-
if (res.done)
|
|
46
|
-
return;
|
|
47
|
-
yield res.value;
|
|
48
|
-
currentIdx++;
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
toJSON() {
|
|
52
|
-
const iterator = this.generator();
|
|
53
|
-
const buffer = [];
|
|
54
|
-
let next = iterator.next();
|
|
55
|
-
while (!next.done) {
|
|
56
|
-
buffer.push(next.value);
|
|
57
|
-
next = iterator.next();
|
|
58
|
-
}
|
|
59
|
-
return buffer;
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
export class PatternResolver {
|
|
64
|
-
visitVariablePattern(node) {
|
|
65
|
-
return node.name.value;
|
|
66
|
-
}
|
|
67
|
-
visitWildcardPattern(node) {
|
|
68
|
-
return "_";
|
|
69
|
-
}
|
|
70
|
-
visitLiteralPattern(node) {
|
|
71
|
-
const { name } = node;
|
|
72
|
-
if (name instanceof ListPrimitive)
|
|
73
|
-
return String(name.value.map((elem) => elem.accept(this)));
|
|
74
|
-
return String(name.value);
|
|
75
|
-
}
|
|
76
|
-
visitTuplePattern(node) {
|
|
77
|
-
return String(node.elements.map((elem) => elem.accept(this)));
|
|
78
|
-
}
|
|
79
|
-
visitListPattern(node) {
|
|
80
|
-
const { elements } = node;
|
|
81
|
-
return elements.length === 0
|
|
82
|
-
? "[]"
|
|
83
|
-
: String(elements.map((elem) => elem.accept(this)));
|
|
84
|
-
}
|
|
85
|
-
visitConsPattern(node) {
|
|
86
|
-
const head = node.left.accept(this);
|
|
87
|
-
const tail = node.right.accept(this);
|
|
88
|
-
return `(${head}:${tail})`;
|
|
89
|
-
}
|
|
90
|
-
visitConstructorPattern(node) {
|
|
91
|
-
const constr = node.identifier.value;
|
|
92
|
-
const args = node.args.map((pat) => pat.accept(this)).join(" ");
|
|
93
|
-
return `${constr} ${args}`;
|
|
94
|
-
}
|
|
95
|
-
visitFunctorPattern(node) {
|
|
96
|
-
// Same as ConstructorPattern (alias)
|
|
97
|
-
return this.visitConstructorPattern(new ConstructorPattern(node.identifier, node.args));
|
|
98
|
-
}
|
|
99
|
-
visitApplicationPattern(node) {
|
|
100
|
-
// Same as FunctorPattern
|
|
101
|
-
return this.visitConstructorPattern(new ConstructorPattern(node.identifier, node.args));
|
|
102
|
-
}
|
|
103
|
-
visitAsPattern(node) {
|
|
104
|
-
const alias = node.left.accept(this);
|
|
105
|
-
const pattern = node.right.accept(this);
|
|
106
|
-
return `${alias}@${pattern}`;
|
|
107
|
-
}
|
|
108
|
-
visitTypePattern(node) {
|
|
109
|
-
const typeStr = node.targetType.toString();
|
|
110
|
-
return node.innerPattern
|
|
111
|
-
? `(${typeStr} ${node.innerPattern.accept(this)})`
|
|
112
|
-
: typeStr;
|
|
113
|
-
}
|
|
114
|
-
visit(node) {
|
|
115
|
-
return node.accept(this);
|
|
116
|
-
}
|
|
117
|
-
fallback(node) {
|
|
118
|
-
throw new UnexpectedNode(node.constructor.name, "PatternResolver");
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Recursively matches a value against a pattern node.
|
|
123
|
-
* Updates `bindings` when variables are bound successfully.
|
|
124
|
-
* Returns true if the pattern matches, false otherwise.
|
|
125
|
-
*/
|
|
126
|
-
export class PatternMatcher {
|
|
127
|
-
value;
|
|
128
|
-
bindings;
|
|
129
|
-
ctx;
|
|
130
|
-
constructor(value, bindings, ctx) {
|
|
131
|
-
this.value = value;
|
|
132
|
-
this.bindings = bindings;
|
|
133
|
-
this.ctx = ctx;
|
|
134
|
-
}
|
|
135
|
-
visitVariablePattern(node) {
|
|
136
|
-
return (k) => {
|
|
137
|
-
this.bindings.push([node.name.value, this.value]);
|
|
138
|
-
return k(true);
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
visitWildcardPattern(node) {
|
|
142
|
-
return (k) => k(true);
|
|
143
|
-
}
|
|
144
|
-
visitLiteralPattern(node) {
|
|
145
|
-
return (k) => {
|
|
146
|
-
const literalValue = InterpreterVisitor.evaluateLiteral(node.name);
|
|
147
|
-
return this.ctx.lazyRuntime.deepEqual(this.value, literalValue, k);
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
visitTuplePattern(node) {
|
|
151
|
-
return (k) => {
|
|
152
|
-
const processValue = (val) => {
|
|
153
|
-
if (!Array.isArray(val))
|
|
154
|
-
return k(false);
|
|
155
|
-
if (val.length !== node.elements.length)
|
|
156
|
-
return k(false);
|
|
157
|
-
const matchNext = (index) => {
|
|
158
|
-
if (index >= node.elements.length)
|
|
159
|
-
return k(true);
|
|
160
|
-
const matcher = new PatternMatcher(val[index], this.bindings, this.ctx);
|
|
161
|
-
return node.elements[index].accept(matcher)((isMatch) => {
|
|
162
|
-
if (!isMatch)
|
|
163
|
-
return k(false);
|
|
164
|
-
return () => matchNext(index + 1);
|
|
165
|
-
});
|
|
166
|
-
};
|
|
167
|
-
return matchNext(0);
|
|
168
|
-
};
|
|
169
|
-
if (isLazyList(this.value)) {
|
|
170
|
-
return this.ctx.lazyRuntime.realizeList(this.value, (val) => {
|
|
171
|
-
return () => processValue(val);
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
return processValue(this.value);
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
visitListPattern(node) {
|
|
178
|
-
return (k) => {
|
|
179
|
-
const value = this.value;
|
|
180
|
-
const neededLength = node.elements.length;
|
|
181
|
-
const finishMatching = (valArr) => {
|
|
182
|
-
if (valArr.length !== neededLength)
|
|
183
|
-
return k(false);
|
|
184
|
-
return this.matchList(node.elements, valArr, k);
|
|
185
|
-
};
|
|
186
|
-
// empty list case
|
|
187
|
-
if (neededLength === 0) {
|
|
188
|
-
if (Array.isArray(value) || typeof value === "string")
|
|
189
|
-
return k(value.length === 0);
|
|
190
|
-
if (isLazyList(value)) {
|
|
191
|
-
const iter = value.generator();
|
|
192
|
-
return k(Boolean(iter.next().done));
|
|
193
|
-
}
|
|
194
|
-
return k(false);
|
|
195
|
-
}
|
|
196
|
-
if (Array.isArray(value))
|
|
197
|
-
return finishMatching(value);
|
|
198
|
-
if (typeof value === "string")
|
|
199
|
-
return finishMatching(value.split(""));
|
|
200
|
-
if (isLazyList(value)) {
|
|
201
|
-
return this.ctx.lazyRuntime.realizeList(value, (valArr) => {
|
|
202
|
-
return () => finishMatching(valArr);
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
return k(false);
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
matchList(elements, value, k) {
|
|
209
|
-
if (value.length !== elements.length)
|
|
210
|
-
return k(false);
|
|
211
|
-
const matchNext = (index) => {
|
|
212
|
-
if (index >= elements.length)
|
|
213
|
-
return k(true);
|
|
214
|
-
const matcher = new PatternMatcher(value[index], this.bindings, this.ctx);
|
|
215
|
-
return elements[index].accept(matcher)((isMatch) => {
|
|
216
|
-
if (!isMatch)
|
|
217
|
-
return k(false);
|
|
218
|
-
return () => matchNext(index + 1);
|
|
219
|
-
});
|
|
220
|
-
};
|
|
221
|
-
return matchNext(0);
|
|
222
|
-
}
|
|
223
|
-
visitConsPattern(node) {
|
|
224
|
-
return (k) => {
|
|
225
|
-
const [head, tail] = this.resolveCons(this.value);
|
|
226
|
-
if (head === null || tail === null)
|
|
227
|
-
return k(false);
|
|
228
|
-
const headMatcher = new PatternMatcher(head, this.bindings, this.ctx);
|
|
229
|
-
return node.left.accept(headMatcher)((headMatches) => {
|
|
230
|
-
if (!headMatches)
|
|
231
|
-
return k(false);
|
|
232
|
-
const tailMatcher = new PatternMatcher(tail, this.bindings, this.ctx);
|
|
233
|
-
return node.right.accept(tailMatcher)(k);
|
|
234
|
-
});
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
visitTypePattern(node) {
|
|
238
|
-
return (k) => {
|
|
239
|
-
const actualType = getYukigoType(this.value);
|
|
240
|
-
let matches = false;
|
|
241
|
-
const targetType = node.targetType;
|
|
242
|
-
if (targetType instanceof SimpleType) {
|
|
243
|
-
matches = targetType.value === actualType;
|
|
244
|
-
}
|
|
245
|
-
else if (targetType instanceof ListType) {
|
|
246
|
-
matches = actualType === "YuList";
|
|
247
|
-
}
|
|
248
|
-
if (!matches)
|
|
249
|
-
return k(false);
|
|
250
|
-
if (node.innerPattern) {
|
|
251
|
-
const innerMatcher = new PatternMatcher(this.value, this.bindings, this.ctx);
|
|
252
|
-
return node.innerPattern.accept(innerMatcher)(k);
|
|
253
|
-
}
|
|
254
|
-
return k(true);
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
resolveCons(list) {
|
|
258
|
-
if (Array.isArray(list)) {
|
|
259
|
-
if (list.length === 0)
|
|
260
|
-
return [null, null];
|
|
261
|
-
const isLazy = this.ctx.config.lazyLoading;
|
|
262
|
-
if (!isLazy)
|
|
263
|
-
return [list[0], list.slice(1)];
|
|
264
|
-
const tail = {
|
|
265
|
-
type: "LazyList",
|
|
266
|
-
generator: function* () {
|
|
267
|
-
for (let i = 1; i < list.length; i++)
|
|
268
|
-
yield list[i];
|
|
269
|
-
},
|
|
270
|
-
};
|
|
271
|
-
return [list[0], tail];
|
|
272
|
-
}
|
|
273
|
-
if (typeof list === "string") {
|
|
274
|
-
if (list.length === 0)
|
|
275
|
-
return [null, null];
|
|
276
|
-
const isLazy = this.ctx.config.lazyLoading;
|
|
277
|
-
if (!isLazy)
|
|
278
|
-
return [list[0], list.slice(1)];
|
|
279
|
-
const tail = {
|
|
280
|
-
type: "LazyList",
|
|
281
|
-
generator: function* () {
|
|
282
|
-
for (let i = 1; i < list.length; i++)
|
|
283
|
-
yield list[i];
|
|
284
|
-
},
|
|
285
|
-
};
|
|
286
|
-
return [list[0], tail];
|
|
287
|
-
}
|
|
288
|
-
// lazy list case
|
|
289
|
-
if (isLazyList(list)) {
|
|
290
|
-
let memoList;
|
|
291
|
-
// optimize, convert to memoized
|
|
292
|
-
if (isMemoizedList(list)) {
|
|
293
|
-
memoList = list;
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
memoList = createMemoizedStream(list.generator);
|
|
297
|
-
}
|
|
298
|
-
const currentRes = memoList._sequence.get(memoList._offset);
|
|
299
|
-
if (currentRes.done)
|
|
300
|
-
return [null, null];
|
|
301
|
-
const tail = createMemoizedStream(list.generator, memoList._sequence, memoList._offset + 1);
|
|
302
|
-
return [currentRes.value, tail];
|
|
303
|
-
}
|
|
304
|
-
return [null, null];
|
|
305
|
-
}
|
|
306
|
-
visitConstructorPattern(node) {
|
|
307
|
-
return (k) => {
|
|
308
|
-
if (!Array.isArray(this.value) || this.value.length === 0)
|
|
309
|
-
return k(false);
|
|
310
|
-
if (this.value[0] !== node.identifier.value)
|
|
311
|
-
return k(false);
|
|
312
|
-
const args = this.value.slice(1);
|
|
313
|
-
return this.matchList(node.args, args, k);
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
visitFunctorPattern(node) {
|
|
317
|
-
return this.visitConstructorPattern(new ConstructorPattern(node.identifier, node.args));
|
|
318
|
-
}
|
|
319
|
-
visitApplicationPattern(node) {
|
|
320
|
-
return this.visitConstructorPattern(new ConstructorPattern(node.identifier, node.args));
|
|
321
|
-
}
|
|
322
|
-
visitAsPattern(node) {
|
|
323
|
-
return (k) => {
|
|
324
|
-
const innerMatcher = new PatternMatcher(this.value, this.bindings, this.ctx);
|
|
325
|
-
return node.right.accept(innerMatcher)((innerMatches) => {
|
|
326
|
-
if (!innerMatches)
|
|
327
|
-
return k(false);
|
|
328
|
-
const aliasMatcher = new PatternMatcher(this.value, this.bindings, this.ctx);
|
|
329
|
-
return node.left.accept(aliasMatcher)(k);
|
|
330
|
-
});
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
visitUnionPattern(node) {
|
|
334
|
-
return (k) => {
|
|
335
|
-
const tryNext = (index) => {
|
|
336
|
-
if (index >= node.elements.length)
|
|
337
|
-
return k(false);
|
|
338
|
-
const pattern = node.elements[index];
|
|
339
|
-
const trialBindings = [];
|
|
340
|
-
const matcher = new PatternMatcher(this.value, trialBindings, this.ctx);
|
|
341
|
-
return pattern.accept(matcher)((isMatch) => {
|
|
342
|
-
if (isMatch) {
|
|
343
|
-
this.bindings.push(...trialBindings);
|
|
344
|
-
return k(true);
|
|
345
|
-
}
|
|
346
|
-
return () => tryNext(index + 1);
|
|
347
|
-
});
|
|
348
|
-
};
|
|
349
|
-
return tryNext(0);
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
visit(node) {
|
|
353
|
-
return node.accept(this);
|
|
354
|
-
}
|
|
355
|
-
fallback(node) {
|
|
356
|
-
throw new UnexpectedNode(node.constructor.name, "PatternMatcher");
|
|
357
|
-
}
|
|
358
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Environment, EnvStack, PrimitiveValue } from "yukigo-ast";
|
|
2
|
-
import { FunctionRuntime } from "./runtimes/FunctionRuntime.js";
|
|
3
|
-
import { LazyRuntime } from "./runtimes/LazyRuntime.js";
|
|
4
|
-
import { ObjectRuntime } from "./runtimes/ObjectRuntime.js";
|
|
5
|
-
export declare const DefaultConfiguration: Required<InterpreterConfig>;
|
|
6
|
-
export type LogicSearchMode = "first" | "all" | "stream";
|
|
7
|
-
export interface InterpreterConfig {
|
|
8
|
-
lazyLoading?: boolean;
|
|
9
|
-
debug?: boolean;
|
|
10
|
-
outputMode?: LogicSearchMode;
|
|
11
|
-
mutability?: boolean;
|
|
12
|
-
}
|
|
13
|
-
export declare class UninitializedConfig extends Error {
|
|
14
|
-
constructor();
|
|
15
|
-
}
|
|
16
|
-
export declare class ReinitializedConfig extends Error {
|
|
17
|
-
constructor();
|
|
18
|
-
}
|
|
19
|
-
export declare class RuntimeContext {
|
|
20
|
-
readonly config: InterpreterConfig;
|
|
21
|
-
env: EnvStack;
|
|
22
|
-
lazyRuntime: LazyRuntime;
|
|
23
|
-
funcRuntime: FunctionRuntime;
|
|
24
|
-
objRuntime: ObjectRuntime;
|
|
25
|
-
constructor(config?: InterpreterConfig);
|
|
26
|
-
setEnv(env: EnvStack): void;
|
|
27
|
-
isDefined(name: string): boolean;
|
|
28
|
-
pushEnv(frame?: Environment): void;
|
|
29
|
-
replace(name: string, value: PrimitiveValue, onReplace?: (env: Environment) => void): boolean;
|
|
30
|
-
popEnv(): void;
|
|
31
|
-
lookup(name: string): PrimitiveValue;
|
|
32
|
-
remove(name: string): void;
|
|
33
|
-
define(name: string, value: PrimitiveValue): void;
|
|
34
|
-
clone(env?: EnvStack): EnvStack;
|
|
35
|
-
}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { FunctionRuntime } from "./runtimes/FunctionRuntime.js";
|
|
2
|
-
import { LazyRuntime } from "./runtimes/LazyRuntime.js";
|
|
3
|
-
import { ObjectRuntime } from "./runtimes/ObjectRuntime.js";
|
|
4
|
-
import { createGlobalEnv } from "../utils.js";
|
|
5
|
-
import { UnboundVariable } from "../errors.js";
|
|
6
|
-
export const DefaultConfiguration = {
|
|
7
|
-
lazyLoading: false,
|
|
8
|
-
debug: false,
|
|
9
|
-
outputMode: "first",
|
|
10
|
-
mutability: true,
|
|
11
|
-
};
|
|
12
|
-
export class UninitializedConfig extends Error {
|
|
13
|
-
constructor() {
|
|
14
|
-
super("GlobalConfig was not initialized. You must call initialize() first.");
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export class ReinitializedConfig extends Error {
|
|
18
|
-
constructor() {
|
|
19
|
-
super("GlobalConfig is already initialized. You cannot change it at execution.");
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
export class RuntimeContext {
|
|
23
|
-
config = DefaultConfiguration;
|
|
24
|
-
env;
|
|
25
|
-
lazyRuntime;
|
|
26
|
-
funcRuntime;
|
|
27
|
-
objRuntime;
|
|
28
|
-
constructor(config) {
|
|
29
|
-
this.config = Object.freeze({ ...DefaultConfiguration, ...config });
|
|
30
|
-
this.lazyRuntime = new LazyRuntime(this);
|
|
31
|
-
this.funcRuntime = new FunctionRuntime(this);
|
|
32
|
-
this.objRuntime = new ObjectRuntime(this);
|
|
33
|
-
this.env = createGlobalEnv();
|
|
34
|
-
}
|
|
35
|
-
setEnv(env) {
|
|
36
|
-
this.env = env;
|
|
37
|
-
}
|
|
38
|
-
isDefined(name) {
|
|
39
|
-
let current = this.env;
|
|
40
|
-
while (current !== null) {
|
|
41
|
-
if (current.head.has(name))
|
|
42
|
-
return true;
|
|
43
|
-
current = current.tail;
|
|
44
|
-
}
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
pushEnv(frame = new Map()) {
|
|
48
|
-
this.env = {
|
|
49
|
-
head: frame,
|
|
50
|
-
tail: this.env,
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
replace(name, value, onReplace) {
|
|
54
|
-
let current = this.env;
|
|
55
|
-
while (current !== null) {
|
|
56
|
-
if (current.head.has(name)) {
|
|
57
|
-
current.head.set(name, value);
|
|
58
|
-
if (onReplace)
|
|
59
|
-
onReplace(current.head);
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
current = current.tail;
|
|
63
|
-
}
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
popEnv() {
|
|
67
|
-
if (!this.env.tail)
|
|
68
|
-
throw new Error("Runtime Error: Cannot pop the global environment scope.");
|
|
69
|
-
this.env = this.env.tail;
|
|
70
|
-
}
|
|
71
|
-
lookup(name) {
|
|
72
|
-
let current = this.env;
|
|
73
|
-
while (current !== null) {
|
|
74
|
-
if (current.head.has(name))
|
|
75
|
-
return current.head.get(name);
|
|
76
|
-
current = current.tail;
|
|
77
|
-
}
|
|
78
|
-
throw new UnboundVariable(name);
|
|
79
|
-
}
|
|
80
|
-
remove(name) {
|
|
81
|
-
this.env.head.delete(name);
|
|
82
|
-
}
|
|
83
|
-
define(name, value) {
|
|
84
|
-
this.env.head.set(name, value);
|
|
85
|
-
}
|
|
86
|
-
clone(env) {
|
|
87
|
-
const target = env ?? this.env;
|
|
88
|
-
return {
|
|
89
|
-
head: new Map(target.head),
|
|
90
|
-
tail: this.env.tail,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Assert, ASTNode, PrimitiveValue, Test, TestGroup, TraverseVisitor } from "yukigo-ast";
|
|
2
|
-
import { InterpreterVisitor } from "./Visitor.js";
|
|
3
|
-
import { CPSThunk } from "../trampoline.js";
|
|
4
|
-
import { LazyRuntime } from "./runtimes/LazyRuntime.js";
|
|
5
|
-
export declare class FailedAssert extends Error {
|
|
6
|
-
actual?: PrimitiveValue;
|
|
7
|
-
expected?: PrimitiveValue;
|
|
8
|
-
constructor(actual?: PrimitiveValue, expected?: PrimitiveValue, message?: string);
|
|
9
|
-
}
|
|
10
|
-
export declare class TestRunner extends TraverseVisitor {
|
|
11
|
-
interpreter: InterpreterVisitor;
|
|
12
|
-
private lazyRuntime;
|
|
13
|
-
constructor(interpreter: InterpreterVisitor, lazyRuntime: LazyRuntime);
|
|
14
|
-
run(node: TestGroup | Test | Assert): CPSThunk<void>;
|
|
15
|
-
visitTestGroup(node: TestGroup): CPSThunk<void>;
|
|
16
|
-
visitTest(node: Test): CPSThunk<void>;
|
|
17
|
-
visitAssert(node: Assert): CPSThunk<void>;
|
|
18
|
-
fallback(node: ASTNode): CPSThunk<PrimitiveValue>;
|
|
19
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { TraverseVisitor, } from "yukigo-ast";
|
|
2
|
-
import { idContinuation, trampoline, } from "../trampoline.js";
|
|
3
|
-
import { UnexpectedNode } from "../../utils/helpers.js";
|
|
4
|
-
export class FailedAssert extends Error {
|
|
5
|
-
actual;
|
|
6
|
-
expected;
|
|
7
|
-
constructor(actual, expected, message) {
|
|
8
|
-
super(message || `Assertion failed: expected ${expected}, got ${actual}`);
|
|
9
|
-
this.actual = actual;
|
|
10
|
-
this.expected = expected;
|
|
11
|
-
this.name = "FailedAssert";
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
class AssertionVisitor extends TraverseVisitor {
|
|
15
|
-
interpreter;
|
|
16
|
-
negated;
|
|
17
|
-
lazyRuntime;
|
|
18
|
-
constructor(interpreter, negated, lazyRuntime) {
|
|
19
|
-
super();
|
|
20
|
-
this.interpreter = interpreter;
|
|
21
|
-
this.negated = negated;
|
|
22
|
-
this.lazyRuntime = lazyRuntime;
|
|
23
|
-
}
|
|
24
|
-
visitFailure(node) {
|
|
25
|
-
return (k) => () => {
|
|
26
|
-
let threw = false;
|
|
27
|
-
let actualError;
|
|
28
|
-
try {
|
|
29
|
-
// We use trampoline here to execute the function under test.
|
|
30
|
-
// While this is a nested trampoline, it's necessary to capture the error
|
|
31
|
-
// and isolate the test execution from the test runner's CPS flow.
|
|
32
|
-
trampoline(this.interpreter.evaluate(node.func, idContinuation));
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
threw = true;
|
|
36
|
-
actualError = error.message;
|
|
37
|
-
}
|
|
38
|
-
return this.interpreter.evaluate(node.message, (expectedError) => {
|
|
39
|
-
const passed = threw &&
|
|
40
|
-
(expectedError === undefined ||
|
|
41
|
-
actualError?.includes(expectedError));
|
|
42
|
-
if (this.negated === passed) {
|
|
43
|
-
if (!threw) {
|
|
44
|
-
throw new FailedAssert(undefined, expectedError, "Expected code to fail, but it succeeded");
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
throw new FailedAssert(actualError, expectedError, `Expected error message to contain "${expectedError}", but got "${actualError}"`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return k(undefined);
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
visitEquality(node) {
|
|
55
|
-
return (k) => this.interpreter.evaluate(node.value, (value) => {
|
|
56
|
-
return () => this.interpreter.evaluate(node.expected, (expected) => {
|
|
57
|
-
this.lazyRuntime.deepEqual(value, expected, (passed) => {
|
|
58
|
-
if (this.negated === passed) {
|
|
59
|
-
throw new FailedAssert(value, expected, this.negated
|
|
60
|
-
? `Expected ${JSON.stringify(value)} NOT to be equal to ${JSON.stringify(expected)}`
|
|
61
|
-
: `Expected ${JSON.stringify(expected)}, but got ${JSON.stringify(value)}`);
|
|
62
|
-
}
|
|
63
|
-
return k(undefined);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
visitTruth(node) {
|
|
69
|
-
return (k) => this.interpreter.evaluate(node.body, (value) => {
|
|
70
|
-
const isTruthy = Boolean(value);
|
|
71
|
-
if (this.negated === isTruthy) {
|
|
72
|
-
throw new FailedAssert(value, !this.negated, this.negated
|
|
73
|
-
? `Expected value to be falsy, but got ${JSON.stringify(value)}`
|
|
74
|
-
: `Expected value to be truthy, but got ${JSON.stringify(value)}`);
|
|
75
|
-
}
|
|
76
|
-
return k(undefined);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
fallback(node) {
|
|
80
|
-
throw new UnexpectedNode(node.constructor.name, "AssertionVisitor");
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
export class TestRunner extends TraverseVisitor {
|
|
84
|
-
interpreter;
|
|
85
|
-
lazyRuntime;
|
|
86
|
-
constructor(interpreter, lazyRuntime) {
|
|
87
|
-
super();
|
|
88
|
-
this.interpreter = interpreter;
|
|
89
|
-
this.lazyRuntime = lazyRuntime;
|
|
90
|
-
}
|
|
91
|
-
run(node) {
|
|
92
|
-
return node.accept(this);
|
|
93
|
-
}
|
|
94
|
-
visitTestGroup(node) {
|
|
95
|
-
return (k) => this.interpreter.evaluate(node.group, () => k(undefined));
|
|
96
|
-
}
|
|
97
|
-
visitTest(node) {
|
|
98
|
-
return (k) => this.interpreter.evaluate(node.body, () => k(undefined));
|
|
99
|
-
}
|
|
100
|
-
visitAssert(node) {
|
|
101
|
-
return (k) => this.interpreter.evaluate(node.negated, (negatedVal) => {
|
|
102
|
-
const isNegated = Boolean(negatedVal);
|
|
103
|
-
const visitor = new AssertionVisitor(this.interpreter, isNegated, this.lazyRuntime);
|
|
104
|
-
return node.body.accept(visitor)(k);
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
fallback(node) {
|
|
108
|
-
throw new UnexpectedNode(node.constructor.name, "TestRunner");
|
|
109
|
-
}
|
|
110
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { Visitor, PrimitiveValue, NumberPrimitive, BooleanPrimitive, StringPrimitive, ListPrimitive, NilPrimitive, SymbolPrimitive, Variable, CharPrimitive, ArithmeticUnaryOperation, ArithmeticBinaryOperation, ListUnaryOperation, ListBinaryOperation, ComparisonOperation, LogicalBinaryOperation, LogicalUnaryOperation, BitwiseBinaryOperation, BitwiseUnaryOperation, StringOperation, UnifyOperation, AssignOperation, Assignment, TupleExpression, FieldExpression, DataExpression, ConsExpression, LetInExpression, Call, Otherwise, CompositionExpression, Expression, Application, Lambda, Sequence, Exist, Not, Findall, Forall, Goal, Send, New, Self, ListComprehension, RangeExpression, Generator as YuGenerator, ASTNode, Raise, Query, TypeCast, Super, If, Assert, Test, TestGroup, LogicConstraint } from "yukigo-ast";
|
|
2
|
-
import { ExpressionEvaluator } from "../utils.js";
|
|
3
|
-
import { ErrorFrame } from "../errors.js";
|
|
4
|
-
import { Continuation, CPSThunk, Thunk } from "../trampoline.js";
|
|
5
|
-
import { RuntimeContext } from "./RuntimeContext.js";
|
|
6
|
-
export declare class InterpreterVisitor implements Visitor<CPSThunk<PrimitiveValue>>, ExpressionEvaluator {
|
|
7
|
-
private context;
|
|
8
|
-
private frames;
|
|
9
|
-
constructor(context: RuntimeContext, frames?: ErrorFrame[]);
|
|
10
|
-
evaluate<R = PrimitiveValue>(node: ASTNode, cont: Continuation<PrimitiveValue, R>): Thunk<R>;
|
|
11
|
-
visitSequence(node: Sequence): CPSThunk<PrimitiveValue>;
|
|
12
|
-
visitAssert(node: Assert): CPSThunk<PrimitiveValue>;
|
|
13
|
-
visitTest(node: Test): CPSThunk<PrimitiveValue>;
|
|
14
|
-
visitTestGroup(node: TestGroup): CPSThunk<PrimitiveValue>;
|
|
15
|
-
visitNumberPrimitive(node: NumberPrimitive): CPSThunk<PrimitiveValue>;
|
|
16
|
-
visitBooleanPrimitive(node: BooleanPrimitive): CPSThunk<PrimitiveValue>;
|
|
17
|
-
visitStringPrimitive(node: StringPrimitive): CPSThunk<PrimitiveValue>;
|
|
18
|
-
visitListPrimitive(node: ListPrimitive): CPSThunk<PrimitiveValue>;
|
|
19
|
-
visitNilPrimitive(node: NilPrimitive): CPSThunk<PrimitiveValue>;
|
|
20
|
-
visitCharPrimitive(node: CharPrimitive): CPSThunk<PrimitiveValue>;
|
|
21
|
-
visitSymbolPrimitive(node: SymbolPrimitive): CPSThunk<PrimitiveValue>;
|
|
22
|
-
visitVariable(node: Variable): CPSThunk<PrimitiveValue>;
|
|
23
|
-
visitAssignment(node: Assignment): CPSThunk<PrimitiveValue>;
|
|
24
|
-
visitArithmeticUnaryOperation(node: ArithmeticUnaryOperation): CPSThunk<PrimitiveValue>;
|
|
25
|
-
visitArithmeticBinaryOperation(node: ArithmeticBinaryOperation): CPSThunk<PrimitiveValue>;
|
|
26
|
-
visitListUnaryOperation(node: ListUnaryOperation): CPSThunk<PrimitiveValue>;
|
|
27
|
-
visitListBinaryOperation(node: ListBinaryOperation): CPSThunk<PrimitiveValue>;
|
|
28
|
-
visitComparisonOperation(node: ComparisonOperation): CPSThunk<PrimitiveValue>;
|
|
29
|
-
visitLogicalBinaryOperation(node: LogicalBinaryOperation): CPSThunk<PrimitiveValue>;
|
|
30
|
-
visitLogicalUnaryOperation(node: LogicalUnaryOperation): CPSThunk<PrimitiveValue>;
|
|
31
|
-
visitBitwiseBinaryOperation(node: BitwiseBinaryOperation): CPSThunk<PrimitiveValue>;
|
|
32
|
-
visitBitwiseUnaryOperation(node: BitwiseUnaryOperation): CPSThunk<PrimitiveValue>;
|
|
33
|
-
visitStringOperation(node: StringOperation): CPSThunk<PrimitiveValue>;
|
|
34
|
-
visitUnifyOperation(node: UnifyOperation): CPSThunk<PrimitiveValue>;
|
|
35
|
-
visitAssignOperation(node: AssignOperation): CPSThunk<PrimitiveValue>;
|
|
36
|
-
visitTupleExpr(node: TupleExpression): CPSThunk<PrimitiveValue>;
|
|
37
|
-
visitFieldExpression(node: FieldExpression): CPSThunk<PrimitiveValue>;
|
|
38
|
-
visitDataExpr(node: DataExpression): CPSThunk<PrimitiveValue>;
|
|
39
|
-
visitConsExpr(node: ConsExpression): CPSThunk<PrimitiveValue>;
|
|
40
|
-
visitLetInExpr(node: LetInExpression): CPSThunk<PrimitiveValue>;
|
|
41
|
-
visitIf(node: If): CPSThunk<PrimitiveValue>;
|
|
42
|
-
visitCall(node: Call): CPSThunk<PrimitiveValue>;
|
|
43
|
-
visitOtherwise(node: Otherwise): CPSThunk<PrimitiveValue>;
|
|
44
|
-
visitCompositionExpression(node: CompositionExpression): CPSThunk<PrimitiveValue>;
|
|
45
|
-
visitLambda(node: Lambda): CPSThunk<PrimitiveValue>;
|
|
46
|
-
visitApplication(node: Application): CPSThunk<PrimitiveValue>;
|
|
47
|
-
visitQuery(node: Query): CPSThunk<PrimitiveValue>;
|
|
48
|
-
visitExist(node: Exist): CPSThunk<PrimitiveValue>;
|
|
49
|
-
visitNot(node: Not): CPSThunk<PrimitiveValue>;
|
|
50
|
-
visitFindall(node: Findall): CPSThunk<PrimitiveValue>;
|
|
51
|
-
visitForall(node: Forall): CPSThunk<PrimitiveValue>;
|
|
52
|
-
visitGoal(node: Goal): CPSThunk<PrimitiveValue>;
|
|
53
|
-
private bindLogicResults;
|
|
54
|
-
visitLogicConstraint(node: LogicConstraint): CPSThunk<PrimitiveValue>;
|
|
55
|
-
visitSuper(node: Super): CPSThunk<PrimitiveValue>;
|
|
56
|
-
visitSend(node: Send): CPSThunk<PrimitiveValue>;
|
|
57
|
-
visitNew(node: New): CPSThunk<PrimitiveValue>;
|
|
58
|
-
visitSelf(node: Self): CPSThunk<PrimitiveValue>;
|
|
59
|
-
visitListComprehension(node: ListComprehension): CPSThunk<PrimitiveValue>;
|
|
60
|
-
visitTypeCast(node: TypeCast): CPSThunk<PrimitiveValue>;
|
|
61
|
-
visitGenerator(node: YuGenerator): CPSThunk<PrimitiveValue>;
|
|
62
|
-
visitRaise(node: Raise): CPSThunk<PrimitiveValue>;
|
|
63
|
-
visitRangeExpression(node: RangeExpression): CPSThunk<PrimitiveValue>;
|
|
64
|
-
visit(node: Expression): CPSThunk<PrimitiveValue>;
|
|
65
|
-
fallback(node: ASTNode): CPSThunk<PrimitiveValue>;
|
|
66
|
-
realizeList<R = PrimitiveValue[]>(val: PrimitiveValue, k: Continuation<PrimitiveValue[], R>): Thunk<R>;
|
|
67
|
-
private processBinary;
|
|
68
|
-
private processUnary;
|
|
69
|
-
private getLogicEngine;
|
|
70
|
-
static evaluateLiteral(node: ASTNode): PrimitiveValue;
|
|
71
|
-
}
|