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
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { NamedArgument, Return, } from "yukigo-ast";
|
|
2
|
+
import { BacktrackCommand, BindCommand, CatchCommand, EvalCommand, FailCommand, RaiseCommand, StepCommand, } from "../kernel/commands.js";
|
|
3
|
+
import { LogicAnswer, LogicResult, RuntimeClass, RuntimeObject, YuBoolean, YuNil, YuString, } from "../../primitives/index.js";
|
|
4
|
+
import { InterpreterError } from "../../errors.js";
|
|
5
|
+
import { boolean, isTrue } from "../../utils.js";
|
|
6
|
+
import { OOPCollector } from "../EnvBuilder.js";
|
|
7
|
+
import { getClassFields, getClassInitializers, getLogicEngine } from "./utils.js";
|
|
8
|
+
export function StatementEvaluator(Base) {
|
|
9
|
+
return class extends Base {
|
|
10
|
+
visitSequence(node) {
|
|
11
|
+
if (node.statements.length === 0)
|
|
12
|
+
return new StepCommand(YuNil.getInstance());
|
|
13
|
+
const evaluateNext = (index, lastResult) => {
|
|
14
|
+
if (index >= node.statements.length)
|
|
15
|
+
return new StepCommand(lastResult);
|
|
16
|
+
const stmt = node.statements[index];
|
|
17
|
+
return new BindCommand(this.evaluate(stmt), (result) => {
|
|
18
|
+
if (stmt instanceof Return)
|
|
19
|
+
return new StepCommand(result);
|
|
20
|
+
return evaluateNext(index + 1, result);
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
return evaluateNext(0, YuNil.getInstance());
|
|
24
|
+
}
|
|
25
|
+
visitSelf(node) {
|
|
26
|
+
try {
|
|
27
|
+
return new StepCommand(this.context.lookup("self"));
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return new FailCommand(new InterpreterError("Self", "'self' is not defined in this context"));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
visitSuper(node) {
|
|
34
|
+
let methodName;
|
|
35
|
+
try {
|
|
36
|
+
methodName = this.context.lookup("__METHOD_NAME__");
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
return new FailCommand(new InterpreterError("Super", "'super' keyword used outside of a method context"));
|
|
40
|
+
}
|
|
41
|
+
const args = [];
|
|
42
|
+
const evaluateNextArg = (index) => {
|
|
43
|
+
if (index >= node.args.length) {
|
|
44
|
+
return this.context.objRuntime.dispatchSuper(methodName.toJSON(), args);
|
|
45
|
+
}
|
|
46
|
+
return new BindCommand(this.evaluate(node.args[index]), (val) => {
|
|
47
|
+
args.push(val);
|
|
48
|
+
return evaluateNextArg(index + 1);
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
return evaluateNextArg(0);
|
|
52
|
+
}
|
|
53
|
+
visitIf(node) {
|
|
54
|
+
return new BindCommand(this.evaluate(node.condition), (condition) => {
|
|
55
|
+
if (!(condition instanceof YuBoolean))
|
|
56
|
+
return new FailCommand(new InterpreterError("If", `Expected boolean in condition and got ${condition.getType()}`));
|
|
57
|
+
const isTrue = condition.value;
|
|
58
|
+
return isTrue ? this.evaluate(node.then) : this.evaluate(node.elseExpr);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
visitReturn(node) {
|
|
62
|
+
if (!node.body)
|
|
63
|
+
return new StepCommand(YuNil.getInstance());
|
|
64
|
+
return this.evaluate(node.body);
|
|
65
|
+
}
|
|
66
|
+
visitFunction(node) {
|
|
67
|
+
return new StepCommand(YuNil.getInstance());
|
|
68
|
+
}
|
|
69
|
+
visitTry(node) {
|
|
70
|
+
const tryStartEnv = this.context.env;
|
|
71
|
+
// Comando que evaluará el cuerpo del Try (ej: block.apply())
|
|
72
|
+
const bodyCommand = new EvalCommand(node.body);
|
|
73
|
+
// Este handler es el que el Kernel llamará cuando reciba el RaiseCommand
|
|
74
|
+
const catchHandler = (errorObj) => {
|
|
75
|
+
this.context.env = tryStartEnv; // Restauramos entorno
|
|
76
|
+
// 1. Reificamos la excepción para Wollok
|
|
77
|
+
let exceptionObj;
|
|
78
|
+
if (errorObj instanceof InterpreterError) {
|
|
79
|
+
const mappedErrors = this.context.dispatchHook("onInterpreterError", errorObj, this.context);
|
|
80
|
+
exceptionObj = mappedErrors.find((res) => res !== undefined && res !== null);
|
|
81
|
+
}
|
|
82
|
+
if (!exceptionObj) {
|
|
83
|
+
if (errorObj instanceof InterpreterError) {
|
|
84
|
+
exceptionObj = new YuString(errorObj.message);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
exceptionObj = errorObj;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// 2. Buscamos si el Try tiene bloques catch
|
|
91
|
+
const catches = node.catchExpr;
|
|
92
|
+
if (catches.length > 0) {
|
|
93
|
+
const catchBlock = catches[0];
|
|
94
|
+
const pattern = catchBlock.patterns[0];
|
|
95
|
+
const paramName = pattern.name?.value ?? pattern.identifier?.value ?? String(pattern);
|
|
96
|
+
// Inyectamos la variable 'e' (excepción) en el scope
|
|
97
|
+
this.context.pushEnv();
|
|
98
|
+
this.context.define(paramName, exceptionObj);
|
|
99
|
+
// Retornamos la evaluación del catchBlock
|
|
100
|
+
return new BindCommand(new EvalCommand(catchBlock.body), (catchRes) => {
|
|
101
|
+
this.context.env = tryStartEnv;
|
|
102
|
+
// Si había bloque finally, podrías evaluarlo acá antes del StepCommand
|
|
103
|
+
if (node.finallyExpr) {
|
|
104
|
+
return new BindCommand(new EvalCommand(node.finallyExpr), () => new StepCommand(catchRes));
|
|
105
|
+
}
|
|
106
|
+
return new StepCommand(catchRes);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
// Si no había bloque catch (solo finally), relanzamos al Kernel superior
|
|
110
|
+
if (node.finallyExpr) {
|
|
111
|
+
return new BindCommand(new EvalCommand(node.finallyExpr), () => new RaiseCommand(errorObj));
|
|
112
|
+
}
|
|
113
|
+
return new RaiseCommand(errorObj);
|
|
114
|
+
};
|
|
115
|
+
// Le entregamos el CatchCommand al Kernel para que lo apile ANTES de ejecutar el cuerpo
|
|
116
|
+
return new CatchCommand(catchHandler, bodyCommand);
|
|
117
|
+
}
|
|
118
|
+
visitRaise(node) {
|
|
119
|
+
return new BindCommand(this.evaluate(node.body), (msg) => {
|
|
120
|
+
if (msg instanceof RuntimeObject)
|
|
121
|
+
return new RaiseCommand(msg);
|
|
122
|
+
const msgStr = msg.toJSON();
|
|
123
|
+
if (typeof msgStr !== "string") {
|
|
124
|
+
return new RaiseCommand(new InterpreterError("Raise", msg.toString()));
|
|
125
|
+
}
|
|
126
|
+
return new RaiseCommand(new InterpreterError("Raise", msgStr));
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
visitPrint(node) {
|
|
130
|
+
return new BindCommand(this.evaluate(node.expression), (res) => {
|
|
131
|
+
console.log(res);
|
|
132
|
+
return new StepCommand(YuNil.getInstance());
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
visitVariable(node) {
|
|
136
|
+
const name = node.identifier.value;
|
|
137
|
+
return new BindCommand(this.evaluate(node.expression), (value) => {
|
|
138
|
+
this.context.define(name, value);
|
|
139
|
+
return boolean(true);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
visitAssignment(node) {
|
|
143
|
+
if (!this.context.config.mutability) {
|
|
144
|
+
return new FailCommand(new InterpreterError("Assignment", `Cannot reassign variable '${node.identifier.value}': mutability is disabled`));
|
|
145
|
+
}
|
|
146
|
+
const name = node.identifier.value;
|
|
147
|
+
return new BindCommand(this.evaluate(node.expression), (value) => {
|
|
148
|
+
const onReplace = (scope) => {
|
|
149
|
+
if (scope.has("self")) {
|
|
150
|
+
const self = scope.get("self");
|
|
151
|
+
if (self instanceof RuntimeObject && self.fields.has(name))
|
|
152
|
+
self.fields.set(name, value);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
if (!this.context.replace(name, value, onReplace))
|
|
156
|
+
return new FailCommand(new InterpreterError("Assignment", `Cannot assign to undefined variable: ${name}`));
|
|
157
|
+
return new StepCommand(value);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
visitQuery(node) {
|
|
161
|
+
return getLogicEngine(this, this.context).solveQuery(node);
|
|
162
|
+
}
|
|
163
|
+
visitAttribute(node) {
|
|
164
|
+
const name = node.identifier.value;
|
|
165
|
+
return new BindCommand(this.evaluate(node.expression), (value) => {
|
|
166
|
+
this.context.define(name, value);
|
|
167
|
+
return boolean(true);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
visitObject(node) {
|
|
171
|
+
const identifier = node.identifier.value;
|
|
172
|
+
const collector = new OOPCollector(this.context);
|
|
173
|
+
node.expression.accept(collector);
|
|
174
|
+
const fields = collector.collectedFields;
|
|
175
|
+
const methods = collector.collectedMethods;
|
|
176
|
+
const extendsSymbol = node.extendsSymbol?.value;
|
|
177
|
+
if (extendsSymbol) {
|
|
178
|
+
const classDef = this.context.lookup(extendsSymbol);
|
|
179
|
+
if (classDef && classDef instanceof RuntimeClass) {
|
|
180
|
+
const classFields = getClassFields(classDef, this.context);
|
|
181
|
+
for (const [k, v] of classFields.entries()) {
|
|
182
|
+
if (!fields.has(k)) {
|
|
183
|
+
fields.set(k, v);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const runtimeObject = new RuntimeObject(identifier, extendsSymbol || "", fields, methods);
|
|
189
|
+
const extendsArgs = node.extendsArgs || [];
|
|
190
|
+
const evaluateArgs = (index) => {
|
|
191
|
+
if (index >= extendsArgs.length) {
|
|
192
|
+
this.context.define(identifier, runtimeObject);
|
|
193
|
+
return new StepCommand(runtimeObject);
|
|
194
|
+
}
|
|
195
|
+
const arg = extendsArgs[index];
|
|
196
|
+
if (arg instanceof NamedArgument ||
|
|
197
|
+
arg.constructor.name === "NamedArgument") {
|
|
198
|
+
const fieldName = arg.identifier.value;
|
|
199
|
+
return new BindCommand(this.evaluate(arg.expression), (val) => {
|
|
200
|
+
runtimeObject.setField(fieldName, val);
|
|
201
|
+
return evaluateArgs(index + 1);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return evaluateArgs(index + 1);
|
|
205
|
+
};
|
|
206
|
+
const evaluateClassInitializers = (classDef, keys, keyIndex) => {
|
|
207
|
+
if (keyIndex >= keys.length) {
|
|
208
|
+
return evaluateArgs(0);
|
|
209
|
+
}
|
|
210
|
+
const key = keys[keyIndex];
|
|
211
|
+
if (collector.fieldInitializers.has(key)) {
|
|
212
|
+
return evaluateClassInitializers(classDef, keys, keyIndex + 1);
|
|
213
|
+
}
|
|
214
|
+
const expr = classInitializers.get(key);
|
|
215
|
+
return new BindCommand(this.evaluate(expr), (val) => {
|
|
216
|
+
runtimeObject.setField(key, val);
|
|
217
|
+
return evaluateClassInitializers(classDef, keys, keyIndex + 1);
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
let classInitializers = new Map();
|
|
221
|
+
let classInitializerKeys = [];
|
|
222
|
+
if (extendsSymbol) {
|
|
223
|
+
const classDef = this.context.lookup(extendsSymbol);
|
|
224
|
+
if (classDef && classDef instanceof RuntimeClass) {
|
|
225
|
+
classInitializers = getClassInitializers(classDef, this.context);
|
|
226
|
+
classInitializerKeys = Array.from(classInitializers.keys());
|
|
227
|
+
return evaluateClassInitializers(classDef, classInitializerKeys, 0);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return evaluateArgs(0);
|
|
231
|
+
}
|
|
232
|
+
visitLogicConstraint(node) {
|
|
233
|
+
return new BindCommand(this.evaluate(node.expression), (val) => {
|
|
234
|
+
if (isTrue(val))
|
|
235
|
+
return new StepCommand(new LogicResult([new LogicAnswer(true, new Map())]));
|
|
236
|
+
return new BacktrackCommand();
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Assert, Test, TestGroup } from "yukigo-ast";
|
|
2
|
+
import { ExecutionCommand } from "../kernel/commands.js";
|
|
3
|
+
import { Constructor, EvaluatorBase } from "./BaseEvaluator.js";
|
|
4
|
+
export declare function TestingEvaluator<TBase extends Constructor<EvaluatorBase>>(Base: TBase): {
|
|
5
|
+
new (...args: any[]): {
|
|
6
|
+
visitTestGroup(node: TestGroup): ExecutionCommand;
|
|
7
|
+
visitTest(node: Test): ExecutionCommand;
|
|
8
|
+
visitAssert(node: Assert): ExecutionCommand;
|
|
9
|
+
context: import("../RuntimeContext.js").RuntimeContext;
|
|
10
|
+
getContext(): import("../RuntimeContext.js").RuntimeContext;
|
|
11
|
+
evaluate(node: import("yukigo-ast").ASTNode): ExecutionCommand;
|
|
12
|
+
realizeList(val: any): ExecutionCommand;
|
|
13
|
+
fallback(node: import("yukigo-ast").ASTNode): ExecutionCommand;
|
|
14
|
+
};
|
|
15
|
+
} & TBase;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TestRunner } from "../TestRunner.js";
|
|
2
|
+
export function TestingEvaluator(Base) {
|
|
3
|
+
return class extends Base {
|
|
4
|
+
visitTestGroup(node) {
|
|
5
|
+
return new TestRunner(this, this.context.lazyRuntime).visitTestGroup(node);
|
|
6
|
+
}
|
|
7
|
+
visitTest(node) {
|
|
8
|
+
return new TestRunner(this, this.context.lazyRuntime).visitTest(node);
|
|
9
|
+
}
|
|
10
|
+
visitAssert(node) {
|
|
11
|
+
return new TestRunner(this, this.context.lazyRuntime).visitAssert(node);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { ASTNode, Visitor } from "yukigo-ast";
|
|
2
|
+
import { EvaluatorBase } from "./BaseEvaluator.js";
|
|
3
|
+
import { ExecutionCommand } from "../kernel/commands.js";
|
|
4
|
+
import { RuntimeContext } from "../RuntimeContext.js";
|
|
5
|
+
import { YuValue } from "../../primitives/index.js";
|
|
6
|
+
declare const InterpreterVisitor_base: {
|
|
7
|
+
new (...args: any[]): {
|
|
8
|
+
visitTestGroup(node: import("yukigo-ast").TestGroup): ExecutionCommand;
|
|
9
|
+
visitTest(node: import("yukigo-ast").Test): ExecutionCommand;
|
|
10
|
+
visitAssert(node: import("yukigo-ast").Assert): ExecutionCommand;
|
|
11
|
+
context: RuntimeContext;
|
|
12
|
+
getContext(): RuntimeContext;
|
|
13
|
+
evaluate(node: ASTNode): ExecutionCommand;
|
|
14
|
+
realizeList(val: any): ExecutionCommand;
|
|
15
|
+
fallback(node: ASTNode): ExecutionCommand;
|
|
16
|
+
};
|
|
17
|
+
} & {
|
|
18
|
+
new (...args: any[]): {
|
|
19
|
+
visitSequence(node: import("yukigo-ast").Sequence): ExecutionCommand;
|
|
20
|
+
visitSelf(node: import("yukigo-ast").Self): ExecutionCommand;
|
|
21
|
+
visitSuper(node: import("yukigo-ast").Super): ExecutionCommand;
|
|
22
|
+
visitIf(node: import("yukigo-ast").If): ExecutionCommand;
|
|
23
|
+
visitReturn(node: import("yukigo-ast").Return): ExecutionCommand;
|
|
24
|
+
visitFunction(node: import("yukigo-ast").Function): ExecutionCommand;
|
|
25
|
+
visitTry(node: import("yukigo-ast").Try): ExecutionCommand;
|
|
26
|
+
visitRaise(node: import("yukigo-ast").Raise): ExecutionCommand;
|
|
27
|
+
visitPrint(node: import("yukigo-ast").Print): ExecutionCommand;
|
|
28
|
+
visitVariable(node: import("yukigo-ast").Variable): ExecutionCommand;
|
|
29
|
+
visitAssignment(node: import("yukigo-ast").Assignment): ExecutionCommand;
|
|
30
|
+
visitQuery(node: import("yukigo-ast").Query): ExecutionCommand;
|
|
31
|
+
visitAttribute(node: import("yukigo-ast").Attribute): ExecutionCommand;
|
|
32
|
+
visitObject(node: import("yukigo-ast").Object): ExecutionCommand;
|
|
33
|
+
visitLogicConstraint(node: import("yukigo-ast").LogicConstraint): ExecutionCommand;
|
|
34
|
+
context: RuntimeContext;
|
|
35
|
+
getContext(): RuntimeContext;
|
|
36
|
+
evaluate(node: ASTNode): ExecutionCommand;
|
|
37
|
+
realizeList(val: any): ExecutionCommand;
|
|
38
|
+
fallback(node: ASTNode): ExecutionCommand;
|
|
39
|
+
};
|
|
40
|
+
} & {
|
|
41
|
+
new (...args: any[]): {
|
|
42
|
+
visitTupleExpr(node: import("yukigo-ast").TupleExpression): ExecutionCommand;
|
|
43
|
+
visitFieldExpression(node: import("yukigo-ast").FieldExpression): ExecutionCommand;
|
|
44
|
+
visitDataExpr(node: import("yukigo-ast").DataExpression): ExecutionCommand;
|
|
45
|
+
visitConsExpr(node: import("yukigo-ast").ConsExpression): ExecutionCommand;
|
|
46
|
+
visitLetInExpr(node: import("yukigo-ast").LetInExpression): ExecutionCommand;
|
|
47
|
+
visitCall(node: import("yukigo-ast").Call): ExecutionCommand;
|
|
48
|
+
visitOtherwise(node: import("yukigo-ast").Otherwise): ExecutionCommand;
|
|
49
|
+
visitCompositionExpression(node: import("yukigo-ast").CompositionExpression): ExecutionCommand;
|
|
50
|
+
visitLambda(node: import("yukigo-ast").Lambda): ExecutionCommand;
|
|
51
|
+
visitGuardedExpression(expr: import("yukigo-ast").GuardedExpression): ExecutionCommand;
|
|
52
|
+
visitApplication(node: import("yukigo-ast").Application): ExecutionCommand;
|
|
53
|
+
visitExist(node: import("yukigo-ast").Exist): ExecutionCommand;
|
|
54
|
+
visitNot(node: import("yukigo-ast").Not): ExecutionCommand;
|
|
55
|
+
visitFindall(node: import("yukigo-ast").Findall): ExecutionCommand;
|
|
56
|
+
visitForall(node: import("yukigo-ast").Forall): ExecutionCommand;
|
|
57
|
+
visitGoal(node: import("yukigo-ast").Goal): ExecutionCommand;
|
|
58
|
+
visitSend(node: import("yukigo-ast").Send): ExecutionCommand;
|
|
59
|
+
visitNew(node: import("yukigo-ast").New): ExecutionCommand;
|
|
60
|
+
visitListComprehension(node: import("yukigo-ast").ListComprehension): ExecutionCommand;
|
|
61
|
+
visitGenerator(node: import("yukigo-ast").Generator): ExecutionCommand;
|
|
62
|
+
visitRangeExpression(node: import("yukigo-ast").RangeExpression): ExecutionCommand;
|
|
63
|
+
visitTypeCast(node: import("yukigo-ast").TypeCast): ExecutionCommand;
|
|
64
|
+
context: RuntimeContext;
|
|
65
|
+
getContext(): RuntimeContext;
|
|
66
|
+
evaluate(node: ASTNode): ExecutionCommand;
|
|
67
|
+
realizeList(val: any): ExecutionCommand;
|
|
68
|
+
fallback(node: ASTNode): ExecutionCommand;
|
|
69
|
+
};
|
|
70
|
+
} & {
|
|
71
|
+
new (...args: any[]): {
|
|
72
|
+
visitArithmeticUnaryOperation(node: import("yukigo-ast").ArithmeticUnaryOperation): ExecutionCommand;
|
|
73
|
+
visitListUnaryOperation(node: import("yukigo-ast").ListUnaryOperation): ExecutionCommand;
|
|
74
|
+
visitListBinaryOperation(node: import("yukigo-ast").ListBinaryOperation): ExecutionCommand;
|
|
75
|
+
visitComparisonOperation(node: import("yukigo-ast").ComparisonOperation): ExecutionCommand;
|
|
76
|
+
visitLogicalBinaryOperation(node: import("yukigo-ast").LogicalBinaryOperation): ExecutionCommand;
|
|
77
|
+
visitLogicalUnaryOperation(node: import("yukigo-ast").LogicalUnaryOperation): ExecutionCommand;
|
|
78
|
+
visitBitwiseBinaryOperation(node: import("yukigo-ast").BitwiseBinaryOperation): ExecutionCommand;
|
|
79
|
+
visitBitwiseUnaryOperation(node: import("yukigo-ast").BitwiseUnaryOperation): ExecutionCommand;
|
|
80
|
+
visitStringOperation(node: import("yukigo-ast").StringOperation): ExecutionCommand;
|
|
81
|
+
visitUnifyOperation(node: import("yukigo-ast").UnifyOperation): ExecutionCommand;
|
|
82
|
+
visitAssignOperation(node: import("yukigo-ast").AssignOperation): ExecutionCommand;
|
|
83
|
+
visitArithmeticBinaryOperation(node: import("yukigo-ast").ArithmeticBinaryOperation): ExecutionCommand;
|
|
84
|
+
context: RuntimeContext;
|
|
85
|
+
getContext(): RuntimeContext;
|
|
86
|
+
evaluate(node: ASTNode): ExecutionCommand;
|
|
87
|
+
realizeList(val: any): ExecutionCommand;
|
|
88
|
+
fallback(node: ASTNode): ExecutionCommand;
|
|
89
|
+
};
|
|
90
|
+
} & {
|
|
91
|
+
new (...args: any[]): {
|
|
92
|
+
visitStringPrimitive(node: import("yukigo-ast").StringPrimitive): ExecutionCommand;
|
|
93
|
+
visitListPrimitive(node: import("yukigo-ast").ListPrimitive): ExecutionCommand;
|
|
94
|
+
visitNilPrimitive(node: import("yukigo-ast").NilPrimitive): ExecutionCommand;
|
|
95
|
+
visitSymbolPrimitive(node: import("yukigo-ast").SymbolPrimitive): ExecutionCommand;
|
|
96
|
+
visitCharPrimitive(node: import("yukigo-ast").CharPrimitive): ExecutionCommand;
|
|
97
|
+
visitNumberPrimitive(node: import("yukigo-ast").NumberPrimitive): ExecutionCommand;
|
|
98
|
+
visitBooleanPrimitive(node: import("yukigo-ast").BooleanPrimitive): ExecutionCommand;
|
|
99
|
+
context: RuntimeContext;
|
|
100
|
+
getContext(): RuntimeContext;
|
|
101
|
+
evaluate(node: ASTNode): ExecutionCommand;
|
|
102
|
+
realizeList(val: any): ExecutionCommand;
|
|
103
|
+
fallback(node: ASTNode): ExecutionCommand;
|
|
104
|
+
};
|
|
105
|
+
} & typeof EvaluatorBase;
|
|
106
|
+
export declare class InterpreterVisitor extends InterpreterVisitor_base implements Visitor<ExecutionCommand> {
|
|
107
|
+
constructor(context: RuntimeContext);
|
|
108
|
+
static evaluateLiteral(node: ASTNode, ctx?: RuntimeContext): YuValue;
|
|
109
|
+
}
|
|
110
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EvaluatorBase } from "./BaseEvaluator.js";
|
|
2
|
+
import { ExpressionEvaluator } from "./ExpressionEvaluator.js";
|
|
3
|
+
import { OperationEvaluator } from "./OperationEvaluator.js";
|
|
4
|
+
import { PrimitiveEvaluator } from "./PrimitiveEvaluator.js";
|
|
5
|
+
import { StatementEvaluator } from "./StatementEvaluator.js";
|
|
6
|
+
import { TestingEvaluator } from "./TestingEvaluator.js";
|
|
7
|
+
import { EvalCommand } from "../kernel/commands.js";
|
|
8
|
+
import { RuntimeContext } from "../RuntimeContext.js";
|
|
9
|
+
import { YukigoKernel } from "../kernel/index.js";
|
|
10
|
+
export class InterpreterVisitor extends TestingEvaluator(StatementEvaluator(ExpressionEvaluator(OperationEvaluator(PrimitiveEvaluator(EvaluatorBase))))) {
|
|
11
|
+
constructor(context) {
|
|
12
|
+
super(context);
|
|
13
|
+
this.context.evaluatorFactory = (ctx) => new InterpreterVisitor(ctx);
|
|
14
|
+
}
|
|
15
|
+
static evaluateLiteral(node, ctx) {
|
|
16
|
+
const targetCtx = ctx ?? new RuntimeContext();
|
|
17
|
+
const visitor = new InterpreterVisitor(targetCtx);
|
|
18
|
+
const kernel = new YukigoKernel(visitor);
|
|
19
|
+
return kernel.run(new EvalCommand(node));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { RuntimeClass, YuValue } from "../../primitives/index.js";
|
|
2
|
+
import { LogicEngine } from "../logic/LogicEngine.js";
|
|
3
|
+
import { RuntimeContext } from "../RuntimeContext.js";
|
|
4
|
+
import { EvaluatorBase } from "./BaseEvaluator.js";
|
|
5
|
+
export declare const getLogicEngine: (evaluator: EvaluatorBase, ctx: RuntimeContext) => LogicEngine;
|
|
6
|
+
export declare const getClassInitializers: (classDef: RuntimeClass, ctx: RuntimeContext) => Map<string, any>;
|
|
7
|
+
export declare const getClassFields: (classDef: RuntimeClass, ctx: RuntimeContext) => Map<string, YuValue>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { RuntimeClass } from "../../primitives/index.js";
|
|
2
|
+
import { LogicEngine } from "../logic/LogicEngine.js";
|
|
3
|
+
export const getLogicEngine = (evaluator, ctx) => new LogicEngine(evaluator, ctx);
|
|
4
|
+
export const getClassInitializers = (classDef, ctx) => {
|
|
5
|
+
const initializers = new Map();
|
|
6
|
+
const collect = (c) => {
|
|
7
|
+
if (c.superclass) {
|
|
8
|
+
const superDef = ctx.lookup(c.superclass);
|
|
9
|
+
if (superDef && superDef instanceof RuntimeClass) {
|
|
10
|
+
collect(superDef);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
for (const mixin of c.mixins) {
|
|
14
|
+
const mixinDef = ctx.lookup(mixin);
|
|
15
|
+
if (mixinDef && mixinDef instanceof RuntimeClass) {
|
|
16
|
+
collect(mixinDef);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const cInitializers = c.fieldInitializers;
|
|
20
|
+
if (cInitializers) {
|
|
21
|
+
for (const [k, v] of cInitializers.entries()) {
|
|
22
|
+
initializers.set(k, v);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
collect(classDef);
|
|
27
|
+
return initializers;
|
|
28
|
+
};
|
|
29
|
+
export const getClassFields = (classDef, ctx) => {
|
|
30
|
+
const fields = new Map();
|
|
31
|
+
const collect = (c) => {
|
|
32
|
+
if (c.superclass) {
|
|
33
|
+
const superDef = ctx.lookup(c.superclass);
|
|
34
|
+
if (superDef && superDef instanceof RuntimeClass) {
|
|
35
|
+
collect(superDef);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
for (const mixin of c.mixins) {
|
|
39
|
+
const mixinDef = ctx.lookup(mixin);
|
|
40
|
+
if (mixinDef && mixinDef instanceof RuntimeClass) {
|
|
41
|
+
collect(mixinDef);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
for (const [k, v] of c.fields.entries()) {
|
|
45
|
+
fields.set(k, v);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
collect(classDef);
|
|
49
|
+
return fields;
|
|
50
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { InterpreterError } from "../../errors.js";
|
|
2
|
+
import { RuntimeContext } from "../RuntimeContext.js";
|
|
3
|
+
import { ASTNode, Test } from "yukigo-ast";
|
|
4
|
+
import { YuValue } from "../../primitives/YuValue.js";
|
|
5
|
+
import { Interpreter } from "../../index.js";
|
|
6
|
+
export interface YukigoHook {
|
|
7
|
+
/**
|
|
8
|
+
* Executed when an InterpreterError is raised, allows languages to extends error handling.
|
|
9
|
+
*/
|
|
10
|
+
onInterpreterError?: (error: InterpreterError, ctx: RuntimeContext) => Error | YuValue | void;
|
|
11
|
+
/**
|
|
12
|
+
* Executed before evaluating a node.
|
|
13
|
+
*/
|
|
14
|
+
beforeNodeEvaluate?: (node: ASTNode, ctx: RuntimeContext) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Executed after evaluating a nove.
|
|
17
|
+
*/
|
|
18
|
+
afterNodeEvaluate?: (node: ASTNode, result: YuValue, ctx: RuntimeContext) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Executed before dispatching a message to an object.
|
|
21
|
+
*/
|
|
22
|
+
onMessageDispatch?: (receiver: YuValue, message: string, args: YuValue[], ctx: RuntimeContext) => void;
|
|
23
|
+
/** Executed before each individual Test */
|
|
24
|
+
beforeEachTest?: (interpreter: Interpreter, testNode: Test) => void;
|
|
25
|
+
/** Executed after each individual Test */
|
|
26
|
+
afterEachTest?: (interpreter: Interpreter, testNode: Test) => void;
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { ASTNode, Pattern } from "yukigo-ast";
|
|
2
|
+
import { YukigoKernel } from "./index.js";
|
|
3
|
+
import { ErrorFrame, InterpreterError } from "../../errors.js";
|
|
4
|
+
import { LogicTranslator } from "../logic/LogicTranslator.js";
|
|
5
|
+
import { Scope } from "../logic/LogicEngine.js";
|
|
6
|
+
import { YuValue, Substitution } from "../../primitives/index.js";
|
|
7
|
+
/**
|
|
8
|
+
* A Continuation is a function that receives a value and decides what is the next Command to execute
|
|
9
|
+
*/
|
|
10
|
+
export type Continuation = (result: YuValue) => ExecutionCommand;
|
|
11
|
+
export type TraceEntry = {
|
|
12
|
+
frame: ErrorFrame;
|
|
13
|
+
key: string;
|
|
14
|
+
};
|
|
15
|
+
export interface ExecutionCommand {
|
|
16
|
+
readonly name: string;
|
|
17
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void;
|
|
18
|
+
createTraceEntry(): TraceEntry | void;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Commands the Kernel to evaluate an AST node.
|
|
22
|
+
*/
|
|
23
|
+
export declare class EvalCommand implements ExecutionCommand {
|
|
24
|
+
readonly node: ASTNode;
|
|
25
|
+
readonly name = "EVAL";
|
|
26
|
+
constructor(node: ASTNode);
|
|
27
|
+
execute(kernel: YukigoKernel): ExecutionCommand;
|
|
28
|
+
createTraceEntry(): TraceEntry;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Commands the Kernel to move to the next step with a produced value.
|
|
32
|
+
* It triggers the popping of the continuation stack in the kernel.
|
|
33
|
+
*/
|
|
34
|
+
export declare class StepCommand implements ExecutionCommand {
|
|
35
|
+
readonly value: YuValue;
|
|
36
|
+
readonly name = "STEP";
|
|
37
|
+
constructor(value: YuValue);
|
|
38
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void;
|
|
39
|
+
createTraceEntry(): void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Commands the Kernel to bind a produced value to a continuation.
|
|
43
|
+
* It pushes the continuation to the kernel's stack and proceeds with the command.
|
|
44
|
+
*/
|
|
45
|
+
export declare class BindCommand implements ExecutionCommand {
|
|
46
|
+
readonly command: ExecutionCommand;
|
|
47
|
+
readonly next: Continuation;
|
|
48
|
+
readonly name = "BIND";
|
|
49
|
+
constructor(command: ExecutionCommand, next: Continuation);
|
|
50
|
+
execute(kernel: YukigoKernel): ExecutionCommand;
|
|
51
|
+
createTraceEntry(): void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Commands the Kernel to handle multiple branches of execution (non-determinism).
|
|
55
|
+
* It will try the first one and save the others as choice points.
|
|
56
|
+
*/
|
|
57
|
+
export declare class ChoiceCommand implements ExecutionCommand {
|
|
58
|
+
readonly alternatives: ExecutionCommand[];
|
|
59
|
+
readonly name = "CHOICE";
|
|
60
|
+
constructor(alternatives: ExecutionCommand[]);
|
|
61
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void;
|
|
62
|
+
createTraceEntry(): void;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Commands the Kernel to halt execution due to an error.
|
|
66
|
+
*/
|
|
67
|
+
export declare class FailCommand implements ExecutionCommand {
|
|
68
|
+
readonly error: Error;
|
|
69
|
+
readonly name = "FAIL";
|
|
70
|
+
constructor(error: Error);
|
|
71
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void;
|
|
72
|
+
createTraceEntry(): void;
|
|
73
|
+
}
|
|
74
|
+
export declare class RaiseCommand implements ExecutionCommand {
|
|
75
|
+
readonly exceptionObj: YuValue | InterpreterError;
|
|
76
|
+
readonly name = "RAISE";
|
|
77
|
+
constructor(exceptionObj: YuValue | InterpreterError);
|
|
78
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void;
|
|
79
|
+
createTraceEntry(): void;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A CatchHandler is a function that receives the raised exception (YuValue)
|
|
83
|
+
* and decides what Command to execute next (usually the catch block body).
|
|
84
|
+
*/
|
|
85
|
+
export type CatchHandler = (exception: YuValue | InterpreterError) => ExecutionCommand;
|
|
86
|
+
export declare class CatchCommand implements ExecutionCommand {
|
|
87
|
+
readonly handler: CatchHandler;
|
|
88
|
+
readonly innerCommand: ExecutionCommand;
|
|
89
|
+
readonly name = "CATCH";
|
|
90
|
+
constructor(handler: CatchHandler, innerCommand: ExecutionCommand);
|
|
91
|
+
execute(kernel: YukigoKernel): ExecutionCommand;
|
|
92
|
+
createTraceEntry(): void;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Commands the Kernel to stop execution and backtrack due to logical failure.
|
|
96
|
+
*/
|
|
97
|
+
export declare class BacktrackCommand implements ExecutionCommand {
|
|
98
|
+
readonly name = "BACKTRACK";
|
|
99
|
+
constructor();
|
|
100
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void;
|
|
101
|
+
createTraceEntry(): void;
|
|
102
|
+
}
|
|
103
|
+
export declare class NotCommand implements ExecutionCommand {
|
|
104
|
+
readonly innerGoalCmd: ExecutionCommand;
|
|
105
|
+
readonly currentSubsts: Substitution;
|
|
106
|
+
readonly name = "NOT";
|
|
107
|
+
constructor(innerGoalCmd: ExecutionCommand, currentSubsts: Substitution);
|
|
108
|
+
execute(kernel: YukigoKernel): ExecutionCommand;
|
|
109
|
+
createTraceEntry(): void;
|
|
110
|
+
}
|
|
111
|
+
export declare class FindallCommand implements ExecutionCommand {
|
|
112
|
+
readonly template: Pattern;
|
|
113
|
+
readonly bag: Pattern;
|
|
114
|
+
readonly innerGoalCmd: ExecutionCommand;
|
|
115
|
+
readonly currentSubsts: Substitution;
|
|
116
|
+
readonly scope: Scope;
|
|
117
|
+
readonly translator: LogicTranslator;
|
|
118
|
+
readonly name = "FINDALL";
|
|
119
|
+
constructor(template: Pattern, bag: Pattern, innerGoalCmd: ExecutionCommand, currentSubsts: Substitution, scope: Scope, translator: LogicTranslator);
|
|
120
|
+
execute(kernel: YukigoKernel): ExecutionCommand;
|
|
121
|
+
createTraceEntry(): void;
|
|
122
|
+
}
|