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.
Files changed (266) hide show
  1. package/.mocharc.json +3 -3
  2. package/CHANGELOG.md +105 -40
  3. package/README.md +193 -193
  4. package/dist/analyzer/GraphBuilder.d.ts +1 -0
  5. package/dist/analyzer/GraphBuilder.js +2 -1
  6. package/dist/analyzer/index.d.ts +0 -7
  7. package/dist/analyzer/index.js +3 -3
  8. package/dist/analyzer/inspections/functional/functional.d.ts +1 -1
  9. package/dist/analyzer/inspections/functional/functional.js +1 -1
  10. package/dist/analyzer/inspections/functional/smells.d.ts +3 -3
  11. package/dist/analyzer/inspections/functional/smells.js +27 -35
  12. package/dist/analyzer/inspections/generic/generic.d.ts +22 -14
  13. package/dist/analyzer/inspections/generic/generic.js +82 -60
  14. package/dist/analyzer/inspections/generic/smells.d.ts +2 -0
  15. package/dist/analyzer/inspections/generic/smells.js +55 -64
  16. package/dist/analyzer/inspections/imperative/imperative.d.ts +3 -3
  17. package/dist/analyzer/inspections/imperative/imperative.js +3 -3
  18. package/dist/analyzer/inspections/imperative/smells.d.ts +1 -0
  19. package/dist/analyzer/inspections/imperative/smells.js +8 -5
  20. package/dist/analyzer/inspections/logic/logic.d.ts +8 -8
  21. package/dist/analyzer/inspections/logic/logic.js +5 -5
  22. package/dist/analyzer/inspections/logic/smells.js +2 -2
  23. package/dist/analyzer/inspections/object/object.d.ts +5 -5
  24. package/dist/analyzer/inspections/object/object.js +22 -21
  25. package/dist/analyzer/inspections/object/smells.js +11 -23
  26. package/dist/analyzer/utils.d.ts +6 -2
  27. package/dist/analyzer/utils.js +6 -2
  28. package/dist/interpreter/components/EnvBuilder.d.ts +20 -1
  29. package/dist/interpreter/components/EnvBuilder.js +211 -66
  30. package/dist/interpreter/components/EqualityComparer.d.ts +15 -0
  31. package/dist/interpreter/components/EqualityComparer.js +69 -0
  32. package/dist/interpreter/components/Operations.d.ts +17 -14
  33. package/dist/interpreter/components/Operations.js +61 -62
  34. package/dist/interpreter/components/PatternMatcher.d.ts +19 -58
  35. package/dist/interpreter/components/PatternMatcher.js +185 -286
  36. package/dist/interpreter/components/RuntimeContext.d.ts +24 -15
  37. package/dist/interpreter/components/RuntimeContext.js +43 -4
  38. package/dist/interpreter/components/TestRunner.d.ts +15 -13
  39. package/dist/interpreter/components/TestRunner.js +86 -53
  40. package/dist/interpreter/components/evaluators/BaseEvaluator.d.ts +19 -0
  41. package/dist/interpreter/components/evaluators/BaseEvaluator.js +20 -0
  42. package/dist/interpreter/components/evaluators/ExpressionEvaluator.d.ts +34 -0
  43. package/dist/interpreter/components/evaluators/ExpressionEvaluator.js +298 -0
  44. package/dist/interpreter/components/evaluators/OperationEvaluator.d.ts +25 -0
  45. package/dist/interpreter/components/evaluators/OperationEvaluator.js +226 -0
  46. package/dist/interpreter/components/evaluators/PrimitiveEvaluator.d.ts +19 -0
  47. package/dist/interpreter/components/evaluators/PrimitiveEvaluator.js +50 -0
  48. package/dist/interpreter/components/evaluators/StatementEvaluator.d.ts +28 -0
  49. package/dist/interpreter/components/evaluators/StatementEvaluator.js +240 -0
  50. package/dist/interpreter/components/evaluators/TestingEvaluator.d.ts +15 -0
  51. package/dist/interpreter/components/evaluators/TestingEvaluator.js +14 -0
  52. package/dist/interpreter/components/evaluators/index.d.ts +110 -0
  53. package/dist/interpreter/components/evaluators/index.js +21 -0
  54. package/dist/interpreter/components/evaluators/utils.d.ts +7 -0
  55. package/dist/interpreter/components/evaluators/utils.js +50 -0
  56. package/dist/interpreter/components/hooks/YukigoHook.d.ts +27 -0
  57. package/dist/interpreter/components/hooks/YukigoHook.js +1 -0
  58. package/dist/interpreter/components/kernel/commands.d.ts +122 -0
  59. package/dist/interpreter/components/kernel/commands.js +203 -0
  60. package/dist/interpreter/components/kernel/index.d.ts +51 -0
  61. package/dist/interpreter/components/kernel/index.js +176 -0
  62. package/dist/interpreter/components/logic/LogicEngine.d.ts +26 -23
  63. package/dist/interpreter/components/logic/LogicEngine.js +234 -221
  64. package/dist/interpreter/components/logic/LogicResolver.d.ts +22 -51
  65. package/dist/interpreter/components/logic/LogicResolver.js +73 -440
  66. package/dist/interpreter/components/logic/LogicTerm.d.ts +104 -0
  67. package/dist/interpreter/components/logic/LogicTerm.js +423 -0
  68. package/dist/interpreter/components/logic/LogicTranslator.d.ts +25 -9
  69. package/dist/interpreter/components/logic/LogicTranslator.js +211 -74
  70. package/dist/interpreter/components/runtimes/FunctionRuntime.d.ts +6 -4
  71. package/dist/interpreter/components/runtimes/FunctionRuntime.js +82 -84
  72. package/dist/interpreter/components/runtimes/LazyRuntime.d.ts +13 -14
  73. package/dist/interpreter/components/runtimes/LazyRuntime.js +112 -239
  74. package/dist/interpreter/components/runtimes/ObjectRuntime.d.ts +17 -14
  75. package/dist/interpreter/components/runtimes/ObjectRuntime.js +105 -74
  76. package/dist/interpreter/errors.d.ts +10 -0
  77. package/dist/interpreter/errors.js +17 -0
  78. package/dist/interpreter/index.d.ts +9 -5
  79. package/dist/interpreter/index.js +11 -13
  80. package/dist/interpreter/primitives/YuValue.d.ts +21 -0
  81. package/dist/interpreter/primitives/YuValue.js +40 -0
  82. package/dist/interpreter/primitives/capabilities.d.ts +90 -0
  83. package/dist/interpreter/primitives/capabilities.js +1 -0
  84. package/dist/interpreter/primitives/entities/LazyList.d.ts +48 -0
  85. package/dist/interpreter/primitives/entities/LazyList.js +164 -0
  86. package/dist/interpreter/primitives/entities/LogicResult.d.ts +51 -0
  87. package/dist/interpreter/primitives/entities/LogicResult.js +101 -0
  88. package/dist/interpreter/primitives/entities/RuntimeClass.d.ts +24 -0
  89. package/dist/interpreter/primitives/entities/RuntimeClass.js +57 -0
  90. package/dist/interpreter/primitives/entities/RuntimeFunction.d.ts +40 -0
  91. package/dist/interpreter/primitives/entities/RuntimeFunction.js +73 -0
  92. package/dist/interpreter/primitives/entities/RuntimeObject.d.ts +27 -0
  93. package/dist/interpreter/primitives/entities/RuntimeObject.js +82 -0
  94. package/dist/interpreter/primitives/entities/RuntimePredicate.d.ts +19 -0
  95. package/dist/interpreter/primitives/entities/RuntimePredicate.js +59 -0
  96. package/dist/interpreter/primitives/index.d.ts +14 -0
  97. package/dist/interpreter/primitives/index.js +14 -0
  98. package/dist/interpreter/primitives/scalars/YuBoolean.d.ts +24 -0
  99. package/dist/interpreter/primitives/scalars/YuBoolean.js +69 -0
  100. package/dist/interpreter/primitives/scalars/YuChar.d.ts +28 -0
  101. package/dist/interpreter/primitives/scalars/YuChar.js +78 -0
  102. package/dist/interpreter/primitives/scalars/YuNil.d.ts +25 -0
  103. package/dist/interpreter/primitives/scalars/YuNil.js +73 -0
  104. package/dist/interpreter/primitives/scalars/YuNumber.d.ts +49 -0
  105. package/dist/interpreter/primitives/scalars/YuNumber.js +165 -0
  106. package/dist/interpreter/primitives/sequences/YuArray.d.ts +33 -0
  107. package/dist/interpreter/primitives/sequences/YuArray.js +150 -0
  108. package/dist/interpreter/primitives/sequences/YuString.d.ts +40 -0
  109. package/dist/interpreter/primitives/sequences/YuString.js +130 -0
  110. package/dist/interpreter/utils.d.ts +23 -10
  111. package/dist/interpreter/utils.js +25 -30
  112. package/dist/tester/index.d.ts +3 -3
  113. package/dist/tester/index.js +39 -28
  114. package/dist/utils/helpers.d.ts +13 -5
  115. package/dist/utils/helpers.js +38 -85
  116. package/package.json +3 -16
  117. package/src/analyzer/GraphBuilder.ts +143 -142
  118. package/src/analyzer/index.ts +185 -194
  119. package/src/analyzer/inspections/functional/functional.ts +121 -121
  120. package/src/analyzer/inspections/functional/smells.ts +88 -102
  121. package/src/analyzer/inspections/generic/generic.ts +610 -577
  122. package/src/analyzer/inspections/generic/smells.ts +355 -365
  123. package/src/analyzer/inspections/imperative/imperative.ts +104 -101
  124. package/src/analyzer/inspections/imperative/smells.ts +58 -54
  125. package/src/analyzer/inspections/logic/logic.ts +84 -90
  126. package/src/analyzer/inspections/logic/smells.ts +54 -54
  127. package/src/analyzer/inspections/object/object.ts +295 -264
  128. package/src/analyzer/inspections/object/smells.ts +130 -144
  129. package/src/analyzer/utils.ts +115 -109
  130. package/src/index.ts +3 -3
  131. package/src/interpreter/components/EnvBuilder.ts +405 -202
  132. package/src/interpreter/components/EqualityComparer.ts +107 -0
  133. package/src/interpreter/components/Operations.ts +126 -99
  134. package/src/interpreter/components/PatternMatcher.ts +375 -475
  135. package/src/interpreter/components/RuntimeContext.ts +181 -119
  136. package/src/interpreter/components/TestRunner.ts +223 -151
  137. package/src/interpreter/components/evaluators/BaseEvaluator.ts +36 -0
  138. package/src/interpreter/components/evaluators/ExpressionEvaluator.ts +475 -0
  139. package/src/interpreter/components/evaluators/OperationEvaluator.ts +477 -0
  140. package/src/interpreter/components/evaluators/PrimitiveEvaluator.ts +80 -0
  141. package/src/interpreter/components/evaluators/StatementEvaluator.ts +372 -0
  142. package/src/interpreter/components/evaluators/TestingEvaluator.ts +26 -0
  143. package/src/interpreter/components/evaluators/index.ts +34 -0
  144. package/src/interpreter/components/evaluators/utils.ts +64 -0
  145. package/src/interpreter/components/hooks/YukigoHook.ts +45 -0
  146. package/src/interpreter/components/kernel/commands.ts +255 -0
  147. package/src/interpreter/components/kernel/index.ts +222 -0
  148. package/src/interpreter/components/logic/LogicEngine.ts +509 -519
  149. package/src/interpreter/components/logic/LogicResolver.ts +209 -858
  150. package/src/interpreter/components/logic/LogicTerm.ts +477 -0
  151. package/src/interpreter/components/logic/LogicTranslator.ts +362 -149
  152. package/src/interpreter/components/runtimes/FunctionRuntime.ts +249 -227
  153. package/src/interpreter/components/runtimes/LazyRuntime.ts +226 -334
  154. package/src/interpreter/components/runtimes/ObjectRuntime.ts +262 -224
  155. package/src/interpreter/errors.ts +68 -47
  156. package/src/interpreter/index.ts +50 -52
  157. package/src/interpreter/primitives/YuValue.ts +64 -0
  158. package/src/interpreter/primitives/capabilities.ts +111 -0
  159. package/src/interpreter/primitives/entities/LazyList.ts +225 -0
  160. package/src/interpreter/primitives/entities/LogicResult.ts +133 -0
  161. package/src/interpreter/primitives/entities/RuntimeClass.ts +70 -0
  162. package/src/interpreter/primitives/entities/RuntimeFunction.ts +108 -0
  163. package/src/interpreter/primitives/entities/RuntimeObject.ts +110 -0
  164. package/src/interpreter/primitives/entities/RuntimePredicate.ts +77 -0
  165. package/src/interpreter/primitives/index.ts +17 -0
  166. package/src/interpreter/primitives/scalars/YuBoolean.ts +79 -0
  167. package/src/interpreter/primitives/scalars/YuChar.ts +94 -0
  168. package/src/interpreter/primitives/scalars/YuNil.ts +84 -0
  169. package/src/interpreter/primitives/scalars/YuNumber.ts +187 -0
  170. package/src/interpreter/primitives/sequences/YuArray.ts +178 -0
  171. package/src/interpreter/primitives/sequences/YuString.ts +165 -0
  172. package/src/interpreter/utils.ts +105 -84
  173. package/src/tester/index.ts +157 -128
  174. package/src/utils/helpers.ts +113 -169
  175. package/tests/analyzer/functional.spec.ts +221 -207
  176. package/tests/analyzer/generic.spec.ts +178 -178
  177. package/tests/analyzer/helpers.spec.ts +83 -89
  178. package/tests/analyzer/logic.spec.ts +237 -237
  179. package/tests/analyzer/oop.spec.ts +320 -323
  180. package/tests/analyzer/transitive.spec.ts +166 -166
  181. package/tests/interpreter/EnvBuilder.spec.ts +181 -183
  182. package/tests/interpreter/FunctionRuntime.spec.ts +224 -223
  183. package/tests/interpreter/LazyRuntime.spec.ts +227 -225
  184. package/tests/interpreter/LogicEngine.spec.ts +371 -327
  185. package/tests/interpreter/LogicSubstitution.spec.ts +90 -80
  186. package/tests/interpreter/ObjectRuntime.spec.ts +431 -606
  187. package/tests/interpreter/Operations.spec.ts +312 -220
  188. package/tests/interpreter/PatternSystem.spec.ts +254 -213
  189. package/tests/interpreter/Tests.spec.ts +122 -122
  190. package/tests/interpreter/interpreter.spec.ts +1143 -991
  191. package/tests/kernel/YukigoKernel.spec.ts +112 -0
  192. package/tests/tester/Tester.spec.ts +156 -152
  193. package/tsconfig.build.json +16 -15
  194. package/tsconfig.json +27 -25
  195. package/dist/interpreter/components/Visitor.d.ts +0 -70
  196. package/dist/interpreter/components/Visitor.js +0 -634
  197. package/dist/interpreter/entities.d.ts +0 -105
  198. package/dist/interpreter/entities.js +0 -96
  199. package/dist/interpreter/trampoline.d.ts +0 -17
  200. package/dist/interpreter/trampoline.js +0 -38
  201. package/dist/src/analyzer/GraphBuilder.d.ts +0 -30
  202. package/dist/src/analyzer/GraphBuilder.js +0 -100
  203. package/dist/src/analyzer/index.d.ts +0 -59
  204. package/dist/src/analyzer/index.js +0 -152
  205. package/dist/src/analyzer/inspections/functional/functional.d.ts +0 -44
  206. package/dist/src/analyzer/inspections/functional/functional.js +0 -149
  207. package/dist/src/analyzer/inspections/functional/smells.d.ts +0 -16
  208. package/dist/src/analyzer/inspections/functional/smells.js +0 -98
  209. package/dist/src/analyzer/inspections/generic/generic.d.ts +0 -178
  210. package/dist/src/analyzer/inspections/generic/generic.js +0 -604
  211. package/dist/src/analyzer/inspections/generic/smells.d.ts +0 -61
  212. package/dist/src/analyzer/inspections/generic/smells.js +0 -349
  213. package/dist/src/analyzer/inspections/imperative/imperative.d.ts +0 -35
  214. package/dist/src/analyzer/inspections/imperative/imperative.js +0 -109
  215. package/dist/src/analyzer/inspections/imperative/smells.d.ts +0 -16
  216. package/dist/src/analyzer/inspections/imperative/smells.js +0 -58
  217. package/dist/src/analyzer/inspections/logic/logic.d.ts +0 -32
  218. package/dist/src/analyzer/inspections/logic/logic.js +0 -96
  219. package/dist/src/analyzer/inspections/logic/smells.d.ts +0 -15
  220. package/dist/src/analyzer/inspections/logic/smells.js +0 -60
  221. package/dist/src/analyzer/inspections/object/object.d.ts +0 -90
  222. package/dist/src/analyzer/inspections/object/object.js +0 -321
  223. package/dist/src/analyzer/inspections/object/smells.d.ts +0 -30
  224. package/dist/src/analyzer/inspections/object/smells.js +0 -135
  225. package/dist/src/analyzer/utils.d.ts +0 -30
  226. package/dist/src/analyzer/utils.js +0 -78
  227. package/dist/src/index.d.ts +0 -4
  228. package/dist/src/index.js +0 -4
  229. package/dist/src/interpreter/components/EnvBuilder.d.ts +0 -21
  230. package/dist/src/interpreter/components/EnvBuilder.js +0 -155
  231. package/dist/src/interpreter/components/Operations.d.ts +0 -14
  232. package/dist/src/interpreter/components/Operations.js +0 -84
  233. package/dist/src/interpreter/components/PatternMatcher.d.ts +0 -73
  234. package/dist/src/interpreter/components/PatternMatcher.js +0 -358
  235. package/dist/src/interpreter/components/RuntimeContext.d.ts +0 -35
  236. package/dist/src/interpreter/components/RuntimeContext.js +0 -93
  237. package/dist/src/interpreter/components/TestRunner.d.ts +0 -19
  238. package/dist/src/interpreter/components/TestRunner.js +0 -110
  239. package/dist/src/interpreter/components/Visitor.d.ts +0 -71
  240. package/dist/src/interpreter/components/Visitor.js +0 -638
  241. package/dist/src/interpreter/components/logic/LogicEngine.d.ts +0 -29
  242. package/dist/src/interpreter/components/logic/LogicEngine.js +0 -259
  243. package/dist/src/interpreter/components/logic/LogicResolver.d.ts +0 -53
  244. package/dist/src/interpreter/components/logic/LogicResolver.js +0 -484
  245. package/dist/src/interpreter/components/logic/LogicTranslator.d.ts +0 -14
  246. package/dist/src/interpreter/components/logic/LogicTranslator.js +0 -99
  247. package/dist/src/interpreter/components/runtimes/FunctionRuntime.d.ts +0 -12
  248. package/dist/src/interpreter/components/runtimes/FunctionRuntime.js +0 -149
  249. package/dist/src/interpreter/components/runtimes/LazyRuntime.d.ts +0 -19
  250. package/dist/src/interpreter/components/runtimes/LazyRuntime.js +0 -269
  251. package/dist/src/interpreter/components/runtimes/ObjectRuntime.d.ts +0 -35
  252. package/dist/src/interpreter/components/runtimes/ObjectRuntime.js +0 -126
  253. package/dist/src/interpreter/errors.d.ts +0 -19
  254. package/dist/src/interpreter/errors.js +0 -36
  255. package/dist/src/interpreter/index.d.ts +0 -24
  256. package/dist/src/interpreter/index.js +0 -41
  257. package/dist/src/interpreter/trampoline.d.ts +0 -17
  258. package/dist/src/interpreter/trampoline.js +0 -38
  259. package/dist/src/interpreter/utils.d.ts +0 -11
  260. package/dist/src/interpreter/utils.js +0 -65
  261. package/dist/src/tester/index.d.ts +0 -25
  262. package/dist/src/tester/index.js +0 -113
  263. package/dist/src/utils/helpers.d.ts +0 -13
  264. package/dist/src/utils/helpers.js +0 -52
  265. package/src/interpreter/components/Visitor.ts +0 -1065
  266. package/src/interpreter/trampoline.ts +0 -71
@@ -1,475 +1,375 @@
1
- import {
2
- ApplicationPattern,
3
- AsPattern,
4
- ASTNode,
5
- ConsPattern,
6
- ConstructorPattern,
7
- Expression,
8
- FunctorPattern,
9
- isLazyList,
10
- LazyList,
11
- ListPattern,
12
- ListPrimitive,
13
- LiteralPattern,
14
- Pattern,
15
- PrimitiveValue,
16
- TuplePattern,
17
- UnionPattern,
18
- VariablePattern,
19
- Visitor,
20
- WildcardPattern,
21
- TypePattern,
22
- SimpleType,
23
- ListType,
24
- EnvStack,
25
- } from "yukigo-ast";
26
- import { Bindings } from "../index.js";
27
- import { InterpreterVisitor } from "./Visitor.js";
28
- import { CPSThunk, Thunk, Continuation } from "../trampoline.js";
29
- import { RuntimeContext } from "./RuntimeContext.js";
30
- import { ExpressionEvaluator, getYukigoType } from "../utils.js";
31
-
32
- class SharedSequence {
33
- private cache: PrimitiveValue[] = [];
34
- private source: Generator<PrimitiveValue, void, unknown>;
35
- private isDone: boolean = false;
36
-
37
- constructor(
38
- generatorFactory: () => Generator<PrimitiveValue, void, unknown>,
39
- ) {
40
- this.source = generatorFactory();
41
- }
42
-
43
- get(index: number): { value: PrimitiveValue | null; done: boolean } {
44
- if (index < this.cache.length)
45
- return { value: this.cache[index], done: false };
46
-
47
- if (this.isDone) return { value: null, done: true };
48
-
49
- while (this.cache.length <= index) {
50
- const next = this.source.next();
51
- if (next.done) {
52
- this.isDone = true;
53
- return { value: null, done: true };
54
- }
55
- this.cache.push(next.value);
56
- }
57
-
58
- return { value: this.cache[index], done: false };
59
- }
60
- }
61
- export interface InternalConsState {
62
- readonly head: PrimitiveValue;
63
- readonly tailExpr: Expression;
64
- readonly evaluator: ExpressionEvaluator;
65
- readonly capturedEnv: EnvStack;
66
- realizedTail?: PrimitiveValue;
67
- }
68
-
69
- export interface MemoizedLazyList extends LazyList {
70
- _sequence: SharedSequence;
71
- _offset: number;
72
- _consState?: InternalConsState;
73
- toJSON: () => any;
74
- }
75
-
76
- export function isMemoizedList(list: unknown): list is MemoizedLazyList {
77
- return (
78
- list &&
79
- typeof list === "object" &&
80
- "type" in list &&
81
- "_offset" in list &&
82
- "_sequence" in list
83
- );
84
- }
85
-
86
- export function createMemoizedStream(
87
- genFactory: () => Generator<PrimitiveValue, void, unknown>,
88
- sequence?: SharedSequence,
89
- offset: number = 0,
90
- ): MemoizedLazyList {
91
- const seq = sequence ?? new SharedSequence(genFactory);
92
-
93
- return {
94
- type: "LazyList",
95
- _sequence: seq,
96
- _offset: offset,
97
-
98
- generator: function* () {
99
- let currentIdx = offset;
100
- while (true) {
101
- const res = seq.get(currentIdx);
102
- if (res.done) return;
103
- yield res.value!;
104
- currentIdx++;
105
- }
106
- },
107
- toJSON() {
108
- const iterator = this.generator();
109
- const buffer: PrimitiveValue[] = [];
110
- let next = iterator.next();
111
-
112
- while (!next.done) {
113
- buffer.push(next.value);
114
- next = iterator.next();
115
- }
116
-
117
- return buffer;
118
- },
119
- };
120
- }
121
-
122
- export class PatternResolver implements Visitor<string> {
123
- visitVariablePattern(node: VariablePattern): string {
124
- return node.name.value;
125
- }
126
-
127
- visitWildcardPattern(node: WildcardPattern): string {
128
- return "_";
129
- }
130
-
131
- visitLiteralPattern(node: LiteralPattern): string {
132
- const { name } = node;
133
- if (name instanceof ListPrimitive)
134
- return String(name.value.map((elem) => elem.accept(this)));
135
- return String(name.value);
136
- }
137
-
138
- visitTuplePattern(node: TuplePattern): string {
139
- return String(node.elements.map((elem) => elem.accept(this)));
140
- }
141
-
142
- visitListPattern(node: ListPattern): string {
143
- const { elements } = node;
144
- return elements.length === 0
145
- ? "[]"
146
- : String(elements.map((elem) => elem.accept(this)));
147
- }
148
-
149
- visitConsPattern(node: ConsPattern): string {
150
- const head = node.left.accept(this);
151
- const tail = node.right.accept(this);
152
- return `(${head}:${tail})`;
153
- }
154
-
155
- visitConstructorPattern(node: ConstructorPattern): string {
156
- const constr = node.identifier.value;
157
- const args = node.args.map((pat) => pat.accept(this)).join(" ");
158
- return `${constr} ${args}`;
159
- }
160
-
161
- visitFunctorPattern(node: FunctorPattern): string {
162
- // Same as ConstructorPattern (alias)
163
- return this.visitConstructorPattern(
164
- new ConstructorPattern(node.identifier, node.args),
165
- );
166
- }
167
-
168
- visitApplicationPattern(node: ApplicationPattern): string {
169
- // Same as FunctorPattern
170
- return this.visitConstructorPattern(
171
- new ConstructorPattern(node.identifier, node.args),
172
- );
173
- }
174
-
175
- visitAsPattern(node: AsPattern): string {
176
- const alias = node.left.accept(this);
177
- const pattern = node.right.accept(this);
178
- return `${alias}@${pattern}`;
179
- }
180
-
181
- visitTypePattern(node: TypePattern): string {
182
- const typeStr = node.targetType.toString();
183
- return node.innerPattern
184
- ? `(${typeStr} ${node.innerPattern.accept(this)})`
185
- : typeStr;
186
- }
187
-
188
- visit(node: ASTNode): string {
189
- return node.accept(this);
190
- }
191
- }
192
- /**
193
- * Recursively matches a value against a pattern node.
194
- * Updates `bindings` when variables are bound successfully.
195
- * Returns true if the pattern matches, false otherwise.
196
- */
197
- export class PatternMatcher implements Visitor<CPSThunk<boolean>> {
198
- constructor(
199
- private value: PrimitiveValue,
200
- private bindings: Bindings,
201
- private ctx: RuntimeContext,
202
- ) {}
203
-
204
- visitVariablePattern(node: VariablePattern): CPSThunk<boolean> {
205
- return (k) => {
206
- this.bindings.push([node.name.value, this.value]);
207
- return k(true);
208
- };
209
- }
210
-
211
- visitWildcardPattern(node: WildcardPattern): CPSThunk<boolean> {
212
- return (k) => k(true);
213
- }
214
-
215
- visitLiteralPattern(node: LiteralPattern): CPSThunk<boolean> {
216
- return (k) => {
217
- const literalValue = InterpreterVisitor.evaluateLiteral(node.name);
218
- return this.ctx.lazyRuntime.deepEqual(this.value, literalValue, k);
219
- };
220
- }
221
-
222
- visitTuplePattern(node: TuplePattern): CPSThunk<boolean> {
223
- return (k) => {
224
- const processValue = (val: PrimitiveValue): Thunk<boolean> => {
225
- if (!Array.isArray(val)) return k(false);
226
- if (val.length !== node.elements.length) return k(false);
227
-
228
- const matchNext = (index: number): Thunk<boolean> => {
229
- if (index >= node.elements.length) return k(true);
230
- const matcher = new PatternMatcher(
231
- val[index],
232
- this.bindings,
233
- this.ctx,
234
- );
235
- return node.elements[index].accept(matcher)((isMatch) => {
236
- if (!isMatch) return k(false);
237
- return () => matchNext(index + 1);
238
- });
239
- };
240
- return matchNext(0);
241
- };
242
-
243
- if (isLazyList(this.value)) {
244
- return this.ctx.lazyRuntime.realizeList(this.value, (val) => {
245
- return () => processValue(val);
246
- });
247
- }
248
- return processValue(this.value);
249
- };
250
- }
251
-
252
- visitListPattern(node: ListPattern): CPSThunk<boolean> {
253
- return (k) => {
254
- const value = this.value;
255
- const neededLength = node.elements.length;
256
-
257
- const finishMatching = (valArr: PrimitiveValue[]): Thunk<boolean> => {
258
- if (valArr.length !== neededLength) return k(false);
259
- return this.matchList(node.elements, valArr, k);
260
- };
261
-
262
- // empty list case
263
- if (neededLength === 0) {
264
- if (Array.isArray(value) || typeof value === "string")
265
- return k(value.length === 0);
266
-
267
- if (isLazyList(value)) {
268
- const iter = value.generator();
269
- return k(iter.next().done);
270
- }
271
- return k(false);
272
- }
273
-
274
- if (Array.isArray(value)) return finishMatching(value);
275
- if (typeof value === "string") return finishMatching(value.split(""));
276
-
277
- if (isLazyList(value)) {
278
- return this.ctx.lazyRuntime.realizeList(value, (valArr) => {
279
- return () => finishMatching(valArr);
280
- });
281
- }
282
-
283
- return k(false);
284
- };
285
- }
286
-
287
- private matchList(
288
- elements: Pattern[],
289
- value: PrimitiveValue[],
290
- k: Continuation<boolean>,
291
- ): Thunk<boolean> {
292
- if (value.length !== elements.length) return k(false);
293
- const matchNext = (index: number): Thunk<boolean> => {
294
- if (index >= elements.length) return k(true);
295
- const matcher = new PatternMatcher(
296
- value[index],
297
- this.bindings,
298
- this.ctx,
299
- );
300
- return elements[index].accept(matcher)((isMatch) => {
301
- if (!isMatch) return k(false);
302
- return () => matchNext(index + 1);
303
- });
304
- };
305
- return matchNext(0);
306
- }
307
-
308
- visitConsPattern(node: ConsPattern): CPSThunk<boolean> {
309
- return (k) => {
310
- const [head, tail] = this.resolveCons(this.value);
311
- if (head === null || tail === null) return k(false);
312
-
313
- const headMatcher = new PatternMatcher(
314
- head,
315
- this.bindings,
316
- this.ctx,
317
- );
318
- return node.left.accept(headMatcher)((headMatches) => {
319
- if (!headMatches) return k(false);
320
- const tailMatcher = new PatternMatcher(
321
- tail,
322
- this.bindings,
323
- this.ctx,
324
- );
325
- return node.right.accept(tailMatcher)(k);
326
- });
327
- };
328
- }
329
-
330
- visitTypePattern(node: TypePattern): CPSThunk<boolean> {
331
- return (k) => {
332
- const actualType = getYukigoType(this.value);
333
-
334
- let matches = false;
335
- const targetType = node.targetType;
336
-
337
- if (targetType instanceof SimpleType) {
338
- matches = targetType.value === actualType;
339
- } else if (targetType instanceof ListType) {
340
- matches = actualType === "YuList";
341
- }
342
-
343
- if (!matches) return k(false);
344
-
345
- if (node.innerPattern) {
346
- const innerMatcher = new PatternMatcher(
347
- this.value,
348
- this.bindings,
349
- this.ctx,
350
- );
351
- return node.innerPattern.accept(innerMatcher)(k);
352
- }
353
-
354
- return k(true);
355
- };
356
- }
357
-
358
- private resolveCons(list: PrimitiveValue): [PrimitiveValue, PrimitiveValue] {
359
- if (Array.isArray(list)) {
360
- if (list.length === 0) return [null, null];
361
- const isLazy = this.ctx.config.lazyLoading;
362
- if (!isLazy) return [list[0], list.slice(1)];
363
- const tail: LazyList = {
364
- type: "LazyList",
365
- generator: function* () {
366
- for (let i = 1; i < list.length; i++) yield list[i];
367
- },
368
- };
369
- return [list[0], tail];
370
- }
371
-
372
- if (typeof list === "string") {
373
- if (list.length === 0) return [null, null];
374
-
375
- const isLazy = this.ctx.config.lazyLoading;
376
- if (!isLazy) return [list[0], list.slice(1)];
377
- const tail: LazyList = {
378
- type: "LazyList",
379
- generator: function* () {
380
- for (let i = 1; i < list.length; i++) yield list[i];
381
- },
382
- };
383
- return [list[0], tail];
384
- }
385
-
386
- // lazy list case
387
- if (isLazyList(list)) {
388
- let memoList: MemoizedLazyList;
389
-
390
- // optimize, convert to memoized
391
- if (isMemoizedList(list)) {
392
- memoList = list;
393
- } else {
394
- memoList = createMemoizedStream(list.generator);
395
- }
396
- const currentRes = memoList._sequence.get(memoList._offset);
397
-
398
- if (currentRes.done) return [null, null];
399
- const tail = createMemoizedStream(
400
- list.generator,
401
- memoList._sequence,
402
- memoList._offset + 1,
403
- );
404
-
405
- return [currentRes.value!, tail];
406
- }
407
-
408
- return [null, null];
409
- }
410
-
411
- visitConstructorPattern(node: ConstructorPattern): CPSThunk<boolean> {
412
- return (k) => {
413
- if (!Array.isArray(this.value) || this.value.length === 0)
414
- return k(false);
415
- if (this.value[0] !== node.identifier.value) return k(false);
416
-
417
- const args = this.value.slice(1);
418
- return this.matchList(node.args, args, k);
419
- };
420
- }
421
-
422
- visitFunctorPattern(node: FunctorPattern): CPSThunk<boolean> {
423
- return this.visitConstructorPattern(
424
- new ConstructorPattern(node.identifier, node.args),
425
- );
426
- }
427
-
428
- visitApplicationPattern(node: ApplicationPattern): CPSThunk<boolean> {
429
- return this.visitConstructorPattern(
430
- new ConstructorPattern(node.identifier, node.args),
431
- );
432
- }
433
-
434
- visitAsPattern(node: AsPattern): CPSThunk<boolean> {
435
- return (k) => {
436
- const innerMatcher = new PatternMatcher(
437
- this.value,
438
- this.bindings,
439
- this.ctx,
440
- );
441
- return node.right.accept(innerMatcher)((innerMatches) => {
442
- if (!innerMatches) return k(false);
443
- const aliasMatcher = new PatternMatcher(
444
- this.value,
445
- this.bindings,
446
- this.ctx,
447
- );
448
- return node.left.accept(aliasMatcher)(k);
449
- });
450
- };
451
- }
452
-
453
- visitUnionPattern(node: UnionPattern): CPSThunk<boolean> {
454
- return (k) => {
455
- const tryNext = (index: number): Thunk<boolean> => {
456
- if (index >= node.elements.length) return k(false);
457
- const pattern = node.elements[index];
458
- const trialBindings: Bindings = [];
459
- const matcher = new PatternMatcher(this.value, trialBindings, this.ctx);
460
- return pattern.accept(matcher)((isMatch) => {
461
- if (isMatch) {
462
- this.bindings.push(...trialBindings);
463
- return k(true);
464
- }
465
- return () => tryNext(index + 1);
466
- });
467
- };
468
- return tryNext(0);
469
- };
470
- }
471
-
472
- visit(node: ASTNode): CPSThunk<boolean> {
473
- return node.accept(this);
474
- }
475
- }
1
+ import {
2
+ ApplicationPattern,
3
+ AsPattern,
4
+ ASTNode,
5
+ ConsPattern,
6
+ ConstructorPattern,
7
+ FunctorPattern,
8
+ ListPattern,
9
+ LiteralPattern,
10
+ Pattern,
11
+ TuplePattern,
12
+ UnionPattern,
13
+ VariablePattern,
14
+ WildcardPattern,
15
+ TypePattern,
16
+ SimpleType,
17
+ ListType,
18
+ } from "yukigo-ast";
19
+ import { Bindings } from "../index.js";
20
+ import {
21
+ ExecutionCommand,
22
+ StepCommand,
23
+ BindCommand,
24
+ FailCommand,
25
+ RaiseCommand,
26
+ } from "../components/kernel/commands.js";
27
+ import { RuntimeContext } from "./RuntimeContext.js";
28
+ import { boolean, getYukigoType } from "../utils.js";
29
+ import { InterpreterError, UnexpectedNode } from "../errors.js";
30
+ import { EqualityComparer } from "./EqualityComparer.js";
31
+ import {
32
+ YuValue,
33
+ YuBoolean,
34
+ YuArray,
35
+ isLazyList,
36
+ YuNil,
37
+ YuString,
38
+ LazyList,
39
+ LazyStepResult,
40
+ YuChar,
41
+ } from "../primitives/index.js";
42
+
43
+ /**
44
+ * Recursively matches a value against a pattern node.
45
+ * Updates `bindings` when variables are bound successfully.
46
+ * Returns true if the pattern matches, false otherwise.
47
+ */
48
+ export class PatternMatcher {
49
+ constructor(
50
+ private value: YuValue,
51
+ private bindings: Bindings,
52
+ private ctx: RuntimeContext,
53
+ ) {}
54
+
55
+ visitVariablePattern(node: VariablePattern): ExecutionCommand {
56
+ this.bindings.push([node.name.value, this.value]);
57
+ return boolean(true);
58
+ }
59
+
60
+ visitWildcardPattern(node: WildcardPattern): ExecutionCommand {
61
+ return boolean(true);
62
+ }
63
+
64
+ visitLiteralPattern(node: LiteralPattern): ExecutionCommand {
65
+ return new BindCommand(this.ctx.forceValue(this.value), (val) => {
66
+ const visitor = this.ctx.evaluatorFactory!(this.ctx);
67
+ const VisitorClass = visitor.constructor as any;
68
+ const literalValue = VisitorClass.evaluateLiteral(
69
+ node.name,
70
+ this.ctx,
71
+ );
72
+ return EqualityComparer.compare(val, literalValue, this.ctx);
73
+ });
74
+ }
75
+
76
+ visitTuplePattern(node: TuplePattern): ExecutionCommand {
77
+ return new BindCommand(this.ctx.forceValue(this.value), (forcedVal) => {
78
+ const processValue = (val: YuValue): ExecutionCommand => {
79
+ const seq = val.asSequence;
80
+ if (!(seq instanceof YuArray)) return boolean(false);
81
+ if (seq.items.length !== node.elements.length) return boolean(false);
82
+
83
+ return this.matchList(node.elements, seq, 0);
84
+ };
85
+
86
+ const valSeq = forcedVal.asSequence;
87
+ if (valSeq && isLazyList(valSeq)) {
88
+ return new BindCommand(
89
+ this.ctx.lazyRuntime.realizeList(forcedVal),
90
+ processValue,
91
+ );
92
+ }
93
+ return processValue(forcedVal);
94
+ });
95
+ }
96
+
97
+ visitListPattern(node: ListPattern): ExecutionCommand {
98
+ return new BindCommand(this.ctx.forceValue(this.value), (value) => {
99
+ const neededLength = node.elements.length;
100
+
101
+ const finishMatching = (valArr: YuValue): ExecutionCommand => {
102
+ const seq = valArr.asSequence;
103
+ if (!(seq instanceof YuArray))
104
+ return new RaiseCommand(
105
+ new InterpreterError(
106
+ "[PatternMatcher]",
107
+ `Expected ${valArr} to be a list.`,
108
+ ),
109
+ );
110
+ if (seq.items.length !== neededLength) return boolean(false);
111
+ return this.matchList(node.elements, seq, 0);
112
+ };
113
+
114
+ if (neededLength === 0) {
115
+ if (value instanceof YuNil) return boolean(true);
116
+ const seq = value.asSequence;
117
+ if (seq) {
118
+ if (seq instanceof YuArray || seq instanceof YuString) {
119
+ return new StepCommand(
120
+ new YuBoolean((seq.toJSON() as any).length === 0),
121
+ );
122
+ }
123
+ if (isLazyList(seq)) {
124
+ return new BindCommand(seq.step(), (res) =>
125
+ boolean(res instanceof YuNil),
126
+ );
127
+ }
128
+ }
129
+ return boolean(false);
130
+ }
131
+
132
+ if (value instanceof YuNil) return boolean(false);
133
+
134
+ const seq = value.asSequence;
135
+ if (seq) {
136
+ if (seq instanceof YuArray) return finishMatching(value);
137
+ if (seq instanceof YuString)
138
+ return new BindCommand(seq.split(), (splitted) =>
139
+ finishMatching(splitted),
140
+ );
141
+
142
+ if (isLazyList(seq)) {
143
+ return new BindCommand(
144
+ this.ctx.lazyRuntime.realizeList(value),
145
+ finishMatching,
146
+ );
147
+ }
148
+ }
149
+
150
+ return boolean(false);
151
+ });
152
+ }
153
+
154
+ private matchList(
155
+ elements: Pattern[],
156
+ value: YuArray,
157
+ index: number,
158
+ ): ExecutionCommand {
159
+ if (index >= elements.length) return boolean(true);
160
+
161
+ const matcher = new PatternMatcher(
162
+ value.at(index),
163
+ this.bindings,
164
+ this.ctx,
165
+ );
166
+ return new BindCommand(elements[index].accept(matcher), (res) => {
167
+ const isMatch = res instanceof YuBoolean && res.value;
168
+ if (!isMatch) return boolean(false);
169
+ return this.matchList(elements, value, index + 1);
170
+ });
171
+ }
172
+
173
+ visitConsPattern(node: ConsPattern): ExecutionCommand {
174
+ return new BindCommand(this.ctx.forceValue(this.value), (forcedVal) => {
175
+ return new BindCommand(this.resolveCons(forcedVal), (resolved) => {
176
+ if (!(resolved instanceof YuArray) || resolved.items.length !== 2)
177
+ return boolean(false);
178
+ const head = resolved.at(0);
179
+ const tail = resolved.at(1);
180
+
181
+ const headMatcher = new PatternMatcher(head, this.bindings, this.ctx);
182
+ return new BindCommand(node.left.accept(headMatcher), (headRes) => {
183
+ const headMatches = headRes instanceof YuBoolean && headRes.value;
184
+ if (!headMatches) return boolean(false);
185
+ const tailMatcher = new PatternMatcher(tail, this.bindings, this.ctx);
186
+ return node.right.accept(tailMatcher);
187
+ });
188
+ });
189
+ });
190
+ }
191
+
192
+ visitTypePattern(node: TypePattern): ExecutionCommand {
193
+ const actualType = getYukigoType(this.value);
194
+ let matches = false;
195
+ const targetType = node.targetType;
196
+
197
+ if (targetType instanceof SimpleType) {
198
+ matches = targetType.value === actualType;
199
+ } else if (targetType instanceof ListType) {
200
+ matches =
201
+ actualType === "List" ||
202
+ actualType === "Array" ||
203
+ actualType === "String";
204
+ }
205
+
206
+ if (!matches) return boolean(false);
207
+
208
+ if (node.innerPattern) {
209
+ const innerMatcher = new PatternMatcher(
210
+ this.value,
211
+ this.bindings,
212
+ this.ctx,
213
+ );
214
+ return node.innerPattern.accept(innerMatcher);
215
+ }
216
+
217
+ return boolean(true);
218
+ }
219
+
220
+ private resolveCons(val: YuValue): ExecutionCommand {
221
+ const list = val.asSequence;
222
+ if (!list) return new StepCommand(YuNil.getInstance());
223
+
224
+ if (list instanceof YuArray) {
225
+ if (list.items.length === 0) return new StepCommand(YuNil.getInstance());
226
+ const isLazy = this.ctx.config.lazyLoading;
227
+ if (!isLazy)
228
+ return new StepCommand(
229
+ new YuArray([
230
+ list.at(0),
231
+ new YuArray(list.items.slice(list.index + 1)),
232
+ ]),
233
+ );
234
+
235
+ const tail = new LazyList(() => {
236
+ const next = (arr: YuArray, idx: number): ExecutionCommand => {
237
+ if (idx >= arr.items.length)
238
+ return new StepCommand(YuNil.getInstance());
239
+ return new StepCommand(
240
+ new LazyStepResult(
241
+ arr.items[idx],
242
+ new LazyList(() => next(arr, idx + 1)),
243
+ ),
244
+ );
245
+ };
246
+ return next(list, list.index + 1);
247
+ });
248
+ const arr = new YuArray([list.at(0), tail]);
249
+ return new StepCommand(arr);
250
+ }
251
+
252
+ if (list instanceof YuString) {
253
+ const s = list.toJSON();
254
+ if (s.length === 0) return new StepCommand(YuNil.getInstance());
255
+
256
+ const isLazy = this.ctx.config.lazyLoading;
257
+ if (!isLazy)
258
+ return new StepCommand(
259
+ new YuArray([new YuString(s[0]), new YuString(s.slice(1))]),
260
+ );
261
+ const tail = new LazyList(() => {
262
+ const next = (str: string, idx: number): ExecutionCommand => {
263
+ if (idx >= str.length) return new StepCommand(YuNil.getInstance());
264
+ return new StepCommand(
265
+ new LazyStepResult(
266
+ new YuChar(str[idx]),
267
+ new LazyList(() => next(str, idx + 1)),
268
+ ),
269
+ );
270
+ };
271
+ return next(s, 1);
272
+ });
273
+ return new StepCommand(new YuArray([new YuChar(s[0]), tail]));
274
+ }
275
+
276
+ // lazy list case
277
+ if (isLazyList(list)) {
278
+ return new BindCommand(list.step(), (stepRes) => {
279
+ if (stepRes.isNil) return new StepCommand(YuNil.getInstance());
280
+
281
+ const stepResult = stepRes.asStepResult;
282
+ if (!stepResult)
283
+ return new RaiseCommand(
284
+ new InterpreterError(
285
+ "PatternMatcher",
286
+ "step did not return StepResult",
287
+ ),
288
+ );
289
+
290
+ return new StepCommand(
291
+ new YuArray([
292
+ stepResult.head,
293
+ stepResult.tail || YuNil.getInstance(),
294
+ ]),
295
+ );
296
+ });
297
+ }
298
+
299
+ return new StepCommand(YuNil.getInstance());
300
+ }
301
+
302
+ visitConstructorPattern(node: ConstructorPattern): ExecutionCommand {
303
+ const seq = this.value.asSequence;
304
+ if (!(seq instanceof YuArray) || seq.items.length === 0)
305
+ return boolean(false);
306
+ const head = seq.at(0);
307
+ if (!(head instanceof YuString) || head.toJSON() !== node.identifier.value)
308
+ return boolean(false);
309
+
310
+ const args = new YuArray(seq.items.slice(seq.index + 1));
311
+ return this.matchList(node.args, args, 0);
312
+ }
313
+
314
+ visitFunctorPattern(node: FunctorPattern): ExecutionCommand {
315
+ return this.visitConstructorPattern(
316
+ new ConstructorPattern(node.identifier, node.args),
317
+ );
318
+ }
319
+
320
+ visitApplicationPattern(node: ApplicationPattern): ExecutionCommand {
321
+ return this.visitConstructorPattern(
322
+ new ConstructorPattern(node.identifier, node.args),
323
+ );
324
+ }
325
+
326
+ visitAsPattern(node: AsPattern): ExecutionCommand {
327
+ const innerMatcher = new PatternMatcher(
328
+ this.value,
329
+ this.bindings,
330
+ this.ctx,
331
+ );
332
+
333
+ return new BindCommand(node.right.accept(innerMatcher), (res) => {
334
+ const innerMatches = res instanceof YuBoolean && res.value;
335
+ if (!innerMatches) return boolean(false);
336
+
337
+ const aliasMatcher = new PatternMatcher(
338
+ this.value,
339
+ this.bindings,
340
+ this.ctx,
341
+ );
342
+ return node.left.accept(aliasMatcher);
343
+ });
344
+ }
345
+
346
+ visitUnionPattern(node: UnionPattern): ExecutionCommand {
347
+ const tryNext = (index: number): ExecutionCommand => {
348
+ if (index >= node.elements.length) return boolean(false);
349
+
350
+ const pattern = node.elements[index];
351
+ const trialBindings: Bindings = [];
352
+ const matcher = new PatternMatcher(this.value, trialBindings, this.ctx);
353
+
354
+ return new BindCommand(pattern.accept(matcher), (res) => {
355
+ const isMatch = res instanceof YuBoolean && res.value;
356
+ if (isMatch) {
357
+ this.bindings.push(...trialBindings);
358
+ return boolean(true);
359
+ }
360
+ return tryNext(index + 1);
361
+ });
362
+ };
363
+
364
+ return tryNext(0);
365
+ }
366
+
367
+ visit(node: ASTNode): ExecutionCommand {
368
+ return node.accept(this);
369
+ }
370
+ public fallback(node: ASTNode): ExecutionCommand {
371
+ return new FailCommand(
372
+ new UnexpectedNode(node.constructor.name, "PatternMatcher"),
373
+ );
374
+ }
375
+ }