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,187 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExecutionCommand,
|
|
3
|
+
StepCommand,
|
|
4
|
+
} from "../../components/kernel/commands.js";
|
|
5
|
+
import { UnsupportedOperation } from "../../errors.js";
|
|
6
|
+
import { boolean, number, raise } from "../../utils.js";
|
|
7
|
+
import {
|
|
8
|
+
Comparable,
|
|
9
|
+
Numeric,
|
|
10
|
+
Summable,
|
|
11
|
+
YuComparable,
|
|
12
|
+
YuNumeric,
|
|
13
|
+
YuSummable,
|
|
14
|
+
} from "../capabilities.js";
|
|
15
|
+
import { YuString } from "../sequences/YuString.js";
|
|
16
|
+
import { YuValue } from "../YuValue.js";
|
|
17
|
+
|
|
18
|
+
export class YuNumber extends YuValue implements Summable, Numeric, Comparable {
|
|
19
|
+
constructor(public readonly value: number) {
|
|
20
|
+
super();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get asSummable(): YuSummable {
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
get asNumeric(): YuNumeric {
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
get asComparable(): YuComparable {
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public round(): ExecutionCommand {
|
|
34
|
+
return number(Math.round(this.value));
|
|
35
|
+
}
|
|
36
|
+
public abs(): ExecutionCommand {
|
|
37
|
+
return number(Math.abs(this.value));
|
|
38
|
+
}
|
|
39
|
+
public ceil(): ExecutionCommand {
|
|
40
|
+
return number(Math.ceil(this.value));
|
|
41
|
+
}
|
|
42
|
+
public floor(): ExecutionCommand {
|
|
43
|
+
return number(Math.floor(this.value));
|
|
44
|
+
}
|
|
45
|
+
public negation(): ExecutionCommand {
|
|
46
|
+
return number(this.value * -1);
|
|
47
|
+
}
|
|
48
|
+
public sqrt(): ExecutionCommand {
|
|
49
|
+
return number(Math.sqrt(this.value));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public plus(other: YuValue): ExecutionCommand {
|
|
53
|
+
const s = other.asSummable;
|
|
54
|
+
if (!s) return raise(new UnsupportedOperation(other, "plus"));
|
|
55
|
+
return s.plusWithNumber(this);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public plusWithNumber(left: YuNumber): ExecutionCommand {
|
|
59
|
+
return number(left.value + this.value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public plusWithString(left: YuString): ExecutionCommand {
|
|
63
|
+
return new StepCommand(new YuString(left.toJSON() + this.value.toString()));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public realize(): ExecutionCommand {
|
|
67
|
+
return new StepCommand(this);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public minus(other: YuValue): ExecutionCommand {
|
|
71
|
+
const n = other.asNumeric;
|
|
72
|
+
if (!n) return raise(new UnsupportedOperation(other, "minus"));
|
|
73
|
+
return n.minusWithNumber(this);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public minusWithNumber(left: YuNumber): ExecutionCommand {
|
|
77
|
+
return number(left.value - this.value);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public multiply(other: YuValue): ExecutionCommand {
|
|
81
|
+
const n = other.asNumeric;
|
|
82
|
+
if (!n) return raise(new UnsupportedOperation(other, "multiply"));
|
|
83
|
+
return n.multiplyWithNumber(this);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public multiplyWithNumber(left: YuNumber): ExecutionCommand {
|
|
87
|
+
return number(left.value * this.value);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public divide(other: YuValue): ExecutionCommand {
|
|
91
|
+
const n = other.asNumeric;
|
|
92
|
+
if (!n) return raise(new UnsupportedOperation(other, "divide"));
|
|
93
|
+
return n.divideWithNumber(this);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public divideWithNumber(left: YuNumber): ExecutionCommand {
|
|
97
|
+
return number(left.value / this.value);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public modulo(other: YuValue): ExecutionCommand {
|
|
101
|
+
const n = other.asNumeric;
|
|
102
|
+
if (!n) return raise(new UnsupportedOperation(other, "modulo"));
|
|
103
|
+
return n.moduloWithNumber(this);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public moduloWithNumber(left: YuNumber): ExecutionCommand {
|
|
107
|
+
return number(left.value % this.value);
|
|
108
|
+
}
|
|
109
|
+
public power(other: YuValue): ExecutionCommand {
|
|
110
|
+
const n = other.asNumeric;
|
|
111
|
+
if (!n) return raise(new UnsupportedOperation(other, "power"));
|
|
112
|
+
return n.powerWithNumber(this);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public powerWithNumber(left: YuNumber): ExecutionCommand {
|
|
116
|
+
return number(left.value ** this.value);
|
|
117
|
+
}
|
|
118
|
+
public min(other: YuValue): ExecutionCommand {
|
|
119
|
+
const n = other.asNumeric;
|
|
120
|
+
if (!n) return raise(new UnsupportedOperation(other, "power"));
|
|
121
|
+
return n.minWithNumber(this);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public minWithNumber(left: YuNumber): ExecutionCommand {
|
|
125
|
+
return number(Math.min(left.value, this.value));
|
|
126
|
+
}
|
|
127
|
+
public max(other: YuValue): ExecutionCommand {
|
|
128
|
+
const n = other.asNumeric;
|
|
129
|
+
if (!n) return raise(new UnsupportedOperation(other, "power"));
|
|
130
|
+
return n.maxWithNumber(this);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public maxWithNumber(left: YuNumber): ExecutionCommand {
|
|
134
|
+
return number(Math.max(left.value, this.value));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public equals(other: YuValue): ExecutionCommand {
|
|
138
|
+
const c = other.asComparable;
|
|
139
|
+
if (!c) return boolean(false);
|
|
140
|
+
return c.equalsWithNumber(this);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public equalsWithNumber(left: YuNumber): ExecutionCommand {
|
|
144
|
+
return boolean(left.value === this.value);
|
|
145
|
+
}
|
|
146
|
+
public equalsWithBoolean(): ExecutionCommand {
|
|
147
|
+
return boolean(false);
|
|
148
|
+
}
|
|
149
|
+
public equalsWithString(left: YuString): ExecutionCommand {
|
|
150
|
+
return boolean(left.toJSON() === this.value.toString());
|
|
151
|
+
}
|
|
152
|
+
public equalsWithArray(): ExecutionCommand {
|
|
153
|
+
return boolean(false);
|
|
154
|
+
}
|
|
155
|
+
public equalsWithNil(): ExecutionCommand {
|
|
156
|
+
return boolean(false);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public compare(other: YuValue): ExecutionCommand {
|
|
160
|
+
const c = other.asComparable;
|
|
161
|
+
if (!c) return raise(new UnsupportedOperation(other, "compare"));
|
|
162
|
+
return c.compareWithNumber(this);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public compareWithNumber(left: YuNumber): ExecutionCommand {
|
|
166
|
+
return number(left.value - this.value);
|
|
167
|
+
}
|
|
168
|
+
public compareWithBoolean(): ExecutionCommand {
|
|
169
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
170
|
+
}
|
|
171
|
+
public compareWithString(left: YuString): ExecutionCommand {
|
|
172
|
+
return number(Number(left.toJSON()) - this.value);
|
|
173
|
+
}
|
|
174
|
+
public compareWithArray(): ExecutionCommand {
|
|
175
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
176
|
+
}
|
|
177
|
+
public compareWithNil(): ExecutionCommand {
|
|
178
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
public toString(): string {
|
|
182
|
+
return this.value.toString();
|
|
183
|
+
}
|
|
184
|
+
public toJSON(): number {
|
|
185
|
+
return this.value;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { YuValue } from "../YuValue.js";
|
|
2
|
+
import {
|
|
3
|
+
BindCommand,
|
|
4
|
+
ExecutionCommand,
|
|
5
|
+
StepCommand,
|
|
6
|
+
} from "../../components/kernel/commands.js";
|
|
7
|
+
import { YuNil } from "../scalars/YuNil.js";
|
|
8
|
+
import { UnsupportedOperation } from "../../errors.js";
|
|
9
|
+
import {
|
|
10
|
+
Comparable,
|
|
11
|
+
Sequence,
|
|
12
|
+
YuComparable,
|
|
13
|
+
YuSequence,
|
|
14
|
+
} from "../capabilities.js";
|
|
15
|
+
import { LazyStepResult } from "../entities/LazyList.js";
|
|
16
|
+
import { YuBoolean, YuNumber, YuString } from "../index.js";
|
|
17
|
+
import { boolean, compareResult, number, raise } from "../../utils.js";
|
|
18
|
+
|
|
19
|
+
export class YuArray extends YuValue implements Sequence, Comparable {
|
|
20
|
+
constructor(
|
|
21
|
+
public readonly items: YuValue[],
|
|
22
|
+
public readonly index: number = 0,
|
|
23
|
+
) {
|
|
24
|
+
super();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get asSequence(): YuSequence {
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
get asComparable(): YuComparable {
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public *[Symbol.iterator](): Iterator<YuValue> {
|
|
35
|
+
for (let i = this.index; i < this.items.length; i++) {
|
|
36
|
+
yield this.items[i];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public size(): ExecutionCommand {
|
|
41
|
+
return number(this.items.length);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public step(): ExecutionCommand {
|
|
45
|
+
if (this.index >= this.items.length) {
|
|
46
|
+
return new StepCommand(YuNil.getInstance());
|
|
47
|
+
}
|
|
48
|
+
const item = this.items[this.index];
|
|
49
|
+
const tail = new YuArray(this.items, this.index + 1);
|
|
50
|
+
return new StepCommand(new LazyStepResult(item, tail));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public concat(other: YuValue): ExecutionCommand {
|
|
54
|
+
const seq = other.asSequence;
|
|
55
|
+
if (!seq) return raise(new UnsupportedOperation(other, "concat"));
|
|
56
|
+
return seq.concatWithArray(this);
|
|
57
|
+
}
|
|
58
|
+
public concatWithArray(arr: YuArray): ExecutionCommand {
|
|
59
|
+
return new StepCommand(new YuArray([...arr, ...this]));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public concatWithString(str: YuString): ExecutionCommand {
|
|
63
|
+
return new StepCommand(new YuString(this.toJSON() + str.toJSON()));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public at(index: number) {
|
|
67
|
+
return this.items[this.index + index];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public flat() {
|
|
71
|
+
return new YuArray(
|
|
72
|
+
[...this].flatMap((item) => {
|
|
73
|
+
const seq = item.asSequence;
|
|
74
|
+
return seq instanceof YuArray ? [...seq] : [item];
|
|
75
|
+
}),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public equals(other: YuValue): ExecutionCommand {
|
|
80
|
+
const c = other.asComparable;
|
|
81
|
+
if (!c) return boolean(false);
|
|
82
|
+
return c.equalsWithArray(this);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public equalsWithNumber(): ExecutionCommand {
|
|
86
|
+
return boolean(false);
|
|
87
|
+
}
|
|
88
|
+
public equalsWithBoolean(): ExecutionCommand {
|
|
89
|
+
return boolean(false);
|
|
90
|
+
}
|
|
91
|
+
public equalsWithString(): ExecutionCommand {
|
|
92
|
+
return boolean(false);
|
|
93
|
+
}
|
|
94
|
+
public equalsWithArray(left: YuArray): ExecutionCommand {
|
|
95
|
+
return new BindCommand(left.size(), (res) => {
|
|
96
|
+
const leftSize = res.asNumeric?.asComparable;
|
|
97
|
+
if (!leftSize || !leftSize.equals(new YuNumber(this.items.length)))
|
|
98
|
+
return boolean(false);
|
|
99
|
+
const compareNext = (index: number): ExecutionCommand => {
|
|
100
|
+
if (
|
|
101
|
+
compareResult(
|
|
102
|
+
leftSize.compare(new YuNumber(index)),
|
|
103
|
+
(res) => res >= 0,
|
|
104
|
+
)
|
|
105
|
+
) {
|
|
106
|
+
return boolean(true);
|
|
107
|
+
}
|
|
108
|
+
return new BindCommand(left.at(index).equals(this.at(index)), (res) => {
|
|
109
|
+
if (!res.toJSON()) return boolean(false);
|
|
110
|
+
return compareNext(index + 1);
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
return compareNext(0);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
public equalsWithNil(): ExecutionCommand {
|
|
118
|
+
return boolean(false);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public compare(other: YuValue): ExecutionCommand {
|
|
122
|
+
const c = other.asComparable;
|
|
123
|
+
if (!c) return raise(new UnsupportedOperation(other, "compare"));
|
|
124
|
+
return c.compareWithArray(this);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public compareWithNumber(): ExecutionCommand {
|
|
128
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
129
|
+
}
|
|
130
|
+
public compareWithBoolean(): ExecutionCommand {
|
|
131
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
132
|
+
}
|
|
133
|
+
public compareWithString(): ExecutionCommand {
|
|
134
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
135
|
+
}
|
|
136
|
+
public compareWithArray(left: YuArray): ExecutionCommand {
|
|
137
|
+
const a = [...left];
|
|
138
|
+
const b = [...this];
|
|
139
|
+
const minLen = Math.min(a.length, b.length);
|
|
140
|
+
|
|
141
|
+
const compareNext = (index: number): ExecutionCommand => {
|
|
142
|
+
if (index >= minLen) {
|
|
143
|
+
return new StepCommand(new YuNumber(a.length - b.length));
|
|
144
|
+
}
|
|
145
|
+
return new BindCommand(a[index].compare(b[index]), (res) => {
|
|
146
|
+
const cmp = res.toJSON() as number;
|
|
147
|
+
if (cmp !== 0) {
|
|
148
|
+
return new StepCommand(res);
|
|
149
|
+
}
|
|
150
|
+
return compareNext(index + 1);
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
return compareNext(0);
|
|
155
|
+
}
|
|
156
|
+
public compareWithNil(): ExecutionCommand {
|
|
157
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public toJSON(keyOrSeen?: string | Set<YuValue>): unknown {
|
|
161
|
+
const seen = keyOrSeen instanceof Set ? keyOrSeen : new Set<YuValue>();
|
|
162
|
+
if (seen.has(this)) return "[Circular]";
|
|
163
|
+
seen.add(this);
|
|
164
|
+
const result = this.index === 0
|
|
165
|
+
? this.items.map((i) => i.toJSON(seen))
|
|
166
|
+
: this.items.slice(this.index).map((i) => i.toJSON(seen));
|
|
167
|
+
seen.delete(this);
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
public toString(seen = new Set<YuValue>()): string {
|
|
172
|
+
if (seen.has(this)) return "[Circular]";
|
|
173
|
+
seen.add(this);
|
|
174
|
+
const result = `[${[...this].map((i) => i.toString(seen)).join(", ")}]`;
|
|
175
|
+
seen.delete(this);
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { YuValue } from "../YuValue.js";
|
|
2
|
+
import { YuChar } from "../scalars/YuChar.js";
|
|
3
|
+
import {
|
|
4
|
+
ExecutionCommand,
|
|
5
|
+
StepCommand,
|
|
6
|
+
} from "../../components/kernel/commands.js";
|
|
7
|
+
import { YuNil } from "../scalars/YuNil.js";
|
|
8
|
+
import { YuNumber } from "../scalars/YuNumber.js";
|
|
9
|
+
import { YuArray } from "./YuArray.js";
|
|
10
|
+
import { UnsupportedOperation } from "../../errors.js";
|
|
11
|
+
import {
|
|
12
|
+
Comparable,
|
|
13
|
+
Sequence,
|
|
14
|
+
Summable,
|
|
15
|
+
YuComparable,
|
|
16
|
+
YuSequence,
|
|
17
|
+
YuSummable,
|
|
18
|
+
} from "../capabilities.js";
|
|
19
|
+
import { LazyStepResult, YuBoolean } from "../index.js";
|
|
20
|
+
import { boolean, number, raise } from "../../utils.js";
|
|
21
|
+
|
|
22
|
+
export class YuString
|
|
23
|
+
extends YuValue
|
|
24
|
+
implements Sequence, Summable, Comparable
|
|
25
|
+
{
|
|
26
|
+
constructor(
|
|
27
|
+
public readonly value: string,
|
|
28
|
+
public readonly index: number = 0,
|
|
29
|
+
) {
|
|
30
|
+
super();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get asSequence(): YuSequence {
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
get asSummable(): YuSummable {
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
get asComparable(): YuComparable {
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public *[Symbol.iterator](): Iterator<YuChar> {
|
|
44
|
+
for (let i = this.index; i < this.value.length; i++) {
|
|
45
|
+
yield new YuChar(this.value[i]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public step(): ExecutionCommand {
|
|
50
|
+
if (this.index >= this.value.length) {
|
|
51
|
+
return new StepCommand(YuNil.getInstance());
|
|
52
|
+
}
|
|
53
|
+
const char = new YuChar(this.value[this.index]);
|
|
54
|
+
const tail = new YuString(this.value, this.index + 1);
|
|
55
|
+
return new StepCommand(new LazyStepResult(char, tail));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public size(): ExecutionCommand {
|
|
59
|
+
return number(this.value.length)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
public plus(other: YuValue): ExecutionCommand {
|
|
64
|
+
const s = other.asSummable;
|
|
65
|
+
if (!s) return raise(new UnsupportedOperation(other, "plus"));
|
|
66
|
+
return s.plusWithString(this);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public plusWithNumber(left: YuNumber): ExecutionCommand {
|
|
70
|
+
return new StepCommand(
|
|
71
|
+
new YuString(left.toJSON().toString() + this.toJSON()),
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public plusWithString(left: YuString): ExecutionCommand {
|
|
76
|
+
return new StepCommand(new YuString(left.toJSON() + this.toJSON()));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public split(): ExecutionCommand {
|
|
80
|
+
const items = this.toJSON()
|
|
81
|
+
.split("")
|
|
82
|
+
.map((char) => new YuChar(char));
|
|
83
|
+
return new StepCommand(new YuArray(items));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public concat(other: YuValue): ExecutionCommand {
|
|
87
|
+
const seq = other.asSequence
|
|
88
|
+
if (!seq) return raise(new UnsupportedOperation(other, "concat"));
|
|
89
|
+
return seq.concatWithString(this)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public concatWithString(str: YuString): ExecutionCommand {
|
|
93
|
+
return new StepCommand(new YuString(str.toJSON() + this.toJSON()));
|
|
94
|
+
}
|
|
95
|
+
public concatWithArray(arr: YuArray): ExecutionCommand {
|
|
96
|
+
return new StepCommand(new YuString(arr.toString()));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public realize(): ExecutionCommand {
|
|
100
|
+
return this.split();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public equals(other: YuValue): ExecutionCommand {
|
|
104
|
+
const c = other.asComparable;
|
|
105
|
+
if (!c) return boolean(false);
|
|
106
|
+
return c.equalsWithString(this);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public contains(other: YuValue): ExecutionCommand {
|
|
110
|
+
if (other instanceof YuString) {
|
|
111
|
+
return boolean(this.value.includes(other.value));
|
|
112
|
+
}
|
|
113
|
+
return raise(new UnsupportedOperation(other, "contains"));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public equalsWithNumber(left: YuNumber): ExecutionCommand {
|
|
117
|
+
return new StepCommand(
|
|
118
|
+
new YuBoolean(left.value.toString() === this.toJSON()),
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
public equalsWithBoolean(): ExecutionCommand {
|
|
122
|
+
return boolean(false);
|
|
123
|
+
}
|
|
124
|
+
public equalsWithString(left: YuString): ExecutionCommand {
|
|
125
|
+
return boolean(left.toJSON() === this.toJSON());
|
|
126
|
+
}
|
|
127
|
+
public equalsWithArray(): ExecutionCommand {
|
|
128
|
+
return boolean(false);
|
|
129
|
+
}
|
|
130
|
+
public equalsWithNil(): ExecutionCommand {
|
|
131
|
+
return boolean(false);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public compare(other: YuValue): ExecutionCommand {
|
|
135
|
+
const c = other.asComparable;
|
|
136
|
+
if (!c) return raise(new UnsupportedOperation(other, "compare"));
|
|
137
|
+
return c.compareWithString(this);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public compareWithNumber(left: YuNumber): ExecutionCommand {
|
|
141
|
+
return new StepCommand(new YuNumber(left.value - Number(this.toJSON())));
|
|
142
|
+
}
|
|
143
|
+
public compareWithBoolean(): ExecutionCommand {
|
|
144
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
145
|
+
}
|
|
146
|
+
public compareWithString(left: YuString): ExecutionCommand {
|
|
147
|
+
return new StepCommand(
|
|
148
|
+
new YuNumber(left.toJSON().localeCompare(this.toJSON())),
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
public compareWithArray(): ExecutionCommand {
|
|
152
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
153
|
+
}
|
|
154
|
+
public compareWithNil(): ExecutionCommand {
|
|
155
|
+
return raise(new UnsupportedOperation(this, "compare"));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public toJSON(): string {
|
|
159
|
+
return this.index === 0 ? this.value : this.value.slice(this.index);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public toString(): string {
|
|
163
|
+
return this.toJSON();
|
|
164
|
+
}
|
|
165
|
+
}
|