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,227 +1,249 @@
1
- import {
2
- EquationRuntime,
3
- PrimitiveValue,
4
- UnguardedBody,
5
- Sequence,
6
- Return,
7
- Function,
8
- RuntimeFunction,
9
- isRuntimeFunction,
10
- } from "yukigo-ast";
11
- import { Bindings } from "../../index.js";
12
- import { PatternMatcher } from "../PatternMatcher.js";
13
- import { ExpressionEvaluator } from "../../utils.js";
14
- import { InterpreterError } from "../../errors.js";
15
- import { EnvBuilderVisitor } from "../EnvBuilder.js";
16
- import { Continuation, CPSThunk, Thunk, valueToCPS } from "../../trampoline.js";
17
- import { RuntimeContext } from "../RuntimeContext.js";
18
- import { InterpreterVisitor } from "../Visitor.js";
19
-
20
- class NonExhaustivePatterns extends InterpreterError {
21
- constructor(funcName: string) {
22
- super("PatternMatch", `Non-exhaustive patterns in '${funcName}'`);
23
- }
24
- }
25
-
26
- type EvaluatorFactory = (ctx: RuntimeContext) => ExpressionEvaluator;
27
-
28
- export class FunctionRuntime {
29
- constructor(private context: RuntimeContext) {}
30
-
31
- public apply(
32
- func: RuntimeFunction,
33
- args: PrimitiveValue[],
34
- k: Continuation<PrimitiveValue>,
35
- ): Thunk<PrimitiveValue> {
36
- const funcName = func.identifier;
37
- const equations = func.equations;
38
- const oldEnv = this.context.env;
39
-
40
- if (this.context.config.debug)
41
- console.log(
42
- `[FunctionRuntime] Applying function: ${funcName} with args:`,
43
- args,
44
- );
45
-
46
- const tryNextEquation = (eqIndex: number): Thunk<PrimitiveValue> => {
47
- if (eqIndex >= equations.length) {
48
- this.context.setEnv(oldEnv);
49
- throw new NonExhaustivePatterns(funcName);
50
- }
51
-
52
- const eq = equations[eqIndex];
53
- if (eq.patterns.length !== args.length)
54
- return () => tryNextEquation(eqIndex + 1);
55
-
56
- const bindings: Bindings = [];
57
-
58
- return this.patternsMatch(eq, args, bindings, (isMatch) => {
59
- if (!isMatch) return () => tryNextEquation(eqIndex + 1);
60
-
61
- if (this.context.config.debug)
62
- console.log(
63
- `[FunctionRuntime] Match successful for ${funcName} equation ${eqIndex}`,
64
- );
65
-
66
- const localEnv = new Map<string, PrimitiveValue>(bindings);
67
- if (func.closure) this.context.setEnv(func.closure);
68
- this.context.pushEnv(localEnv);
69
-
70
- const wrappedK = (res: PrimitiveValue) => {
71
- this.context.setEnv(oldEnv);
72
- return k(res);
73
- };
74
-
75
- const evaluatorFactory: EvaluatorFactory = (ctx) =>
76
- new InterpreterVisitor(ctx);
77
-
78
- const body = eq.body;
79
-
80
- // UnguardedBody
81
- if (body instanceof UnguardedBody)
82
- return this.evaluateSequence(
83
- body.sequence,
84
- this.context,
85
- evaluatorFactory,
86
- wrappedK,
87
- );
88
-
89
- // GuardedBody
90
- return () => {
91
- if (Array.isArray(body) && body.length > 0) {
92
- const prototypeBody = body[0].body;
93
- if (prototypeBody instanceof Sequence)
94
- this.preloadDefinitions(prototypeBody, evaluatorFactory);
95
- }
96
-
97
- const tryNextGuard = (guardIndex: number): Thunk<PrimitiveValue> => {
98
- if (guardIndex >= body.length) {
99
- this.context.setEnv(oldEnv);
100
- return () => tryNextEquation(eqIndex + 1);
101
- }
102
-
103
- const evaluator = evaluatorFactory(this.context);
104
- const guard = body[guardIndex];
105
- return evaluator.evaluate(guard.condition, (cond) => {
106
- if (cond !== true) return () => tryNextGuard(guardIndex + 1);
107
-
108
- if (!(guard.body instanceof Sequence))
109
- return evaluator.evaluate(guard.body, wrappedK);
110
-
111
- return this.evaluateSequence(
112
- guard.body,
113
- this.context,
114
- evaluatorFactory,
115
- wrappedK,
116
- );
117
- });
118
- };
119
-
120
- return tryNextGuard(0);
121
- };
122
- });
123
- };
124
-
125
- return tryNextEquation(0);
126
- }
127
-
128
- public applyArguments(
129
- func: RuntimeFunction,
130
- args: (PrimitiveValue | (() => PrimitiveValue))[],
131
- ): CPSThunk<PrimitiveValue> {
132
- if (args.length < func.arity) {
133
- return valueToCPS({
134
- ...func,
135
- pendingArgs: args,
136
- });
137
- }
138
-
139
- const argsToConsume = args.slice(0, func.arity);
140
- const remainingArgs = args.slice(func.arity);
141
-
142
- const evaluatedArgs = argsToConsume.map((arg) =>
143
- typeof arg === "function" ? arg() : arg,
144
- );
145
-
146
- return (cont) => () =>
147
- this.apply(func, evaluatedArgs, (result) => {
148
- if (remainingArgs.length > 0) {
149
- if (isRuntimeFunction(result)) {
150
- const nextArgs = result.pendingArgs
151
- ? [...result.pendingArgs, ...remainingArgs]
152
- : remainingArgs;
153
-
154
- return this.applyArguments(result, nextArgs)(cont);
155
- } else {
156
- throw new InterpreterError(
157
- "Application",
158
- `Too many arguments provided. Result was '${result}' (not a function), but had ${remainingArgs.length} args left.`,
159
- );
160
- }
161
- }
162
- return cont(result);
163
- });
164
- }
165
-
166
- private preloadDefinitions(
167
- seq: Sequence,
168
- evaluatorFactory: EvaluatorFactory,
169
- ): void {
170
- const ctx = new RuntimeContext();
171
- new EnvBuilderVisitor(ctx).build(seq.statements);
172
- const evaluator = evaluatorFactory(ctx);
173
-
174
- for (const stmt of seq.statements) {
175
- if (stmt instanceof Function || stmt instanceof Return) continue;
176
- evaluator.evaluate(stmt, (val) => val);
177
- }
178
- }
179
-
180
- private patternsMatch(
181
- eq: EquationRuntime,
182
- args: PrimitiveValue[],
183
- bindings: Bindings,
184
- k: Continuation<boolean>,
185
- ): Thunk<boolean> {
186
- const matchNext = (index: number): Thunk<boolean> => {
187
- if (index >= args.length) return k(true);
188
- const matcher = new PatternMatcher(args[index], bindings, this.context);
189
- return eq.patterns[index].accept(matcher)((isMatch) => {
190
- if (!isMatch) return k(false);
191
- return () => matchNext(index + 1);
192
- });
193
- };
194
- return matchNext(0);
195
- }
196
-
197
- private evaluateSequence(
198
- seq: Sequence,
199
- ctx: RuntimeContext,
200
- evaluatorFactory: EvaluatorFactory,
201
- k: Continuation<PrimitiveValue>,
202
- ): Thunk<PrimitiveValue> {
203
- new EnvBuilderVisitor(ctx).build(seq.statements);
204
- const evaluator = evaluatorFactory(ctx);
205
-
206
- const evaluateNext = (
207
- index: number,
208
- lastResult: PrimitiveValue,
209
- ): Thunk<PrimitiveValue> => {
210
- if (index >= seq.statements.length) return k(lastResult);
211
-
212
- const stmt = seq.statements[index];
213
- if (stmt instanceof Function)
214
- return () => evaluateNext(index + 1, lastResult);
215
-
216
- if (stmt instanceof Return) {
217
- return evaluator.evaluate(stmt.body, k);
218
- } else {
219
- return evaluator.evaluate(stmt, (result) => {
220
- return () => evaluateNext(index + 1, result);
221
- });
222
- }
223
- };
224
-
225
- return evaluateNext(0, undefined);
226
- }
227
- }
1
+ import {
2
+ Sequence,
3
+ Return,
4
+ Function,
5
+ NativeBody,
6
+ UnguardedBody,
7
+ Visitor,
8
+ GuardedExpression,
9
+ } from "yukigo-ast";
10
+ import { Bindings } from "../../index.js";
11
+ import { PatternMatcher } from "../PatternMatcher.js";
12
+ import { boolean, PrimitiveThunk, isTrue, raise, error } from "../../utils.js";
13
+ import { InterpreterError } from "../../errors.js";
14
+ import { EnvBuilderVisitor } from "../EnvBuilder.js";
15
+ import { EvaluatorFactory, RuntimeContext } from "../RuntimeContext.js";
16
+ import { Evaluator } from "../evaluators/BaseEvaluator.js";
17
+ import {
18
+ ExecutionCommand,
19
+ StepCommand,
20
+ BindCommand,
21
+ FailCommand,
22
+ RaiseCommand,
23
+ } from "../kernel/commands.js";
24
+ import { YuValue } from "../../primitives/YuValue.js";
25
+ import {
26
+ RuntimeFunction,
27
+ isRuntimeFunction,
28
+ EquationRuntime,
29
+ YuNil,
30
+ RuntimeClass,
31
+ } from "../../primitives/index.js";
32
+
33
+ class NonExhaustivePatterns extends InterpreterError {
34
+ constructor(funcName: string) {
35
+ super("PatternMatch", `Non-exhaustive patterns in '${funcName}'`);
36
+ }
37
+ }
38
+
39
+ export class FunctionRuntime {
40
+ constructor(private context: RuntimeContext) {}
41
+
42
+ public apply(func: RuntimeFunction, args: YuValue[]): ExecutionCommand {
43
+ const funcName = func.identifier;
44
+ const selfContext = this.context;
45
+ const equations = func.equations;
46
+ const oldEnv = this.context.env;
47
+
48
+ const tryNextEquation = (eqIndex: number): ExecutionCommand => {
49
+ if (eqIndex >= equations.length) {
50
+ this.context.setEnv(oldEnv);
51
+ return raise(new NonExhaustivePatterns(func.name));
52
+ }
53
+
54
+ const eq = equations[eqIndex];
55
+ if (eq.patterns.length !== args.length)
56
+ return tryNextEquation(eqIndex + 1);
57
+
58
+ const bindings: Bindings = [];
59
+
60
+ return new BindCommand(
61
+ this.patternsMatch(eq, args, bindings),
62
+ (matchRes) => {
63
+ if (!isTrue(matchRes)) return tryNextEquation(eqIndex + 1);
64
+
65
+ const localEnv = new Map<string, YuValue>(bindings);
66
+ if (func.closure)
67
+ this.context.setEnv(this.context.cloneEnv(func.closure));
68
+ this.context.pushEnv(localEnv);
69
+
70
+ const nextWithEnvRestore = (res: YuValue) => {
71
+ this.context.setEnv(oldEnv);
72
+ return new StepCommand(res);
73
+ };
74
+
75
+ const dummyVisitor = this.context.evaluatorFactory!(this.context);
76
+ const BaseVisitor = dummyVisitor.constructor as new (
77
+ ctx: RuntimeContext,
78
+ ) => Evaluator & {
79
+ visitGuardedExpression(
80
+ expr: GuardedExpression,
81
+ ): ExecutionCommand;
82
+ };
83
+
84
+ class GuardBacktrackingVisitor extends BaseVisitor {
85
+ override visitGuardedExpression(
86
+ expr: GuardedExpression,
87
+ ): ExecutionCommand {
88
+ const tryNextGuard = (guardIndex: number): ExecutionCommand => {
89
+ if (guardIndex >= expr.guards.length) {
90
+ selfContext.setEnv(oldEnv);
91
+ return tryNextEquation(eqIndex + 1);
92
+ }
93
+ const guard = expr.guards[guardIndex];
94
+ return new BindCommand(
95
+ this.evaluate(guard.condition),
96
+ (cond) => {
97
+ if (!isTrue(cond)) return tryNextGuard(guardIndex + 1);
98
+ return this.evaluate(guard.body);
99
+ },
100
+ );
101
+ };
102
+ return tryNextGuard(0);
103
+ }
104
+ }
105
+
106
+ const evaluatorFactory: EvaluatorFactory = (ctx) =>
107
+ new GuardBacktrackingVisitor(ctx);
108
+ return eq.body.accept({
109
+ visitUnguardedBody: (b: UnguardedBody) => {
110
+ return new BindCommand(
111
+ this.evaluateSequence(
112
+ b.sequence,
113
+ this.context,
114
+ evaluatorFactory,
115
+ ),
116
+ nextWithEnvRestore,
117
+ );
118
+ },
119
+
120
+ visitNativeBody: (b: NativeBody) => {
121
+ const currentHolder = this.context.lookup(
122
+ "__CONTEXT_CLASS__",
123
+ ) as RuntimeClass;
124
+ const lookupKey = `${currentHolder.identifier}.${funcName}`;
125
+ const nativeImpl =
126
+ this.context.config.nativeProviders.get(lookupKey);
127
+
128
+ if (nativeImpl) {
129
+ const capturedContext = this.context.clone();
130
+ return new BindCommand(
131
+ nativeImpl(
132
+ this.context.lookup("self"),
133
+ args,
134
+ capturedContext,
135
+ ),
136
+ nextWithEnvRestore,
137
+ );
138
+ }
139
+
140
+ const voidObj = this.context.lookup("void");
141
+ return nextWithEnvRestore(voidObj ?? YuNil.getInstance());
142
+ },
143
+ fallback: (node) => {
144
+ return new RaiseCommand(
145
+ new InterpreterError(
146
+ "FunctionRuntime.apply",
147
+ `Unexpected node: ${node.toJSON()}`,
148
+ ),
149
+ );
150
+ },
151
+ } as Visitor<ExecutionCommand>);
152
+ },
153
+ );
154
+ };
155
+
156
+ return tryNextEquation(0);
157
+ }
158
+
159
+ public applyArguments(
160
+ func: RuntimeFunction,
161
+ args?: (YuValue | PrimitiveThunk)[],
162
+ ): ExecutionCommand {
163
+ const targetFunc = args ? func.bind(...args) : func;
164
+ const allArgs = targetFunc.pendingArgs ?? [];
165
+
166
+ if (allArgs.length < targetFunc.arity) return new StepCommand(targetFunc);
167
+
168
+ const argsToConsume = allArgs.slice(0, targetFunc.arity);
169
+ const remainingArgs = allArgs.slice(targetFunc.arity);
170
+
171
+ const evaluatedArgs = argsToConsume.map((arg) =>
172
+ typeof arg === "function" ? arg() : arg,
173
+ );
174
+
175
+ return new BindCommand(this.apply(targetFunc, evaluatedArgs), (result) => {
176
+ if (remainingArgs.length == 0) return new StepCommand(result);
177
+ if (!isRuntimeFunction(result))
178
+ return new FailCommand(
179
+ new Error(
180
+ `[Application] Too many arguments provided. Result was '${result}' (not a function), but had ${remainingArgs.length} args left.`,
181
+ ),
182
+ );
183
+ return this.applyArguments(result, remainingArgs);
184
+ });
185
+ }
186
+
187
+ private preloadDefinitions(
188
+ seq: Sequence,
189
+ evaluatorFactory: EvaluatorFactory,
190
+ ): void {
191
+ const ctx = new RuntimeContext();
192
+ new EnvBuilderVisitor(ctx).build(seq.statements);
193
+ const evaluator = evaluatorFactory(ctx);
194
+
195
+ for (const stmt of seq.statements) {
196
+ if (stmt.is(Function) || stmt.is(Return)) continue;
197
+ evaluator.evaluate(stmt);
198
+ }
199
+ }
200
+
201
+ private patternsMatch(
202
+ eq: EquationRuntime,
203
+ args: YuValue[],
204
+ bindings: Bindings,
205
+ ): ExecutionCommand {
206
+ const matchNext = (index: number): ExecutionCommand => {
207
+ if (index >= args.length) return boolean(true);
208
+
209
+ const matcher = new PatternMatcher(args[index], bindings, this.context);
210
+
211
+ return new BindCommand(eq.patterns[index].accept(matcher), (res) => {
212
+ if (!isTrue(res)) return boolean(false);
213
+ return matchNext(index + 1);
214
+ });
215
+ };
216
+
217
+ return matchNext(0);
218
+ }
219
+
220
+ private evaluateSequence(
221
+ seq: Sequence,
222
+ ctx: RuntimeContext,
223
+ evaluatorFactory: EvaluatorFactory,
224
+ ): ExecutionCommand {
225
+ new EnvBuilderVisitor(ctx).build(seq.statements);
226
+ const evaluator = evaluatorFactory(ctx);
227
+
228
+ const evaluateNext = (
229
+ index: number,
230
+ lastResult: YuValue,
231
+ ): ExecutionCommand => {
232
+ if (index >= seq.statements.length) return new StepCommand(lastResult);
233
+
234
+ const stmt = seq.statements[index];
235
+ if (stmt.is(Function)) return evaluateNext(index + 1, lastResult);
236
+
237
+ if (!stmt.is(Return))
238
+ return new BindCommand(evaluator.evaluate(stmt), (result) => {
239
+ return evaluateNext(index + 1, result);
240
+ });
241
+
242
+ if (!stmt.body)
243
+ return raise(error("FunctionRuntime", "Return \`body\` was undefined"));
244
+ return evaluator.evaluate(stmt.body);
245
+ };
246
+
247
+ return evaluateNext(0, YuNil.getInstance());
248
+ }
249
+ }