yukigo 0.2.2 → 0.3.1

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 +102 -41
  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 +21 -14
  13. package/dist/analyzer/inspections/generic/generic.js +78 -59
  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 +12 -5
  115. package/dist/utils/helpers.js +6 -79
  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 -581
  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 +81 -168
  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 +188 -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
@@ -0,0 +1,372 @@
1
+ import {
2
+ Assignment,
3
+ Attribute,
4
+ Function,
5
+ If,
6
+ LogicConstraint,
7
+ NamedArgument,
8
+ Object,
9
+ Print,
10
+ Query,
11
+ Raise,
12
+ Return,
13
+ Self,
14
+ Sequence,
15
+ StatementVisitor,
16
+ Super,
17
+ Try,
18
+ Variable,
19
+ } from "yukigo-ast";
20
+ import {
21
+ Constructor as EvaluatorConstructor,
22
+ EvaluatorBase,
23
+ } from "./BaseEvaluator.js";
24
+ import {
25
+ BacktrackCommand,
26
+ BindCommand,
27
+ CatchCommand,
28
+ CatchHandler,
29
+ EvalCommand,
30
+ ExecutionCommand,
31
+ FailCommand,
32
+ RaiseCommand,
33
+ StepCommand,
34
+ } from "../kernel/commands.js";
35
+ import {
36
+ LogicAnswer,
37
+ LogicResult,
38
+ RuntimeClass,
39
+ RuntimeObject,
40
+ YuBoolean,
41
+ YuNil,
42
+ YuString,
43
+ YuValue,
44
+ } from "../../primitives/index.js";
45
+ import { InterpreterError } from "../../errors.js";
46
+ import { boolean, Environment, isTrue } from "../../utils.js";
47
+ import { OOPCollector } from "../EnvBuilder.js";
48
+ import { RuntimeContext } from "../RuntimeContext.js";
49
+ import { getClassFields, getClassInitializers, getLogicEngine } from "./utils.js";
50
+
51
+ export function StatementEvaluator<
52
+ TBase extends EvaluatorConstructor<EvaluatorBase>,
53
+ >(Base: TBase) {
54
+ return class
55
+ extends Base
56
+ implements Partial<StatementVisitor<ExecutionCommand>>
57
+ {
58
+ visitSequence(node: Sequence): ExecutionCommand {
59
+ if (node.statements.length === 0)
60
+ return new StepCommand(YuNil.getInstance());
61
+
62
+ const evaluateNext = (
63
+ index: number,
64
+ lastResult: YuValue,
65
+ ): ExecutionCommand => {
66
+ if (index >= node.statements.length) return new StepCommand(lastResult);
67
+
68
+ const stmt = node.statements[index];
69
+ return new BindCommand(this.evaluate(stmt), (result) => {
70
+ if (stmt instanceof Return) return new StepCommand(result);
71
+ return evaluateNext(index + 1, result);
72
+ });
73
+ };
74
+
75
+ return evaluateNext(0, YuNil.getInstance());
76
+ }
77
+ visitSelf(node: Self): ExecutionCommand {
78
+ try {
79
+ return new StepCommand(this.context.lookup("self"));
80
+ } catch {
81
+ return new FailCommand(
82
+ new InterpreterError("Self", "'self' is not defined in this context"),
83
+ );
84
+ }
85
+ }
86
+ visitSuper(node: Super): ExecutionCommand {
87
+ let methodName: YuString;
88
+ try {
89
+ methodName = this.context.lookup("__METHOD_NAME__") as YuString;
90
+ } catch (e) {
91
+ return new FailCommand(
92
+ new InterpreterError(
93
+ "Super",
94
+ "'super' keyword used outside of a method context",
95
+ ),
96
+ );
97
+ }
98
+
99
+ const args: YuValue[] = [];
100
+ const evaluateNextArg = (index: number): ExecutionCommand => {
101
+ if (index >= node.args.length) {
102
+ return this.context.objRuntime.dispatchSuper(
103
+ methodName.toJSON(),
104
+ args,
105
+ );
106
+ }
107
+ return new BindCommand(this.evaluate(node.args[index]), (val) => {
108
+ args.push(val);
109
+ return evaluateNextArg(index + 1);
110
+ });
111
+ };
112
+ return evaluateNextArg(0);
113
+ }
114
+ visitIf(node: If): ExecutionCommand {
115
+ return new BindCommand(this.evaluate(node.condition), (condition) => {
116
+ if (!(condition instanceof YuBoolean))
117
+ return new FailCommand(
118
+ new InterpreterError(
119
+ "If",
120
+ `Expected boolean in condition and got ${condition.getType()}`,
121
+ ),
122
+ );
123
+ const isTrue = condition.value;
124
+ return isTrue ? this.evaluate(node.then) : this.evaluate(node.elseExpr);
125
+ });
126
+ }
127
+ visitReturn(node: Return): ExecutionCommand {
128
+ if (!node.body) return new StepCommand(YuNil.getInstance());
129
+ return this.evaluate(node.body);
130
+ }
131
+ visitFunction(node: Function): ExecutionCommand {
132
+ return new StepCommand(YuNil.getInstance());
133
+ }
134
+ visitTry(node: Try): ExecutionCommand {
135
+ const tryStartEnv = this.context.env;
136
+
137
+ // Comando que evaluará el cuerpo del Try (ej: block.apply())
138
+ const bodyCommand = new EvalCommand(node.body);
139
+
140
+ // Este handler es el que el Kernel llamará cuando reciba el RaiseCommand
141
+ const catchHandler: CatchHandler = (errorObj) => {
142
+ this.context.env = tryStartEnv; // Restauramos entorno
143
+
144
+ // 1. Reificamos la excepción para Wollok
145
+ let exceptionObj: YuValue | undefined;
146
+
147
+ if (errorObj instanceof InterpreterError) {
148
+ const mappedErrors = this.context.dispatchHook(
149
+ "onInterpreterError",
150
+ errorObj,
151
+ this.context,
152
+ );
153
+ exceptionObj = mappedErrors.find(
154
+ (res: any) => res !== undefined && res !== null,
155
+ ) as YuValue;
156
+ }
157
+
158
+ if (!exceptionObj) {
159
+ if (errorObj instanceof InterpreterError) {
160
+ exceptionObj = new YuString(errorObj.message);
161
+ } else {
162
+ exceptionObj = errorObj as YuValue;
163
+ }
164
+ }
165
+
166
+ // 2. Buscamos si el Try tiene bloques catch
167
+ const catches = node.catchExpr;
168
+
169
+ if (catches.length > 0) {
170
+ const catchBlock = catches[0];
171
+ const pattern = catchBlock.patterns[0] as any;
172
+ const paramName =
173
+ pattern.name?.value ?? pattern.identifier?.value ?? String(pattern);
174
+
175
+ // Inyectamos la variable 'e' (excepción) en el scope
176
+ this.context.pushEnv();
177
+ this.context.define(paramName, exceptionObj);
178
+
179
+ // Retornamos la evaluación del catchBlock
180
+ return new BindCommand(
181
+ new EvalCommand(catchBlock.body),
182
+ (catchRes) => {
183
+ this.context.env = tryStartEnv;
184
+
185
+ // Si había bloque finally, podrías evaluarlo acá antes del StepCommand
186
+ if (node.finallyExpr) {
187
+ return new BindCommand(
188
+ new EvalCommand(node.finallyExpr),
189
+ () => new StepCommand(catchRes),
190
+ );
191
+ }
192
+ return new StepCommand(catchRes);
193
+ },
194
+ );
195
+ }
196
+
197
+ // Si no había bloque catch (solo finally), relanzamos al Kernel superior
198
+ if (node.finallyExpr) {
199
+ return new BindCommand(
200
+ new EvalCommand(node.finallyExpr),
201
+ () => new RaiseCommand(errorObj),
202
+ );
203
+ }
204
+ return new RaiseCommand(errorObj);
205
+ };
206
+
207
+ // Le entregamos el CatchCommand al Kernel para que lo apile ANTES de ejecutar el cuerpo
208
+ return new CatchCommand(catchHandler, bodyCommand);
209
+ }
210
+
211
+ visitRaise(node: Raise): ExecutionCommand {
212
+ return new BindCommand(this.evaluate(node.body), (msg) => {
213
+ if (msg instanceof RuntimeObject) return new RaiseCommand(msg);
214
+
215
+ const msgStr = msg.toJSON();
216
+ if (typeof msgStr !== "string") {
217
+ return new RaiseCommand(
218
+ new InterpreterError("Raise", msg.toString()),
219
+ );
220
+ }
221
+ return new RaiseCommand(new InterpreterError("Raise", msgStr));
222
+ });
223
+ }
224
+ visitPrint(node: Print): ExecutionCommand {
225
+ return new BindCommand(this.evaluate(node.expression), (res) => {
226
+ console.log(res);
227
+ return new StepCommand(YuNil.getInstance());
228
+ });
229
+ }
230
+ visitVariable(node: Variable): ExecutionCommand {
231
+ const name = node.identifier.value;
232
+ return new BindCommand(this.evaluate(node.expression), (value) => {
233
+ this.context.define(name, value);
234
+ return boolean(true);
235
+ });
236
+ }
237
+ visitAssignment(node: Assignment): ExecutionCommand {
238
+ if (!this.context.config.mutability) {
239
+ return new FailCommand(
240
+ new InterpreterError(
241
+ "Assignment",
242
+ `Cannot reassign variable '${node.identifier.value}': mutability is disabled`,
243
+ ),
244
+ );
245
+ }
246
+
247
+ const name = node.identifier.value;
248
+ return new BindCommand(this.evaluate(node.expression), (value) => {
249
+ const onReplace = (scope: Environment) => {
250
+ if (scope.has("self")) {
251
+ const self = scope.get("self");
252
+ if (self instanceof RuntimeObject && self.fields.has(name))
253
+ self.fields.set(name, value);
254
+ }
255
+ };
256
+
257
+ if (!this.context.replace(name, value, onReplace))
258
+ return new FailCommand(
259
+ new InterpreterError(
260
+ "Assignment",
261
+ `Cannot assign to undefined variable: ${name}`,
262
+ ),
263
+ );
264
+
265
+ return new StepCommand(value);
266
+ });
267
+ }
268
+
269
+ visitQuery(node: Query): ExecutionCommand {
270
+ return getLogicEngine(this, this.context).solveQuery(node);
271
+ }
272
+ visitAttribute(node: Attribute): ExecutionCommand {
273
+ const name = node.identifier.value;
274
+ return new BindCommand(this.evaluate(node.expression), (value) => {
275
+ this.context.define(name, value);
276
+ return boolean(true);
277
+ });
278
+ }
279
+ visitObject(node: Object): ExecutionCommand {
280
+ const identifier = node.identifier.value;
281
+ const collector = new OOPCollector(this.context);
282
+ node.expression.accept(collector);
283
+
284
+ const fields = collector.collectedFields;
285
+ const methods = collector.collectedMethods;
286
+
287
+ const extendsSymbol = (node as any).extendsSymbol?.value;
288
+ if (extendsSymbol) {
289
+ const classDef = this.context.lookup(extendsSymbol);
290
+ if (classDef && classDef instanceof RuntimeClass) {
291
+ const classFields = getClassFields(classDef, this.context);
292
+ for (const [k, v] of classFields.entries()) {
293
+ if (!fields.has(k)) {
294
+ fields.set(k, v);
295
+ }
296
+ }
297
+ }
298
+ }
299
+
300
+ const runtimeObject = new RuntimeObject(
301
+ identifier,
302
+ extendsSymbol || "",
303
+ fields,
304
+ methods,
305
+ );
306
+
307
+ const extendsArgs = (node as any).extendsArgs || [];
308
+ const evaluateArgs = (index: number): ExecutionCommand => {
309
+ if (index >= extendsArgs.length) {
310
+ this.context.define(identifier, runtimeObject);
311
+ return new StepCommand(runtimeObject);
312
+ }
313
+ const arg = extendsArgs[index];
314
+ if (
315
+ arg instanceof NamedArgument ||
316
+ arg.constructor.name === "NamedArgument"
317
+ ) {
318
+ const fieldName = (arg as any).identifier.value;
319
+ return new BindCommand(
320
+ this.evaluate((arg as any).expression),
321
+ (val) => {
322
+ runtimeObject.setField(fieldName, val);
323
+ return evaluateArgs(index + 1);
324
+ },
325
+ );
326
+ }
327
+ return evaluateArgs(index + 1);
328
+ };
329
+
330
+ const evaluateClassInitializers = (
331
+ classDef: RuntimeClass,
332
+ keys: string[],
333
+ keyIndex: number,
334
+ ): ExecutionCommand => {
335
+ if (keyIndex >= keys.length) {
336
+ return evaluateArgs(0);
337
+ }
338
+ const key = keys[keyIndex];
339
+ if (collector.fieldInitializers.has(key)) {
340
+ return evaluateClassInitializers(classDef, keys, keyIndex + 1);
341
+ }
342
+ const expr = classInitializers.get(key);
343
+ return new BindCommand(this.evaluate(expr), (val) => {
344
+ runtimeObject.setField(key, val);
345
+ return evaluateClassInitializers(classDef, keys, keyIndex + 1);
346
+ });
347
+ };
348
+
349
+ let classInitializers: Map<string, any> = new Map();
350
+ let classInitializerKeys: string[] = [];
351
+ if (extendsSymbol) {
352
+ const classDef = this.context.lookup(extendsSymbol);
353
+ if (classDef && classDef instanceof RuntimeClass) {
354
+ classInitializers = getClassInitializers(classDef, this.context);
355
+ classInitializerKeys = Array.from(classInitializers.keys());
356
+ return evaluateClassInitializers(classDef, classInitializerKeys, 0);
357
+ }
358
+ }
359
+
360
+ return evaluateArgs(0);
361
+ }
362
+ visitLogicConstraint(node: LogicConstraint): ExecutionCommand {
363
+ return new BindCommand(this.evaluate(node.expression), (val) => {
364
+ if (isTrue(val))
365
+ return new StepCommand(
366
+ new LogicResult([new LogicAnswer(true, new Map())]),
367
+ );
368
+ return new BacktrackCommand();
369
+ });
370
+ }
371
+ };
372
+ }
@@ -0,0 +1,26 @@
1
+ import { Assert, Test, TestGroup, TestingVisitor } from "yukigo-ast";
2
+ import { ExecutionCommand } from "../kernel/commands.js";
3
+ import { Constructor, EvaluatorBase } from "./BaseEvaluator.js";
4
+ import { TestRunner } from "../TestRunner.js";
5
+
6
+ export function TestingEvaluator<TBase extends Constructor<EvaluatorBase>>(
7
+ Base: TBase,
8
+ ) {
9
+ return class
10
+ extends Base
11
+ implements Partial<TestingVisitor<ExecutionCommand>>
12
+ {
13
+ visitTestGroup(node: TestGroup): ExecutionCommand {
14
+ return new TestRunner(this, this.context.lazyRuntime).visitTestGroup(
15
+ node,
16
+ );
17
+ }
18
+ visitTest(node: Test): ExecutionCommand {
19
+ return new TestRunner(this, this.context.lazyRuntime).visitTest(node);
20
+ }
21
+
22
+ visitAssert(node: Assert): ExecutionCommand {
23
+ return new TestRunner(this, this.context.lazyRuntime).visitAssert(node);
24
+ }
25
+ };
26
+ }
@@ -0,0 +1,34 @@
1
+ import { ASTNode, Visitor } from "yukigo-ast";
2
+ import { EvaluatorBase } from "./BaseEvaluator.js";
3
+ import { ExpressionEvaluator } from "./ExpressionEvaluator.js";
4
+ import { OperationEvaluator } from "./OperationEvaluator.js";
5
+ import { PrimitiveEvaluator } from "./PrimitiveEvaluator.js";
6
+ import { StatementEvaluator } from "./StatementEvaluator.js";
7
+ import { TestingEvaluator } from "./TestingEvaluator.js";
8
+ import { EvalCommand, ExecutionCommand } from "../kernel/commands.js";
9
+ import { RuntimeContext } from "../RuntimeContext.js";
10
+ import { YuValue } from "../../primitives/index.js";
11
+ import { YukigoKernel } from "../kernel/index.js";
12
+
13
+ export class InterpreterVisitor
14
+ extends TestingEvaluator(
15
+ StatementEvaluator(
16
+ ExpressionEvaluator(
17
+ OperationEvaluator(PrimitiveEvaluator(EvaluatorBase)),
18
+ ),
19
+ ),
20
+ )
21
+ implements Visitor<ExecutionCommand>
22
+ {
23
+ constructor(context: RuntimeContext) {
24
+ super(context);
25
+ this.context.evaluatorFactory = (ctx) => new InterpreterVisitor(ctx);
26
+ }
27
+
28
+ static evaluateLiteral(node: ASTNode, ctx?: RuntimeContext): YuValue {
29
+ const targetCtx = ctx ?? new RuntimeContext();
30
+ const visitor = new InterpreterVisitor(targetCtx);
31
+ const kernel = new YukigoKernel(visitor);
32
+ return kernel.run(new EvalCommand(node));
33
+ }
34
+ }
@@ -0,0 +1,64 @@
1
+ import { RuntimeClass, YuValue } from "../../primitives/index.js";
2
+ import { LogicEngine } from "../logic/LogicEngine.js";
3
+ import { RuntimeContext } from "../RuntimeContext.js";
4
+ import { EvaluatorBase } from "./BaseEvaluator.js";
5
+
6
+ export const getLogicEngine = (
7
+ evaluator: EvaluatorBase,
8
+ ctx: RuntimeContext,
9
+ ): LogicEngine => new LogicEngine(evaluator, ctx);
10
+
11
+ export const getClassInitializers = (
12
+ classDef: RuntimeClass,
13
+ ctx: RuntimeContext,
14
+ ): Map<string, any> => {
15
+ const initializers = new Map<string, any>();
16
+ const collect = (c: RuntimeClass) => {
17
+ if (c.superclass) {
18
+ const superDef = ctx.lookup(c.superclass);
19
+ if (superDef && superDef instanceof RuntimeClass) {
20
+ collect(superDef);
21
+ }
22
+ }
23
+ for (const mixin of c.mixins) {
24
+ const mixinDef = ctx.lookup(mixin);
25
+ if (mixinDef && mixinDef instanceof RuntimeClass) {
26
+ collect(mixinDef);
27
+ }
28
+ }
29
+ const cInitializers = c.fieldInitializers;
30
+ if (cInitializers) {
31
+ for (const [k, v] of cInitializers.entries()) {
32
+ initializers.set(k, v);
33
+ }
34
+ }
35
+ };
36
+ collect(classDef);
37
+ return initializers;
38
+ };
39
+
40
+ export const getClassFields = (
41
+ classDef: RuntimeClass,
42
+ ctx: RuntimeContext,
43
+ ): Map<string, YuValue> => {
44
+ const fields = new Map<string, YuValue>();
45
+ const collect = (c: RuntimeClass) => {
46
+ if (c.superclass) {
47
+ const superDef = ctx.lookup(c.superclass);
48
+ if (superDef && superDef instanceof RuntimeClass) {
49
+ collect(superDef);
50
+ }
51
+ }
52
+ for (const mixin of c.mixins) {
53
+ const mixinDef = ctx.lookup(mixin);
54
+ if (mixinDef && mixinDef instanceof RuntimeClass) {
55
+ collect(mixinDef);
56
+ }
57
+ }
58
+ for (const [k, v] of c.fields.entries()) {
59
+ fields.set(k, v);
60
+ }
61
+ };
62
+ collect(classDef);
63
+ return fields;
64
+ };
@@ -0,0 +1,45 @@
1
+ import { InterpreterError } from "../../errors.js";
2
+ import { RuntimeContext } from "../RuntimeContext.js";
3
+ import { ASTNode, Test } from "yukigo-ast";
4
+ import { YuValue } from "../../primitives/YuValue.js";
5
+ import { Interpreter } from "../../index.js";
6
+
7
+ export interface YukigoHook {
8
+ /**
9
+ * Executed when an InterpreterError is raised, allows languages to extends error handling.
10
+ */
11
+ onInterpreterError?: (
12
+ error: InterpreterError,
13
+ ctx: RuntimeContext,
14
+ ) => Error | YuValue | void;
15
+
16
+ /**
17
+ * Executed before evaluating a node.
18
+ */
19
+ beforeNodeEvaluate?: (node: ASTNode, ctx: RuntimeContext) => void;
20
+
21
+ /**
22
+ * Executed after evaluating a nove.
23
+ */
24
+ afterNodeEvaluate?: (
25
+ node: ASTNode,
26
+ result: YuValue,
27
+ ctx: RuntimeContext,
28
+ ) => void;
29
+
30
+ /**
31
+ * Executed before dispatching a message to an object.
32
+ */
33
+ onMessageDispatch?: (
34
+ receiver: YuValue,
35
+ message: string,
36
+ args: YuValue[],
37
+ ctx: RuntimeContext,
38
+ ) => void;
39
+
40
+ /** Executed before each individual Test */
41
+ beforeEachTest?: (interpreter: Interpreter, testNode: Test) => void;
42
+
43
+ /** Executed after each individual Test */
44
+ afterEachTest?: (interpreter: Interpreter, testNode: Test) => void;
45
+ }