yukigo 0.2.2 → 0.3.1
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 +102 -41
- 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 +21 -14
- package/dist/analyzer/inspections/generic/generic.js +78 -59
- 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 +12 -5
- package/dist/utils/helpers.js +6 -79
- 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 -581
- 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 +81 -168
- 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 +188 -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,255 @@
|
|
|
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 { ListTerm } from "../logic/LogicTerm.js";
|
|
7
|
+
import {
|
|
8
|
+
YuValue,
|
|
9
|
+
Substitution,
|
|
10
|
+
isLogicResult,
|
|
11
|
+
LogicResult,
|
|
12
|
+
LogicAnswer,
|
|
13
|
+
LogicTerm,
|
|
14
|
+
} from "../../primitives/index.js";
|
|
15
|
+
import { InterpreterVisitor } from "../evaluators/index.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A Continuation is a function that receives a value and decides what is the next Command to execute
|
|
19
|
+
*/
|
|
20
|
+
export type Continuation = (result: YuValue) => ExecutionCommand;
|
|
21
|
+
|
|
22
|
+
export type TraceEntry = { frame: ErrorFrame; key: string };
|
|
23
|
+
|
|
24
|
+
export interface ExecutionCommand {
|
|
25
|
+
readonly name: string;
|
|
26
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void;
|
|
27
|
+
createTraceEntry(): TraceEntry | void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Commands the Kernel to evaluate an AST node.
|
|
32
|
+
*/
|
|
33
|
+
export class EvalCommand implements ExecutionCommand {
|
|
34
|
+
readonly name = "EVAL";
|
|
35
|
+
|
|
36
|
+
constructor(public readonly node: ASTNode) {}
|
|
37
|
+
|
|
38
|
+
execute(kernel: YukigoKernel): ExecutionCommand {
|
|
39
|
+
return kernel.evaluator.evaluate(this.node);
|
|
40
|
+
}
|
|
41
|
+
createTraceEntry(): TraceEntry {
|
|
42
|
+
const { constructor, loc } = this.node;
|
|
43
|
+
const frame = {
|
|
44
|
+
nodeType: constructor.name,
|
|
45
|
+
loc,
|
|
46
|
+
};
|
|
47
|
+
const startLoc = loc ? `${loc.line}:${loc.column}:` : "";
|
|
48
|
+
const key = `${startLoc}${constructor.name}`;
|
|
49
|
+
return { key, frame };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Commands the Kernel to move to the next step with a produced value.
|
|
55
|
+
* It triggers the popping of the continuation stack in the kernel.
|
|
56
|
+
*/
|
|
57
|
+
export class StepCommand implements ExecutionCommand {
|
|
58
|
+
readonly name = "STEP";
|
|
59
|
+
|
|
60
|
+
constructor(public readonly value: YuValue) {}
|
|
61
|
+
|
|
62
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void {
|
|
63
|
+
return kernel.popAndExecute(this.value);
|
|
64
|
+
}
|
|
65
|
+
createTraceEntry() {}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Commands the Kernel to bind a produced value to a continuation.
|
|
70
|
+
* It pushes the continuation to the kernel's stack and proceeds with the command.
|
|
71
|
+
*/
|
|
72
|
+
export class BindCommand implements ExecutionCommand {
|
|
73
|
+
readonly name = "BIND";
|
|
74
|
+
|
|
75
|
+
constructor(
|
|
76
|
+
public readonly command: ExecutionCommand,
|
|
77
|
+
public readonly next: Continuation,
|
|
78
|
+
) {}
|
|
79
|
+
|
|
80
|
+
execute(kernel: YukigoKernel): ExecutionCommand {
|
|
81
|
+
kernel.pushContinuation(this.next);
|
|
82
|
+
return this.command;
|
|
83
|
+
}
|
|
84
|
+
createTraceEntry() {}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Commands the Kernel to handle multiple branches of execution (non-determinism).
|
|
89
|
+
* It will try the first one and save the others as choice points.
|
|
90
|
+
*/
|
|
91
|
+
export class ChoiceCommand implements ExecutionCommand {
|
|
92
|
+
readonly name = "CHOICE";
|
|
93
|
+
|
|
94
|
+
constructor(public readonly alternatives: ExecutionCommand[]) {}
|
|
95
|
+
|
|
96
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void {
|
|
97
|
+
return kernel.handleChoice(this.alternatives);
|
|
98
|
+
}
|
|
99
|
+
createTraceEntry() {}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Commands the Kernel to halt execution due to an error.
|
|
104
|
+
*/
|
|
105
|
+
export class FailCommand implements ExecutionCommand {
|
|
106
|
+
readonly name = "FAIL";
|
|
107
|
+
|
|
108
|
+
constructor(public readonly error: Error) {}
|
|
109
|
+
|
|
110
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void {
|
|
111
|
+
throw kernel.buildSemanticError(this.error);
|
|
112
|
+
}
|
|
113
|
+
createTraceEntry() {}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// error handling commands
|
|
117
|
+
|
|
118
|
+
export class RaiseCommand implements ExecutionCommand {
|
|
119
|
+
readonly name = "RAISE";
|
|
120
|
+
|
|
121
|
+
constructor(public readonly exceptionObj: YuValue | InterpreterError) {}
|
|
122
|
+
|
|
123
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void {
|
|
124
|
+
// Le decimos al kernel que inicie el proceso de desapilado para buscar un Catch
|
|
125
|
+
return kernel.handleRaise(this.exceptionObj);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
createTraceEntry() {}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* A CatchHandler is a function that receives the raised exception (YuValue)
|
|
133
|
+
* and decides what Command to execute next (usually the catch block body).
|
|
134
|
+
*/
|
|
135
|
+
export type CatchHandler = (exception: YuValue | InterpreterError) => ExecutionCommand;
|
|
136
|
+
|
|
137
|
+
export class CatchCommand implements ExecutionCommand {
|
|
138
|
+
readonly name = "CATCH";
|
|
139
|
+
|
|
140
|
+
constructor(
|
|
141
|
+
public readonly handler: CatchHandler,
|
|
142
|
+
public readonly innerCommand: ExecutionCommand // El cuerpo del try
|
|
143
|
+
) {}
|
|
144
|
+
|
|
145
|
+
execute(kernel: YukigoKernel): ExecutionCommand {
|
|
146
|
+
// 1. Registramos este manejador de errores en el Kernel
|
|
147
|
+
kernel.pushCatchHandler(this.handler);
|
|
148
|
+
|
|
149
|
+
// 2. Ejecutamos el cuerpo del try envuelto en una limpieza
|
|
150
|
+
// Si el innerCommand termina con éxito (sin errores), necesitamos sacar el handler de la pila
|
|
151
|
+
return new BindCommand(this.innerCommand, (result) => {
|
|
152
|
+
kernel.popCatchHandler(); // Removemos el handler porque no hubo error
|
|
153
|
+
return new StepCommand(result);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
createTraceEntry() {}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
// Specific commands for LogicRuntime
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Commands the Kernel to stop execution and backtrack due to logical failure.
|
|
165
|
+
*/
|
|
166
|
+
export class BacktrackCommand implements ExecutionCommand {
|
|
167
|
+
readonly name = "BACKTRACK";
|
|
168
|
+
|
|
169
|
+
constructor() {}
|
|
170
|
+
|
|
171
|
+
execute(kernel: YukigoKernel): ExecutionCommand | void {
|
|
172
|
+
// the backtracking is delegated to the kernel because it manipulates the execution stack
|
|
173
|
+
return kernel.handleBacktrack();
|
|
174
|
+
}
|
|
175
|
+
createTraceEntry() {}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export class NotCommand implements ExecutionCommand {
|
|
179
|
+
readonly name = "NOT";
|
|
180
|
+
|
|
181
|
+
constructor(
|
|
182
|
+
public readonly innerGoalCmd: ExecutionCommand,
|
|
183
|
+
public readonly currentSubsts: Substitution,
|
|
184
|
+
) {}
|
|
185
|
+
|
|
186
|
+
execute(kernel: YukigoKernel): ExecutionCommand {
|
|
187
|
+
const isolatedContext = kernel.evaluator.getContext().clone();
|
|
188
|
+
const isolatedEvaluator = new InterpreterVisitor(isolatedContext);
|
|
189
|
+
const isolatedKernel = new YukigoKernel(isolatedEvaluator, "first");
|
|
190
|
+
|
|
191
|
+
// Clone substs to avoid leaking bindings from the inner search
|
|
192
|
+
const result = isolatedKernel.run(this.innerGoalCmd);
|
|
193
|
+
if (isLogicResult(result) && result.allSuccessful())
|
|
194
|
+
return new BacktrackCommand();
|
|
195
|
+
|
|
196
|
+
return new StepCommand(
|
|
197
|
+
new LogicResult([new LogicAnswer(true, new Map(this.currentSubsts))]),
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
createTraceEntry() {}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export class FindallCommand implements ExecutionCommand {
|
|
204
|
+
readonly name = "FINDALL";
|
|
205
|
+
|
|
206
|
+
constructor(
|
|
207
|
+
public readonly template: Pattern,
|
|
208
|
+
public readonly bag: Pattern,
|
|
209
|
+
public readonly innerGoalCmd: ExecutionCommand,
|
|
210
|
+
public readonly currentSubsts: Substitution,
|
|
211
|
+
public readonly scope: Scope,
|
|
212
|
+
public readonly translator: LogicTranslator,
|
|
213
|
+
) {}
|
|
214
|
+
|
|
215
|
+
execute(kernel: YukigoKernel): ExecutionCommand {
|
|
216
|
+
// we need to _find all_ (jaja lol) successful branches
|
|
217
|
+
const isolatedContext = kernel.evaluator.getContext().clone();
|
|
218
|
+
const isolatedEvaluator = new InterpreterVisitor(isolatedContext);
|
|
219
|
+
const isolatedKernel = new YukigoKernel(isolatedEvaluator, "all");
|
|
220
|
+
|
|
221
|
+
// run inner goal - it will return an array of LogicResult if successful
|
|
222
|
+
const results = isolatedKernel.run(this.innerGoalCmd);
|
|
223
|
+
const gathered: LogicTerm[] = [];
|
|
224
|
+
|
|
225
|
+
if (Array.isArray(results)) {
|
|
226
|
+
for (const res of results) {
|
|
227
|
+
if (isLogicResult(res) && res.allSuccessful()) {
|
|
228
|
+
// then we extract all solutions and instantiate the template
|
|
229
|
+
res.getSuccessfulSolutions().forEach((solutionEnv) => {
|
|
230
|
+
const templateTerm = this.translator.patternToTerm(
|
|
231
|
+
this.template,
|
|
232
|
+
this.scope,
|
|
233
|
+
);
|
|
234
|
+
gathered.push(templateTerm.instantiate(solutionEnv));
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// then we build a logic list
|
|
241
|
+
const resultList = new ListTerm(gathered);
|
|
242
|
+
const bagTerm = this.translator.patternToTerm(this.bag, this.scope);
|
|
243
|
+
|
|
244
|
+
// then we try to unify the list with the bag
|
|
245
|
+
const finalSubsts = new Map(this.currentSubsts);
|
|
246
|
+
if (bagTerm.unify(resultList, finalSubsts))
|
|
247
|
+
return new StepCommand(
|
|
248
|
+
new LogicResult([new LogicAnswer(true, finalSubsts)]),
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
// finally if unification fails, findall fails
|
|
252
|
+
return new BacktrackCommand();
|
|
253
|
+
}
|
|
254
|
+
createTraceEntry() {}
|
|
255
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CatchHandler,
|
|
3
|
+
Continuation,
|
|
4
|
+
ExecutionCommand,
|
|
5
|
+
} from "./commands.js";
|
|
6
|
+
import { boolean, EnvStack } from "../../utils.js";
|
|
7
|
+
import { ErrorFrame, InterpreterError } from "../../errors.js";
|
|
8
|
+
import { YuNil, YuValue } from "../../primitives/index.js";
|
|
9
|
+
import { Evaluator } from "../evaluators/BaseEvaluator.js";
|
|
10
|
+
|
|
11
|
+
export type LogicSearchMode = "first" | "all" | "stream";
|
|
12
|
+
|
|
13
|
+
export interface ChoicePoint {
|
|
14
|
+
alternatives: ExecutionCommand[];
|
|
15
|
+
continuationStack: Continuation[];
|
|
16
|
+
envSnapshot: EnvStack;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class YukigoKernel {
|
|
20
|
+
public readonly kid = Math.floor(Math.random() * 9999);
|
|
21
|
+
private logicalTrace: ExecutionCommand[] = [];
|
|
22
|
+
private continuationStack: Continuation[] = [];
|
|
23
|
+
private choiceStack: ChoicePoint[] = [];
|
|
24
|
+
private finalResult: YuValue | undefined;
|
|
25
|
+
private searchExhausted = false;
|
|
26
|
+
|
|
27
|
+
private catchHandlerStack: { handler: CatchHandler; continuationDepth: number }[] = [];
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
public readonly evaluator: Evaluator,
|
|
31
|
+
private readonly mode: LogicSearchMode = "first",
|
|
32
|
+
) {}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Pushes a continuation to the logical stack.
|
|
36
|
+
*/
|
|
37
|
+
public pushContinuation(cont: Continuation): void {
|
|
38
|
+
this.continuationStack.push(cont);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Pops a continuation from the stack and executes it with the provided value.
|
|
43
|
+
* If the stack is empty, sets the final result.
|
|
44
|
+
*/
|
|
45
|
+
public popAndExecute(value: YuValue): ExecutionCommand | void {
|
|
46
|
+
const next = this.continuationStack.pop();
|
|
47
|
+
if (!next) {
|
|
48
|
+
this.finalResult = value;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
return next(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public pushCatchHandler(handler: CatchHandler): void {
|
|
55
|
+
// Save current continuation stack length so we know where to unwind if an error is raised inside the try block
|
|
56
|
+
this.catchHandlerStack.push({
|
|
57
|
+
handler,
|
|
58
|
+
continuationDepth: this.continuationStack.length,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public popCatchHandler(): CatchHandler | undefined {
|
|
63
|
+
const entry = this.catchHandlerStack.pop();
|
|
64
|
+
return entry?.handler;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Handles RaiseCommand.
|
|
69
|
+
*/
|
|
70
|
+
public handleRaise(
|
|
71
|
+
exception: YuValue | InterpreterError,
|
|
72
|
+
): ExecutionCommand | void {
|
|
73
|
+
const entry = this.catchHandlerStack.pop();
|
|
74
|
+
if (!entry) {
|
|
75
|
+
// if no catch found, we have no other option than to throw the error and break the flow.
|
|
76
|
+
if (exception instanceof InterpreterError) {
|
|
77
|
+
throw this.buildSemanticError(exception);
|
|
78
|
+
} else {
|
|
79
|
+
// TODO: I think this if-else is unnecessary
|
|
80
|
+
throw this.buildSemanticError(
|
|
81
|
+
new InterpreterError("UncaughtException", String(exception)),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Unwind continuation stack to the point where the try block was entered
|
|
87
|
+
this.continuationStack.length = entry.continuationDepth;
|
|
88
|
+
|
|
89
|
+
return entry.handler(exception);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Handles a choice point by trying the first alternative and saving the others.
|
|
94
|
+
*/
|
|
95
|
+
public handleChoice(
|
|
96
|
+
alternatives: ExecutionCommand[],
|
|
97
|
+
): ExecutionCommand | void {
|
|
98
|
+
if (alternatives.length === 0) return this.handleBacktrack();
|
|
99
|
+
|
|
100
|
+
const [first, ...rest] = alternatives;
|
|
101
|
+
|
|
102
|
+
if (rest.length > 0) {
|
|
103
|
+
this.choiceStack.push({
|
|
104
|
+
alternatives: rest,
|
|
105
|
+
continuationStack: [...this.continuationStack],
|
|
106
|
+
envSnapshot: this.evaluator.getContext().cloneEnv(),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return first;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Attempts to backtrack to the last saved choice point.
|
|
115
|
+
*/
|
|
116
|
+
public handleBacktrack(): ExecutionCommand {
|
|
117
|
+
const lastChoice = this.choiceStack.pop();
|
|
118
|
+
|
|
119
|
+
if (!lastChoice) {
|
|
120
|
+
// logic tree is empty sooo
|
|
121
|
+
this.searchExhausted = true;
|
|
122
|
+
this.continuationStack = [];
|
|
123
|
+
return boolean(false);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this.searchExhausted = false;
|
|
127
|
+
|
|
128
|
+
// clones the stack and environment
|
|
129
|
+
this.continuationStack = [...lastChoice.continuationStack];
|
|
130
|
+
const clonedEnv = this.evaluator
|
|
131
|
+
.getContext()
|
|
132
|
+
.cloneEnv(lastChoice.envSnapshot);
|
|
133
|
+
this.evaluator.getContext().setEnv(clonedEnv);
|
|
134
|
+
|
|
135
|
+
const [next, ...remaining] = lastChoice.alternatives;
|
|
136
|
+
|
|
137
|
+
if (remaining.length > 0) {
|
|
138
|
+
this.choiceStack.push({
|
|
139
|
+
...lastChoice,
|
|
140
|
+
alternatives: remaining,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return next;
|
|
145
|
+
}
|
|
146
|
+
// TODO: remove this f- any
|
|
147
|
+
public run(initialCommand: ExecutionCommand): any {
|
|
148
|
+
const stream = this.executionStream(initialCommand);
|
|
149
|
+
|
|
150
|
+
switch (this.mode) {
|
|
151
|
+
case "stream":
|
|
152
|
+
return stream;
|
|
153
|
+
case "first":
|
|
154
|
+
const result = stream.next();
|
|
155
|
+
return result.done ? undefined : result.value;
|
|
156
|
+
case "all":
|
|
157
|
+
return Array.from(stream);
|
|
158
|
+
|
|
159
|
+
default:
|
|
160
|
+
throw new Error(`Output mode "${this.mode}" not supported.`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private *executionStream(
|
|
165
|
+
initialCommand: ExecutionCommand,
|
|
166
|
+
): Generator<YuValue, void, unknown> {
|
|
167
|
+
let current: ExecutionCommand | void = initialCommand;
|
|
168
|
+
this.searchExhausted = false;
|
|
169
|
+
let steps = 0;
|
|
170
|
+
|
|
171
|
+
while (current) {
|
|
172
|
+
steps++;
|
|
173
|
+
this.logicalTrace.push(current);
|
|
174
|
+
// cleans the older traces to avoid consuming memory
|
|
175
|
+
if (this.logicalTrace.length > 50) this.logicalTrace.shift();
|
|
176
|
+
|
|
177
|
+
current = current.execute(this);
|
|
178
|
+
|
|
179
|
+
if (!current) {
|
|
180
|
+
if (this.searchExhausted) break;
|
|
181
|
+
yield this.finalResult || YuNil.getInstance();
|
|
182
|
+
current = this.handleBacktrack();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public getLogicalTrace() {
|
|
188
|
+
return this.logicalTrace;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public buildSemanticError(baseError: Error): Error {
|
|
192
|
+
if (baseError.constructor.name === "FailedAssert") return baseError;
|
|
193
|
+
|
|
194
|
+
const frames = this.getStackFromTrace();
|
|
195
|
+
|
|
196
|
+
if (baseError instanceof InterpreterError) {
|
|
197
|
+
if (baseError.frames.length === 0) {
|
|
198
|
+
frames.forEach((f) => baseError.pushFrame(f));
|
|
199
|
+
}
|
|
200
|
+
return baseError;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const newErr = new InterpreterError("Yukigo VM Error", baseError.message);
|
|
204
|
+
frames.forEach((f) => newErr.pushFrame(f));
|
|
205
|
+
return newErr;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private getStackFromTrace(): ErrorFrame[] {
|
|
209
|
+
const frames: ErrorFrame[] = [];
|
|
210
|
+
let index = this.logicalTrace.length - 1;
|
|
211
|
+
// loops through logicalTrace and collects traces
|
|
212
|
+
for (index; index >= 0; index--) {
|
|
213
|
+
const cmd = this.logicalTrace[index];
|
|
214
|
+
const trace = cmd.createTraceEntry();
|
|
215
|
+
// not all commands leave traces
|
|
216
|
+
if (!trace) continue;
|
|
217
|
+
frames.push(trace.frame);
|
|
218
|
+
if (frames.length >= 10) break;
|
|
219
|
+
}
|
|
220
|
+
return frames;
|
|
221
|
+
}
|
|
222
|
+
}
|