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,213 +1,254 @@
1
- import { expect } from "chai";
2
- import {
3
- VariablePattern,
4
- LiteralPattern,
5
- SymbolPrimitive,
6
- NumberPrimitive,
7
- WildcardPattern,
8
- ConsPattern,
9
- ListPattern,
10
- ConstructorPattern,
11
- UnionPattern,
12
- AsPattern,
13
- LazyList,
14
- } from "yukigo-ast";
15
- import {
16
- PatternMatcher,
17
- PatternResolver,
18
- } from "../../src/interpreter/components/PatternMatcher.js";
19
- import { createStream } from "../../src/interpreter/utils.js";
20
- import { idContinuation, trampoline } from "../../src/interpreter/trampoline.js";
21
- import { RuntimeContext } from "../../src/interpreter/components/RuntimeContext.js";
22
-
23
- const s = (v: string) => new SymbolPrimitive(v);
24
- const n = (v: number) => new NumberPrimitive(v);
25
- const lit = (v: string | number) =>
26
- new LiteralPattern(typeof v === "string" ? s(v) : n(v));
27
- const variable = (v: string) => new VariablePattern(s(v));
28
- const wildcard = () => new WildcardPattern();
29
-
30
- describe("Pattern System", () => {
31
- describe("PatternResolver (Pretty Printing)", () => {
32
- let resolver: PatternResolver;
33
-
34
- beforeEach(() => {
35
- resolver = new PatternResolver();
36
- });
37
-
38
- it("should resolve a variable pattern", () => {
39
- const p = variable("X");
40
- expect(p.accept(resolver)).to.equal("X");
41
- });
42
-
43
- it("should resolve a wildcard pattern", () => {
44
- const p = wildcard();
45
- expect(p.accept(resolver)).to.equal("_");
46
- });
47
-
48
- it("should resolve a literal pattern", () => {
49
- const p = lit(42);
50
- expect(p.accept(resolver)).to.equal("42");
51
- });
52
-
53
- it("should resolve a cons pattern", () => {
54
- // (1:X)
55
- const p = new ConsPattern(lit(1), variable("X"));
56
- expect(p.accept(resolver)).to.equal("(1:X)");
57
- });
58
-
59
- it("should resolve a constructor pattern", () => {
60
- // Just(X)
61
- const p = new ConstructorPattern(new SymbolPrimitive("Just"), [variable("X")]);
62
- expect(p.accept(resolver)).to.equal("Just X");
63
- });
64
-
65
- it("should resolve nested patterns", () => {
66
- // (1:(2:[]))
67
- const p = new ConsPattern(
68
- lit(1),
69
- new ConsPattern(lit(2), new ListPattern([]))
70
- );
71
- expect(p.accept(resolver)).to.equal("(1:(2:[]))");
72
- });
73
- });
74
-
75
- describe("PatternMatcher (Logic)", () => {
76
- const match = (
77
- pattern: any,
78
- value: any
79
- ): { success: boolean; bindings: [string, any][] } => {
80
- const bindings: [string, any][] = [];
81
- const matcher = new PatternMatcher(value, bindings, new RuntimeContext({lazyLoading: false}));
82
-
83
- const success = trampoline(pattern.accept(matcher)(idContinuation));
84
- return { success, bindings };
85
- };
86
-
87
- it("should match a variable and bind the value", () => {
88
- const p = variable("X");
89
- const { success, bindings } = match(p, 100);
90
-
91
- expect(success).to.be.true;
92
- expect(bindings).to.deep.equal([["X", 100]]);
93
- });
94
-
95
- it("should match a wildcard but not bind anything", () => {
96
- const p = wildcard();
97
- const { success, bindings } = match(p, 100);
98
-
99
- expect(success).to.be.true;
100
- expect(bindings).to.be.empty;
101
- });
102
-
103
- it("should match equal literals", () => {
104
- const p = lit(42);
105
- expect(match(p, 42).success).to.be.true;
106
- expect(match(p, 99).success).to.be.false;
107
- });
108
-
109
- it("should match a list pattern exactly", () => {
110
- const p = new ListPattern([lit(1), lit(2)]);
111
- expect(match(p, [1, 2]).success).to.be.true;
112
- expect(match(p, [1]).success).to.be.false;
113
- expect(match(p, [1, 3]).success).to.be.false;
114
- });
115
-
116
- it("should match a Cons pattern with array input", () => {
117
- const p = new ConsPattern(variable("H"), variable("T"));
118
- const { success, bindings } = match(p, [1, 2, 3]);
119
-
120
- expect(success).to.be.true;
121
- const map = new Map(bindings);
122
- expect(map.get("H")).to.equal(1);
123
- expect(map.get("T")).to.deep.equal([2, 3]);
124
- });
125
-
126
- it("should fail Cons pattern on empty array", () => {
127
- const p = new ConsPattern(variable("H"), variable("T"));
128
- expect(match(p, []).success).to.be.false;
129
- });
130
-
131
- it("should match a Constructor pattern", () => {
132
- const p = new ConstructorPattern(new SymbolPrimitive("Just"), [variable("X")]);
133
- const val = ["Just", 10];
134
-
135
- const { success, bindings } = match(p, val);
136
- expect(success).to.be.true;
137
- expect(bindings).to.deep.equal([["X", 10]]);
138
- });
139
-
140
- it("should match an AsPattern (@)", () => {
141
- const p = new AsPattern(
142
- variable("List"),
143
- new ListPattern([variable("X"), wildcard()])
144
- );
145
- const { success, bindings } = match(p, [1, 2]);
146
- expect(success).to.be.true;
147
- const map = new Map(bindings);
148
- expect(map.get("List")).to.deep.equal([1, 2]);
149
- expect(map.get("X")).to.equal(1);
150
- });
151
-
152
- it("should match a UnionPattern", () => {
153
- const p = new UnionPattern([lit(1), lit(2)]);
154
- expect(match(p, 1).success).to.be.true;
155
- expect(match(p, 2).success).to.be.true;
156
- expect(match(p, 3).success).to.be.false;
157
- });
158
-
159
- describe("String Matching as List", () => {
160
- it("should match an empty string against an empty list pattern", () => {
161
- const p = new ListPattern([]);
162
- expect(match(p, "").success).to.be.true;
163
- });
164
-
165
- it("should match a string against a cons pattern", () => {
166
- const p = new ConsPattern(variable("H"), variable("T"));
167
- const { success, bindings } = match(p, "abc");
168
-
169
- expect(success).to.be.true;
170
- const map = new Map(bindings);
171
- expect(map.get("H")).to.equal("a");
172
- expect(map.get("T")).to.equal("bc");
173
- });
174
-
175
- it("should fail to match an empty string against a cons pattern", () => {
176
- const p = new ConsPattern(variable("H"), variable("T"));
177
- expect(match(p, "").success).to.be.false;
178
- });
179
- });
180
-
181
- describe("LazyList Matching", () => {
182
- const createLazy = (items: number[]): LazyList =>
183
- createStream(function* () {
184
- for (const i of items) yield i;
185
- });
186
-
187
- it("should match Cons pattern against LazyList", () => {
188
- const p = new ConsPattern(variable("X"), variable("Xs"));
189
- const lazyVal = createLazy([1, 2, 3]);
190
-
191
- const { success, bindings } = match(p, lazyVal);
192
- expect(success).to.be.true;
193
-
194
- const map = new Map(bindings);
195
- expect(map.get("X")).to.equal(1);
196
-
197
- const tail = map.get("Xs") as LazyList;
198
- expect(tail.type).to.equal("LazyList");
199
-
200
- const gen = tail.generator();
201
- expect(gen.next().value).to.equal(2);
202
- expect(gen.next().value).to.equal(3);
203
- expect(gen.next().done).to.be.true;
204
- });
205
-
206
- it("should match ListPattern against LazyList (forcing realization)", () => {
207
- const p = new ListPattern([lit(1), lit(2)]);
208
- const lazyVal = createLazy([1, 2]);
209
- expect(match(p, lazyVal).success).to.be.true;
210
- });
211
- });
212
- });
213
- });
1
+ import { expect } from "chai";
2
+ import {
3
+ VariablePattern,
4
+ LiteralPattern,
5
+ SymbolPrimitive,
6
+ NumberPrimitive,
7
+ WildcardPattern,
8
+ ConsPattern,
9
+ ListPattern,
10
+ ConstructorPattern,
11
+ UnionPattern,
12
+ AsPattern,
13
+ } from "yukigo-ast";
14
+ import { PatternMatcher } from "../../src/interpreter/components/PatternMatcher.js";
15
+ import { RuntimeContext } from "../../src/interpreter/components/RuntimeContext.js";
16
+ import { YukigoKernel } from "../../src/interpreter/components/kernel/index.js";
17
+ import { StepCommand } from "../../src/interpreter/components/kernel/commands.js";
18
+ import {
19
+ YuValue,
20
+ YuNumber,
21
+ YuArray,
22
+ YuString,
23
+ LazyList,
24
+ YuNil,
25
+ LazyStepResult,
26
+ isLazyList,
27
+ YuSequence,
28
+ } from "../../src/interpreter/primitives/index.js";
29
+ import { InterpreterVisitor } from "../../src/interpreter/components/evaluators/index.js";
30
+
31
+ const s = (v: string) => new SymbolPrimitive(v);
32
+ const n = (v: number) => new NumberPrimitive(v);
33
+ const lit = (v: string | number) =>
34
+ new LiteralPattern(typeof v === "string" ? s(v) : n(v));
35
+ const variable = (v: string) => new VariablePattern(s(v));
36
+ const wildcard = () => new WildcardPattern();
37
+
38
+ describe("Pattern System", () => {
39
+ describe("PatternResolver (Pretty Printing)", () => {
40
+ it("should resolve a variable pattern", () => {
41
+ const p = variable("X");
42
+ expect(p.toString()).to.equal("X");
43
+ });
44
+
45
+ it("should resolve a wildcard pattern", () => {
46
+ const p = wildcard();
47
+ expect(p.toString()).to.equal("_");
48
+ });
49
+
50
+ it("should resolve a literal pattern", () => {
51
+ const p = lit(42);
52
+ expect(p.toString()).to.equal("42");
53
+ });
54
+
55
+ it("should resolve a cons pattern", () => {
56
+ // (1:X)
57
+ const p = new ConsPattern(lit(1), variable("X"));
58
+ expect(p.toString()).to.equal("(1:X)");
59
+ });
60
+
61
+ it("should resolve a constructor pattern", () => {
62
+ // Just(X)
63
+ const p = new ConstructorPattern(new SymbolPrimitive("Just"), [
64
+ variable("X"),
65
+ ]);
66
+ expect(p.toString()).to.equal("Just X");
67
+ });
68
+
69
+ it("should resolve nested patterns", () => {
70
+ // (1:(2:[]))
71
+ const p = new ConsPattern(
72
+ lit(1),
73
+ new ConsPattern(lit(2), new ListPattern([])),
74
+ );
75
+ expect(p.toString()).to.equal("(1:(2:[]))");
76
+ });
77
+ });
78
+
79
+ describe("PatternMatcher (Logic)", () => {
80
+ const match = (
81
+ pattern: any,
82
+ yuValue: YuValue,
83
+ ): { success: boolean; bindings: [string, any][] } => {
84
+ const bindings: [string, any][] = [];
85
+ const ctx = new RuntimeContext({ lazyLoading: false });
86
+ const matcher = new PatternMatcher(yuValue, bindings, ctx);
87
+ const kernel = new YukigoKernel(new InterpreterVisitor(ctx));
88
+
89
+ const res = kernel.run(pattern.accept(matcher)) as YuValue;
90
+ return { success: res.toJSON() === true, bindings };
91
+ };
92
+
93
+ it("should match a variable and bind the value", () => {
94
+ const p = variable("X");
95
+ const { success, bindings } = match(p, new YuNumber(100));
96
+
97
+ expect(success).to.be.true;
98
+ expect(bindings[0][0]).to.equal("X");
99
+ expect((bindings[0][1] as YuValue).toJSON()).to.equal(100);
100
+ });
101
+
102
+ it("should match a wildcard but not bind anything", () => {
103
+ const p = wildcard();
104
+ const { success, bindings } = match(p, new YuNumber(100));
105
+
106
+ expect(success).to.be.true;
107
+ expect(bindings).to.be.empty;
108
+ });
109
+
110
+ it("should match equal literals", () => {
111
+ const p = lit(42);
112
+ expect(match(p, new YuNumber(42)).success).to.be.true;
113
+ expect(match(p, new YuNumber(99)).success).to.be.false;
114
+ });
115
+
116
+ it("should match a list pattern exactly", () => {
117
+ const p = new ListPattern([lit(1), lit(2)]);
118
+ expect(match(p, new YuArray([new YuNumber(1), new YuNumber(2)])).success)
119
+ .to.be.true;
120
+ expect(match(p, new YuArray([new YuNumber(1)])).success).to.be.false;
121
+ expect(match(p, new YuArray([new YuNumber(1), new YuNumber(3)])).success)
122
+ .to.be.false;
123
+ });
124
+
125
+ it("should match a Cons pattern with array input", () => {
126
+ const p = new ConsPattern(variable("H"), variable("T"));
127
+ const { success, bindings } = match(
128
+ p,
129
+ new YuArray([new YuNumber(1), new YuNumber(2), new YuNumber(3)]),
130
+ );
131
+
132
+ expect(success).to.be.true;
133
+ const map = new Map(
134
+ bindings.map(([k, v]) => [k, (v as YuValue).toJSON()]),
135
+ );
136
+ expect(map.get("H")).to.equal(1);
137
+ expect(map.get("T")).to.deep.equal([2, 3]);
138
+ });
139
+
140
+ it("should fail Cons pattern on empty array", () => {
141
+ const p = new ConsPattern(variable("H"), variable("T"));
142
+ expect(match(p, new YuArray([])).success).to.be.false;
143
+ });
144
+
145
+ it("should match a Constructor pattern", () => {
146
+ const p = new ConstructorPattern(new SymbolPrimitive("Just"), [
147
+ variable("X"),
148
+ ]);
149
+ const val = new YuArray([new YuString("Just"), new YuNumber(10)]);
150
+
151
+ const { success, bindings } = match(p, val);
152
+ expect(success).to.be.true;
153
+ expect(bindings[0][0]).to.equal("X");
154
+ expect((bindings[0][1] as YuValue).toJSON()).to.equal(10);
155
+ });
156
+
157
+ it("should match an AsPattern (@)", () => {
158
+ const p = new AsPattern(
159
+ variable("List"),
160
+ new ListPattern([variable("X"), wildcard()]),
161
+ );
162
+ const { success, bindings } = match(
163
+ p,
164
+ new YuArray([new YuNumber(1), new YuNumber(2)]),
165
+ );
166
+ expect(success).to.be.true;
167
+ const map = new Map(
168
+ bindings.map(([k, v]) => [k, (v as YuValue).toJSON()]),
169
+ );
170
+ expect(map.get("List")).to.deep.equal([1, 2]);
171
+ expect(map.get("X")).to.equal(1);
172
+ });
173
+
174
+ it("should match a UnionPattern", () => {
175
+ const p = new UnionPattern([lit(1), lit(2)]);
176
+ expect(match(p, new YuNumber(1)).success).to.be.true;
177
+ expect(match(p, new YuNumber(2)).success).to.be.true;
178
+ expect(match(p, new YuNumber(3)).success).to.be.false;
179
+ });
180
+
181
+ describe("String Matching as List", () => {
182
+ it("should match an empty string against an empty list pattern", () => {
183
+ const p = new ListPattern([]);
184
+ expect(match(p, new YuString("")).success).to.be.true;
185
+ });
186
+
187
+ it("should match a string against a cons pattern", () => {
188
+ const p = new ConsPattern(variable("H"), variable("T"));
189
+ const { success, bindings } = match(p, new YuString("abc"));
190
+
191
+ expect(success).to.be.true;
192
+ const map = new Map(
193
+ bindings.map(([k, v]) => [k, (v as YuValue).toJSON()]),
194
+ );
195
+ expect(map.get("H")).to.equal("a");
196
+ expect(map.get("T")).to.equal("bc");
197
+ });
198
+
199
+ it("should fail to match an empty string against a cons pattern", () => {
200
+ const p = new ConsPattern(variable("H"), variable("T"));
201
+ expect(match(p, new YuString("")).success).to.be.false;
202
+ });
203
+ });
204
+
205
+ describe("LazyList Matching", () => {
206
+ const createLazy = (items: number[]): LazyList => {
207
+ const next = (idx: number): YuValue => {
208
+ if (idx >= items.length) return YuNil.getInstance();
209
+ return new LazyList(
210
+ () =>
211
+ new StepCommand(
212
+ new LazyStepResult(
213
+ new YuNumber(items[idx]),
214
+ next(idx + 1) as YuSequence,
215
+ ),
216
+ ),
217
+ );
218
+ };
219
+ return next(0) as LazyList;
220
+ };
221
+
222
+ it("should match Cons pattern against LazyList", () => {
223
+ const p = new ConsPattern(variable("X"), variable("Xs"));
224
+ const lazyVal = createLazy([1, 2, 3]);
225
+
226
+ const { success, bindings } = match(p, lazyVal);
227
+ expect(success).to.be.true;
228
+
229
+ const map = new Map(bindings);
230
+ expect((map.get("X") as YuValue).toJSON()).to.equal(1);
231
+
232
+ const tail = map.get("Xs") as LazyList;
233
+ expect(isLazyList(tail)).to.be.true;
234
+
235
+ const ctx = new RuntimeContext({ lazyLoading: false });
236
+ const kernel = new YukigoKernel(new InterpreterVisitor(ctx));
237
+
238
+ const step1 = kernel.run(tail.step()) as LazyStepResult;
239
+ expect((step1.head as YuValue).toJSON()).to.equal(2);
240
+ const step2 = kernel.run(
241
+ (step1.tail as LazyList).step(),
242
+ ) as LazyStepResult;
243
+ expect((step2.head as YuValue).toJSON()).to.equal(3);
244
+ expect(step2.tail).to.be.instanceOf(YuNil);
245
+ });
246
+
247
+ it("should match ListPattern against LazyList (forcing realization)", () => {
248
+ const p = new ListPattern([lit(1), lit(2)]);
249
+ const lazyVal = createLazy([1, 2]);
250
+ expect(match(p, lazyVal).success).to.be.true;
251
+ });
252
+ });
253
+ });
254
+ });