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
@@ -0,0 +1,70 @@
1
+ import {
2
+ ExecutionCommand,
3
+ StepCommand,
4
+ } from "../../components/kernel/commands.js";
5
+ import { InterpreterError } from "../../errors.js";
6
+ import { boolean, error, raise } from "../../utils.js";
7
+ import { YuBoolean } from "../index.js";
8
+ import { YuValue } from "../YuValue.js";
9
+ import { RuntimeFunction } from "./RuntimeFunction.js";
10
+ import { RuntimeObject } from "./RuntimeObject.js";
11
+
12
+ export class RuntimeClass extends YuValue {
13
+ constructor(
14
+ public identifier: string,
15
+ public fields: Map<string, YuValue>,
16
+ public methods: Map<string, RuntimeFunction>,
17
+ public mixins: string[],
18
+ public superclass?: string,
19
+ public fieldInitializers?: Map<string, any>,
20
+ private isAbstract: boolean = false,
21
+ ) {
22
+ super();
23
+ }
24
+
25
+ public equals(other: YuValue): ExecutionCommand {
26
+ return boolean(other === this);
27
+ }
28
+ public compare(other: YuValue): ExecutionCommand {
29
+ return raise(error("RuntimeClass.compare", "Classes are not comparable"));
30
+ }
31
+
32
+ public getHierarchy(): string[] {
33
+ const hierarchy = [...this.mixins.reverse()];
34
+ if (this.superclass) hierarchy.push(this.superclass);
35
+ return hierarchy;
36
+ }
37
+ /**
38
+ * Creates a new instance of an Object.
39
+ */
40
+ public instantiate(identifier: string): RuntimeObject {
41
+ if (this.isAbstract)
42
+ throw new InterpreterError(
43
+ "[ObjectRuntime]",
44
+ `Cannot instantiate abstract class ${this.identifier}`,
45
+ );
46
+ return new RuntimeObject(
47
+ identifier,
48
+ this.identifier,
49
+ new Map(this.fields),
50
+ new Map(),
51
+ );
52
+ }
53
+
54
+ public toJSON(): unknown {
55
+ return {
56
+ identifier: this.identifier,
57
+ superclass: this.superclass,
58
+ mixins: this.mixins,
59
+ isAbstract: this.isAbstract,
60
+ };
61
+ }
62
+
63
+ public toString(): string {
64
+ return `[Class: ${this.identifier}]`;
65
+ }
66
+ }
67
+
68
+ export function isRuntimeClass(val: YuValue): val is RuntimeClass {
69
+ return val instanceof RuntimeClass;
70
+ }
@@ -0,0 +1,108 @@
1
+ import { Body, Pattern } from "yukigo-ast";
2
+ import { YuValue } from "../YuValue.js";
3
+ import {
4
+ boolean,
5
+ EnvStack,
6
+ error,
7
+ PrimitiveThunk,
8
+ raise,
9
+ } from "../../utils.js";
10
+ import { ExecutionCommand } from "../../components/kernel/commands.js";
11
+
12
+ export interface EquationRuntime {
13
+ patterns: Pattern[];
14
+ body: Body;
15
+ }
16
+ /**
17
+ * Runtime Function used in the Interpreter
18
+ */
19
+ export class RuntimeFunction extends YuValue {
20
+ constructor(
21
+ public arity: number,
22
+ public equations: EquationRuntime[],
23
+ public identifier?: string,
24
+ public pendingArgs?: (YuValue | PrimitiveThunk)[],
25
+ public closure?: EnvStack,
26
+ ) {
27
+ super();
28
+ }
29
+
30
+ public equals(other: YuValue): ExecutionCommand {
31
+ return boolean(other === this);
32
+ }
33
+ public compare(other: YuValue): ExecutionCommand {
34
+ return raise(
35
+ error("RuntimeFunction.compare", "Functions are not comparable"),
36
+ );
37
+ }
38
+
39
+ public get name(): string {
40
+ return this.identifier ?? "<anonymous>";
41
+ }
42
+
43
+ public get remainingArity(): number {
44
+ return this.arity - (this.pendingArgs?.length ?? 0);
45
+ }
46
+
47
+ public get isFullyApplied(): boolean {
48
+ return this.remainingArity <= 0;
49
+ }
50
+
51
+ public get hasPendingArgs(): boolean {
52
+ return (this.pendingArgs?.length ?? 0) > 0;
53
+ }
54
+
55
+ /**
56
+ * Partially applies the function with new arguments.
57
+ */
58
+ public bind(...args: (YuValue | PrimitiveThunk)[]): RuntimeFunction {
59
+ return new RuntimeFunction(
60
+ this.arity,
61
+ this.equations,
62
+ this.identifier,
63
+ [...(this.pendingArgs ?? []), ...args],
64
+ this.closure,
65
+ );
66
+ }
67
+
68
+ /**
69
+ * Returns a new RuntimeFunction with the given closure.
70
+ */
71
+ public withClosure(closure: EnvStack): RuntimeFunction {
72
+ return new RuntimeFunction(
73
+ this.arity,
74
+ this.equations,
75
+ this.identifier,
76
+ this.pendingArgs,
77
+ closure,
78
+ );
79
+ }
80
+
81
+ /**
82
+ * Evaluates all pending arguments (resolving thunks).
83
+ */
84
+ public getEvaluatedArgs(): YuValue[] {
85
+ return (this.pendingArgs ?? []).map((arg) =>
86
+ typeof arg === "function" ? arg() : arg,
87
+ );
88
+ }
89
+
90
+ public toJSON(): unknown {
91
+ return {
92
+ name: this.name,
93
+ arity: this.arity,
94
+ remainingArity: this.remainingArity,
95
+ };
96
+ }
97
+
98
+ public toString(): string {
99
+ const arityInfo = this.arity > 0 ? `/${this.arity}` : "";
100
+ const pendingInfo = this.hasPendingArgs
101
+ ? ` (applied ${this.pendingArgs!.length})`
102
+ : "";
103
+ return `[Function: ${this.name}${arityInfo}${pendingInfo}]`;
104
+ }
105
+ }
106
+ export function isRuntimeFunction(val: YuValue): val is RuntimeFunction {
107
+ return val instanceof RuntimeFunction;
108
+ }
@@ -0,0 +1,110 @@
1
+ import {
2
+ ExecutionCommand,
3
+ StepCommand,
4
+ } from "../../components/kernel/commands.js";
5
+ import { InterpreterError } from "../../errors.js";
6
+ import { boolean, error, raise } from "../../utils.js";
7
+ import { YuBoolean } from "../index.js";
8
+ import { YuValue } from "../YuValue.js";
9
+ import { RuntimeClass } from "./RuntimeClass.js";
10
+ import { RuntimeFunction } from "./RuntimeFunction.js";
11
+
12
+ type OOPMatch = {
13
+ method: RuntimeFunction;
14
+ holder: RuntimeObject | RuntimeClass;
15
+ };
16
+
17
+ export class RuntimeObject extends YuValue {
18
+ constructor(
19
+ public identifier: string,
20
+ public className: string,
21
+ public fields: Map<string, YuValue>,
22
+ public methods: Map<string, RuntimeFunction>,
23
+ ) {
24
+ super();
25
+ }
26
+
27
+ public equals(other: YuValue): ExecutionCommand {
28
+ return boolean(other === this);
29
+ }
30
+ public compare(other: YuValue): ExecutionCommand {
31
+ return raise(error("RuntimeClass.compare", "Objects are not comparable"));
32
+ }
33
+
34
+ public hasField(name: string): boolean {
35
+ return this.fields.has(name);
36
+ }
37
+
38
+ public getField(name: string): YuValue {
39
+ if (!this.hasField(name))
40
+ throw new InterpreterError(
41
+ "[ObjectRuntime]",
42
+ `Field '${name}' not found in ${this.className}`,
43
+ );
44
+ return this.fields.get(name)!;
45
+ }
46
+
47
+ public setField(name: string, value: YuValue): void {
48
+ if (!this.hasField(name))
49
+ throw new InterpreterError(
50
+ "[ObjectRuntime]",
51
+ `Cannot set unknown field '${name}'`,
52
+ );
53
+ this.fields.set(name, value);
54
+ }
55
+
56
+ public getMethod(name: string): RuntimeFunction {
57
+ if (!this.hasMethod(name))
58
+ throw new InterpreterError(
59
+ "[ObjectRuntime]",
60
+ `${this.className} does not understand '${name}`,
61
+ );
62
+ return this.methods.get(name)!;
63
+ }
64
+ public hasMethod(name: string): boolean {
65
+ return this.methods.has(name);
66
+ }
67
+ public createDispatchScope(match: OOPMatch, targetName: YuValue) {
68
+ const objectScope = new Map<string, YuValue>();
69
+
70
+ objectScope.set("self", this);
71
+ objectScope.set("__CONTEXT_CLASS__", match.holder);
72
+ objectScope.set("__METHOD_NAME__", targetName);
73
+ for (const [key, val] of this.fields) objectScope.set(key, val);
74
+
75
+ return objectScope;
76
+ }
77
+
78
+ public toJSON(keyOrSeen?: string | Set<YuValue>): unknown {
79
+ const seen = keyOrSeen instanceof Set ? keyOrSeen : new Set<YuValue>();
80
+ if (seen.has(this)) return { identifier: this.identifier, circular: true };
81
+ seen.add(this);
82
+
83
+ const fieldsJSON: Record<string, unknown> = {};
84
+ for (const [key, val] of this.fields) fieldsJSON[key] = val.toJSON(seen);
85
+
86
+ seen.delete(this);
87
+ return {
88
+ identifier: this.identifier,
89
+ className: this.className,
90
+ fields: fieldsJSON,
91
+ };
92
+ }
93
+
94
+ public toString(seen = new Set<YuValue>()): string {
95
+ if (seen.has(this)) return this.identifier || this.className || "object (circular)";
96
+ seen.add(this);
97
+
98
+ const fieldsArray: string[] = [];
99
+ for (const [k, v] of this.fields.entries()) {
100
+ fieldsArray.push(`${k}=${v ? v.toString(seen) : "nil"}`);
101
+ }
102
+ const name = this.identifier || this.className || "object";
103
+ seen.delete(this);
104
+ return `${name}[${fieldsArray.join(", ")}]`;
105
+ }
106
+ }
107
+
108
+ export function isRuntimeObject(val: YuValue): val is RuntimeObject {
109
+ return val instanceof RuntimeObject;
110
+ }
@@ -0,0 +1,77 @@
1
+ import { Fact, Rule, Visitor } from "yukigo-ast";
2
+ import { InterpreterError } from "../../errors.js";
3
+ import { YuValue } from "../YuValue.js";
4
+ import {
5
+ ExecutionCommand,
6
+ StepCommand,
7
+ } from "../../components/kernel/commands.js";
8
+ import { YuBoolean } from "../index.js";
9
+ import { boolean, error, raise } from "../../utils.js";
10
+
11
+ export class RuntimePredicate extends YuValue {
12
+ public readonly arity: number;
13
+ constructor(
14
+ public identifier: string,
15
+ public equations: (Fact | Rule)[],
16
+ ) {
17
+ super();
18
+ if (equations.length === 0) {
19
+ throw new InterpreterError(
20
+ "RuntimePredicate",
21
+ `Predicate "${identifier}" must have at least one clause.`,
22
+ );
23
+ }
24
+ this.arity = this.getArityFromClause(equations[0]);
25
+ }
26
+
27
+ public equals(other: YuValue): ExecutionCommand {
28
+ return boolean(other === this);
29
+ }
30
+ public compare(other: YuValue): ExecutionCommand {
31
+ return raise(
32
+ error("RuntimePredicate.compare", "Predicates are not comparable"),
33
+ );
34
+ }
35
+
36
+ public apply<T>(visitor: Visitor<T>): T[] {
37
+ return this.equations.map((clause) => clause.accept(visitor));
38
+ }
39
+ public addClause(clause: Fact | Rule) {
40
+ const clauseArity = this.getArityFromClause(clause);
41
+ if (!this.validateClause(clause)) {
42
+ throw new InterpreterError(
43
+ "RuntimePredicate",
44
+ `Arity mismatch for "${this.identifier}": expected ${this.arity}, got ${clauseArity}.`,
45
+ );
46
+ }
47
+ this.equations.push(clause);
48
+ }
49
+ private validateClause(clause: Fact | Rule): boolean {
50
+ const clauseArity = this.getArityFromClause(clause);
51
+ return this.validateArity(clauseArity);
52
+ }
53
+ public validateArity(args: number): boolean {
54
+ return this.arity === args;
55
+ }
56
+ private getArityFromClause(clause: Fact | Rule): number {
57
+ if (clause.is(Fact)) return clause.patterns.length;
58
+ const patterns =
59
+ clause.equations.length > 0 ? clause.equations[0].patterns : [];
60
+ return patterns.length;
61
+ }
62
+
63
+ public toJSON(): unknown {
64
+ return {
65
+ identifier: this.identifier,
66
+ arity: this.arity,
67
+ };
68
+ }
69
+
70
+ public toString() {
71
+ return `[Predicate: ${this.identifier}/${this.arity}]`;
72
+ }
73
+ }
74
+
75
+ export const isRuntimePredicate = (prim: unknown): prim is RuntimePredicate => {
76
+ return prim instanceof RuntimePredicate;
77
+ };
@@ -0,0 +1,17 @@
1
+ export * from "./YuValue.js";
2
+ export * from "./capabilities.js";
3
+
4
+ export { YuBoolean } from "./scalars/YuBoolean.js";
5
+ export { YuNil } from "./scalars/YuNil.js";
6
+ export { YuNumber } from "./scalars/YuNumber.js";
7
+
8
+ export { YuArray } from "./sequences/YuArray.js";
9
+ export { YuChar } from "./scalars/YuChar.js";
10
+ export { YuString } from "./sequences/YuString.js";
11
+
12
+ export * from "./entities/LazyList.js";
13
+ export * from "./entities/LogicResult.js";
14
+ export * from "./entities/RuntimeClass.js";
15
+ export * from "./entities/RuntimeFunction.js";
16
+ export * from "./entities/RuntimeObject.js";
17
+ export * from "./entities/RuntimePredicate.js";
@@ -0,0 +1,79 @@
1
+ import {
2
+ ExecutionCommand,
3
+ StepCommand,
4
+ } from "../../components/kernel/commands.js";
5
+ import { UnsupportedOperation } from "../../errors.js";
6
+ import { boolean, raise } from "../../utils.js";
7
+ import { Comparable, Logic, YuComparable, YuLogic } from "../capabilities.js";
8
+ import { YuValue } from "../YuValue.js";
9
+ import { YuNumber } from "./YuNumber.js";
10
+
11
+ export class YuBoolean extends YuValue implements Logic, Comparable {
12
+ constructor(public readonly value: boolean) {
13
+ super();
14
+ }
15
+
16
+ get asLogic(): YuLogic {
17
+ return this;
18
+ }
19
+ get asComparable(): YuComparable {
20
+ return this;
21
+ }
22
+
23
+ public not(): ExecutionCommand {
24
+ return boolean(!this.value);
25
+ }
26
+
27
+ public equals(other: YuValue): ExecutionCommand {
28
+ const c = other.asComparable;
29
+ if (!c) return boolean(false);
30
+ return c.equalsWithBoolean(this);
31
+ }
32
+
33
+ public equalsWithNumber(): ExecutionCommand {
34
+ return boolean(false);
35
+ }
36
+ public equalsWithBoolean(left: YuBoolean): ExecutionCommand {
37
+ return boolean(left.value === this.value);
38
+ }
39
+ public equalsWithString(): ExecutionCommand {
40
+ return boolean(false);
41
+ }
42
+ public equalsWithArray(): ExecutionCommand {
43
+ return boolean(false);
44
+ }
45
+ public equalsWithNil(): ExecutionCommand {
46
+ return boolean(false);
47
+ }
48
+
49
+ public compare(other: YuValue): ExecutionCommand {
50
+ const c = other.asComparable;
51
+ if (!c) return raise(new UnsupportedOperation(other, "compare"));
52
+ return c.compareWithBoolean(this);
53
+ }
54
+
55
+ public compareWithNumber(): ExecutionCommand {
56
+ return raise(new UnsupportedOperation(this, "compare"));
57
+ }
58
+ public compareWithBoolean(left: YuBoolean): ExecutionCommand {
59
+ return new StepCommand(
60
+ new YuNumber((left.value ? 1 : 0) - (this.value ? 1 : 0)),
61
+ );
62
+ }
63
+ public compareWithString(): ExecutionCommand {
64
+ return raise(new UnsupportedOperation(this, "compare"));
65
+ }
66
+ public compareWithArray(): ExecutionCommand {
67
+ return raise(new UnsupportedOperation(this, "compare"));
68
+ }
69
+ public compareWithNil(): ExecutionCommand {
70
+ return raise(new UnsupportedOperation(this, "compare"));
71
+ }
72
+
73
+ public toString(): string {
74
+ return this.value.toString();
75
+ }
76
+ public toJSON(): boolean {
77
+ return this.value;
78
+ }
79
+ }
@@ -0,0 +1,94 @@
1
+ import {
2
+ ExecutionCommand,
3
+ StepCommand,
4
+ } from "../../components/kernel/commands.js";
5
+ import { UnsupportedOperation } from "../../errors.js";
6
+ import { boolean, raise } from "../../utils.js";
7
+ import {
8
+ Comparable,
9
+ Summable,
10
+ YuComparable,
11
+ YuSummable,
12
+ } from "../capabilities.js";
13
+ import { YuString } from "../sequences/YuString.js";
14
+ import { YuValue } from "../YuValue.js";
15
+ import { YuBoolean } from "./YuBoolean.js";
16
+ import { YuNumber } from "./YuNumber.js";
17
+
18
+ export class YuChar extends YuValue implements Comparable, Summable {
19
+ constructor(public readonly value: string) {
20
+ super();
21
+ if (value.length !== 1)
22
+ throw new Error("YuChar must be a single character");
23
+ }
24
+
25
+ get asSummable(): YuSummable {
26
+ return this;
27
+ }
28
+ get asComparable(): YuComparable {
29
+ return this;
30
+ }
31
+
32
+ public equals(other: YuValue): ExecutionCommand {
33
+ const c = other.asComparable;
34
+ if (!c) return boolean(false);
35
+ return c.equalsWithString(this);
36
+ }
37
+
38
+ public equalsWithNumber(): ExecutionCommand {
39
+ return boolean(false);
40
+ }
41
+ public equalsWithBoolean(): ExecutionCommand {
42
+ return boolean(false);
43
+ }
44
+ public equalsWithString(left: YuString): ExecutionCommand {
45
+ return boolean(left.toJSON() === this.value);
46
+ }
47
+ public equalsWithArray(): ExecutionCommand {
48
+ return boolean(false);
49
+ }
50
+ public equalsWithNil(): ExecutionCommand {
51
+ return boolean(false);
52
+ }
53
+
54
+ public compare(other: YuValue): ExecutionCommand {
55
+ const c = other.asComparable;
56
+ if (!c) return raise(new UnsupportedOperation(other, "compare"));
57
+ return c.compareWithString(new YuString(this.value));
58
+ }
59
+
60
+ public compareWithNumber(): ExecutionCommand {
61
+ return raise(new UnsupportedOperation(this, "compare"));
62
+ }
63
+ public compareWithBoolean(): ExecutionCommand {
64
+ return raise(new UnsupportedOperation(this, "compare"));
65
+ }
66
+ public compareWithString(left: YuString): ExecutionCommand {
67
+ return new StepCommand(
68
+ new YuNumber(left.toJSON().localeCompare(this.value)),
69
+ );
70
+ }
71
+ public compareWithArray(): ExecutionCommand {
72
+ return raise(new UnsupportedOperation(this, "compare"));
73
+ }
74
+ public compareWithNil(): ExecutionCommand {
75
+ return raise(new UnsupportedOperation(this, "compare"));
76
+ }
77
+
78
+ public plus(other: YuValue): ExecutionCommand {
79
+ return new StepCommand(new YuString(this.value + other.toString()));
80
+ }
81
+ public plusWithNumber(left: YuNumber): ExecutionCommand {
82
+ return new StepCommand(new YuString(left.toJSON().toString() + this.value));
83
+ }
84
+ public plusWithString(left: YuString): ExecutionCommand {
85
+ return new StepCommand(new YuString(left.toJSON() + this.value));
86
+ }
87
+
88
+ public toString(): string {
89
+ return this.value;
90
+ }
91
+ public toJSON(): string {
92
+ return this.value;
93
+ }
94
+ }
@@ -0,0 +1,84 @@
1
+ import {
2
+ ExecutionCommand,
3
+ StepCommand,
4
+ } from "../../components/kernel/commands.js";
5
+ import { UnsupportedOperation } from "../../errors.js";
6
+ import { boolean, raise } from "../../utils.js";
7
+ import { Comparable, YuComparable } from "../capabilities.js";
8
+ import { YuValue } from "../YuValue.js";
9
+ import { YuBoolean } from "./YuBoolean.js";
10
+ import { YuNumber } from "./YuNumber.js";
11
+
12
+ export class YuNil extends YuValue implements Comparable {
13
+ private static instance: YuNil;
14
+ private constructor() {
15
+ super();
16
+ }
17
+ public static getInstance(): YuNil {
18
+ if (!YuNil.instance) YuNil.instance = new YuNil();
19
+ return YuNil.instance;
20
+ }
21
+
22
+ get isNil(): boolean {
23
+ return true;
24
+ }
25
+
26
+ get asComparable(): YuComparable {
27
+ return this;
28
+ }
29
+
30
+ public equals(other: YuValue): ExecutionCommand {
31
+ const c = other.asComparable;
32
+ if (!c) return boolean(false);
33
+ return c.equalsWithNil(this);
34
+ }
35
+
36
+ public equalsWithNumber(): ExecutionCommand {
37
+ return boolean(false);
38
+ }
39
+ public equalsWithBoolean(): ExecutionCommand {
40
+ return boolean(false);
41
+ }
42
+ public equalsWithString(): ExecutionCommand {
43
+ return boolean(false);
44
+ }
45
+ public equalsWithArray(): ExecutionCommand {
46
+ return boolean(false);
47
+ }
48
+ public equalsWithNil(left: YuNil): ExecutionCommand {
49
+ return boolean(true);
50
+ }
51
+
52
+ public compare(other: YuValue): ExecutionCommand {
53
+ const c = other.asComparable;
54
+ if (!c) return raise(new UnsupportedOperation(other, "compare"));
55
+ return c.compareWithNil(this);
56
+ }
57
+
58
+ public compareWithNumber(): ExecutionCommand {
59
+ return this.unsupportedCompare();
60
+ }
61
+ public compareWithBoolean(): ExecutionCommand {
62
+ return this.unsupportedCompare();
63
+ }
64
+ public compareWithString(): ExecutionCommand {
65
+ return this.unsupportedCompare();
66
+ }
67
+ public compareWithArray(): ExecutionCommand {
68
+ return this.unsupportedCompare();
69
+ }
70
+ public compareWithNil(): ExecutionCommand {
71
+ return new StepCommand(new YuNumber(0));
72
+ }
73
+
74
+ private unsupportedCompare(): ExecutionCommand {
75
+ return raise(new UnsupportedOperation(this, "compare"));
76
+ }
77
+
78
+ public toString(): string {
79
+ return "nil";
80
+ }
81
+ public toJSON(): null {
82
+ return null;
83
+ }
84
+ }