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,35 +1,44 @@
|
|
|
1
|
-
import { Environment, EnvStack, PrimitiveValue } from "yukigo-ast";
|
|
2
1
|
import { FunctionRuntime } from "./runtimes/FunctionRuntime.js";
|
|
3
2
|
import { LazyRuntime } from "./runtimes/LazyRuntime.js";
|
|
4
3
|
import { ObjectRuntime } from "./runtimes/ObjectRuntime.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
import { Environment, EnvStack } from "../utils.js";
|
|
5
|
+
import { YuValue } from "../primitives/YuValue.js";
|
|
6
|
+
import { YukigoHook } from "./hooks/YukigoHook.js";
|
|
7
|
+
import { ExecutionCommand } from "./kernel/commands.js";
|
|
8
|
+
import { InterpreterConfig } from "../../utils/helpers.js";
|
|
9
|
+
import { Evaluator } from "./evaluators/BaseEvaluator.js";
|
|
10
|
+
export declare const DefaultConfiguration: InterpreterConfig;
|
|
11
|
+
export type EvaluatorFactory = (ctx: RuntimeContext) => Evaluator;
|
|
13
12
|
export declare class UninitializedConfig extends Error {
|
|
14
13
|
constructor();
|
|
15
14
|
}
|
|
16
15
|
export declare class ReinitializedConfig extends Error {
|
|
17
16
|
constructor();
|
|
18
17
|
}
|
|
18
|
+
export interface LogicState {
|
|
19
|
+
variableCounter: number;
|
|
20
|
+
idToName: Map<number, string>;
|
|
21
|
+
}
|
|
19
22
|
export declare class RuntimeContext {
|
|
20
|
-
readonly config: InterpreterConfig
|
|
23
|
+
readonly config: InterpreterConfig;
|
|
21
24
|
env: EnvStack;
|
|
22
25
|
lazyRuntime: LazyRuntime;
|
|
23
26
|
funcRuntime: FunctionRuntime;
|
|
24
27
|
objRuntime: ObjectRuntime;
|
|
25
|
-
|
|
28
|
+
evaluatorFactory?: EvaluatorFactory;
|
|
29
|
+
logicState?: LogicState;
|
|
30
|
+
dispatchHook<K extends keyof YukigoHook>(eventName: K, ...args: Parameters<NonNullable<YukigoHook[K]>>): ReturnType<NonNullable<YukigoHook[K]>>[];
|
|
31
|
+
constructor(config?: Partial<InterpreterConfig>);
|
|
26
32
|
setEnv(env: EnvStack): void;
|
|
33
|
+
forceValue(val: YuValue): ExecutionCommand;
|
|
27
34
|
isDefined(name: string): boolean;
|
|
28
35
|
pushEnv(frame?: Environment): void;
|
|
29
|
-
replace(name: string, value:
|
|
36
|
+
replace(name: string, value: YuValue, onReplace?: (env: Environment) => void): boolean;
|
|
30
37
|
popEnv(): void;
|
|
31
|
-
lookup(name: string):
|
|
38
|
+
lookup(name: string): YuValue;
|
|
32
39
|
remove(name: string): void;
|
|
33
|
-
define(name: string, value:
|
|
34
|
-
|
|
40
|
+
define(name: string, value: YuValue): void;
|
|
41
|
+
defineGlobal(name: string, value: YuValue): void;
|
|
42
|
+
cloneEnv(env?: EnvStack): EnvStack;
|
|
43
|
+
clone(env?: EnvStack): RuntimeContext;
|
|
35
44
|
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { FunctionRuntime } from "./runtimes/FunctionRuntime.js";
|
|
2
2
|
import { LazyRuntime } from "./runtimes/LazyRuntime.js";
|
|
3
3
|
import { ObjectRuntime } from "./runtimes/ObjectRuntime.js";
|
|
4
|
-
import { createGlobalEnv } from "../utils.js";
|
|
4
|
+
import { createGlobalEnv, } from "../utils.js";
|
|
5
5
|
import { UnboundVariable } from "../errors.js";
|
|
6
|
+
import { RuntimeFunction } from "../primitives/index.js";
|
|
7
|
+
import { StepCommand, BindCommand, } from "./kernel/commands.js";
|
|
6
8
|
export const DefaultConfiguration = {
|
|
9
|
+
nativeProviders: new Map(),
|
|
7
10
|
lazyLoading: false,
|
|
8
11
|
debug: false,
|
|
9
12
|
outputMode: "first",
|
|
@@ -20,11 +23,24 @@ export class ReinitializedConfig extends Error {
|
|
|
20
23
|
}
|
|
21
24
|
}
|
|
22
25
|
export class RuntimeContext {
|
|
23
|
-
config =
|
|
26
|
+
config = DefaultConfiguration;
|
|
24
27
|
env;
|
|
25
28
|
lazyRuntime;
|
|
26
29
|
funcRuntime;
|
|
27
30
|
objRuntime;
|
|
31
|
+
evaluatorFactory;
|
|
32
|
+
logicState;
|
|
33
|
+
dispatchHook(eventName, ...args) {
|
|
34
|
+
const results = [];
|
|
35
|
+
if (this.config.hooks) {
|
|
36
|
+
for (const hook of this.config.hooks) {
|
|
37
|
+
if (hook[eventName]) {
|
|
38
|
+
results.push(hook[eventName](...args));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return results;
|
|
43
|
+
}
|
|
28
44
|
constructor(config) {
|
|
29
45
|
this.config = Object.freeze({ ...DefaultConfiguration, ...config });
|
|
30
46
|
this.lazyRuntime = new LazyRuntime(this);
|
|
@@ -35,6 +51,14 @@ export class RuntimeContext {
|
|
|
35
51
|
setEnv(env) {
|
|
36
52
|
this.env = env;
|
|
37
53
|
}
|
|
54
|
+
forceValue(val) {
|
|
55
|
+
if (val instanceof RuntimeFunction &&
|
|
56
|
+
val.arity === 0 &&
|
|
57
|
+
(val.pendingArgs?.length ?? 0) === 0) {
|
|
58
|
+
return new BindCommand(this.funcRuntime.apply(val, []), (res) => this.forceValue(res));
|
|
59
|
+
}
|
|
60
|
+
return new StepCommand(val);
|
|
61
|
+
}
|
|
38
62
|
isDefined(name) {
|
|
39
63
|
let current = this.env;
|
|
40
64
|
while (current !== null) {
|
|
@@ -83,11 +107,26 @@ export class RuntimeContext {
|
|
|
83
107
|
define(name, value) {
|
|
84
108
|
this.env.head.set(name, value);
|
|
85
109
|
}
|
|
86
|
-
|
|
110
|
+
defineGlobal(name, value) {
|
|
111
|
+
let current = this.env;
|
|
112
|
+
while (current.tail !== null) {
|
|
113
|
+
current = current.tail;
|
|
114
|
+
}
|
|
115
|
+
current.head.set(name, value);
|
|
116
|
+
}
|
|
117
|
+
cloneEnv(env) {
|
|
87
118
|
const target = env ?? this.env;
|
|
88
119
|
return {
|
|
89
120
|
head: new Map(target.head),
|
|
90
|
-
tail: this.
|
|
121
|
+
tail: target.tail ? this.cloneEnv(target.tail) : null,
|
|
91
122
|
};
|
|
92
123
|
}
|
|
124
|
+
clone(env) {
|
|
125
|
+
const target = env ?? this.env;
|
|
126
|
+
const newCtx = new RuntimeContext(this.config);
|
|
127
|
+
newCtx.setEnv(this.cloneEnv(target));
|
|
128
|
+
newCtx.logicState = this.logicState; // share logic state
|
|
129
|
+
newCtx.evaluatorFactory = this.evaluatorFactory;
|
|
130
|
+
return newCtx;
|
|
131
|
+
}
|
|
93
132
|
}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import { Assert,
|
|
2
|
-
import {
|
|
3
|
-
import { CPSThunk } from "../trampoline.js";
|
|
1
|
+
import { Assert, ASTNode, Test, TestGroup, Visitor } from "yukigo-ast";
|
|
2
|
+
import { ExecutionCommand } from "./kernel/commands.js";
|
|
4
3
|
import { LazyRuntime } from "./runtimes/LazyRuntime.js";
|
|
4
|
+
import { YuValue } from "../primitives/YuValue.js";
|
|
5
|
+
import { Evaluator } from "./evaluators/BaseEvaluator.js";
|
|
5
6
|
export declare class FailedAssert extends Error {
|
|
6
|
-
actual?:
|
|
7
|
-
expected?:
|
|
8
|
-
constructor(actual?:
|
|
7
|
+
actual?: YuValue | undefined;
|
|
8
|
+
expected?: YuValue | undefined;
|
|
9
|
+
constructor(actual?: YuValue | undefined, expected?: YuValue | undefined, message?: string);
|
|
9
10
|
}
|
|
10
|
-
export declare class TestRunner
|
|
11
|
-
interpreter:
|
|
11
|
+
export declare class TestRunner implements Visitor<ExecutionCommand> {
|
|
12
|
+
interpreter: Evaluator;
|
|
12
13
|
private lazyRuntime;
|
|
13
|
-
constructor(interpreter:
|
|
14
|
-
run(node: TestGroup | Test | Assert):
|
|
15
|
-
visitTestGroup(node: TestGroup):
|
|
16
|
-
visitTest(node: Test):
|
|
17
|
-
visitAssert(node: Assert):
|
|
14
|
+
constructor(interpreter: Evaluator, lazyRuntime: LazyRuntime);
|
|
15
|
+
run(node: TestGroup | Test | Assert): ExecutionCommand;
|
|
16
|
+
visitTestGroup(node: TestGroup): ExecutionCommand;
|
|
17
|
+
visitTest(node: Test): ExecutionCommand;
|
|
18
|
+
visitAssert(node: Assert): ExecutionCommand;
|
|
19
|
+
fallback(node: ASTNode): ExecutionCommand;
|
|
18
20
|
}
|
|
@@ -1,5 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Assert, Sequence, Exist, LogicConstraint, } from "yukigo-ast";
|
|
2
|
+
import { LogicEngine } from "./logic/LogicEngine.js";
|
|
3
|
+
import { StepCommand, BindCommand, FailCommand, } from "./kernel/commands.js";
|
|
4
|
+
import { UnexpectedNode } from "../errors.js";
|
|
5
|
+
import { YukigoKernel } from "./kernel/index.js";
|
|
6
|
+
import { EqualityComparer } from "./EqualityComparer.js";
|
|
7
|
+
import { YuBoolean } from "../primitives/scalars/YuBoolean.js";
|
|
8
|
+
import { YuNil } from "../primitives/scalars/YuNil.js";
|
|
9
|
+
import { YuString } from "../primitives/sequences/YuString.js";
|
|
10
|
+
import { isTrue } from "../utils.js";
|
|
11
|
+
import { LogicResult } from "../primitives/entities/LogicResult.js";
|
|
12
|
+
import { RuntimeFunction } from "../primitives/entities/RuntimeFunction.js";
|
|
3
13
|
export class FailedAssert extends Error {
|
|
4
14
|
actual;
|
|
5
15
|
expected;
|
|
@@ -10,77 +20,86 @@ export class FailedAssert extends Error {
|
|
|
10
20
|
this.name = "FailedAssert";
|
|
11
21
|
}
|
|
12
22
|
}
|
|
13
|
-
class AssertionVisitor
|
|
23
|
+
class AssertionVisitor {
|
|
14
24
|
interpreter;
|
|
15
25
|
negated;
|
|
16
|
-
|
|
17
|
-
constructor(interpreter, negated, lazyRuntime) {
|
|
18
|
-
super();
|
|
26
|
+
constructor(interpreter, negated) {
|
|
19
27
|
this.interpreter = interpreter;
|
|
20
28
|
this.negated = negated;
|
|
21
|
-
this.lazyRuntime = lazyRuntime;
|
|
22
29
|
}
|
|
23
30
|
visitFailure(node) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
actualError
|
|
31
|
+
let threw = false;
|
|
32
|
+
let actualError;
|
|
33
|
+
try {
|
|
34
|
+
new YukigoKernel(this.interpreter).run(this.interpreter.evaluate(node.func));
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
threw = true;
|
|
38
|
+
actualError = new YuString(error.message);
|
|
39
|
+
}
|
|
40
|
+
if (!node.message) {
|
|
41
|
+
if (this.negated === threw) {
|
|
42
|
+
return new FailCommand(new FailedAssert(actualError, undefined, threw
|
|
43
|
+
? `Expected code NOT to fail, but it failed with "${actualError?.value}"`
|
|
44
|
+
: `Expected code to fail, but it succeeded`));
|
|
36
45
|
}
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
return new StepCommand(YuNil.getInstance());
|
|
47
|
+
}
|
|
48
|
+
return new BindCommand(this.interpreter.evaluate(node.message), (expectedError) => {
|
|
49
|
+
const isAnyException = expectedError instanceof RuntimeFunction ||
|
|
50
|
+
(expectedError && String(expectedError).includes("anyException"));
|
|
51
|
+
const expMsg = isAnyException || expectedError instanceof YuNil || !expectedError
|
|
52
|
+
? undefined
|
|
53
|
+
: expectedError instanceof YuString
|
|
54
|
+
? expectedError.value
|
|
55
|
+
: expectedError.toString();
|
|
56
|
+
const passed = threw &&
|
|
57
|
+
(expMsg === undefined || actualError?.value?.includes(expMsg));
|
|
58
|
+
if (this.negated === passed) {
|
|
59
|
+
if (!threw) {
|
|
60
|
+
return new FailCommand(new FailedAssert(undefined, expectedError, "Expected code to fail, but it succeeded"));
|
|
48
61
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
62
|
+
else {
|
|
63
|
+
return new FailCommand(new FailedAssert(actualError, expectedError, `Expected error message to contain "${expMsg}", but got "${actualError?.value}"`));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return new StepCommand(YuNil.getInstance());
|
|
67
|
+
});
|
|
52
68
|
}
|
|
53
69
|
visitEquality(node) {
|
|
54
|
-
return (
|
|
55
|
-
return (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
70
|
+
return new BindCommand(this.interpreter.evaluate(node.value), (value) => {
|
|
71
|
+
return new BindCommand(this.interpreter.evaluate(node.expected), (expected) => {
|
|
72
|
+
return new BindCommand(EqualityComparer.compare(value, expected, this.interpreter.getContext()), (passed) => {
|
|
73
|
+
const isPassed = isTrue(passed);
|
|
74
|
+
if (this.negated === isPassed) {
|
|
75
|
+
return new FailCommand(new FailedAssert(value, expected, this.negated
|
|
59
76
|
? `Expected ${JSON.stringify(value)} NOT to be equal to ${JSON.stringify(expected)}`
|
|
60
|
-
: `Expected ${JSON.stringify(expected)}, but got ${JSON.stringify(value)}`);
|
|
77
|
+
: `Expected ${JSON.stringify(expected)}, but got ${JSON.stringify(value)}`));
|
|
61
78
|
}
|
|
62
|
-
return
|
|
79
|
+
return new StepCommand(YuNil.getInstance());
|
|
63
80
|
});
|
|
64
81
|
});
|
|
65
82
|
});
|
|
66
83
|
}
|
|
67
84
|
visitTruth(node) {
|
|
68
|
-
return (
|
|
69
|
-
const isTruthy =
|
|
85
|
+
return new BindCommand(this.interpreter.evaluate(node.body), (value) => {
|
|
86
|
+
const isTruthy = isTrue(value) || (value instanceof LogicResult && value.success);
|
|
70
87
|
if (this.negated === isTruthy) {
|
|
71
|
-
|
|
88
|
+
return new FailCommand(new FailedAssert(value, new YuBoolean(!this.negated), this.negated
|
|
72
89
|
? `Expected value to be falsy, but got ${JSON.stringify(value)}`
|
|
73
|
-
: `Expected value to be truthy, but got ${JSON.stringify(value)}`);
|
|
90
|
+
: `Expected value to be truthy, but got ${JSON.stringify(value)}`));
|
|
74
91
|
}
|
|
75
|
-
return
|
|
92
|
+
return new StepCommand(YuNil.getInstance());
|
|
76
93
|
});
|
|
77
94
|
}
|
|
95
|
+
fallback(node) {
|
|
96
|
+
return new FailCommand(new UnexpectedNode(node.constructor.name, "AssertionVisitor"));
|
|
97
|
+
}
|
|
78
98
|
}
|
|
79
|
-
export class TestRunner
|
|
99
|
+
export class TestRunner {
|
|
80
100
|
interpreter;
|
|
81
101
|
lazyRuntime;
|
|
82
102
|
constructor(interpreter, lazyRuntime) {
|
|
83
|
-
super();
|
|
84
103
|
this.interpreter = interpreter;
|
|
85
104
|
this.lazyRuntime = lazyRuntime;
|
|
86
105
|
}
|
|
@@ -88,16 +107,30 @@ export class TestRunner extends TraverseVisitor {
|
|
|
88
107
|
return node.accept(this);
|
|
89
108
|
}
|
|
90
109
|
visitTestGroup(node) {
|
|
91
|
-
return
|
|
110
|
+
return this.interpreter.evaluate(node.group);
|
|
92
111
|
}
|
|
93
112
|
visitTest(node) {
|
|
94
|
-
|
|
113
|
+
if (node.body.is(Sequence)) {
|
|
114
|
+
const hasLogicGoal = node.body.statements.some((stmt) => stmt.is(Exist) || stmt.is(LogicConstraint) || stmt.is(Assert));
|
|
115
|
+
if (hasLogicGoal) {
|
|
116
|
+
const engine = new LogicEngine(this.interpreter, this.interpreter.getContext());
|
|
117
|
+
const scope = new Map();
|
|
118
|
+
return engine.solveConjunction(node.body.statements, new Map(), scope);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return this.interpreter.evaluate(node.body);
|
|
95
122
|
}
|
|
96
123
|
visitAssert(node) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
124
|
+
const negatedCmd = node.negated
|
|
125
|
+
? this.interpreter.evaluate(node.negated)
|
|
126
|
+
: new StepCommand(new YuBoolean(false));
|
|
127
|
+
return new BindCommand(negatedCmd, (negatedVal) => {
|
|
128
|
+
const isNegated = isTrue(negatedVal);
|
|
129
|
+
const visitor = new AssertionVisitor(this.interpreter, isNegated);
|
|
130
|
+
return node.body.accept(visitor);
|
|
101
131
|
});
|
|
102
132
|
}
|
|
133
|
+
fallback(node) {
|
|
134
|
+
return new FailCommand(new UnexpectedNode(node.constructor.name, "TestRunner"));
|
|
135
|
+
}
|
|
103
136
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ASTNode } from "yukigo-ast";
|
|
2
|
+
import { ExecutionCommand } from "../kernel/commands.js";
|
|
3
|
+
import { RuntimeContext } from "../RuntimeContext.js";
|
|
4
|
+
import { YuValue } from "../../primitives/index.js";
|
|
5
|
+
export type Constructor<T = {}> = new (...args: any[]) => T;
|
|
6
|
+
export interface Evaluator {
|
|
7
|
+
evaluate(node: ASTNode): ExecutionCommand;
|
|
8
|
+
realizeList(val: YuValue): ExecutionCommand;
|
|
9
|
+
fallback(node: ASTNode): ExecutionCommand;
|
|
10
|
+
getContext(): RuntimeContext;
|
|
11
|
+
}
|
|
12
|
+
export declare class EvaluatorBase implements Evaluator {
|
|
13
|
+
context: RuntimeContext;
|
|
14
|
+
constructor(context: RuntimeContext);
|
|
15
|
+
getContext(): RuntimeContext;
|
|
16
|
+
evaluate(node: ASTNode): ExecutionCommand;
|
|
17
|
+
realizeList(val: any): ExecutionCommand;
|
|
18
|
+
fallback(node: ASTNode): ExecutionCommand;
|
|
19
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { FailCommand } from "../kernel/commands.js";
|
|
2
|
+
import { UnexpectedNode } from "../../errors.js";
|
|
3
|
+
export class EvaluatorBase {
|
|
4
|
+
context;
|
|
5
|
+
constructor(context) {
|
|
6
|
+
this.context = context;
|
|
7
|
+
}
|
|
8
|
+
getContext() {
|
|
9
|
+
return this.context;
|
|
10
|
+
}
|
|
11
|
+
evaluate(node) {
|
|
12
|
+
return node.accept(this);
|
|
13
|
+
}
|
|
14
|
+
realizeList(val) {
|
|
15
|
+
return this.context.lazyRuntime.realizeList(val);
|
|
16
|
+
}
|
|
17
|
+
fallback(node) {
|
|
18
|
+
return new FailCommand(new UnexpectedNode(node.constructor.name, this.constructor.name));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Application, Call, CompositionExpression, ConsExpression, DataExpression, Exist, FieldExpression, Findall, Forall, Goal, GuardedExpression, Lambda, LetInExpression, ListComprehension, New, Not, Generator as YuGenerator, Otherwise, RangeExpression, Send, TupleExpression, TypeCast } from "yukigo-ast";
|
|
2
|
+
import { ExecutionCommand } from "../kernel/commands.js";
|
|
3
|
+
import { Constructor, EvaluatorBase } from "./BaseEvaluator.js";
|
|
4
|
+
export declare function ExpressionEvaluator<TBase extends Constructor<EvaluatorBase>>(Base: TBase): {
|
|
5
|
+
new (...args: any[]): {
|
|
6
|
+
visitTupleExpr(node: TupleExpression): ExecutionCommand;
|
|
7
|
+
visitFieldExpression(node: FieldExpression): ExecutionCommand;
|
|
8
|
+
visitDataExpr(node: DataExpression): ExecutionCommand;
|
|
9
|
+
visitConsExpr(node: ConsExpression): ExecutionCommand;
|
|
10
|
+
visitLetInExpr(node: LetInExpression): ExecutionCommand;
|
|
11
|
+
visitCall(node: Call): ExecutionCommand;
|
|
12
|
+
visitOtherwise(node: Otherwise): ExecutionCommand;
|
|
13
|
+
visitCompositionExpression(node: CompositionExpression): ExecutionCommand;
|
|
14
|
+
visitLambda(node: Lambda): ExecutionCommand;
|
|
15
|
+
visitGuardedExpression(expr: GuardedExpression): ExecutionCommand;
|
|
16
|
+
visitApplication(node: Application): ExecutionCommand;
|
|
17
|
+
visitExist(node: Exist): ExecutionCommand;
|
|
18
|
+
visitNot(node: Not): ExecutionCommand;
|
|
19
|
+
visitFindall(node: Findall): ExecutionCommand;
|
|
20
|
+
visitForall(node: Forall): ExecutionCommand;
|
|
21
|
+
visitGoal(node: Goal): ExecutionCommand;
|
|
22
|
+
visitSend(node: Send): ExecutionCommand;
|
|
23
|
+
visitNew(node: New): ExecutionCommand;
|
|
24
|
+
visitListComprehension(node: ListComprehension): ExecutionCommand;
|
|
25
|
+
visitGenerator(node: YuGenerator): ExecutionCommand;
|
|
26
|
+
visitRangeExpression(node: RangeExpression): ExecutionCommand;
|
|
27
|
+
visitTypeCast(node: TypeCast): ExecutionCommand;
|
|
28
|
+
context: import("../RuntimeContext.js").RuntimeContext;
|
|
29
|
+
getContext(): import("../RuntimeContext.js").RuntimeContext;
|
|
30
|
+
evaluate(node: import("yukigo-ast").ASTNode): ExecutionCommand;
|
|
31
|
+
realizeList(val: any): ExecutionCommand;
|
|
32
|
+
fallback(node: import("yukigo-ast").ASTNode): ExecutionCommand;
|
|
33
|
+
};
|
|
34
|
+
} & TBase;
|