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,71 +1,32 @@
1
- import { ApplicationPattern, AsPattern, ASTNode, ConsPattern, ConstructorPattern, Expression, FunctorPattern, LazyList, ListPattern, LiteralPattern, PrimitiveValue, TuplePattern, UnionPattern, VariablePattern, Visitor, WildcardPattern, TypePattern, EnvStack } from "yukigo-ast";
1
+ import { ApplicationPattern, AsPattern, ASTNode, ConsPattern, ConstructorPattern, FunctorPattern, ListPattern, LiteralPattern, TuplePattern, UnionPattern, VariablePattern, WildcardPattern, TypePattern } from "yukigo-ast";
2
2
  import { Bindings } from "../index.js";
3
- import { CPSThunk } from "../trampoline.js";
3
+ import { ExecutionCommand } from "../components/kernel/commands.js";
4
4
  import { RuntimeContext } from "./RuntimeContext.js";
5
- import { ExpressionEvaluator } from "../utils.js";
6
- declare class SharedSequence {
7
- private cache;
8
- private source;
9
- private isDone;
10
- constructor(generatorFactory: () => Generator<PrimitiveValue, void, unknown>);
11
- get(index: number): {
12
- value: PrimitiveValue | null;
13
- done: boolean;
14
- };
15
- }
16
- export interface InternalConsState {
17
- readonly head: PrimitiveValue;
18
- readonly tailExpr: Expression;
19
- readonly evaluator: ExpressionEvaluator;
20
- readonly capturedEnv: EnvStack;
21
- realizedTail?: PrimitiveValue;
22
- }
23
- export interface MemoizedLazyList extends LazyList {
24
- _sequence: SharedSequence;
25
- _offset: number;
26
- _consState?: InternalConsState;
27
- toJSON: () => any;
28
- }
29
- export declare function isMemoizedList(list: unknown): list is MemoizedLazyList;
30
- export declare function createMemoizedStream(genFactory: () => Generator<PrimitiveValue, void, unknown>, sequence?: SharedSequence, offset?: number): MemoizedLazyList;
31
- export declare class PatternResolver implements Visitor<string> {
32
- visitVariablePattern(node: VariablePattern): string;
33
- visitWildcardPattern(node: WildcardPattern): string;
34
- visitLiteralPattern(node: LiteralPattern): string;
35
- visitTuplePattern(node: TuplePattern): string;
36
- visitListPattern(node: ListPattern): string;
37
- visitConsPattern(node: ConsPattern): string;
38
- visitConstructorPattern(node: ConstructorPattern): string;
39
- visitFunctorPattern(node: FunctorPattern): string;
40
- visitApplicationPattern(node: ApplicationPattern): string;
41
- visitAsPattern(node: AsPattern): string;
42
- visitTypePattern(node: TypePattern): string;
43
- visit(node: ASTNode): string;
44
- }
5
+ import { YuValue } from "../primitives/index.js";
45
6
  /**
46
7
  * Recursively matches a value against a pattern node.
47
8
  * Updates `bindings` when variables are bound successfully.
48
9
  * Returns true if the pattern matches, false otherwise.
49
10
  */
50
- export declare class PatternMatcher implements Visitor<CPSThunk<boolean>> {
11
+ export declare class PatternMatcher {
51
12
  private value;
52
13
  private bindings;
53
14
  private ctx;
54
- constructor(value: PrimitiveValue, bindings: Bindings, ctx: RuntimeContext);
55
- visitVariablePattern(node: VariablePattern): CPSThunk<boolean>;
56
- visitWildcardPattern(node: WildcardPattern): CPSThunk<boolean>;
57
- visitLiteralPattern(node: LiteralPattern): CPSThunk<boolean>;
58
- visitTuplePattern(node: TuplePattern): CPSThunk<boolean>;
59
- visitListPattern(node: ListPattern): CPSThunk<boolean>;
15
+ constructor(value: YuValue, bindings: Bindings, ctx: RuntimeContext);
16
+ visitVariablePattern(node: VariablePattern): ExecutionCommand;
17
+ visitWildcardPattern(node: WildcardPattern): ExecutionCommand;
18
+ visitLiteralPattern(node: LiteralPattern): ExecutionCommand;
19
+ visitTuplePattern(node: TuplePattern): ExecutionCommand;
20
+ visitListPattern(node: ListPattern): ExecutionCommand;
60
21
  private matchList;
61
- visitConsPattern(node: ConsPattern): CPSThunk<boolean>;
62
- visitTypePattern(node: TypePattern): CPSThunk<boolean>;
22
+ visitConsPattern(node: ConsPattern): ExecutionCommand;
23
+ visitTypePattern(node: TypePattern): ExecutionCommand;
63
24
  private resolveCons;
64
- visitConstructorPattern(node: ConstructorPattern): CPSThunk<boolean>;
65
- visitFunctorPattern(node: FunctorPattern): CPSThunk<boolean>;
66
- visitApplicationPattern(node: ApplicationPattern): CPSThunk<boolean>;
67
- visitAsPattern(node: AsPattern): CPSThunk<boolean>;
68
- visitUnionPattern(node: UnionPattern): CPSThunk<boolean>;
69
- visit(node: ASTNode): CPSThunk<boolean>;
25
+ visitConstructorPattern(node: ConstructorPattern): ExecutionCommand;
26
+ visitFunctorPattern(node: FunctorPattern): ExecutionCommand;
27
+ visitApplicationPattern(node: ApplicationPattern): ExecutionCommand;
28
+ visitAsPattern(node: AsPattern): ExecutionCommand;
29
+ visitUnionPattern(node: UnionPattern): ExecutionCommand;
30
+ visit(node: ASTNode): ExecutionCommand;
31
+ fallback(node: ASTNode): ExecutionCommand;
70
32
  }
71
- export {};
@@ -1,119 +1,9 @@
1
- import { ConstructorPattern, isLazyList, ListPrimitive, SimpleType, ListType, } from "yukigo-ast";
2
- import { InterpreterVisitor } from "./Visitor.js";
3
- import { getYukigoType } from "../utils.js";
4
- class SharedSequence {
5
- cache = [];
6
- source;
7
- isDone = false;
8
- constructor(generatorFactory) {
9
- this.source = generatorFactory();
10
- }
11
- get(index) {
12
- if (index < this.cache.length)
13
- return { value: this.cache[index], done: false };
14
- if (this.isDone)
15
- return { value: null, done: true };
16
- while (this.cache.length <= index) {
17
- const next = this.source.next();
18
- if (next.done) {
19
- this.isDone = true;
20
- return { value: null, done: true };
21
- }
22
- this.cache.push(next.value);
23
- }
24
- return { value: this.cache[index], done: false };
25
- }
26
- }
27
- export function isMemoizedList(list) {
28
- return (list &&
29
- typeof list === "object" &&
30
- "type" in list &&
31
- "_offset" in list &&
32
- "_sequence" in list);
33
- }
34
- export function createMemoizedStream(genFactory, sequence, offset = 0) {
35
- const seq = sequence ?? new SharedSequence(genFactory);
36
- return {
37
- type: "LazyList",
38
- _sequence: seq,
39
- _offset: offset,
40
- generator: function* () {
41
- let currentIdx = offset;
42
- while (true) {
43
- const res = seq.get(currentIdx);
44
- if (res.done)
45
- return;
46
- yield res.value;
47
- currentIdx++;
48
- }
49
- },
50
- toJSON() {
51
- const iterator = this.generator();
52
- const buffer = [];
53
- let next = iterator.next();
54
- while (!next.done) {
55
- buffer.push(next.value);
56
- next = iterator.next();
57
- }
58
- return buffer;
59
- },
60
- };
61
- }
62
- export class PatternResolver {
63
- visitVariablePattern(node) {
64
- return node.name.value;
65
- }
66
- visitWildcardPattern(node) {
67
- return "_";
68
- }
69
- visitLiteralPattern(node) {
70
- const { name } = node;
71
- if (name instanceof ListPrimitive)
72
- return String(name.value.map((elem) => elem.accept(this)));
73
- return String(name.value);
74
- }
75
- visitTuplePattern(node) {
76
- return String(node.elements.map((elem) => elem.accept(this)));
77
- }
78
- visitListPattern(node) {
79
- const { elements } = node;
80
- return elements.length === 0
81
- ? "[]"
82
- : String(elements.map((elem) => elem.accept(this)));
83
- }
84
- visitConsPattern(node) {
85
- const head = node.left.accept(this);
86
- const tail = node.right.accept(this);
87
- return `(${head}:${tail})`;
88
- }
89
- visitConstructorPattern(node) {
90
- const constr = node.identifier.value;
91
- const args = node.args.map((pat) => pat.accept(this)).join(" ");
92
- return `${constr} ${args}`;
93
- }
94
- visitFunctorPattern(node) {
95
- // Same as ConstructorPattern (alias)
96
- return this.visitConstructorPattern(new ConstructorPattern(node.identifier, node.args));
97
- }
98
- visitApplicationPattern(node) {
99
- // Same as FunctorPattern
100
- return this.visitConstructorPattern(new ConstructorPattern(node.identifier, node.args));
101
- }
102
- visitAsPattern(node) {
103
- const alias = node.left.accept(this);
104
- const pattern = node.right.accept(this);
105
- return `${alias}@${pattern}`;
106
- }
107
- visitTypePattern(node) {
108
- const typeStr = node.targetType.toString();
109
- return node.innerPattern
110
- ? `(${typeStr} ${node.innerPattern.accept(this)})`
111
- : typeStr;
112
- }
113
- visit(node) {
114
- return node.accept(this);
115
- }
116
- }
1
+ import { ConstructorPattern, SimpleType, ListType, } from "yukigo-ast";
2
+ import { StepCommand, BindCommand, FailCommand, RaiseCommand, } from "../components/kernel/commands.js";
3
+ import { boolean, getYukigoType } from "../utils.js";
4
+ import { InterpreterError, UnexpectedNode } from "../errors.js";
5
+ import { EqualityComparer } from "./EqualityComparer.js";
6
+ import { YuBoolean, YuArray, isLazyList, YuNil, YuString, LazyList, LazyStepResult, YuChar, } from "../primitives/index.js";
117
7
  /**
118
8
  * Recursively matches a value against a pattern node.
119
9
  * Updates `bindings` when variables are bound successfully.
@@ -129,185 +19,193 @@ export class PatternMatcher {
129
19
  this.ctx = ctx;
130
20
  }
131
21
  visitVariablePattern(node) {
132
- return (k) => {
133
- this.bindings.push([node.name.value, this.value]);
134
- return k(true);
135
- };
22
+ this.bindings.push([node.name.value, this.value]);
23
+ return boolean(true);
136
24
  }
137
25
  visitWildcardPattern(node) {
138
- return (k) => k(true);
26
+ return boolean(true);
139
27
  }
140
28
  visitLiteralPattern(node) {
141
- return (k) => {
142
- const literalValue = InterpreterVisitor.evaluateLiteral(node.name);
143
- return this.ctx.lazyRuntime.deepEqual(this.value, literalValue, k);
144
- };
29
+ return new BindCommand(this.ctx.forceValue(this.value), (val) => {
30
+ const visitor = this.ctx.evaluatorFactory(this.ctx);
31
+ const VisitorClass = visitor.constructor;
32
+ const literalValue = VisitorClass.evaluateLiteral(node.name, this.ctx);
33
+ return EqualityComparer.compare(val, literalValue, this.ctx);
34
+ });
145
35
  }
146
36
  visitTuplePattern(node) {
147
- return (k) => {
37
+ return new BindCommand(this.ctx.forceValue(this.value), (forcedVal) => {
148
38
  const processValue = (val) => {
149
- if (!Array.isArray(val))
150
- return k(false);
151
- if (val.length !== node.elements.length)
152
- return k(false);
153
- const matchNext = (index) => {
154
- if (index >= node.elements.length)
155
- return k(true);
156
- const matcher = new PatternMatcher(val[index], this.bindings, this.ctx);
157
- return node.elements[index].accept(matcher)((isMatch) => {
158
- if (!isMatch)
159
- return k(false);
160
- return () => matchNext(index + 1);
161
- });
162
- };
163
- return matchNext(0);
39
+ const seq = val.asSequence;
40
+ if (!(seq instanceof YuArray))
41
+ return boolean(false);
42
+ if (seq.items.length !== node.elements.length)
43
+ return boolean(false);
44
+ return this.matchList(node.elements, seq, 0);
164
45
  };
165
- if (isLazyList(this.value)) {
166
- return this.ctx.lazyRuntime.realizeList(this.value, (val) => {
167
- return () => processValue(val);
168
- });
46
+ const valSeq = forcedVal.asSequence;
47
+ if (valSeq && isLazyList(valSeq)) {
48
+ return new BindCommand(this.ctx.lazyRuntime.realizeList(forcedVal), processValue);
169
49
  }
170
- return processValue(this.value);
171
- };
50
+ return processValue(forcedVal);
51
+ });
172
52
  }
173
53
  visitListPattern(node) {
174
- return (k) => {
175
- const value = this.value;
54
+ return new BindCommand(this.ctx.forceValue(this.value), (value) => {
176
55
  const neededLength = node.elements.length;
177
56
  const finishMatching = (valArr) => {
178
- if (valArr.length !== neededLength)
179
- return k(false);
180
- return this.matchList(node.elements, valArr, k);
57
+ const seq = valArr.asSequence;
58
+ if (!(seq instanceof YuArray))
59
+ return new RaiseCommand(new InterpreterError("[PatternMatcher]", `Expected ${valArr} to be a list.`));
60
+ if (seq.items.length !== neededLength)
61
+ return boolean(false);
62
+ return this.matchList(node.elements, seq, 0);
181
63
  };
182
- // empty list case
183
64
  if (neededLength === 0) {
184
- if (Array.isArray(value) || typeof value === "string")
185
- return k(value.length === 0);
186
- if (isLazyList(value)) {
187
- const iter = value.generator();
188
- return k(iter.next().done);
65
+ if (value instanceof YuNil)
66
+ return boolean(true);
67
+ const seq = value.asSequence;
68
+ if (seq) {
69
+ if (seq instanceof YuArray || seq instanceof YuString) {
70
+ return new StepCommand(new YuBoolean(seq.toJSON().length === 0));
71
+ }
72
+ if (isLazyList(seq)) {
73
+ return new BindCommand(seq.step(), (res) => boolean(res instanceof YuNil));
74
+ }
189
75
  }
190
- return k(false);
76
+ return boolean(false);
191
77
  }
192
- if (Array.isArray(value))
193
- return finishMatching(value);
194
- if (typeof value === "string")
195
- return finishMatching(value.split(""));
196
- if (isLazyList(value)) {
197
- return this.ctx.lazyRuntime.realizeList(value, (valArr) => {
198
- return () => finishMatching(valArr);
199
- });
78
+ if (value instanceof YuNil)
79
+ return boolean(false);
80
+ const seq = value.asSequence;
81
+ if (seq) {
82
+ if (seq instanceof YuArray)
83
+ return finishMatching(value);
84
+ if (seq instanceof YuString)
85
+ return new BindCommand(seq.split(), (splitted) => finishMatching(splitted));
86
+ if (isLazyList(seq)) {
87
+ return new BindCommand(this.ctx.lazyRuntime.realizeList(value), finishMatching);
88
+ }
200
89
  }
201
- return k(false);
202
- };
203
- }
204
- matchList(elements, value, k) {
205
- if (value.length !== elements.length)
206
- return k(false);
207
- const matchNext = (index) => {
208
- if (index >= elements.length)
209
- return k(true);
210
- const matcher = new PatternMatcher(value[index], this.bindings, this.ctx);
211
- return elements[index].accept(matcher)((isMatch) => {
212
- if (!isMatch)
213
- return k(false);
214
- return () => matchNext(index + 1);
215
- });
216
- };
217
- return matchNext(0);
90
+ return boolean(false);
91
+ });
92
+ }
93
+ matchList(elements, value, index) {
94
+ if (index >= elements.length)
95
+ return boolean(true);
96
+ const matcher = new PatternMatcher(value.at(index), this.bindings, this.ctx);
97
+ return new BindCommand(elements[index].accept(matcher), (res) => {
98
+ const isMatch = res instanceof YuBoolean && res.value;
99
+ if (!isMatch)
100
+ return boolean(false);
101
+ return this.matchList(elements, value, index + 1);
102
+ });
218
103
  }
219
104
  visitConsPattern(node) {
220
- return (k) => {
221
- const [head, tail] = this.resolveCons(this.value);
222
- if (head === null || tail === null)
223
- return k(false);
224
- const headMatcher = new PatternMatcher(head, this.bindings, this.ctx);
225
- return node.left.accept(headMatcher)((headMatches) => {
226
- if (!headMatches)
227
- return k(false);
228
- const tailMatcher = new PatternMatcher(tail, this.bindings, this.ctx);
229
- return node.right.accept(tailMatcher)(k);
105
+ return new BindCommand(this.ctx.forceValue(this.value), (forcedVal) => {
106
+ return new BindCommand(this.resolveCons(forcedVal), (resolved) => {
107
+ if (!(resolved instanceof YuArray) || resolved.items.length !== 2)
108
+ return boolean(false);
109
+ const head = resolved.at(0);
110
+ const tail = resolved.at(1);
111
+ const headMatcher = new PatternMatcher(head, this.bindings, this.ctx);
112
+ return new BindCommand(node.left.accept(headMatcher), (headRes) => {
113
+ const headMatches = headRes instanceof YuBoolean && headRes.value;
114
+ if (!headMatches)
115
+ return boolean(false);
116
+ const tailMatcher = new PatternMatcher(tail, this.bindings, this.ctx);
117
+ return node.right.accept(tailMatcher);
118
+ });
230
119
  });
231
- };
120
+ });
232
121
  }
233
122
  visitTypePattern(node) {
234
- return (k) => {
235
- const actualType = getYukigoType(this.value);
236
- let matches = false;
237
- const targetType = node.targetType;
238
- if (targetType instanceof SimpleType) {
239
- matches = targetType.value === actualType;
240
- }
241
- else if (targetType instanceof ListType) {
242
- matches = actualType === "YuList";
243
- }
244
- if (!matches)
245
- return k(false);
246
- if (node.innerPattern) {
247
- const innerMatcher = new PatternMatcher(this.value, this.bindings, this.ctx);
248
- return node.innerPattern.accept(innerMatcher)(k);
249
- }
250
- return k(true);
251
- };
252
- }
253
- resolveCons(list) {
254
- if (Array.isArray(list)) {
255
- if (list.length === 0)
256
- return [null, null];
123
+ const actualType = getYukigoType(this.value);
124
+ let matches = false;
125
+ const targetType = node.targetType;
126
+ if (targetType instanceof SimpleType) {
127
+ matches = targetType.value === actualType;
128
+ }
129
+ else if (targetType instanceof ListType) {
130
+ matches =
131
+ actualType === "List" ||
132
+ actualType === "Array" ||
133
+ actualType === "String";
134
+ }
135
+ if (!matches)
136
+ return boolean(false);
137
+ if (node.innerPattern) {
138
+ const innerMatcher = new PatternMatcher(this.value, this.bindings, this.ctx);
139
+ return node.innerPattern.accept(innerMatcher);
140
+ }
141
+ return boolean(true);
142
+ }
143
+ resolveCons(val) {
144
+ const list = val.asSequence;
145
+ if (!list)
146
+ return new StepCommand(YuNil.getInstance());
147
+ if (list instanceof YuArray) {
148
+ if (list.items.length === 0)
149
+ return new StepCommand(YuNil.getInstance());
257
150
  const isLazy = this.ctx.config.lazyLoading;
258
151
  if (!isLazy)
259
- return [list[0], list.slice(1)];
260
- const tail = {
261
- type: "LazyList",
262
- generator: function* () {
263
- for (let i = 1; i < list.length; i++)
264
- yield list[i];
265
- },
266
- };
267
- return [list[0], tail];
152
+ return new StepCommand(new YuArray([
153
+ list.at(0),
154
+ new YuArray(list.items.slice(list.index + 1)),
155
+ ]));
156
+ const tail = new LazyList(() => {
157
+ const next = (arr, idx) => {
158
+ if (idx >= arr.items.length)
159
+ return new StepCommand(YuNil.getInstance());
160
+ return new StepCommand(new LazyStepResult(arr.items[idx], new LazyList(() => next(arr, idx + 1))));
161
+ };
162
+ return next(list, list.index + 1);
163
+ });
164
+ const arr = new YuArray([list.at(0), tail]);
165
+ return new StepCommand(arr);
268
166
  }
269
- if (typeof list === "string") {
270
- if (list.length === 0)
271
- return [null, null];
167
+ if (list instanceof YuString) {
168
+ const s = list.toJSON();
169
+ if (s.length === 0)
170
+ return new StepCommand(YuNil.getInstance());
272
171
  const isLazy = this.ctx.config.lazyLoading;
273
172
  if (!isLazy)
274
- return [list[0], list.slice(1)];
275
- const tail = {
276
- type: "LazyList",
277
- generator: function* () {
278
- for (let i = 1; i < list.length; i++)
279
- yield list[i];
280
- },
281
- };
282
- return [list[0], tail];
173
+ return new StepCommand(new YuArray([new YuString(s[0]), new YuString(s.slice(1))]));
174
+ const tail = new LazyList(() => {
175
+ const next = (str, idx) => {
176
+ if (idx >= str.length)
177
+ return new StepCommand(YuNil.getInstance());
178
+ return new StepCommand(new LazyStepResult(new YuChar(str[idx]), new LazyList(() => next(str, idx + 1))));
179
+ };
180
+ return next(s, 1);
181
+ });
182
+ return new StepCommand(new YuArray([new YuChar(s[0]), tail]));
283
183
  }
284
184
  // lazy list case
285
185
  if (isLazyList(list)) {
286
- let memoList;
287
- // optimize, convert to memoized
288
- if (isMemoizedList(list)) {
289
- memoList = list;
290
- }
291
- else {
292
- memoList = createMemoizedStream(list.generator);
293
- }
294
- const currentRes = memoList._sequence.get(memoList._offset);
295
- if (currentRes.done)
296
- return [null, null];
297
- const tail = createMemoizedStream(list.generator, memoList._sequence, memoList._offset + 1);
298
- return [currentRes.value, tail];
186
+ return new BindCommand(list.step(), (stepRes) => {
187
+ if (stepRes.isNil)
188
+ return new StepCommand(YuNil.getInstance());
189
+ const stepResult = stepRes.asStepResult;
190
+ if (!stepResult)
191
+ return new RaiseCommand(new InterpreterError("PatternMatcher", "step did not return StepResult"));
192
+ return new StepCommand(new YuArray([
193
+ stepResult.head,
194
+ stepResult.tail || YuNil.getInstance(),
195
+ ]));
196
+ });
299
197
  }
300
- return [null, null];
198
+ return new StepCommand(YuNil.getInstance());
301
199
  }
302
200
  visitConstructorPattern(node) {
303
- return (k) => {
304
- if (!Array.isArray(this.value) || this.value.length === 0)
305
- return k(false);
306
- if (this.value[0] !== node.identifier.value)
307
- return k(false);
308
- const args = this.value.slice(1);
309
- return this.matchList(node.args, args, k);
310
- };
201
+ const seq = this.value.asSequence;
202
+ if (!(seq instanceof YuArray) || seq.items.length === 0)
203
+ return boolean(false);
204
+ const head = seq.at(0);
205
+ if (!(head instanceof YuString) || head.toJSON() !== node.identifier.value)
206
+ return boolean(false);
207
+ const args = new YuArray(seq.items.slice(seq.index + 1));
208
+ return this.matchList(node.args, args, 0);
311
209
  }
312
210
  visitFunctorPattern(node) {
313
211
  return this.visitConstructorPattern(new ConstructorPattern(node.identifier, node.args));
@@ -316,36 +214,37 @@ export class PatternMatcher {
316
214
  return this.visitConstructorPattern(new ConstructorPattern(node.identifier, node.args));
317
215
  }
318
216
  visitAsPattern(node) {
319
- return (k) => {
320
- const innerMatcher = new PatternMatcher(this.value, this.bindings, this.ctx);
321
- return node.right.accept(innerMatcher)((innerMatches) => {
322
- if (!innerMatches)
323
- return k(false);
324
- const aliasMatcher = new PatternMatcher(this.value, this.bindings, this.ctx);
325
- return node.left.accept(aliasMatcher)(k);
326
- });
327
- };
217
+ const innerMatcher = new PatternMatcher(this.value, this.bindings, this.ctx);
218
+ return new BindCommand(node.right.accept(innerMatcher), (res) => {
219
+ const innerMatches = res instanceof YuBoolean && res.value;
220
+ if (!innerMatches)
221
+ return boolean(false);
222
+ const aliasMatcher = new PatternMatcher(this.value, this.bindings, this.ctx);
223
+ return node.left.accept(aliasMatcher);
224
+ });
328
225
  }
329
226
  visitUnionPattern(node) {
330
- return (k) => {
331
- const tryNext = (index) => {
332
- if (index >= node.elements.length)
333
- return k(false);
334
- const pattern = node.elements[index];
335
- const trialBindings = [];
336
- const matcher = new PatternMatcher(this.value, trialBindings, this.ctx);
337
- return pattern.accept(matcher)((isMatch) => {
338
- if (isMatch) {
339
- this.bindings.push(...trialBindings);
340
- return k(true);
341
- }
342
- return () => tryNext(index + 1);
343
- });
344
- };
345
- return tryNext(0);
227
+ const tryNext = (index) => {
228
+ if (index >= node.elements.length)
229
+ return boolean(false);
230
+ const pattern = node.elements[index];
231
+ const trialBindings = [];
232
+ const matcher = new PatternMatcher(this.value, trialBindings, this.ctx);
233
+ return new BindCommand(pattern.accept(matcher), (res) => {
234
+ const isMatch = res instanceof YuBoolean && res.value;
235
+ if (isMatch) {
236
+ this.bindings.push(...trialBindings);
237
+ return boolean(true);
238
+ }
239
+ return tryNext(index + 1);
240
+ });
346
241
  };
242
+ return tryNext(0);
347
243
  }
348
244
  visit(node) {
349
245
  return node.accept(this);
350
246
  }
247
+ fallback(node) {
248
+ return new FailCommand(new UnexpectedNode(node.constructor.name, "PatternMatcher"));
249
+ }
351
250
  }