@rsconcept/domain 1.0.0

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 (224) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +55 -0
  3. package/dist/cctext/index.d.ts +1 -0
  4. package/dist/cctext/index.js +42 -0
  5. package/dist/cctext/index.js.map +1 -0
  6. package/dist/cctext/language-api.d.ts +43 -0
  7. package/dist/cctext/language-api.js +252 -0
  8. package/dist/cctext/language-api.js.map +1 -0
  9. package/dist/cctext/language.d.ts +58 -0
  10. package/dist/cctext/language.js +44 -0
  11. package/dist/cctext/language.js.map +1 -0
  12. package/dist/graph/graph.d.ts +62 -0
  13. package/dist/graph/graph.js +385 -0
  14. package/dist/graph/graph.js.map +1 -0
  15. package/dist/graph/index.d.ts +1 -0
  16. package/dist/graph/index.js +384 -0
  17. package/dist/graph/index.js.map +1 -0
  18. package/dist/index.d.ts +28 -0
  19. package/dist/index.js +5851 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/library/folder-tree.d.ts +32 -0
  22. package/dist/library/folder-tree.js +136 -0
  23. package/dist/library/folder-tree.js.map +1 -0
  24. package/dist/library/index.d.ts +17 -0
  25. package/dist/library/index.js +2800 -0
  26. package/dist/library/index.js.map +1 -0
  27. package/dist/library/library-api.d.ts +6 -0
  28. package/dist/library/library-api.js +13 -0
  29. package/dist/library/library-api.js.map +1 -0
  30. package/dist/library/library.d.ts +56 -0
  31. package/dist/library/library.js +23 -0
  32. package/dist/library/library.js.map +1 -0
  33. package/dist/library/oss-api.d.ts +47 -0
  34. package/dist/library/oss-api.js +1105 -0
  35. package/dist/library/oss-api.js.map +1 -0
  36. package/dist/library/oss-layout-api.d.ts +36 -0
  37. package/dist/library/oss-layout-api.js +330 -0
  38. package/dist/library/oss-layout-api.js.map +1 -0
  39. package/dist/library/oss-layout.d.ts +25 -0
  40. package/dist/library/oss-layout.js +1 -0
  41. package/dist/library/oss-layout.js.map +1 -0
  42. package/dist/library/oss.d.ts +136 -0
  43. package/dist/library/oss.js +30 -0
  44. package/dist/library/oss.js.map +1 -0
  45. package/dist/library/rsengine.d.ts +116 -0
  46. package/dist/library/rsengine.js +2604 -0
  47. package/dist/library/rsengine.js.map +1 -0
  48. package/dist/library/rsform-api.d.ts +74 -0
  49. package/dist/library/rsform-api.js +879 -0
  50. package/dist/library/rsform-api.js.map +1 -0
  51. package/dist/library/rsform.d.ts +206 -0
  52. package/dist/library/rsform.js +32 -0
  53. package/dist/library/rsform.js.map +1 -0
  54. package/dist/library/rsmodel-api.d.ts +43 -0
  55. package/dist/library/rsmodel-api.js +836 -0
  56. package/dist/library/rsmodel-api.js.map +1 -0
  57. package/dist/library/rsmodel.d.ts +52 -0
  58. package/dist/library/rsmodel.js +25 -0
  59. package/dist/library/rsmodel.js.map +1 -0
  60. package/dist/library/structure-planner.d.ts +33 -0
  61. package/dist/library/structure-planner.js +481 -0
  62. package/dist/library/structure-planner.js.map +1 -0
  63. package/dist/parsing/ast.d.ts +49 -0
  64. package/dist/parsing/ast.js +93 -0
  65. package/dist/parsing/ast.js.map +1 -0
  66. package/dist/parsing/index.d.ts +3 -0
  67. package/dist/parsing/index.js +141 -0
  68. package/dist/parsing/index.js.map +1 -0
  69. package/dist/parsing/lezer-tree.d.ts +13 -0
  70. package/dist/parsing/lezer-tree.js +50 -0
  71. package/dist/parsing/lezer-tree.js.map +1 -0
  72. package/dist/rslang/api.d.ts +53 -0
  73. package/dist/rslang/api.js +846 -0
  74. package/dist/rslang/api.js.map +1 -0
  75. package/dist/rslang/ast-annotations.d.ts +18 -0
  76. package/dist/rslang/ast-annotations.js +56 -0
  77. package/dist/rslang/ast-annotations.js.map +1 -0
  78. package/dist/rslang/error.d.ts +85 -0
  79. package/dist/rslang/error.js +159 -0
  80. package/dist/rslang/error.js.map +1 -0
  81. package/dist/rslang/eval/calculator.d.ts +43 -0
  82. package/dist/rslang/eval/calculator.js +1639 -0
  83. package/dist/rslang/eval/calculator.js.map +1 -0
  84. package/dist/rslang/eval/evaluation-cache.d.ts +36 -0
  85. package/dist/rslang/eval/evaluation-cache.js +310 -0
  86. package/dist/rslang/eval/evaluation-cache.js.map +1 -0
  87. package/dist/rslang/eval/evaluator.d.ts +70 -0
  88. package/dist/rslang/eval/evaluator.js +1514 -0
  89. package/dist/rslang/eval/evaluator.js.map +1 -0
  90. package/dist/rslang/eval/value-api.d.ts +48 -0
  91. package/dist/rslang/eval/value-api.js +490 -0
  92. package/dist/rslang/eval/value-api.js.map +1 -0
  93. package/dist/rslang/eval/value.d.ts +36 -0
  94. package/dist/rslang/eval/value.js +118 -0
  95. package/dist/rslang/eval/value.js.map +1 -0
  96. package/dist/rslang/index.d.ts +17 -0
  97. package/dist/rslang/index.js +4314 -0
  98. package/dist/rslang/index.js.map +1 -0
  99. package/dist/rslang/labels.d.ts +16 -0
  100. package/dist/rslang/labels.js +315 -0
  101. package/dist/rslang/labels.js.map +1 -0
  102. package/dist/rslang/parser/expression-generator.d.ts +10 -0
  103. package/dist/rslang/parser/expression-generator.js +451 -0
  104. package/dist/rslang/parser/expression-generator.js.map +1 -0
  105. package/dist/rslang/parser/normalize.d.ts +11 -0
  106. package/dist/rslang/parser/normalize.js +507 -0
  107. package/dist/rslang/parser/normalize.js.map +1 -0
  108. package/dist/rslang/parser/parser.d.ts +5 -0
  109. package/dist/rslang/parser/parser.js +24 -0
  110. package/dist/rslang/parser/parser.js.map +1 -0
  111. package/dist/rslang/parser/parser.terms.d.ts +42 -0
  112. package/dist/rslang/parser/parser.terms.js +84 -0
  113. package/dist/rslang/parser/parser.terms.js.map +1 -0
  114. package/dist/rslang/parser/syntax-errors.d.ts +11 -0
  115. package/dist/rslang/parser/syntax-errors.js +403 -0
  116. package/dist/rslang/parser/syntax-errors.js.map +1 -0
  117. package/dist/rslang/parser/token.d.ts +79 -0
  118. package/dist/rslang/parser/token.js +95 -0
  119. package/dist/rslang/parser/token.js.map +1 -0
  120. package/dist/rslang/semantic/analyzer.d.ts +39 -0
  121. package/dist/rslang/semantic/analyzer.js +2604 -0
  122. package/dist/rslang/semantic/analyzer.js.map +1 -0
  123. package/dist/rslang/semantic/arguments-extractor.d.ts +42 -0
  124. package/dist/rslang/semantic/arguments-extractor.js +366 -0
  125. package/dist/rslang/semantic/arguments-extractor.js.map +1 -0
  126. package/dist/rslang/semantic/type-auditor.d.ts +73 -0
  127. package/dist/rslang/semantic/type-auditor.js +1570 -0
  128. package/dist/rslang/semantic/type-auditor.js.map +1 -0
  129. package/dist/rslang/semantic/typification-api.d.ts +27 -0
  130. package/dist/rslang/semantic/typification-api.js +320 -0
  131. package/dist/rslang/semantic/typification-api.js.map +1 -0
  132. package/dist/rslang/semantic/typification-parser.d.ts +12 -0
  133. package/dist/rslang/semantic/typification-parser.js +226 -0
  134. package/dist/rslang/semantic/typification-parser.js.map +1 -0
  135. package/dist/rslang/semantic/typification.d.ts +119 -0
  136. package/dist/rslang/semantic/typification.js +74 -0
  137. package/dist/rslang/semantic/typification.js.map +1 -0
  138. package/dist/rslang/semantic/value-auditor.d.ts +43 -0
  139. package/dist/rslang/semantic/value-auditor.js +523 -0
  140. package/dist/rslang/semantic/value-auditor.js.map +1 -0
  141. package/dist/rslang/semantic/value-class.d.ts +10 -0
  142. package/dist/rslang/semantic/value-class.js +9 -0
  143. package/dist/rslang/semantic/value-class.js.map +1 -0
  144. package/dist/rslang/typification-graph.d.ts +33 -0
  145. package/dist/rslang/typification-graph.js +311 -0
  146. package/dist/rslang/typification-graph.js.map +1 -0
  147. package/dist/shared/branded.d.ts +7 -0
  148. package/dist/shared/branded.js +1 -0
  149. package/dist/shared/branded.js.map +1 -0
  150. package/dist/shared/hash.d.ts +6 -0
  151. package/dist/shared/hash.js +18 -0
  152. package/dist/shared/hash.js.map +1 -0
  153. package/dist/shared/index.d.ts +2 -0
  154. package/dist/shared/index.js +18 -0
  155. package/dist/shared/index.js.map +1 -0
  156. package/package.json +184 -0
  157. package/src/cctext/index.ts +9 -0
  158. package/src/cctext/language-api.test.ts +149 -0
  159. package/src/cctext/language-api.ts +285 -0
  160. package/src/cctext/language.ts +80 -0
  161. package/src/graph/graph.test.ts +392 -0
  162. package/src/graph/graph.ts +433 -0
  163. package/src/graph/index.ts +1 -0
  164. package/src/index.ts +96 -0
  165. package/src/library/folder-tree.test.ts +47 -0
  166. package/src/library/folder-tree.ts +156 -0
  167. package/src/library/index.ts +46 -0
  168. package/src/library/library-api.test.ts +32 -0
  169. package/src/library/library-api.ts +11 -0
  170. package/src/library/library.ts +61 -0
  171. package/src/library/oss-api.ts +449 -0
  172. package/src/library/oss-layout-api.ts +377 -0
  173. package/src/library/oss-layout.ts +27 -0
  174. package/src/library/oss.ts +150 -0
  175. package/src/library/rsengine.ts +593 -0
  176. package/src/library/rsform-api.ts +533 -0
  177. package/src/library/rsform.ts +228 -0
  178. package/src/library/rsmodel-api.ts +340 -0
  179. package/src/library/rsmodel.ts +50 -0
  180. package/src/library/structure-planner.ts +143 -0
  181. package/src/parsing/ast.ts +136 -0
  182. package/src/parsing/index.ts +15 -0
  183. package/src/parsing/lezer-tree.ts +69 -0
  184. package/src/rslang/api.test.ts +116 -0
  185. package/src/rslang/api.ts +183 -0
  186. package/src/rslang/ast-annotations.ts +70 -0
  187. package/src/rslang/error.ts +129 -0
  188. package/src/rslang/eval/calculator.test.ts +124 -0
  189. package/src/rslang/eval/calculator.ts +121 -0
  190. package/src/rslang/eval/evaluation-cache.ts +257 -0
  191. package/src/rslang/eval/evaluator.test.ts +352 -0
  192. package/src/rslang/eval/evaluator.ts +935 -0
  193. package/src/rslang/eval/value-api.test.ts +105 -0
  194. package/src/rslang/eval/value-api.ts +444 -0
  195. package/src/rslang/eval/value.ts +102 -0
  196. package/src/rslang/index.ts +23 -0
  197. package/src/rslang/labels.ts +191 -0
  198. package/src/rslang/parser/expression-generator.test.ts +100 -0
  199. package/src/rslang/parser/expression-generator.ts +466 -0
  200. package/src/rslang/parser/normalize.test.ts +99 -0
  201. package/src/rslang/parser/normalize.ts +462 -0
  202. package/src/rslang/parser/parser.terms.ts +42 -0
  203. package/src/rslang/parser/parser.test.ts +153 -0
  204. package/src/rslang/parser/parser.ts +20 -0
  205. package/src/rslang/parser/rslang.grammar +251 -0
  206. package/src/rslang/parser/syntax-errors.ts +209 -0
  207. package/src/rslang/parser/token.ts +106 -0
  208. package/src/rslang/semantic/analyzer.test.ts +59 -0
  209. package/src/rslang/semantic/analyzer.ts +179 -0
  210. package/src/rslang/semantic/arguments-extractor.ts +327 -0
  211. package/src/rslang/semantic/type-auditor.test.ts +326 -0
  212. package/src/rslang/semantic/type-auditor.ts +1049 -0
  213. package/src/rslang/semantic/typification-api.test.ts +46 -0
  214. package/src/rslang/semantic/typification-api.ts +321 -0
  215. package/src/rslang/semantic/typification-parser.test.ts +50 -0
  216. package/src/rslang/semantic/typification-parser.ts +220 -0
  217. package/src/rslang/semantic/typification.ts +180 -0
  218. package/src/rslang/semantic/value-auditor.test.ts +206 -0
  219. package/src/rslang/semantic/value-auditor.ts +332 -0
  220. package/src/rslang/semantic/value-class.ts +11 -0
  221. package/src/rslang/typification-graph.ts +155 -0
  222. package/src/shared/branded.ts +6 -0
  223. package/src/shared/hash.ts +17 -0
  224. package/src/shared/index.ts +2 -0
@@ -0,0 +1,935 @@
1
+ /**
2
+ * Module: AST evaluation - visitor-pattern evaluator for RS expressions.
3
+ */
4
+
5
+ import { type AstNode, getNodeIndices, getNodeText } from '../../parsing';
6
+ import { annotateError } from '../ast-annotations';
7
+ import { type ErrorReporter, RSErrorCode } from '../error';
8
+ import { TokenID } from '../parser/token';
9
+
10
+ import { EvaluationCache, EvaluationMetadata } from './evaluation-cache';
11
+ import {
12
+ BOOL_INFINITY,
13
+ compare,
14
+ EmptySetV,
15
+ set,
16
+ SET_INFINITY,
17
+ tuple,
18
+ type Value,
19
+ VALUE_FALSE,
20
+ VALUE_TRUE,
21
+ type ValueContext
22
+ } from './value';
23
+ import {
24
+ boolean,
25
+ cartesianProduct,
26
+ contains,
27
+ isSubsetOrEq,
28
+ projection,
29
+ reduce,
30
+ setDiff,
31
+ setIntersection,
32
+ setSymDiff,
33
+ setUnion
34
+ } from './value-api';
35
+
36
+ /** Maximum iterations to prevent infinite loops (recursion, quantifiers, etc.). */
37
+ const MAX_ITERATIONS = 1_000_000;
38
+
39
+ const TICK_PER_FUNCTION = 3;
40
+ const TICK_PER_RECURSION = 3;
41
+ const TICK_PER_IMPERATIVE = 1;
42
+ const TICK_PER_DECLARATIVE = 1;
43
+ const TICK_PER_QUANTIFIER = 1;
44
+
45
+ /** AST context. */
46
+ export type ASTContext = Map<string, AstNode>;
47
+
48
+ /** AST calculator - evaluates RS expressions via visitor pattern and provides updates via listeners.
49
+ * Not safe for concurrent {@link run} calls on the same instance; use separate evaluators instead. */
50
+ export class Evaluator {
51
+ private reporter?: ErrorReporter;
52
+ private annotateErrors = false;
53
+ private disableCache = false;
54
+ private locals: LocalContext = new LocalContext();
55
+ private nodeMetadata: EvaluationMetadata = new EvaluationMetadata();
56
+ private evalCache: EvaluationCache = new EvaluationCache();
57
+ private callSiteStack: AstNode[] = [];
58
+ private context: ValueContext;
59
+ private treeContext: ASTContext;
60
+
61
+ public iterationCounter = 0;
62
+
63
+ /** Cache hits in the current evaluation run (for tests/diagnostics). */
64
+ public get cacheHits(): number {
65
+ return this.evalCache.hits;
66
+ }
67
+
68
+ constructor(context: ValueContext, astContext: ASTContext) {
69
+ this.treeContext = astContext;
70
+ this.context = context;
71
+ }
72
+
73
+ public run(
74
+ ast: AstNode,
75
+ reporter?: ErrorReporter,
76
+ annotateErrors: boolean = false,
77
+ disableCache: boolean = false
78
+ ): Value | null {
79
+ if (ast.hasError) {
80
+ return null;
81
+ }
82
+ this.reporter = reporter;
83
+ this.annotateErrors = annotateErrors;
84
+ this.disableCache = disableCache;
85
+ this.clear();
86
+ return this.dispatchVisit(ast);
87
+ }
88
+
89
+ private clear(): void {
90
+ this.locals = new LocalContext();
91
+ this.evalCache.clear();
92
+ this.callSiteStack = [];
93
+ this.iterationCounter = 0;
94
+ }
95
+
96
+ private errorNode(node: AstNode): AstNode {
97
+ if (this.callSiteStack.length > 0) {
98
+ return this.callSiteStack[this.callSiteStack.length - 1];
99
+ }
100
+ return node;
101
+ }
102
+
103
+ private onError(code: RSErrorCode, node: AstNode, params?: string[]): null {
104
+ const target = this.errorNode(node);
105
+ this.reporter?.({ code, from: target.from, to: target.to, params });
106
+ if (this.annotateErrors) {
107
+ annotateError(target, code, params);
108
+ }
109
+ return null;
110
+ }
111
+
112
+ private tick(node: AstNode, counter: number = 1): boolean {
113
+ this.iterationCounter += counter;
114
+ if (this.iterationCounter > MAX_ITERATIONS) {
115
+ this.onError(RSErrorCode.iterationsLimit, node, [String(MAX_ITERATIONS)]);
116
+ return false;
117
+ }
118
+ return true;
119
+ }
120
+
121
+ private dispatchDeclare(node: AstNode, value: Value): void {
122
+ switch (node.typeID) {
123
+ case TokenID.ID_LOCAL:
124
+ return this.declareLocal(node, value);
125
+ case TokenID.NT_TUPLE_DECL:
126
+ return this.declareTuple(node, value as Value[]);
127
+ case TokenID.NT_ARG_DECL:
128
+ return this.dispatchDeclare(node.children[0], value);
129
+ }
130
+ }
131
+
132
+ private declareLocal(node: AstNode, value: Value): void {
133
+ const alias = getNodeText(node);
134
+ this.locals.setLocal(alias, value);
135
+ }
136
+
137
+ private declareTuple(node: AstNode, value: Value[]): void {
138
+ for (let child = 0; child < node.children.length; child++) {
139
+ this.dispatchDeclare(node.children[child], value[child + 1]);
140
+ }
141
+ }
142
+
143
+ private dispatchVisit(node: AstNode): Value | null {
144
+ const info = this.nodeMetadata.get(node);
145
+ let stamp: string | null = null;
146
+ if (info.cacheable && !this.disableCache) {
147
+ stamp = this.locals.buildDependencyStamp(info.reads);
148
+ if (stamp !== null) {
149
+ const cached = this.evalCache.lookup(info.structuralKey, stamp);
150
+ if (cached !== undefined) {
151
+ return cached;
152
+ }
153
+ }
154
+ }
155
+
156
+ const result = this.dispatchVisitImpl(node);
157
+ if (!this.disableCache && result !== null && info.cacheable && stamp !== null) {
158
+ this.evalCache.store(info.structuralKey, stamp, result);
159
+ }
160
+ return result;
161
+ }
162
+
163
+ private dispatchVisitImpl(node: AstNode): Value | null {
164
+ switch (node.typeID) {
165
+ case TokenID.ID_GLOBAL:
166
+ return this.visitGlobal(node);
167
+
168
+ case TokenID.NT_FUNC_CALL:
169
+ return this.visitFunctionCall(node);
170
+
171
+ case TokenID.ID_LOCAL:
172
+ case TokenID.ID_RADICAL:
173
+ return this.visitLocal(node);
174
+
175
+ case TokenID.LIT_INTEGER:
176
+ return this.visitInteger(node);
177
+
178
+ case TokenID.LIT_WHOLE_NUMBERS:
179
+ return this.onError(RSErrorCode.iterateInfinity, node);
180
+
181
+ case TokenID.LIT_EMPTYSET:
182
+ return EmptySetV;
183
+
184
+ case TokenID.PLUS:
185
+ case TokenID.MINUS:
186
+ case TokenID.MULTIPLY:
187
+ return this.visitArithmetic(node);
188
+
189
+ case TokenID.CARD:
190
+ return this.visitCard(node);
191
+
192
+ case TokenID.QUANTOR_UNIVERSAL:
193
+ case TokenID.QUANTOR_EXISTS:
194
+ return this.visitQuantifier(node);
195
+
196
+ case TokenID.LOGIC_NOT:
197
+ return this.visitNegation(node);
198
+
199
+ case TokenID.LOGIC_AND:
200
+ case TokenID.LOGIC_OR:
201
+ case TokenID.LOGIC_IMPLICATION:
202
+ case TokenID.LOGIC_EQUIVALENT:
203
+ return this.visitLogicBinary(node);
204
+
205
+ case TokenID.EQUAL:
206
+ case TokenID.NOTEQUAL:
207
+ return this.visitEquals(node);
208
+
209
+ case TokenID.GREATER:
210
+ case TokenID.LESSER:
211
+ case TokenID.GREATER_OR_EQ:
212
+ case TokenID.LESSER_OR_EQ:
213
+ return this.visitIntegerPredicate(node);
214
+
215
+ case TokenID.SET_IN:
216
+ case TokenID.SET_NOT_IN:
217
+ case TokenID.SUBSET:
218
+ case TokenID.SUBSET_OR_EQ:
219
+ case TokenID.NOT_SUBSET:
220
+ return this.visitSetexprPredicate(node);
221
+
222
+ case TokenID.DECART:
223
+ return this.visitDecart(node);
224
+
225
+ case TokenID.BOOLEAN:
226
+ return this.visitBoolean(node);
227
+
228
+ case TokenID.NT_TUPLE:
229
+ return this.visitTuple(node);
230
+
231
+ case TokenID.NT_ENUMERATION:
232
+ return this.visitEnumeration(node);
233
+
234
+ case TokenID.BOOL:
235
+ return this.visitBool(node);
236
+
237
+ case TokenID.DEBOOL:
238
+ return this.visitDebool(node);
239
+
240
+ case TokenID.SET_UNION:
241
+ case TokenID.SET_INTERSECTION:
242
+ case TokenID.SET_MINUS:
243
+ case TokenID.SET_SYMMETRIC_MINUS:
244
+ return this.visitSetexprBinary(node);
245
+
246
+ case TokenID.BIGPR:
247
+ return this.visitProjectSet(node);
248
+
249
+ case TokenID.SMALLPR:
250
+ return this.visitProjectTuple(node);
251
+
252
+ case TokenID.FILTER:
253
+ return this.visitFilter(node);
254
+
255
+ case TokenID.REDUCE:
256
+ return this.visitReduce(node);
257
+
258
+ case TokenID.NT_DECLARATIVE_EXPR:
259
+ return this.visitDeclarative(node);
260
+
261
+ case TokenID.NT_IMPERATIVE_EXPR:
262
+ return this.visitImperative(node);
263
+
264
+ case TokenID.ASSIGN:
265
+ return this.visitAssign(node);
266
+
267
+ case TokenID.NT_RECURSIVE_FULL:
268
+ case TokenID.NT_RECURSIVE_SHORT:
269
+ return this.visitRecursion(node);
270
+
271
+ case TokenID.NT_FUNC_DEFINITION:
272
+ return this.onError(RSErrorCode.calculationNotSupported, node);
273
+ }
274
+ return null;
275
+ }
276
+
277
+ private visitChild(node: AstNode, index: number): Value | null {
278
+ return this.dispatchVisit(node.children[index]);
279
+ }
280
+
281
+ private visitGlobal(node: AstNode): Value | null {
282
+ const alias = getNodeText(node);
283
+ const value = this.context.get(alias);
284
+ if (value === undefined) {
285
+ return this.onError(RSErrorCode.calcGlobalMissing, node, [alias]);
286
+ }
287
+ return value;
288
+ }
289
+
290
+ private visitLocal(node: AstNode): Value | null {
291
+ const alias = getNodeText(node);
292
+ return this.locals.getLocal(alias);
293
+ }
294
+
295
+ private visitFunctionCall(node: AstNode): Value | null {
296
+ const funcName = getNodeText(node.children[0]);
297
+ const ast = this.treeContext.get(funcName);
298
+ if (!ast) {
299
+ return this.onError(RSErrorCode.calcGlobalMissing, node.children[0], [funcName]);
300
+ }
301
+ if (!this.tick(node, TICK_PER_FUNCTION)) {
302
+ return null;
303
+ }
304
+
305
+ const args: Value[] = [];
306
+ for (let i = 1; i < node.children.length; i++) {
307
+ const arg = this.visitChild(node, i);
308
+ if (arg === null) {
309
+ return null;
310
+ }
311
+ args.push(arg);
312
+ }
313
+
314
+ this.locals.startScope();
315
+ for (let i = 0; i < args.length; i++) {
316
+ this.dispatchDeclare(ast.children[0].children[i], args[i]);
317
+ }
318
+ this.callSiteStack.push(node);
319
+ let result: Value | null;
320
+ try {
321
+ result = this.visitChild(ast, 1);
322
+ } finally {
323
+ this.callSiteStack.pop();
324
+ }
325
+ this.locals.endScope();
326
+ return result;
327
+ }
328
+
329
+ private visitInteger(node: AstNode): Value | null {
330
+ const value = node.data.dataType === 'number' ? (node.data.value as number) : Number(node.data.value);
331
+ return Math.floor(value);
332
+ }
333
+
334
+ private visitArithmetic(node: AstNode): Value | null {
335
+ const v1 = this.visitChild(node, 0);
336
+ const v2 = this.visitChild(node, 1);
337
+ if (v1 === null || v2 === null) {
338
+ return null;
339
+ }
340
+ const a = v1 as number;
341
+ const b = v2 as number;
342
+ switch (node.typeID) {
343
+ case TokenID.PLUS:
344
+ return a + b;
345
+ case TokenID.MINUS:
346
+ return a - b;
347
+ case TokenID.MULTIPLY:
348
+ return a * b;
349
+ }
350
+ return null;
351
+ }
352
+
353
+ private visitCard(node: AstNode): Value | null {
354
+ const base = this.visitChild(node, 0);
355
+ if (base === null || !Array.isArray(base)) {
356
+ return null;
357
+ }
358
+ return base.length;
359
+ }
360
+
361
+ private visitQuantifier(node: AstNode): Value | null {
362
+ const domain = this.visitChild(node, 1) as Value[];
363
+ if (domain === null) {
364
+ return null;
365
+ }
366
+ const isUniversal = node.typeID === TokenID.QUANTOR_UNIVERSAL;
367
+ if (domain.length === 0) {
368
+ return isUniversal ? VALUE_TRUE : VALUE_FALSE;
369
+ }
370
+
371
+ const varNodes = node.children[0].typeID === TokenID.NT_ENUM_DECL ? node.children[0].children : [node.children[0]];
372
+ const count = domain.length;
373
+ const iterators: number[] = [];
374
+ for (const declaration of varNodes) {
375
+ iterators.push(0);
376
+ this.dispatchDeclare(declaration, domain[0]);
377
+ }
378
+ let finishIteration = false;
379
+ while (!finishIteration) {
380
+ const iterationValue = this.visitChild(node, 2);
381
+ if (iterationValue === null) {
382
+ return null;
383
+ }
384
+ if ((iterationValue === VALUE_TRUE) !== isUniversal) {
385
+ return !isUniversal ? VALUE_TRUE : VALUE_FALSE;
386
+ }
387
+ let incrementIndex = iterators.length - 1;
388
+ while (true) {
389
+ if (iterators[incrementIndex] < count - 1) {
390
+ iterators[incrementIndex]++;
391
+ if (!this.tick(node, TICK_PER_QUANTIFIER)) {
392
+ return null;
393
+ }
394
+ this.dispatchDeclare(varNodes[incrementIndex], domain[iterators[incrementIndex]]);
395
+ incrementIndex = iterators.length - 1;
396
+ break;
397
+ } else if (incrementIndex === 0) {
398
+ finishIteration = true;
399
+ break;
400
+ } else {
401
+ iterators[incrementIndex] = 0;
402
+ this.dispatchDeclare(varNodes[incrementIndex], domain[0]);
403
+ incrementIndex--;
404
+ }
405
+ }
406
+ }
407
+ return isUniversal ? VALUE_TRUE : VALUE_FALSE;
408
+ }
409
+
410
+ private visitNegation(node: AstNode): Value | null {
411
+ const value = this.visitChild(node, 0);
412
+ if (value === null) {
413
+ return null;
414
+ }
415
+ return (value as number) === VALUE_TRUE ? VALUE_FALSE : VALUE_TRUE;
416
+ }
417
+
418
+ private tryEvaluateFromFirstArg(op: number, first: boolean): Value | null {
419
+ if ((op === TokenID.LOGIC_AND && !first) || (op === TokenID.LOGIC_OR && first)) {
420
+ return first ? VALUE_TRUE : VALUE_FALSE;
421
+ }
422
+ if (op === TokenID.LOGIC_IMPLICATION && !first) {
423
+ return VALUE_TRUE;
424
+ }
425
+ return null;
426
+ }
427
+
428
+ private visitLogicBinary(node: AstNode): Value | null {
429
+ const v1 = this.visitChild(node, 0);
430
+ if (v1 === null) {
431
+ return null;
432
+ }
433
+ const b1 = v1 === VALUE_TRUE;
434
+ const attempt = this.tryEvaluateFromFirstArg(node.typeID, b1);
435
+ if (attempt !== null) {
436
+ return attempt;
437
+ }
438
+
439
+ const v2 = this.visitChild(node, 1);
440
+ if (v2 === null) {
441
+ return null;
442
+ }
443
+ const b2 = v2 === VALUE_TRUE;
444
+ let result: boolean;
445
+ switch (node.typeID) {
446
+ case TokenID.LOGIC_AND:
447
+ result = b1 && b2;
448
+ break;
449
+ case TokenID.LOGIC_OR:
450
+ result = b1 || b2;
451
+ break;
452
+ case TokenID.LOGIC_IMPLICATION:
453
+ result = !b1 || b2;
454
+ break;
455
+ case TokenID.LOGIC_EQUIVALENT:
456
+ result = b1 === b2;
457
+ break;
458
+ default:
459
+ return null;
460
+ }
461
+ return result ? VALUE_TRUE : VALUE_FALSE;
462
+ }
463
+
464
+ private visitEquals(node: AstNode): Value | null {
465
+ const v1 = this.visitChild(node, 0);
466
+ if (v1 === null) {
467
+ return null;
468
+ }
469
+ const v2 = this.visitChild(node, 1);
470
+ if (v2 === null) {
471
+ return null;
472
+ }
473
+ const areEqual = compare(v1, v2) === 0;
474
+ return areEqual === (node.typeID !== TokenID.NOTEQUAL) ? VALUE_TRUE : VALUE_FALSE;
475
+ }
476
+
477
+ private visitIntegerPredicate(node: AstNode): Value | null {
478
+ const v1 = this.visitChild(node, 0);
479
+ if (v1 === null) {
480
+ return null;
481
+ }
482
+ const v2 = this.visitChild(node, 1);
483
+ if (v2 === null) {
484
+ return null;
485
+ }
486
+ const a = v1 as number;
487
+ const b = v2 as number;
488
+ let result: boolean;
489
+ switch (node.typeID) {
490
+ case TokenID.GREATER:
491
+ result = a > b;
492
+ break;
493
+ case TokenID.LESSER:
494
+ result = a < b;
495
+ break;
496
+ case TokenID.GREATER_OR_EQ:
497
+ result = a >= b;
498
+ break;
499
+ case TokenID.LESSER_OR_EQ:
500
+ result = a <= b;
501
+ break;
502
+ default:
503
+ return null;
504
+ }
505
+ return result ? VALUE_TRUE : VALUE_FALSE;
506
+ }
507
+
508
+ private visitSetexprPredicate(node: AstNode): Value | null {
509
+ const v1 = this.visitChild(node, 0);
510
+ if (v1 === null) {
511
+ return null;
512
+ }
513
+ const v2 = this.visitChild(node, 1);
514
+ if (v2 === null) {
515
+ return null;
516
+ }
517
+ let result: boolean;
518
+ switch (node.typeID) {
519
+ case TokenID.SET_IN:
520
+ result = contains(v2 as Value[], v1);
521
+ break;
522
+ case TokenID.SET_NOT_IN:
523
+ result = !contains(v2 as Value[], v1);
524
+ break;
525
+ case TokenID.SUBSET:
526
+ result = compare(v1, v2) !== 0 && isSubsetOrEq(v1 as Value[], v2 as Value[]);
527
+ break;
528
+ case TokenID.NOT_SUBSET:
529
+ result = compare(v1, v2) === 0 || !isSubsetOrEq(v1 as Value[], v2 as Value[]);
530
+ break;
531
+ case TokenID.SUBSET_OR_EQ:
532
+ result = isSubsetOrEq(v1 as Value[], v2 as Value[]);
533
+ break;
534
+ default:
535
+ return null;
536
+ }
537
+ return result ? VALUE_TRUE : VALUE_FALSE;
538
+ }
539
+
540
+ private visitDecart(node: AstNode): Value | null {
541
+ const args: Value[] = [];
542
+ for (let i = 0; i < node.children.length; i++) {
543
+ const component = this.visitChild(node, i) as Value[];
544
+ if (component === null) {
545
+ return null;
546
+ }
547
+ if (component.length === 0) {
548
+ return EmptySetV;
549
+ }
550
+ args.push(component);
551
+ }
552
+ const result = cartesianProduct(args as Value[][]);
553
+ if (result === null) {
554
+ this.onError(RSErrorCode.setOverflow, node, [String(SET_INFINITY)]);
555
+ return null;
556
+ }
557
+ return result;
558
+ }
559
+
560
+ private visitBoolean(node: AstNode): Value | null {
561
+ const base = this.visitChild(node, 0) as Value[];
562
+ if (base === null) {
563
+ return null;
564
+ }
565
+ const result = boolean(base);
566
+ if (result === null) {
567
+ this.onError(RSErrorCode.booleanBaseLimit, node.children[0], [String(BOOL_INFINITY)]);
568
+ return null;
569
+ }
570
+ return result;
571
+ }
572
+
573
+ private visitTuple(node: AstNode): Value | null {
574
+ const args: Value[] = [];
575
+ for (let i = 0; i < node.children.length; i++) {
576
+ const component = this.visitChild(node, i);
577
+ if (component === null) {
578
+ return null;
579
+ }
580
+ args.push(component);
581
+ }
582
+ return tuple(args);
583
+ }
584
+
585
+ private visitEnumeration(node: AstNode): Value | null {
586
+ const args: Value[] = [];
587
+ for (let i = 0; i < node.children.length; i++) {
588
+ const element = this.visitChild(node, i);
589
+ if (element === null) {
590
+ return null;
591
+ }
592
+ args.push(element);
593
+ }
594
+ return set(args);
595
+ }
596
+
597
+ private visitBool(node: AstNode): Value | null {
598
+ const element = this.visitChild(node, 0);
599
+ if (element === null) {
600
+ return null;
601
+ }
602
+ return [element];
603
+ }
604
+
605
+ private visitDebool(node: AstNode): Value | null {
606
+ const target = this.visitChild(node, 0) as Value[];
607
+ if (target === null) {
608
+ return null;
609
+ }
610
+ if (target.length !== 1) {
611
+ return this.onError(RSErrorCode.calcInvalidDebool, node.children[0]);
612
+ }
613
+ return target[0];
614
+ }
615
+
616
+ private visitSetexprBinary(node: AstNode): Value | null {
617
+ const v1 = this.visitChild(node, 0);
618
+ if (v1 === null) {
619
+ return null;
620
+ }
621
+ const v2 = this.visitChild(node, 1);
622
+ if (v2 === null) {
623
+ return null;
624
+ }
625
+ switch (node.typeID) {
626
+ case TokenID.SET_UNION:
627
+ return setUnion(v1 as Value[], v2 as Value[]);
628
+ case TokenID.SET_INTERSECTION:
629
+ return setIntersection(v1 as Value[], v2 as Value[]);
630
+ case TokenID.SET_MINUS:
631
+ return setDiff(v1 as Value[], v2 as Value[]);
632
+ case TokenID.SET_SYMMETRIC_MINUS:
633
+ return setSymDiff(v1 as Value[], v2 as Value[]);
634
+ }
635
+ return null;
636
+ }
637
+
638
+ private visitProjectSet(node: AstNode): Value | null {
639
+ const target = this.visitChild(node, 0);
640
+ if (target === null) {
641
+ return null;
642
+ }
643
+ const indices = getNodeIndices(node);
644
+ return projection(target as Value[][], indices);
645
+ }
646
+
647
+ private visitProjectTuple(node: AstNode): Value | null {
648
+ const target = this.visitChild(node, 0) as Value[];
649
+ if (target === null) {
650
+ return null;
651
+ }
652
+ const indices = getNodeIndices(node);
653
+ const components = indices.map(i => target[i]);
654
+ return components.length === 1 ? components[0] : tuple(components);
655
+ }
656
+
657
+ private visitFilter(node: AstNode): Value | null {
658
+ const lastIdx = node.children.length - 1;
659
+ const argVal = this.visitChild(node, lastIdx) as Value[][];
660
+ if (argVal === null) {
661
+ return null;
662
+ }
663
+ if (argVal.length === 0) {
664
+ return EmptySetV;
665
+ }
666
+
667
+ const indices = getNodeIndices(node);
668
+ const tupleParam = indices.length === lastIdx;
669
+ if (tupleParam) {
670
+ const params: Value[] = [];
671
+ for (let i = 0; i < lastIdx; i++) {
672
+ const param = this.visitChild(node, i) as Value[];
673
+ if (param === null) {
674
+ return null;
675
+ }
676
+ if (param.length === 0) {
677
+ return EmptySetV;
678
+ }
679
+ params.push(param);
680
+ }
681
+ const result: Value[] = [];
682
+ for (const element of argVal) {
683
+ let valid = true;
684
+ for (let j = 0; j < indices.length; j++) {
685
+ const comp = element[indices[j]];
686
+ const paramSet = params[j] as Value[];
687
+ if (!contains(paramSet, comp)) {
688
+ valid = false;
689
+ break;
690
+ }
691
+ }
692
+ if (valid) {
693
+ result.push(element);
694
+ }
695
+ }
696
+ return result;
697
+ } else {
698
+ const paramVal = this.visitChild(node, 0) as Value[];
699
+ if (paramVal === null) {
700
+ return null;
701
+ }
702
+ if (paramVal.length === 0) {
703
+ return EmptySetV;
704
+ }
705
+
706
+ const result: Value[] = [];
707
+ for (const element of argVal) {
708
+ const comps = indices.map(i => element[i]);
709
+ const testElement = comps.length === 1 ? comps[0] : tuple(comps);
710
+ if (contains(paramVal, testElement)) result.push(element);
711
+ }
712
+ return result;
713
+ }
714
+ }
715
+
716
+ private visitReduce(node: AstNode): Value | null {
717
+ const target = this.visitChild(node, 0);
718
+ if (target === null) {
719
+ return null;
720
+ }
721
+ return reduce(target as Value[][]);
722
+ }
723
+
724
+ private visitDeclarative(node: AstNode): Value | null {
725
+ const domain = this.visitChild(node, 1);
726
+ if (domain === null) {
727
+ return null;
728
+ }
729
+
730
+ const elements = [];
731
+ for (const element of domain as Value[]) {
732
+ if (!this.tick(node, TICK_PER_DECLARATIVE)) {
733
+ return null;
734
+ }
735
+ this.dispatchDeclare(node.children[0], element);
736
+ const value = this.visitChild(node, 2);
737
+ if (value === null) {
738
+ return null;
739
+ }
740
+ if (value === VALUE_TRUE) {
741
+ elements.push(element);
742
+ }
743
+ }
744
+ return elements.length === 0 ? EmptySetV : elements;
745
+ }
746
+
747
+ private visitImperative(node: AstNode): Value | null {
748
+ const result: Value[] = [];
749
+ const frames: IterationFrame[] = [];
750
+
751
+ let currentChild = 1;
752
+
753
+ // Advance to next iteration
754
+ const advanceIterator = (): boolean => {
755
+ while (frames.length > 0) {
756
+ const top = frames[frames.length - 1];
757
+ if (top.valueID < top.domain.length - 1) {
758
+ top.valueID++;
759
+ const nextValue = top.domain[top.valueID];
760
+ if (!this.tick(node.children[top.childID], TICK_PER_IMPERATIVE)) {
761
+ return false;
762
+ }
763
+ this.dispatchDeclare(node.children[top.childID].children[0], nextValue);
764
+ currentChild = top.childID + 1;
765
+ return true;
766
+ }
767
+ frames.pop();
768
+ }
769
+ return false;
770
+ };
771
+
772
+ while (true) {
773
+ // Iteration end - create element and pop up stack
774
+ if (currentChild >= node.children.length) {
775
+ const element = this.visitChild(node, 0);
776
+ if (element === null) {
777
+ return null;
778
+ }
779
+ result.push(element);
780
+ if (!advanceIterator()) {
781
+ break;
782
+ }
783
+ continue;
784
+ }
785
+
786
+ // Iteration node
787
+ const child = node.children[currentChild];
788
+ if (child.typeID === TokenID.ITERATE) {
789
+ const domain = this.visitChild(child, 1) as Value[];
790
+ if (domain === null) {
791
+ return null;
792
+ }
793
+ if (domain.length === 0) {
794
+ if (!advanceIterator()) {
795
+ break;
796
+ }
797
+ continue;
798
+ }
799
+ if (!this.tick(child, TICK_PER_IMPERATIVE)) {
800
+ return null;
801
+ }
802
+ frames.push({
803
+ childID: currentChild,
804
+ domain,
805
+ valueID: 0
806
+ });
807
+ this.dispatchDeclare(child.children[0], domain[0]);
808
+ currentChild++;
809
+ continue;
810
+ }
811
+
812
+ // Guard expression
813
+ const value = this.dispatchVisit(child);
814
+ if (value === null) {
815
+ return null;
816
+ }
817
+ if (value === VALUE_FALSE) {
818
+ if (!advanceIterator()) {
819
+ break;
820
+ }
821
+ continue;
822
+ }
823
+ currentChild++;
824
+ }
825
+ return set(result);
826
+ }
827
+
828
+ private visitAssign(node: AstNode): Value | null {
829
+ const value = this.visitChild(node, 1);
830
+ if (value === null) {
831
+ return null;
832
+ }
833
+ this.dispatchDeclare(node.children[0], value);
834
+ return VALUE_TRUE;
835
+ }
836
+
837
+ private visitRecursion(node: AstNode): Value | null {
838
+ const initialValue = this.visitChild(node, 1);
839
+ if (initialValue === null) {
840
+ return null;
841
+ }
842
+ const bodyIndex = node.typeID === TokenID.NT_RECURSIVE_FULL ? 3 : 2;
843
+ let current: Value = initialValue;
844
+ while (true) {
845
+ if (!this.tick(node, TICK_PER_RECURSION)) {
846
+ return null;
847
+ }
848
+
849
+ this.dispatchDeclare(node.children[0], current);
850
+ if (node.typeID === TokenID.NT_RECURSIVE_FULL) {
851
+ const pred = this.visitChild(node, 2);
852
+ if (pred === null) {
853
+ return null;
854
+ }
855
+ if (pred !== VALUE_TRUE) {
856
+ break;
857
+ }
858
+ }
859
+
860
+ const next = this.visitChild(node, bodyIndex);
861
+ if (next === null) {
862
+ return null;
863
+ }
864
+ if (compare(current, next) === 0) {
865
+ break;
866
+ }
867
+ current = next;
868
+ }
869
+
870
+ return current;
871
+ }
872
+ }
873
+
874
+ /** Imperative iteration block data. */
875
+ interface IterationFrame {
876
+ childID: number;
877
+ domain: Value[];
878
+ valueID: number;
879
+ }
880
+
881
+ /** Local variable binding with version for cache invalidation. */
882
+ interface LocalBinding {
883
+ id: number;
884
+ version: number;
885
+ value: Value;
886
+ }
887
+
888
+ /** Local variables context. */
889
+ class LocalContext {
890
+ private nextBindingId = 1;
891
+ private data = new Map<string, LocalBinding>();
892
+ private callStack: Map<string, LocalBinding>[] = [];
893
+
894
+ startScope(): void {
895
+ this.callStack.push(this.data);
896
+ this.data = new Map();
897
+ }
898
+
899
+ endScope(): void {
900
+ this.data = this.callStack.pop()!;
901
+ }
902
+
903
+ setLocal(alias: string, value: Value): void {
904
+ const existing = this.data.get(alias);
905
+ if (existing) {
906
+ existing.value = value;
907
+ existing.version++;
908
+ } else {
909
+ this.data.set(alias, { id: this.nextBindingId++, version: 0, value });
910
+ }
911
+ }
912
+
913
+ getLocal(alias: string): Value {
914
+ const binding = this.data.get(alias);
915
+ if (binding === undefined) {
916
+ throw new Error(`Local variable "${alias}" not found`);
917
+ }
918
+ return binding.value;
919
+ }
920
+
921
+ buildDependencyStamp(reads: ReadonlySet<string>): string | null {
922
+ if (reads.size === 0) {
923
+ return '';
924
+ }
925
+ const parts: string[] = [];
926
+ for (const alias of [...reads].sort()) {
927
+ const binding = this.data.get(alias);
928
+ if (binding === undefined) {
929
+ return null;
930
+ }
931
+ parts.push(`${binding.id}:${binding.version}`);
932
+ }
933
+ return parts.join('|');
934
+ }
935
+ }