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,187 @@
1
+ import {
2
+ ExecutionCommand,
3
+ StepCommand,
4
+ } from "../../components/kernel/commands.js";
5
+ import { UnsupportedOperation } from "../../errors.js";
6
+ import { boolean, number, raise } from "../../utils.js";
7
+ import {
8
+ Comparable,
9
+ Numeric,
10
+ Summable,
11
+ YuComparable,
12
+ YuNumeric,
13
+ YuSummable,
14
+ } from "../capabilities.js";
15
+ import { YuString } from "../sequences/YuString.js";
16
+ import { YuValue } from "../YuValue.js";
17
+
18
+ export class YuNumber extends YuValue implements Summable, Numeric, Comparable {
19
+ constructor(public readonly value: number) {
20
+ super();
21
+ }
22
+
23
+ get asSummable(): YuSummable {
24
+ return this;
25
+ }
26
+ get asNumeric(): YuNumeric {
27
+ return this;
28
+ }
29
+ get asComparable(): YuComparable {
30
+ return this;
31
+ }
32
+
33
+ public round(): ExecutionCommand {
34
+ return number(Math.round(this.value));
35
+ }
36
+ public abs(): ExecutionCommand {
37
+ return number(Math.abs(this.value));
38
+ }
39
+ public ceil(): ExecutionCommand {
40
+ return number(Math.ceil(this.value));
41
+ }
42
+ public floor(): ExecutionCommand {
43
+ return number(Math.floor(this.value));
44
+ }
45
+ public negation(): ExecutionCommand {
46
+ return number(this.value * -1);
47
+ }
48
+ public sqrt(): ExecutionCommand {
49
+ return number(Math.sqrt(this.value));
50
+ }
51
+
52
+ public plus(other: YuValue): ExecutionCommand {
53
+ const s = other.asSummable;
54
+ if (!s) return raise(new UnsupportedOperation(other, "plus"));
55
+ return s.plusWithNumber(this);
56
+ }
57
+
58
+ public plusWithNumber(left: YuNumber): ExecutionCommand {
59
+ return number(left.value + this.value);
60
+ }
61
+
62
+ public plusWithString(left: YuString): ExecutionCommand {
63
+ return new StepCommand(new YuString(left.toJSON() + this.value.toString()));
64
+ }
65
+
66
+ public realize(): ExecutionCommand {
67
+ return new StepCommand(this);
68
+ }
69
+
70
+ public minus(other: YuValue): ExecutionCommand {
71
+ const n = other.asNumeric;
72
+ if (!n) return raise(new UnsupportedOperation(other, "minus"));
73
+ return n.minusWithNumber(this);
74
+ }
75
+
76
+ public minusWithNumber(left: YuNumber): ExecutionCommand {
77
+ return number(left.value - this.value);
78
+ }
79
+
80
+ public multiply(other: YuValue): ExecutionCommand {
81
+ const n = other.asNumeric;
82
+ if (!n) return raise(new UnsupportedOperation(other, "multiply"));
83
+ return n.multiplyWithNumber(this);
84
+ }
85
+
86
+ public multiplyWithNumber(left: YuNumber): ExecutionCommand {
87
+ return number(left.value * this.value);
88
+ }
89
+
90
+ public divide(other: YuValue): ExecutionCommand {
91
+ const n = other.asNumeric;
92
+ if (!n) return raise(new UnsupportedOperation(other, "divide"));
93
+ return n.divideWithNumber(this);
94
+ }
95
+
96
+ public divideWithNumber(left: YuNumber): ExecutionCommand {
97
+ return number(left.value / this.value);
98
+ }
99
+
100
+ public modulo(other: YuValue): ExecutionCommand {
101
+ const n = other.asNumeric;
102
+ if (!n) return raise(new UnsupportedOperation(other, "modulo"));
103
+ return n.moduloWithNumber(this);
104
+ }
105
+
106
+ public moduloWithNumber(left: YuNumber): ExecutionCommand {
107
+ return number(left.value % this.value);
108
+ }
109
+ public power(other: YuValue): ExecutionCommand {
110
+ const n = other.asNumeric;
111
+ if (!n) return raise(new UnsupportedOperation(other, "power"));
112
+ return n.powerWithNumber(this);
113
+ }
114
+
115
+ public powerWithNumber(left: YuNumber): ExecutionCommand {
116
+ return number(left.value ** this.value);
117
+ }
118
+ public min(other: YuValue): ExecutionCommand {
119
+ const n = other.asNumeric;
120
+ if (!n) return raise(new UnsupportedOperation(other, "power"));
121
+ return n.minWithNumber(this);
122
+ }
123
+
124
+ public minWithNumber(left: YuNumber): ExecutionCommand {
125
+ return number(Math.min(left.value, this.value));
126
+ }
127
+ public max(other: YuValue): ExecutionCommand {
128
+ const n = other.asNumeric;
129
+ if (!n) return raise(new UnsupportedOperation(other, "power"));
130
+ return n.maxWithNumber(this);
131
+ }
132
+
133
+ public maxWithNumber(left: YuNumber): ExecutionCommand {
134
+ return number(Math.max(left.value, this.value));
135
+ }
136
+
137
+ public equals(other: YuValue): ExecutionCommand {
138
+ const c = other.asComparable;
139
+ if (!c) return boolean(false);
140
+ return c.equalsWithNumber(this);
141
+ }
142
+
143
+ public equalsWithNumber(left: YuNumber): ExecutionCommand {
144
+ return boolean(left.value === this.value);
145
+ }
146
+ public equalsWithBoolean(): ExecutionCommand {
147
+ return boolean(false);
148
+ }
149
+ public equalsWithString(left: YuString): ExecutionCommand {
150
+ return boolean(left.toJSON() === this.value.toString());
151
+ }
152
+ public equalsWithArray(): ExecutionCommand {
153
+ return boolean(false);
154
+ }
155
+ public equalsWithNil(): ExecutionCommand {
156
+ return boolean(false);
157
+ }
158
+
159
+ public compare(other: YuValue): ExecutionCommand {
160
+ const c = other.asComparable;
161
+ if (!c) return raise(new UnsupportedOperation(other, "compare"));
162
+ return c.compareWithNumber(this);
163
+ }
164
+
165
+ public compareWithNumber(left: YuNumber): ExecutionCommand {
166
+ return number(left.value - this.value);
167
+ }
168
+ public compareWithBoolean(): ExecutionCommand {
169
+ return raise(new UnsupportedOperation(this, "compare"));
170
+ }
171
+ public compareWithString(left: YuString): ExecutionCommand {
172
+ return number(Number(left.toJSON()) - this.value);
173
+ }
174
+ public compareWithArray(): ExecutionCommand {
175
+ return raise(new UnsupportedOperation(this, "compare"));
176
+ }
177
+ public compareWithNil(): ExecutionCommand {
178
+ return raise(new UnsupportedOperation(this, "compare"));
179
+ }
180
+
181
+ public toString(): string {
182
+ return this.value.toString();
183
+ }
184
+ public toJSON(): number {
185
+ return this.value;
186
+ }
187
+ }
@@ -0,0 +1,178 @@
1
+ import { YuValue } from "../YuValue.js";
2
+ import {
3
+ BindCommand,
4
+ ExecutionCommand,
5
+ StepCommand,
6
+ } from "../../components/kernel/commands.js";
7
+ import { YuNil } from "../scalars/YuNil.js";
8
+ import { UnsupportedOperation } from "../../errors.js";
9
+ import {
10
+ Comparable,
11
+ Sequence,
12
+ YuComparable,
13
+ YuSequence,
14
+ } from "../capabilities.js";
15
+ import { LazyStepResult } from "../entities/LazyList.js";
16
+ import { YuBoolean, YuNumber, YuString } from "../index.js";
17
+ import { boolean, compareResult, number, raise } from "../../utils.js";
18
+
19
+ export class YuArray extends YuValue implements Sequence, Comparable {
20
+ constructor(
21
+ public readonly items: YuValue[],
22
+ public readonly index: number = 0,
23
+ ) {
24
+ super();
25
+ }
26
+
27
+ get asSequence(): YuSequence {
28
+ return this;
29
+ }
30
+ get asComparable(): YuComparable {
31
+ return this;
32
+ }
33
+
34
+ public *[Symbol.iterator](): Iterator<YuValue> {
35
+ for (let i = this.index; i < this.items.length; i++) {
36
+ yield this.items[i];
37
+ }
38
+ }
39
+
40
+ public size(): ExecutionCommand {
41
+ return number(this.items.length);
42
+ }
43
+
44
+ public step(): ExecutionCommand {
45
+ if (this.index >= this.items.length) {
46
+ return new StepCommand(YuNil.getInstance());
47
+ }
48
+ const item = this.items[this.index];
49
+ const tail = new YuArray(this.items, this.index + 1);
50
+ return new StepCommand(new LazyStepResult(item, tail));
51
+ }
52
+
53
+ public concat(other: YuValue): ExecutionCommand {
54
+ const seq = other.asSequence;
55
+ if (!seq) return raise(new UnsupportedOperation(other, "concat"));
56
+ return seq.concatWithArray(this);
57
+ }
58
+ public concatWithArray(arr: YuArray): ExecutionCommand {
59
+ return new StepCommand(new YuArray([...arr, ...this]));
60
+ }
61
+
62
+ public concatWithString(str: YuString): ExecutionCommand {
63
+ return new StepCommand(new YuString(this.toJSON() + str.toJSON()));
64
+ }
65
+
66
+ public at(index: number) {
67
+ return this.items[this.index + index];
68
+ }
69
+
70
+ public flat() {
71
+ return new YuArray(
72
+ [...this].flatMap((item) => {
73
+ const seq = item.asSequence;
74
+ return seq instanceof YuArray ? [...seq] : [item];
75
+ }),
76
+ );
77
+ }
78
+
79
+ public equals(other: YuValue): ExecutionCommand {
80
+ const c = other.asComparable;
81
+ if (!c) return boolean(false);
82
+ return c.equalsWithArray(this);
83
+ }
84
+
85
+ public equalsWithNumber(): ExecutionCommand {
86
+ return boolean(false);
87
+ }
88
+ public equalsWithBoolean(): ExecutionCommand {
89
+ return boolean(false);
90
+ }
91
+ public equalsWithString(): ExecutionCommand {
92
+ return boolean(false);
93
+ }
94
+ public equalsWithArray(left: YuArray): ExecutionCommand {
95
+ return new BindCommand(left.size(), (res) => {
96
+ const leftSize = res.asNumeric?.asComparable;
97
+ if (!leftSize || !leftSize.equals(new YuNumber(this.items.length)))
98
+ return boolean(false);
99
+ const compareNext = (index: number): ExecutionCommand => {
100
+ if (
101
+ compareResult(
102
+ leftSize.compare(new YuNumber(index)),
103
+ (res) => res >= 0,
104
+ )
105
+ ) {
106
+ return boolean(true);
107
+ }
108
+ return new BindCommand(left.at(index).equals(this.at(index)), (res) => {
109
+ if (!res.toJSON()) return boolean(false);
110
+ return compareNext(index + 1);
111
+ });
112
+ };
113
+
114
+ return compareNext(0);
115
+ });
116
+ }
117
+ public equalsWithNil(): ExecutionCommand {
118
+ return boolean(false);
119
+ }
120
+
121
+ public compare(other: YuValue): ExecutionCommand {
122
+ const c = other.asComparable;
123
+ if (!c) return raise(new UnsupportedOperation(other, "compare"));
124
+ return c.compareWithArray(this);
125
+ }
126
+
127
+ public compareWithNumber(): ExecutionCommand {
128
+ return raise(new UnsupportedOperation(this, "compare"));
129
+ }
130
+ public compareWithBoolean(): ExecutionCommand {
131
+ return raise(new UnsupportedOperation(this, "compare"));
132
+ }
133
+ public compareWithString(): ExecutionCommand {
134
+ return raise(new UnsupportedOperation(this, "compare"));
135
+ }
136
+ public compareWithArray(left: YuArray): ExecutionCommand {
137
+ const a = [...left];
138
+ const b = [...this];
139
+ const minLen = Math.min(a.length, b.length);
140
+
141
+ const compareNext = (index: number): ExecutionCommand => {
142
+ if (index >= minLen) {
143
+ return new StepCommand(new YuNumber(a.length - b.length));
144
+ }
145
+ return new BindCommand(a[index].compare(b[index]), (res) => {
146
+ const cmp = res.toJSON() as number;
147
+ if (cmp !== 0) {
148
+ return new StepCommand(res);
149
+ }
150
+ return compareNext(index + 1);
151
+ });
152
+ };
153
+
154
+ return compareNext(0);
155
+ }
156
+ public compareWithNil(): ExecutionCommand {
157
+ return raise(new UnsupportedOperation(this, "compare"));
158
+ }
159
+
160
+ public toJSON(keyOrSeen?: string | Set<YuValue>): unknown {
161
+ const seen = keyOrSeen instanceof Set ? keyOrSeen : new Set<YuValue>();
162
+ if (seen.has(this)) return "[Circular]";
163
+ seen.add(this);
164
+ const result = this.index === 0
165
+ ? this.items.map((i) => i.toJSON(seen))
166
+ : this.items.slice(this.index).map((i) => i.toJSON(seen));
167
+ seen.delete(this);
168
+ return result;
169
+ }
170
+
171
+ public toString(seen = new Set<YuValue>()): string {
172
+ if (seen.has(this)) return "[Circular]";
173
+ seen.add(this);
174
+ const result = `[${[...this].map((i) => i.toString(seen)).join(", ")}]`;
175
+ seen.delete(this);
176
+ return result;
177
+ }
178
+ }
@@ -0,0 +1,165 @@
1
+ import { YuValue } from "../YuValue.js";
2
+ import { YuChar } from "../scalars/YuChar.js";
3
+ import {
4
+ ExecutionCommand,
5
+ StepCommand,
6
+ } from "../../components/kernel/commands.js";
7
+ import { YuNil } from "../scalars/YuNil.js";
8
+ import { YuNumber } from "../scalars/YuNumber.js";
9
+ import { YuArray } from "./YuArray.js";
10
+ import { UnsupportedOperation } from "../../errors.js";
11
+ import {
12
+ Comparable,
13
+ Sequence,
14
+ Summable,
15
+ YuComparable,
16
+ YuSequence,
17
+ YuSummable,
18
+ } from "../capabilities.js";
19
+ import { LazyStepResult, YuBoolean } from "../index.js";
20
+ import { boolean, number, raise } from "../../utils.js";
21
+
22
+ export class YuString
23
+ extends YuValue
24
+ implements Sequence, Summable, Comparable
25
+ {
26
+ constructor(
27
+ public readonly value: string,
28
+ public readonly index: number = 0,
29
+ ) {
30
+ super();
31
+ }
32
+
33
+ get asSequence(): YuSequence {
34
+ return this;
35
+ }
36
+ get asSummable(): YuSummable {
37
+ return this;
38
+ }
39
+ get asComparable(): YuComparable {
40
+ return this;
41
+ }
42
+
43
+ public *[Symbol.iterator](): Iterator<YuChar> {
44
+ for (let i = this.index; i < this.value.length; i++) {
45
+ yield new YuChar(this.value[i]);
46
+ }
47
+ }
48
+
49
+ public step(): ExecutionCommand {
50
+ if (this.index >= this.value.length) {
51
+ return new StepCommand(YuNil.getInstance());
52
+ }
53
+ const char = new YuChar(this.value[this.index]);
54
+ const tail = new YuString(this.value, this.index + 1);
55
+ return new StepCommand(new LazyStepResult(char, tail));
56
+ }
57
+
58
+ public size(): ExecutionCommand {
59
+ return number(this.value.length)
60
+ }
61
+
62
+
63
+ public plus(other: YuValue): ExecutionCommand {
64
+ const s = other.asSummable;
65
+ if (!s) return raise(new UnsupportedOperation(other, "plus"));
66
+ return s.plusWithString(this);
67
+ }
68
+
69
+ public plusWithNumber(left: YuNumber): ExecutionCommand {
70
+ return new StepCommand(
71
+ new YuString(left.toJSON().toString() + this.toJSON()),
72
+ );
73
+ }
74
+
75
+ public plusWithString(left: YuString): ExecutionCommand {
76
+ return new StepCommand(new YuString(left.toJSON() + this.toJSON()));
77
+ }
78
+
79
+ public split(): ExecutionCommand {
80
+ const items = this.toJSON()
81
+ .split("")
82
+ .map((char) => new YuChar(char));
83
+ return new StepCommand(new YuArray(items));
84
+ }
85
+
86
+ public concat(other: YuValue): ExecutionCommand {
87
+ const seq = other.asSequence
88
+ if (!seq) return raise(new UnsupportedOperation(other, "concat"));
89
+ return seq.concatWithString(this)
90
+ }
91
+
92
+ public concatWithString(str: YuString): ExecutionCommand {
93
+ return new StepCommand(new YuString(str.toJSON() + this.toJSON()));
94
+ }
95
+ public concatWithArray(arr: YuArray): ExecutionCommand {
96
+ return new StepCommand(new YuString(arr.toString()));
97
+ }
98
+
99
+ public realize(): ExecutionCommand {
100
+ return this.split();
101
+ }
102
+
103
+ public equals(other: YuValue): ExecutionCommand {
104
+ const c = other.asComparable;
105
+ if (!c) return boolean(false);
106
+ return c.equalsWithString(this);
107
+ }
108
+
109
+ public contains(other: YuValue): ExecutionCommand {
110
+ if (other instanceof YuString) {
111
+ return boolean(this.value.includes(other.value));
112
+ }
113
+ return raise(new UnsupportedOperation(other, "contains"));
114
+ }
115
+
116
+ public equalsWithNumber(left: YuNumber): ExecutionCommand {
117
+ return new StepCommand(
118
+ new YuBoolean(left.value.toString() === this.toJSON()),
119
+ );
120
+ }
121
+ public equalsWithBoolean(): ExecutionCommand {
122
+ return boolean(false);
123
+ }
124
+ public equalsWithString(left: YuString): ExecutionCommand {
125
+ return boolean(left.toJSON() === this.toJSON());
126
+ }
127
+ public equalsWithArray(): ExecutionCommand {
128
+ return boolean(false);
129
+ }
130
+ public equalsWithNil(): ExecutionCommand {
131
+ return boolean(false);
132
+ }
133
+
134
+ public compare(other: YuValue): ExecutionCommand {
135
+ const c = other.asComparable;
136
+ if (!c) return raise(new UnsupportedOperation(other, "compare"));
137
+ return c.compareWithString(this);
138
+ }
139
+
140
+ public compareWithNumber(left: YuNumber): ExecutionCommand {
141
+ return new StepCommand(new YuNumber(left.value - Number(this.toJSON())));
142
+ }
143
+ public compareWithBoolean(): ExecutionCommand {
144
+ return raise(new UnsupportedOperation(this, "compare"));
145
+ }
146
+ public compareWithString(left: YuString): ExecutionCommand {
147
+ return new StepCommand(
148
+ new YuNumber(left.toJSON().localeCompare(this.toJSON())),
149
+ );
150
+ }
151
+ public compareWithArray(): ExecutionCommand {
152
+ return raise(new UnsupportedOperation(this, "compare"));
153
+ }
154
+ public compareWithNil(): ExecutionCommand {
155
+ return raise(new UnsupportedOperation(this, "compare"));
156
+ }
157
+
158
+ public toJSON(): string {
159
+ return this.index === 0 ? this.value : this.value.slice(this.index);
160
+ }
161
+
162
+ public toString(): string {
163
+ return this.toJSON();
164
+ }
165
+ }