yukigo-ast 0.1.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 (42) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/globals/generics.d.ts +740 -0
  3. package/dist/globals/generics.js +1128 -0
  4. package/dist/globals/helpers.d.ts +4 -0
  5. package/dist/globals/helpers.js +76 -0
  6. package/dist/globals/operators.d.ts +251 -0
  7. package/dist/globals/operators.js +336 -0
  8. package/dist/globals/patterns.d.ts +170 -0
  9. package/dist/globals/patterns.js +261 -0
  10. package/dist/globals/runtime.d.ts +22 -0
  11. package/dist/globals/runtime.js +6 -0
  12. package/dist/globals/types.d.ts +185 -0
  13. package/dist/globals/types.js +329 -0
  14. package/dist/index.d.ts +12 -0
  15. package/dist/index.js +12 -0
  16. package/dist/paradigms/data.d.ts +246 -0
  17. package/dist/paradigms/data.js +348 -0
  18. package/dist/paradigms/functional.d.ts +59 -0
  19. package/dist/paradigms/functional.js +103 -0
  20. package/dist/paradigms/imperative.d.ts +124 -0
  21. package/dist/paradigms/imperative.js +188 -0
  22. package/dist/paradigms/logic.d.ts +163 -0
  23. package/dist/paradigms/logic.js +240 -0
  24. package/dist/paradigms/object.d.ts +201 -0
  25. package/dist/paradigms/object.js +308 -0
  26. package/dist/visitor.d.ts +254 -0
  27. package/dist/visitor.js +426 -0
  28. package/package.json +25 -0
  29. package/src/globals/generics.ts +1421 -0
  30. package/src/globals/helpers.ts +107 -0
  31. package/src/globals/operators.ts +515 -0
  32. package/src/globals/patterns.ts +321 -0
  33. package/src/globals/runtime.ts +33 -0
  34. package/src/globals/types.ts +375 -0
  35. package/src/index.ts +12 -0
  36. package/src/paradigms/data.ts +393 -0
  37. package/src/paradigms/functional.ts +129 -0
  38. package/src/paradigms/imperative.ts +232 -0
  39. package/src/paradigms/logic.ts +314 -0
  40. package/src/paradigms/object.ts +366 -0
  41. package/src/visitor.ts +692 -0
  42. package/tsconfig.json +22 -0
@@ -0,0 +1,232 @@
1
+ import {
2
+ ASTNode,
3
+ Equation,
4
+ Expression,
5
+ Sequence,
6
+ SourceLocation,
7
+ SymbolPrimitive,
8
+ Variable,
9
+ } from "../globals/generics.js";
10
+ import { Visitor } from "../visitor.js";
11
+
12
+ /**
13
+ * Represents the main entry point of a program.
14
+ * @category Declarations
15
+ */
16
+ export class EntryPoint extends ASTNode {
17
+ /** @hidden */
18
+ public expression: Sequence;
19
+ /** @hidden */
20
+ public identifier: SymbolPrimitive;
21
+
22
+ constructor(
23
+ identifier: SymbolPrimitive,
24
+ expression: Sequence,
25
+ loc?: SourceLocation
26
+ ) {
27
+ super(loc);
28
+ this.identifier = identifier;
29
+ this.expression = expression;
30
+ }
31
+ public accept<R>(visitor: Visitor<R>): R {
32
+ return visitor.visitEntryPoint?.(this);
33
+ }
34
+ public toJSON() {
35
+ return {
36
+ type: "EntryPoint",
37
+ identifier: this.identifier.toJSON(),
38
+ expression: this.expression.toJSON(),
39
+ };
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Represents a procedure definition, typically a function without a return value.
45
+ * @category Declarations
46
+ */
47
+ export class Procedure extends ASTNode {
48
+ /** @hidden */
49
+ public equations: Equation[];
50
+ /** @hidden */
51
+ public identifier: SymbolPrimitive;
52
+
53
+ constructor(
54
+ identifier: SymbolPrimitive,
55
+ equations: Equation[],
56
+ loc?: SourceLocation
57
+ ) {
58
+ super(loc);
59
+ this.identifier = identifier;
60
+ this.equations = equations;
61
+ }
62
+ public accept<R>(visitor: Visitor<R>): R {
63
+ return visitor.visitProcedure?.(this);
64
+ }
65
+ public toJSON() {
66
+ return {
67
+ type: "Procedure",
68
+ identifier: this.identifier.toJSON(),
69
+ equations: this.equations.map((eq) => eq.toJSON()),
70
+ };
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Represents a definition of a structured data type (struct).
76
+ * @category Declarations
77
+ */
78
+ export class Structure extends ASTNode {
79
+ /** @hidden */
80
+ public elements: (Variable | Structure)[];
81
+ /** @hidden */
82
+ public identifier: SymbolPrimitive;
83
+
84
+ constructor(
85
+ identifier: SymbolPrimitive,
86
+ elements: (Variable | Structure)[],
87
+ loc?: SourceLocation
88
+ ) {
89
+ super();
90
+ this.identifier = identifier;
91
+ this.elements = elements;
92
+ this.loc = loc;
93
+ }
94
+ public accept<R>(visitor: Visitor<R>): R {
95
+ return visitor.visitStructure?.(this);
96
+ }
97
+ public toJSON() {
98
+ return {
99
+ type: "Structure",
100
+ identifier: this.identifier.toJSON(),
101
+ expressions: this.elements.map((elem) => elem.toJSON()),
102
+ };
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Represents an enumeration type definition.
108
+ * @category Declarations
109
+ */
110
+ export class Enumeration extends ASTNode {
111
+ /** @hidden */
112
+ public contents: SymbolPrimitive[];
113
+ /** @hidden */
114
+ public identifier: SymbolPrimitive;
115
+
116
+ constructor(
117
+ identifier: SymbolPrimitive,
118
+ contents: SymbolPrimitive[],
119
+ loc?: SourceLocation
120
+ ) {
121
+ super(loc);
122
+ this.identifier = identifier;
123
+ this.contents = contents;
124
+ }
125
+ public accept<R>(visitor: Visitor<R>): R {
126
+ return visitor.visitEnumeration?.(this);
127
+ }
128
+ public toJSON() {
129
+ return {
130
+ type: "Enumeration",
131
+ identifier: this.identifier.toJSON(),
132
+ expressions: this.contents.map((c) => c.toJSON()),
133
+ };
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Represents a while loop control structure.
139
+ *
140
+ * @example
141
+ * while (x > 0) { ... }
142
+ * @category Statements
143
+ */
144
+ export class While extends ASTNode {
145
+ /** @hidden */
146
+ public body: Expression;
147
+ /** @hidden */
148
+ public condition: Expression;
149
+
150
+ constructor(condition: Expression, body: Expression, loc?: SourceLocation) {
151
+ super(loc);
152
+ this.condition = condition;
153
+ this.body = body;
154
+ }
155
+ public accept<R>(visitor: Visitor<R>): R {
156
+ return visitor.visitWhile?.(this);
157
+ }
158
+ public toJSON() {
159
+ return {
160
+ type: "While",
161
+ condition: this.condition.toJSON(),
162
+ body: this.body.toJSON(),
163
+ };
164
+ }
165
+ }
166
+ /**
167
+ * Represents a repeat-until loop control structure.
168
+ * @category Statements
169
+ */
170
+ export class Repeat extends ASTNode {
171
+ /** @hidden */
172
+ public body: Expression;
173
+ /** @hidden */
174
+ public count: Expression;
175
+
176
+ constructor(count: Expression, body: Expression, loc?: SourceLocation) {
177
+ super(loc);
178
+ this.count = count;
179
+ this.body = body;
180
+ }
181
+ public accept<R>(visitor: Visitor<R>): R {
182
+ return visitor.visitRepeat?.(this);
183
+ }
184
+ public toJSON() {
185
+ return {
186
+ type: "Repeat",
187
+ count: this.count.toJSON(),
188
+ body: this.body.toJSON(),
189
+ };
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Represents a C-style or range-based for loop.
195
+ * @category Statements
196
+ */
197
+ export class ForLoop extends ASTNode {
198
+ /** @hidden */
199
+ public body: Expression;
200
+ /** @hidden */
201
+ public update: Expression;
202
+ /** @hidden */
203
+ public condition: Expression;
204
+ /** @hidden */
205
+ public initialization: Expression;
206
+
207
+ constructor(
208
+ initialization: Expression,
209
+ condition: Expression,
210
+ update: Expression,
211
+ body: Expression,
212
+ loc?: SourceLocation
213
+ ) {
214
+ super(loc);
215
+ this.initialization = initialization;
216
+ this.condition = condition;
217
+ this.update = update;
218
+ this.body = body;
219
+ }
220
+ public accept<R>(visitor: Visitor<R>): R {
221
+ return visitor.visitForLoop?.(this);
222
+ }
223
+ public toJSON() {
224
+ return {
225
+ type: "ForLoop",
226
+ initialization: this.initialization.toJSON(),
227
+ condition: this.condition.toJSON(),
228
+ update: this.update.toJSON(),
229
+ body: this.body.toJSON(),
230
+ };
231
+ }
232
+ }
@@ -0,0 +1,314 @@
1
+ import {
2
+ ASTNode,
3
+ Expression,
4
+ Primitive,
5
+ PrimitiveValue,
6
+ SourceLocation,
7
+ SymbolPrimitive,
8
+ } from "../globals/generics.js";
9
+ import { Pattern } from "../globals/patterns.js";
10
+ import { Visitor } from "../visitor.js";
11
+
12
+ export type Clause = Rule | Fact | Query | Primitive;
13
+
14
+ /**
15
+ * Represents a logic programming rule (Head :- Body).
16
+ *
17
+ * @example
18
+ * grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
19
+ * @category Logic
20
+ */
21
+ export class Rule extends ASTNode {
22
+ /** @hidden */
23
+ public expressions: Expression[];
24
+ /** @hidden */
25
+ public patterns: Pattern[];
26
+ /** @hidden */
27
+ public identifier: SymbolPrimitive;
28
+
29
+ constructor(
30
+ identifier: SymbolPrimitive,
31
+ patterns: Pattern[],
32
+ expressions: Expression[],
33
+ loc?: SourceLocation
34
+ ) {
35
+ super(loc);
36
+ this.identifier = identifier;
37
+ this.patterns = patterns;
38
+ this.expressions = expressions;
39
+ }
40
+ public accept<R>(visitor: Visitor<R>): R {
41
+ return visitor.visitRule?.(this);
42
+ }
43
+ public toJSON() {
44
+ return {
45
+ type: "Rule",
46
+ identifier: this.identifier.toJSON(),
47
+ patterns: this.patterns.map((p) => p.toJSON()),
48
+ expressions: this.expressions.map((expr) => expr.toJSON()),
49
+ };
50
+ }
51
+ }
52
+ /**
53
+ * Represents a call to a predicate or goal.
54
+ * @category Logic
55
+ */
56
+ export class Call extends ASTNode {
57
+ /** @hidden */
58
+ public args: Expression[];
59
+ /** @hidden */
60
+ public callee: SymbolPrimitive;
61
+
62
+ constructor(
63
+ callee: SymbolPrimitive,
64
+ args: Expression[],
65
+ loc?: SourceLocation
66
+ ) {
67
+ super(loc);
68
+ this.callee = callee;
69
+ this.args = args;
70
+ }
71
+ public accept<R>(visitor: Visitor<R>): R {
72
+ return visitor.visitCall?.(this);
73
+ }
74
+ public toJSON() {
75
+ return {
76
+ type: "Call",
77
+ callee: this.callee.toJSON(),
78
+ patterns: this.args.map((p) => p.toJSON()),
79
+ };
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Represents a fact, a rule with no body (always true).
85
+ *
86
+ * @example
87
+ * human(socrates).
88
+ * @category Logic
89
+ */
90
+ export class Fact extends ASTNode {
91
+ /** @hidden */
92
+ public patterns: Pattern[];
93
+ /** @hidden */
94
+ public identifier: SymbolPrimitive;
95
+
96
+ constructor(
97
+ identifier: SymbolPrimitive,
98
+ patterns: Pattern[],
99
+ loc?: SourceLocation
100
+ ) {
101
+ super(loc);
102
+ this.identifier = identifier;
103
+ this.patterns = patterns;
104
+ }
105
+ public accept<R>(visitor: Visitor<R>): R {
106
+ return visitor.visitFact?.(this);
107
+ }
108
+ public toJSON() {
109
+ return {
110
+ type: "Fact",
111
+ identifier: this.identifier.toJSON(),
112
+ patterns: this.patterns.map((p) => p.toJSON()),
113
+ };
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Represents a query or goal to be proven.
119
+ *
120
+ * @example
121
+ * ?- human(X).
122
+ * @category Logic
123
+ */
124
+ export class Query extends ASTNode {
125
+ /** @hidden */
126
+ public expression: Expression;
127
+
128
+ constructor(expression: Expression, loc?: SourceLocation) {
129
+ super(loc);
130
+ this.expression = expression;
131
+ }
132
+ public accept<R>(visitor: Visitor<R>): R {
133
+ return visitor.visitQuery?.(this);
134
+ }
135
+ public toJSON() {
136
+ return {
137
+ type: "Query",
138
+ expression: this.expression.toJSON(),
139
+ };
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Represents an existential quantification.
145
+ * @category Logic
146
+ */
147
+ export class Exist extends ASTNode {
148
+ /** @hidden */
149
+ public patterns: Pattern[];
150
+ /** @hidden */
151
+ public identifier: SymbolPrimitive;
152
+
153
+ constructor(
154
+ identifier: SymbolPrimitive,
155
+ patterns: Pattern[],
156
+ loc?: SourceLocation
157
+ ) {
158
+ super(loc);
159
+ this.identifier = identifier;
160
+ this.patterns = patterns;
161
+ }
162
+ public accept<R>(visitor: Visitor<R>): R {
163
+ return visitor.visitExist?.(this);
164
+ }
165
+ public toJSON() {
166
+ return {
167
+ type: "Exist",
168
+ identifier: this.identifier.toJSON(),
169
+ patterns: this.patterns.map((p) => p.toJSON()),
170
+ };
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Represents logical negation or failure-as-negation.
176
+ * @category Logic
177
+ */
178
+ export class Not extends ASTNode {
179
+ /** @hidden */
180
+ public expressions: Expression[];
181
+
182
+ constructor(expressions: Expression[], loc?: SourceLocation) {
183
+ super(loc);
184
+ this.expressions = expressions;
185
+ }
186
+ public accept<R>(visitor: Visitor<R>): R {
187
+ return visitor.visitNot?.(this);
188
+ }
189
+ public toJSON() {
190
+ return {
191
+ type: "Not",
192
+ expressions: this.expressions.map((expr) => expr.toJSON()),
193
+ };
194
+ }
195
+ }
196
+ /**
197
+ * Represents a higher-order predicate finding all solutions (e.g., findall/3).
198
+ * @category Logic
199
+ */
200
+ export class Findall extends ASTNode {
201
+ /** @hidden */
202
+ public bag: Expression;
203
+ /** @hidden */
204
+ public goal: Expression;
205
+ /** @hidden */
206
+ public template: Expression;
207
+
208
+ constructor(
209
+ template: Expression,
210
+ goal: Expression,
211
+ bag: Expression,
212
+ loc?: SourceLocation
213
+ ) {
214
+ super(loc);
215
+ this.template = template;
216
+ this.goal = goal;
217
+ this.bag = bag;
218
+ }
219
+ public accept<R>(visitor: Visitor<R>): R {
220
+ return visitor.visitFindall?.(this);
221
+ }
222
+ public toJSON() {
223
+ return {
224
+ type: "Findall",
225
+ template: this.template.toJSON(),
226
+ goal: this.goal.toJSON(),
227
+ bag: this.bag.toJSON(),
228
+ };
229
+ }
230
+ }
231
+ /**
232
+ * Represents a universal quantification or forall predicate.
233
+ * @category Logic
234
+ */
235
+ export class Forall extends ASTNode {
236
+ /** @hidden */
237
+ public action: Expression;
238
+ /** @hidden */
239
+ public condition: Expression;
240
+
241
+ constructor(
242
+ condition: Expression,
243
+ action: Expression,
244
+ loc?: SourceLocation
245
+ ) {
246
+ super(loc);
247
+ this.condition = condition;
248
+ this.action = action;
249
+ }
250
+ public accept<R>(visitor: Visitor<R>): R {
251
+ return visitor.visitForall?.(this);
252
+ }
253
+ public toJSON() {
254
+ return {
255
+ type: "Forall",
256
+ condition: this.condition.toJSON(),
257
+ action: this.action.toJSON(),
258
+ };
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Represents a single goal within a logical rule or query.
264
+ * @category Logic
265
+ */
266
+ export class Goal extends ASTNode {
267
+ /** @hidden */
268
+ public args: Pattern[];
269
+ /** @hidden */
270
+ public identifier: SymbolPrimitive;
271
+
272
+ constructor(
273
+ identifier: SymbolPrimitive,
274
+ args: Pattern[],
275
+ loc?: SourceLocation
276
+ ) {
277
+ super(loc);
278
+ this.identifier = identifier;
279
+ this.args = args;
280
+ }
281
+ public accept<R>(visitor: Visitor<R>): R {
282
+ return visitor.visitGoal?.(this);
283
+ }
284
+ public toJSON() {
285
+ return {
286
+ type: "Goal",
287
+ identifier: this.identifier.toJSON(),
288
+ arguments: this.args.map((arg) => arg.toJSON()),
289
+ };
290
+ }
291
+ }
292
+
293
+ // Runtime Types
294
+
295
+ export type RuntimePredicate = RuntimeFact | RuntimeRule;
296
+
297
+ export const isRuntimePredicate = (prim: PrimitiveValue): prim is RuntimePredicate => {
298
+ return (
299
+ typeof prim === "object" &&
300
+ "kind" in prim &&
301
+ (prim.kind === "Fact" || prim.kind === "Rule")
302
+ );
303
+ };
304
+
305
+ export interface RuntimeFact {
306
+ kind: "Fact";
307
+ identifier: string;
308
+ equations: Fact[];
309
+ }
310
+ export interface RuntimeRule {
311
+ kind: "Rule";
312
+ identifier: string;
313
+ equations: Rule[];
314
+ }