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,99 +1,115 @@
1
- import { isRuntimeObject, isRuntimeClass, } from "yukigo-ast";
2
1
  import { InterpreterError } from "../../errors.js";
2
+ import { isRuntimeObject, YuString, isRuntimeClass, YuBoolean, YuNil, } from "../../primitives/index.js";
3
+ import { error, raise } from "../../utils.js";
4
+ import { BindCommand, StepCommand, RaiseCommand, } from "../kernel/commands.js";
3
5
  export class ObjectRuntime {
4
6
  context;
5
7
  constructor(context) {
6
8
  this.context = context;
7
9
  }
8
- /**
9
- * Creates a new instance of an Object.
10
- * Typically called by visitNew()
11
- */
12
- instantiate(className, identifier, fieldDefinitions, methodDefinitions) {
13
- return {
14
- type: "Object",
15
- className,
16
- identifier,
17
- fields: new Map(fieldDefinitions),
18
- methods: methodDefinitions,
19
- };
20
- }
21
10
  /**
22
11
  * Handles Method Calls (Message Passing).
23
12
  * Reuses FunctionRuntime to execute the method body.
24
13
  */
25
- dispatch(receiver, methodName, args, env, k) {
26
- if (!isRuntimeObject(receiver))
27
- throw new Error(`${receiver} is not an object`);
28
- const chain = this.getResolutionChain(receiver, env);
29
- const match = this.findMethodInChain(chain, methodName);
30
- if (!match)
31
- throw new InterpreterError("MethodDispatch", `${receiver.className} does not understand '${methodName}'.`);
32
- const objectScope = this.createDispatchScope(receiver, match, methodName);
14
+ dispatch(receiver, methodName, args) {
15
+ if (!isRuntimeObject(receiver)) {
16
+ return this.dispatchPrimitive(receiver, methodName, args);
17
+ }
18
+ const chain = this.getResolutionChain(receiver);
19
+ const arity = args.length;
20
+ const arityKey = `${methodName}/${arity}`;
21
+ let match = this.findMethodInChain(chain, arityKey);
22
+ if (!match) {
23
+ match = this.findMethodInChain(chain, methodName);
24
+ }
25
+ if (!match) {
26
+ if (methodName === "toString") {
27
+ return new StepCommand(new YuString(receiver.toString()));
28
+ }
29
+ if (methodName === "error") {
30
+ const errorMsg = args[0] ? args[0].toString() : "An error occurred";
31
+ return new RaiseCommand(new InterpreterError("Raise", errorMsg));
32
+ }
33
+ return raise(error("MethodDispatch", `${receiver.className} does not understand '${methodName}'.`));
34
+ }
35
+ const objectScope = receiver.createDispatchScope(match, new YuString(methodName));
33
36
  this.context.pushEnv(objectScope);
34
- return this.context.funcRuntime.apply(match.method, args, k);
37
+ return new BindCommand(this.context.funcRuntime.apply(match.method, args), (res) => {
38
+ this.context.popEnv();
39
+ return new StepCommand(res);
40
+ });
35
41
  }
36
42
  /**
37
43
  * Handles calls to super() or super.method()
38
44
  */
39
- dispatchSuper(currentEnv, methodName, args, k) {
45
+ dispatchSuper(methodName, args) {
40
46
  const self = this.context.lookup("self");
41
47
  const currentHolder = this.context.lookup("__CONTEXT_CLASS__");
42
48
  const currentMethodName = this.context.lookup("__METHOD_NAME__");
43
- const targetMethodName = methodName || currentMethodName;
44
49
  if (!self || !currentHolder)
45
- throw new InterpreterError("SuperError", "'super' used outside of a method context");
46
- const chain = this.getResolutionChain(self, currentEnv);
50
+ return raise(error("dispatchSuper", "'super' used outside of a method context"));
51
+ const chain = this.getResolutionChain(self);
47
52
  const currentIndex = chain.findIndex((c) => c === currentHolder);
48
53
  if (currentIndex === -1)
49
- throw new Error("Fatal: Execution context not found in hierarchy chain");
54
+ return raise(error("dispatchSuper", "Execution context not found in hierarchy chain"));
50
55
  const remainingChain = chain.slice(currentIndex + 1);
51
- const match = this.findMethodInChain(remainingChain, methodName);
56
+ const baseName = methodName || currentMethodName.toJSON();
57
+ const arityKey = `${baseName}/${args.length}`;
58
+ let match = this.findMethodInChain(remainingChain, arityKey);
59
+ // Fallback: si no encuentra con aridad, buscar solo por nombre (para compatibilidad)
60
+ if (!match && methodName) {
61
+ match = this.findMethodInChain(remainingChain, methodName);
62
+ }
63
+ if (!match && !methodName) {
64
+ match = this.findMethodInChain(remainingChain, currentMethodName.toJSON());
65
+ }
52
66
  if (!match)
53
- throw new InterpreterError("Super", `Super method '${methodName}' not found`);
54
- const objectScope = this.createDispatchScope(self, match, targetMethodName);
67
+ return raise(error("Super", `Super method '${baseName}' not found`));
68
+ const objectScope = self.createDispatchScope(match, new YuString(baseName));
55
69
  this.context.pushEnv(objectScope);
56
- return this.context.funcRuntime.apply(match.method, args, k);
70
+ return new BindCommand(this.context.funcRuntime.apply(match.method, args), (res) => {
71
+ this.context.popEnv();
72
+ return new StepCommand(res);
73
+ });
57
74
  }
58
- createDispatchScope(self, match, targetName) {
59
- const objectScope = new Map();
60
- objectScope.set("self", self);
61
- objectScope.set("__CONTEXT_CLASS__", match.holder);
62
- objectScope.set("__METHOD_NAME__", targetName);
63
- for (const [key, val] of self.fields)
64
- objectScope.set(key, val);
65
- return objectScope;
75
+ getResolutionChain(receiver) {
76
+ const chain = [receiver];
77
+ if (receiver.className) {
78
+ const hierarchy = this.expandClassHierarchy(receiver.className);
79
+ chain.push(...hierarchy);
80
+ }
81
+ return chain;
66
82
  }
67
- getResolutionChain(receiver, env) {
83
+ expandClassHierarchy(className) {
68
84
  const chain = [];
69
- chain.push(receiver);
70
- if (receiver.className) {
71
- this.expandClassHierarchy(receiver.className, env, chain);
85
+ const visited = new Set();
86
+ const queue = [className];
87
+ while (queue.length > 0) {
88
+ const current = queue.shift();
89
+ // occurs check to avoid circular loops
90
+ if (visited.has(current))
91
+ continue;
92
+ visited.add(current);
93
+ const classDef = this.getClassDef(current);
94
+ chain.push(classDef);
95
+ queue.push(...classDef.getHierarchy());
72
96
  }
73
97
  return chain;
74
98
  }
75
- expandClassHierarchy(className, env, chain) {
76
- const classDef = this.context.lookup(className);
99
+ getClassDef(name) {
100
+ const classDef = this.context.lookup(name);
77
101
  if (!isRuntimeClass(classDef))
78
- throw new InterpreterError("expandClassHierarchy", "classDef was expected to be a RuntimeClass");
79
- chain.push(classDef);
80
- const classDefCopy = [...classDef.mixins];
81
- if (classDef.mixins)
82
- classDefCopy.reverse().forEach((mixinName) => {
83
- this.expandClassHierarchy(mixinName, env, chain);
84
- });
85
- if (classDef.superclass)
86
- this.expandClassHierarchy(classDef.superclass, env, chain);
102
+ throw error("expandClassHierarchy", "classDef was expected to be a RuntimeClass");
103
+ return classDef;
87
104
  }
88
105
  findMethodInChain(chain, methodName) {
89
- for (const link of chain) {
90
- if (link.methods.has(methodName))
91
- return {
92
- method: link.methods.get(methodName),
93
- holder: link,
94
- };
95
- }
96
- return undefined;
106
+ const match = chain.find((def) => def.methods.has(methodName));
107
+ if (!match)
108
+ return undefined;
109
+ return {
110
+ method: match.methods.get(methodName),
111
+ holder: match,
112
+ };
97
113
  }
98
114
  /**
99
115
  * Field Access (Get)
@@ -102,25 +118,40 @@ export class ObjectRuntime {
102
118
  getField(receiver, fieldName) {
103
119
  if (!isRuntimeObject(receiver))
104
120
  throw new InterpreterError("FieldAccess", "Target is not an object");
105
- if (!receiver.fields.has(fieldName)) {
106
- if (receiver.methods.has(fieldName))
107
- return receiver.methods.get(fieldName);
108
- throw new InterpreterError("FieldAccess", `Field '${fieldName}' not found in ${receiver.className}`);
109
- }
110
- return receiver.fields.get(fieldName);
121
+ if (receiver.hasMethod(fieldName))
122
+ return receiver.getMethod(fieldName);
123
+ return receiver.getField(fieldName);
111
124
  }
112
125
  /**
113
126
  * Field Mutation (Set)
114
127
  * e.g. self.myField = 10
115
128
  */
116
- setField(receiver, fieldName, value) {
129
+ setField(obj, fieldName, value) {
117
130
  if (!this.context.config.mutability)
118
131
  throw new InterpreterError("FieldAssignment", `Cannot mutate field '${fieldName}': mutability is disabled`);
132
+ const receiver = this.getReceiver(obj);
119
133
  if (!isRuntimeObject(receiver))
120
134
  throw new InterpreterError("FieldAssignment", "Target is not an object");
121
- if (!receiver.fields.has(fieldName))
122
- throw new InterpreterError("FieldAssignment", `Cannot set unknown field '${fieldName}'`);
123
- receiver.fields.set(fieldName, value);
124
- return value;
135
+ receiver.setField(fieldName, value);
136
+ return new YuBoolean(true);
137
+ }
138
+ getReceiver(obj) {
139
+ if (isRuntimeObject(obj) || isRuntimeClass(obj))
140
+ return obj;
141
+ throw new InterpreterError("ObjectRuntime", "Receiver is not an OOP entity");
142
+ }
143
+ dispatchPrimitive(receiver, methodName, args) {
144
+ if (!receiver || receiver.isNil)
145
+ return new StepCommand(YuNil.getInstance());
146
+ // Obtenemos el identificador polimórfico del tipo (ej: "YuArray", "YuNumber", "YuString", "RuntimeFunction")
147
+ const typeKey = receiver.constructor.name;
148
+ const lookupKey = `${typeKey}.${methodName}`;
149
+ // Buscamos si el ecosistema/lenguaje proveyó una implementación externa para este caso
150
+ const nativeImpl = this.context.config.nativeProviders.get(lookupKey);
151
+ if (nativeImpl) {
152
+ // Delegamos la subtarea de forma declarativa pasándole el receptor como contexto
153
+ return new BindCommand(nativeImpl(receiver, args, this.context), (res) => new StepCommand(res));
154
+ }
155
+ return raise(error("ObjectRuntime.dispatch", `Primitive type '${typeKey}' does not understand method '${methodName}'.`));
125
156
  }
126
157
  }
@@ -1,4 +1,5 @@
1
1
  import { SourceLocation } from "yukigo-ast";
2
+ import { YuValue } from "./primitives/YuValue.js";
2
3
  export interface ErrorFrame {
3
4
  nodeType: string;
4
5
  loc?: SourceLocation;
@@ -11,9 +12,18 @@ export declare class InterpreterError extends Error {
11
12
  formatStack(): string;
12
13
  toString(): string;
13
14
  }
15
+ export declare class UnexpectedNode extends InterpreterError {
16
+ constructor(nodeCons: string, context: string);
17
+ }
14
18
  export declare class UnexpectedValue extends InterpreterError {
15
19
  constructor(ctx: string, expected: string, got: string);
16
20
  }
17
21
  export declare class UnboundVariable extends Error {
18
22
  constructor(name: string);
19
23
  }
24
+ export declare class NotConcatenable extends InterpreterError {
25
+ constructor();
26
+ }
27
+ export declare class UnsupportedOperation extends InterpreterError {
28
+ constructor(val: YuValue, op: string);
29
+ }
@@ -5,6 +5,7 @@ export class InterpreterError extends Error {
5
5
  super(`[${context}] ${message}`);
6
6
  this.context = context;
7
7
  this.frames = frames;
8
+ this.name = "InterpreterError";
8
9
  }
9
10
  pushFrame(frame) {
10
11
  this.frames.push(frame);
@@ -24,6 +25,12 @@ export class InterpreterError extends Error {
24
25
  return `${this.message}${this.formatStack()}`;
25
26
  }
26
27
  }
28
+ export class UnexpectedNode extends InterpreterError {
29
+ constructor(nodeCons, context) {
30
+ super(context, `${nodeCons} not expected in ${context}.`);
31
+ this.name = "UnexpectedNode";
32
+ }
33
+ }
27
34
  export class UnexpectedValue extends InterpreterError {
28
35
  constructor(ctx, expected, got) {
29
36
  super(ctx, `Expected ${expected} but got ${got}`);
@@ -34,3 +41,13 @@ export class UnboundVariable extends Error {
34
41
  super(`Unbound variable: ${name}`);
35
42
  }
36
43
  }
44
+ export class NotConcatenable extends InterpreterError {
45
+ constructor() {
46
+ super("[Concat]", "Cannot concat non-concatenable types.");
47
+ }
48
+ }
49
+ export class UnsupportedOperation extends InterpreterError {
50
+ constructor(val, op) {
51
+ super("[Unsupported]", `${val.getType()} does not support '${op}'`);
52
+ }
53
+ }
@@ -1,6 +1,9 @@
1
- import { PrimitiveValue, AST, ASTNode } from "yukigo-ast";
2
- import { InterpreterConfig } from "./components/RuntimeContext.js";
3
- export type Bindings = [string, PrimitiveValue][];
1
+ import { AST, ASTNode } from "yukigo-ast";
2
+ import { RuntimeContext } from "./components/RuntimeContext.js";
3
+ import { YuValue } from "./primitives/index.js";
4
+ import { InterpreterConfig } from "../utils/helpers.js";
5
+ export * from "./primitives/index.js";
6
+ export type Bindings = [string, YuValue][];
4
7
  /**
5
8
  * The Interpreter class is responsible for evaluating the Abstract Syntax Tree (AST)
6
9
  * generated by the parsers.
@@ -13,12 +16,13 @@ export declare class Interpreter {
13
16
  /**
14
17
  * @param ast The Abstract Syntax Tree (AST) of the program to be interpreted.
15
18
  */
16
- constructor(ast: AST, config?: InterpreterConfig);
19
+ constructor(ast: AST, config?: Partial<InterpreterConfig>);
17
20
  /**
18
21
  * Evaluates a single Expression node within the context of the global environment.
19
22
  *
20
23
  * @param expr The root Expression node to be evaluated.
21
24
  * @returns The resulting primitive value (number, string, boolean, etc.) after evaluation.
22
25
  */
23
- evaluate(expr: ASTNode): PrimitiveValue;
26
+ evaluate(expr: ASTNode): YuValue;
27
+ getContext(): RuntimeContext;
24
28
  }
@@ -1,8 +1,9 @@
1
- import { InterpreterVisitor } from "./components/Visitor.js";
2
1
  import { EnvBuilderVisitor } from "./components/EnvBuilder.js";
3
- import { InterpreterError } from "./errors.js";
4
- import { idContinuation, trampoline } from "./trampoline.js";
5
2
  import { RuntimeContext, } from "./components/RuntimeContext.js";
3
+ import { YukigoKernel } from "./components/kernel/index.js";
4
+ import { EvalCommand } from "./components/kernel/commands.js";
5
+ import { InterpreterVisitor } from "./components/evaluators/index.js";
6
+ export * from "./primitives/index.js";
6
7
  /**
7
8
  * The Interpreter class is responsible for evaluating the Abstract Syntax Tree (AST)
8
9
  * generated by the parsers.
@@ -27,15 +28,12 @@ export class Interpreter {
27
28
  * @returns The resulting primitive value (number, string, boolean, etc.) after evaluation.
28
29
  */
29
30
  evaluate(expr) {
30
- try {
31
- const visitor = new InterpreterVisitor(this.context);
32
- const evaluatedCPS = expr.accept(visitor);
33
- return trampoline(evaluatedCPS(idContinuation));
34
- }
35
- catch (error) {
36
- if (error instanceof InterpreterError)
37
- console.log(error.formatStack());
38
- throw error;
39
- }
31
+ const visitor = new InterpreterVisitor(this.context);
32
+ const outputMode = this.context.config.outputMode;
33
+ const kernel = new YukigoKernel(visitor, outputMode);
34
+ return kernel.run(new EvalCommand(expr));
35
+ }
36
+ getContext() {
37
+ return this.context;
40
38
  }
41
39
  }
@@ -0,0 +1,21 @@
1
+ import { ExecutionCommand } from "../components/kernel/commands.js";
2
+ import { YuNumeric, YuSummable, YuSequence, YuLogic, YuComparable, YuStepResult } from "./capabilities.js";
3
+ /**
4
+ * Base class for all Yukigo values.
5
+ */
6
+ export declare abstract class YuValue {
7
+ abstract toString(seen?: Set<YuValue>): string;
8
+ abstract toJSON(keyOrSeen?: string | Set<YuValue>): unknown;
9
+ getType(): string;
10
+ realize(): ExecutionCommand;
11
+ get asSummable(): YuSummable | undefined;
12
+ get asNumeric(): YuNumeric | undefined;
13
+ get asSequence(): YuSequence | undefined;
14
+ get asLogic(): YuLogic | undefined;
15
+ get asComparable(): YuComparable | undefined;
16
+ get asStepResult(): YuStepResult | undefined;
17
+ get isNil(): boolean;
18
+ abstract equals(other: YuValue): ExecutionCommand;
19
+ abstract compare(other: YuValue): ExecutionCommand;
20
+ isSame(other: YuValue): ExecutionCommand;
21
+ }
@@ -0,0 +1,40 @@
1
+ import { StepCommand, } from "../components/kernel/commands.js";
2
+ import { boolean } from "../utils.js";
3
+ /**
4
+ * Base class for all Yukigo values.
5
+ */
6
+ export class YuValue {
7
+ getType() {
8
+ return this.constructor.name.replace("Yu", "");
9
+ }
10
+ realize() {
11
+ return new StepCommand(this);
12
+ }
13
+ // Capability Accessors (Delegation)
14
+ get asSummable() {
15
+ return undefined;
16
+ }
17
+ get asNumeric() {
18
+ return undefined;
19
+ }
20
+ get asSequence() {
21
+ return undefined;
22
+ }
23
+ get asLogic() {
24
+ return undefined;
25
+ }
26
+ get asComparable() {
27
+ return undefined;
28
+ }
29
+ get asStepResult() {
30
+ return undefined;
31
+ }
32
+ get isNil() {
33
+ return false;
34
+ }
35
+ isSame(other) {
36
+ if (this.getType() !== other.getType())
37
+ return boolean(false);
38
+ return this.equals(other);
39
+ }
40
+ }
@@ -0,0 +1,90 @@
1
+ import { ExecutionCommand } from "../components/kernel/commands.js";
2
+ import { YuValue } from "./YuValue.js";
3
+ /**
4
+ * Capability for addition and concatenation.
5
+ */
6
+ export interface Summable {
7
+ plus(other: YuValue): ExecutionCommand;
8
+ plusWithNumber(left: YuValue): ExecutionCommand;
9
+ plusWithString(left: YuValue): ExecutionCommand;
10
+ }
11
+ export type YuSummable = YuValue & Summable;
12
+ /**
13
+ * Capability for numeric operations.
14
+ */
15
+ export interface Numeric extends Summable {
16
+ readonly value: number;
17
+ minus(other: YuValue): ExecutionCommand;
18
+ multiply(other: YuValue): ExecutionCommand;
19
+ divide(other: YuValue): ExecutionCommand;
20
+ modulo(other: YuValue): ExecutionCommand;
21
+ power(other: YuValue): ExecutionCommand;
22
+ min(other: YuValue): ExecutionCommand;
23
+ max(other: YuValue): ExecutionCommand;
24
+ round(): ExecutionCommand;
25
+ abs(): ExecutionCommand;
26
+ ceil(): ExecutionCommand;
27
+ floor(): ExecutionCommand;
28
+ negation(): ExecutionCommand;
29
+ sqrt(): ExecutionCommand;
30
+ minusWithNumber(left: YuValue): ExecutionCommand;
31
+ multiplyWithNumber(left: YuValue): ExecutionCommand;
32
+ divideWithNumber(left: YuValue): ExecutionCommand;
33
+ moduloWithNumber(left: YuValue): ExecutionCommand;
34
+ powerWithNumber(left: YuValue): ExecutionCommand;
35
+ minWithNumber(left: YuValue): ExecutionCommand;
36
+ maxWithNumber(left: YuValue): ExecutionCommand;
37
+ }
38
+ export type YuNumeric = YuValue & Numeric;
39
+ /**
40
+ * Capability for sequences.
41
+ */
42
+ export interface Sequence {
43
+ step(): ExecutionCommand;
44
+ realize(): ExecutionCommand;
45
+ size(): ExecutionCommand;
46
+ concat(other: YuValue): ExecutionCommand;
47
+ concatWithArray(arr: YuValue): ExecutionCommand;
48
+ concatWithString(str: YuValue): ExecutionCommand;
49
+ [Symbol.iterator](): Iterator<YuValue>;
50
+ getType(): string;
51
+ toJSON(): unknown;
52
+ readonly isNil: boolean;
53
+ readonly asStepResult?: StepResult;
54
+ }
55
+ export type YuSequence = YuValue & Sequence;
56
+ /**
57
+ * Capability for logical operations.
58
+ */
59
+ export interface Logic {
60
+ readonly value: boolean;
61
+ not(): ExecutionCommand;
62
+ toJSON(): unknown;
63
+ }
64
+ export type YuLogic = YuValue & Logic;
65
+ /**
66
+ * Capability for comparisons.
67
+ */
68
+ export interface Comparable {
69
+ equals(other: YuValue): ExecutionCommand;
70
+ compare(other: YuValue): ExecutionCommand;
71
+ equalsWithNumber(left: YuValue): ExecutionCommand;
72
+ equalsWithBoolean(left: YuValue): ExecutionCommand;
73
+ equalsWithString(left: YuValue): ExecutionCommand;
74
+ equalsWithArray(left: YuValue): ExecutionCommand;
75
+ equalsWithNil(left: YuValue): ExecutionCommand;
76
+ compareWithNumber(left: YuValue): ExecutionCommand;
77
+ compareWithBoolean(left: YuValue): ExecutionCommand;
78
+ compareWithString(left: YuValue): ExecutionCommand;
79
+ compareWithArray(left: YuValue): ExecutionCommand;
80
+ compareWithNil(left: YuValue): ExecutionCommand;
81
+ }
82
+ export type YuComparable = YuValue & Comparable;
83
+ /**
84
+ * Result of stepping a sequence.
85
+ */
86
+ export interface StepResult {
87
+ readonly head: YuValue;
88
+ readonly tail: YuSequence | null;
89
+ }
90
+ export type YuStepResult = YuValue & StepResult;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,48 @@
1
+ import { ExecutionCommand } from "../../components/kernel/commands.js";
2
+ import { RuntimeContext } from "../../components/RuntimeContext.js";
3
+ import { Sequence, StepResult, YuSequence, YuStepResult } from "../capabilities.js";
4
+ import { YuValue } from "../YuValue.js";
5
+ /**
6
+ * Represents the result of forcing one step of a LazyList.
7
+ * If null, the list is empty.
8
+ */
9
+ export declare class LazyStepResult extends YuValue implements StepResult {
10
+ readonly head: YuValue;
11
+ readonly tail: YuSequence | null;
12
+ constructor(head: YuValue, tail: YuSequence | null);
13
+ get asStepResult(): YuStepResult;
14
+ equals(other: YuValue): ExecutionCommand;
15
+ compare(other: YuValue): ExecutionCommand;
16
+ toString(): string;
17
+ toJSON(): unknown;
18
+ }
19
+ /**
20
+ * A LazyList represents a potentially infinite sequence of values evaluated on demand.
21
+ */
22
+ export declare class LazyList extends YuValue implements Sequence {
23
+ private readonly producer;
24
+ readonly identifier: string;
25
+ readonly capturedContext?: RuntimeContext | undefined;
26
+ private memoized;
27
+ constructor(producer: (ctx?: RuntimeContext) => ExecutionCommand, identifier?: string, capturedContext?: RuntimeContext | undefined);
28
+ equals(other: YuValue): ExecutionCommand;
29
+ compare(other: YuValue): ExecutionCommand;
30
+ get asSequence(): YuSequence;
31
+ [Symbol.iterator](): Iterator<YuValue>;
32
+ /**
33
+ * Evaluates the first element of the list and returns it along with the tail.
34
+ */
35
+ step(): ExecutionCommand;
36
+ size(): ExecutionCommand;
37
+ realize(): ExecutionCommand;
38
+ concat(other: YuValue): ExecutionCommand;
39
+ concatWithArray(arr: YuValue): ExecutionCommand;
40
+ concatWithString(str: YuValue): ExecutionCommand;
41
+ private createLazyConcat;
42
+ toJSON(): {
43
+ identifier: string;
44
+ memoized: unknown;
45
+ };
46
+ toString(): string;
47
+ }
48
+ export declare function isLazyList(prim: unknown): prim is LazyList;