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,188 @@
1
+ import { ASTNode, } from "../globals/generics.js";
2
+ /**
3
+ * Represents the main entry point of a program.
4
+ * @category Declarations
5
+ */
6
+ export class EntryPoint extends ASTNode {
7
+ /** @hidden */
8
+ expression;
9
+ /** @hidden */
10
+ identifier;
11
+ constructor(identifier, expression, loc) {
12
+ super(loc);
13
+ this.identifier = identifier;
14
+ this.expression = expression;
15
+ }
16
+ accept(visitor) {
17
+ return visitor.visitEntryPoint?.(this);
18
+ }
19
+ toJSON() {
20
+ return {
21
+ type: "EntryPoint",
22
+ identifier: this.identifier.toJSON(),
23
+ expression: this.expression.toJSON(),
24
+ };
25
+ }
26
+ }
27
+ /**
28
+ * Represents a procedure definition, typically a function without a return value.
29
+ * @category Declarations
30
+ */
31
+ export class Procedure extends ASTNode {
32
+ /** @hidden */
33
+ equations;
34
+ /** @hidden */
35
+ identifier;
36
+ constructor(identifier, equations, loc) {
37
+ super(loc);
38
+ this.identifier = identifier;
39
+ this.equations = equations;
40
+ }
41
+ accept(visitor) {
42
+ return visitor.visitProcedure?.(this);
43
+ }
44
+ toJSON() {
45
+ return {
46
+ type: "Procedure",
47
+ identifier: this.identifier.toJSON(),
48
+ equations: this.equations.map((eq) => eq.toJSON()),
49
+ };
50
+ }
51
+ }
52
+ /**
53
+ * Represents a definition of a structured data type (struct).
54
+ * @category Declarations
55
+ */
56
+ export class Structure extends ASTNode {
57
+ /** @hidden */
58
+ elements;
59
+ /** @hidden */
60
+ identifier;
61
+ constructor(identifier, elements, loc) {
62
+ super();
63
+ this.identifier = identifier;
64
+ this.elements = elements;
65
+ this.loc = loc;
66
+ }
67
+ accept(visitor) {
68
+ return visitor.visitStructure?.(this);
69
+ }
70
+ toJSON() {
71
+ return {
72
+ type: "Structure",
73
+ identifier: this.identifier.toJSON(),
74
+ expressions: this.elements.map((elem) => elem.toJSON()),
75
+ };
76
+ }
77
+ }
78
+ /**
79
+ * Represents an enumeration type definition.
80
+ * @category Declarations
81
+ */
82
+ export class Enumeration extends ASTNode {
83
+ /** @hidden */
84
+ contents;
85
+ /** @hidden */
86
+ identifier;
87
+ constructor(identifier, contents, loc) {
88
+ super(loc);
89
+ this.identifier = identifier;
90
+ this.contents = contents;
91
+ }
92
+ accept(visitor) {
93
+ return visitor.visitEnumeration?.(this);
94
+ }
95
+ toJSON() {
96
+ return {
97
+ type: "Enumeration",
98
+ identifier: this.identifier.toJSON(),
99
+ expressions: this.contents.map((c) => c.toJSON()),
100
+ };
101
+ }
102
+ }
103
+ /**
104
+ * Represents a while loop control structure.
105
+ *
106
+ * @example
107
+ * while (x > 0) { ... }
108
+ * @category Statements
109
+ */
110
+ export class While extends ASTNode {
111
+ /** @hidden */
112
+ body;
113
+ /** @hidden */
114
+ condition;
115
+ constructor(condition, body, loc) {
116
+ super(loc);
117
+ this.condition = condition;
118
+ this.body = body;
119
+ }
120
+ accept(visitor) {
121
+ return visitor.visitWhile?.(this);
122
+ }
123
+ toJSON() {
124
+ return {
125
+ type: "While",
126
+ condition: this.condition.toJSON(),
127
+ body: this.body.toJSON(),
128
+ };
129
+ }
130
+ }
131
+ /**
132
+ * Represents a repeat-until loop control structure.
133
+ * @category Statements
134
+ */
135
+ export class Repeat extends ASTNode {
136
+ /** @hidden */
137
+ body;
138
+ /** @hidden */
139
+ count;
140
+ constructor(count, body, loc) {
141
+ super(loc);
142
+ this.count = count;
143
+ this.body = body;
144
+ }
145
+ accept(visitor) {
146
+ return visitor.visitRepeat?.(this);
147
+ }
148
+ toJSON() {
149
+ return {
150
+ type: "Repeat",
151
+ count: this.count.toJSON(),
152
+ body: this.body.toJSON(),
153
+ };
154
+ }
155
+ }
156
+ /**
157
+ * Represents a C-style or range-based for loop.
158
+ * @category Statements
159
+ */
160
+ export class ForLoop extends ASTNode {
161
+ /** @hidden */
162
+ body;
163
+ /** @hidden */
164
+ update;
165
+ /** @hidden */
166
+ condition;
167
+ /** @hidden */
168
+ initialization;
169
+ constructor(initialization, condition, update, body, loc) {
170
+ super(loc);
171
+ this.initialization = initialization;
172
+ this.condition = condition;
173
+ this.update = update;
174
+ this.body = body;
175
+ }
176
+ accept(visitor) {
177
+ return visitor.visitForLoop?.(this);
178
+ }
179
+ toJSON() {
180
+ return {
181
+ type: "ForLoop",
182
+ initialization: this.initialization.toJSON(),
183
+ condition: this.condition.toJSON(),
184
+ update: this.update.toJSON(),
185
+ body: this.body.toJSON(),
186
+ };
187
+ }
188
+ }
@@ -0,0 +1,163 @@
1
+ import { ASTNode, Expression, Primitive, PrimitiveValue, SourceLocation, SymbolPrimitive } from "../globals/generics.js";
2
+ import { Pattern } from "../globals/patterns.js";
3
+ import { Visitor } from "../visitor.js";
4
+ export type Clause = Rule | Fact | Query | Primitive;
5
+ /**
6
+ * Represents a logic programming rule (Head :- Body).
7
+ *
8
+ * @example
9
+ * grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
10
+ * @category Logic
11
+ */
12
+ export declare class Rule extends ASTNode {
13
+ /** @hidden */
14
+ expressions: Expression[];
15
+ /** @hidden */
16
+ patterns: Pattern[];
17
+ /** @hidden */
18
+ identifier: SymbolPrimitive;
19
+ constructor(identifier: SymbolPrimitive, patterns: Pattern[], expressions: Expression[], loc?: SourceLocation);
20
+ accept<R>(visitor: Visitor<R>): R;
21
+ toJSON(): any;
22
+ }
23
+ /**
24
+ * Represents a call to a predicate or goal.
25
+ * @category Logic
26
+ */
27
+ export declare class Call extends ASTNode {
28
+ /** @hidden */
29
+ args: Expression[];
30
+ /** @hidden */
31
+ callee: SymbolPrimitive;
32
+ constructor(callee: SymbolPrimitive, args: Expression[], loc?: SourceLocation);
33
+ accept<R>(visitor: Visitor<R>): R;
34
+ toJSON(): any;
35
+ }
36
+ /**
37
+ * Represents a fact, a rule with no body (always true).
38
+ *
39
+ * @example
40
+ * human(socrates).
41
+ * @category Logic
42
+ */
43
+ export declare class Fact extends ASTNode {
44
+ /** @hidden */
45
+ patterns: Pattern[];
46
+ /** @hidden */
47
+ identifier: SymbolPrimitive;
48
+ constructor(identifier: SymbolPrimitive, patterns: Pattern[], loc?: SourceLocation);
49
+ accept<R>(visitor: Visitor<R>): R;
50
+ toJSON(): {
51
+ type: string;
52
+ identifier: {
53
+ type: string;
54
+ value: string;
55
+ };
56
+ patterns: any[];
57
+ };
58
+ }
59
+ /**
60
+ * Represents a query or goal to be proven.
61
+ *
62
+ * @example
63
+ * ?- human(X).
64
+ * @category Logic
65
+ */
66
+ export declare class Query extends ASTNode {
67
+ /** @hidden */
68
+ expression: Expression;
69
+ constructor(expression: Expression, loc?: SourceLocation);
70
+ accept<R>(visitor: Visitor<R>): R;
71
+ toJSON(): any;
72
+ }
73
+ /**
74
+ * Represents an existential quantification.
75
+ * @category Logic
76
+ */
77
+ export declare class Exist extends ASTNode {
78
+ /** @hidden */
79
+ patterns: Pattern[];
80
+ /** @hidden */
81
+ identifier: SymbolPrimitive;
82
+ constructor(identifier: SymbolPrimitive, patterns: Pattern[], loc?: SourceLocation);
83
+ accept<R>(visitor: Visitor<R>): R;
84
+ toJSON(): {
85
+ type: string;
86
+ identifier: {
87
+ type: string;
88
+ value: string;
89
+ };
90
+ patterns: any[];
91
+ };
92
+ }
93
+ /**
94
+ * Represents logical negation or failure-as-negation.
95
+ * @category Logic
96
+ */
97
+ export declare class Not extends ASTNode {
98
+ /** @hidden */
99
+ expressions: Expression[];
100
+ constructor(expressions: Expression[], loc?: SourceLocation);
101
+ accept<R>(visitor: Visitor<R>): R;
102
+ toJSON(): any;
103
+ }
104
+ /**
105
+ * Represents a higher-order predicate finding all solutions (e.g., findall/3).
106
+ * @category Logic
107
+ */
108
+ export declare class Findall extends ASTNode {
109
+ /** @hidden */
110
+ bag: Expression;
111
+ /** @hidden */
112
+ goal: Expression;
113
+ /** @hidden */
114
+ template: Expression;
115
+ constructor(template: Expression, goal: Expression, bag: Expression, loc?: SourceLocation);
116
+ accept<R>(visitor: Visitor<R>): R;
117
+ toJSON(): any;
118
+ }
119
+ /**
120
+ * Represents a universal quantification or forall predicate.
121
+ * @category Logic
122
+ */
123
+ export declare class Forall extends ASTNode {
124
+ /** @hidden */
125
+ action: Expression;
126
+ /** @hidden */
127
+ condition: Expression;
128
+ constructor(condition: Expression, action: Expression, loc?: SourceLocation);
129
+ accept<R>(visitor: Visitor<R>): R;
130
+ toJSON(): any;
131
+ }
132
+ /**
133
+ * Represents a single goal within a logical rule or query.
134
+ * @category Logic
135
+ */
136
+ export declare class Goal extends ASTNode {
137
+ /** @hidden */
138
+ args: Pattern[];
139
+ /** @hidden */
140
+ identifier: SymbolPrimitive;
141
+ constructor(identifier: SymbolPrimitive, args: Pattern[], loc?: SourceLocation);
142
+ accept<R>(visitor: Visitor<R>): R;
143
+ toJSON(): {
144
+ type: string;
145
+ identifier: {
146
+ type: string;
147
+ value: string;
148
+ };
149
+ arguments: any[];
150
+ };
151
+ }
152
+ export type RuntimePredicate = RuntimeFact | RuntimeRule;
153
+ export declare const isRuntimePredicate: (prim: PrimitiveValue) => prim is RuntimePredicate;
154
+ export interface RuntimeFact {
155
+ kind: "Fact";
156
+ identifier: string;
157
+ equations: Fact[];
158
+ }
159
+ export interface RuntimeRule {
160
+ kind: "Rule";
161
+ identifier: string;
162
+ equations: Rule[];
163
+ }
@@ -0,0 +1,240 @@
1
+ import { ASTNode, } from "../globals/generics.js";
2
+ /**
3
+ * Represents a logic programming rule (Head :- Body).
4
+ *
5
+ * @example
6
+ * grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
7
+ * @category Logic
8
+ */
9
+ export class Rule extends ASTNode {
10
+ /** @hidden */
11
+ expressions;
12
+ /** @hidden */
13
+ patterns;
14
+ /** @hidden */
15
+ identifier;
16
+ constructor(identifier, patterns, expressions, loc) {
17
+ super(loc);
18
+ this.identifier = identifier;
19
+ this.patterns = patterns;
20
+ this.expressions = expressions;
21
+ }
22
+ accept(visitor) {
23
+ return visitor.visitRule?.(this);
24
+ }
25
+ toJSON() {
26
+ return {
27
+ type: "Rule",
28
+ identifier: this.identifier.toJSON(),
29
+ patterns: this.patterns.map((p) => p.toJSON()),
30
+ expressions: this.expressions.map((expr) => expr.toJSON()),
31
+ };
32
+ }
33
+ }
34
+ /**
35
+ * Represents a call to a predicate or goal.
36
+ * @category Logic
37
+ */
38
+ export class Call extends ASTNode {
39
+ /** @hidden */
40
+ args;
41
+ /** @hidden */
42
+ callee;
43
+ constructor(callee, args, loc) {
44
+ super(loc);
45
+ this.callee = callee;
46
+ this.args = args;
47
+ }
48
+ accept(visitor) {
49
+ return visitor.visitCall?.(this);
50
+ }
51
+ toJSON() {
52
+ return {
53
+ type: "Call",
54
+ callee: this.callee.toJSON(),
55
+ patterns: this.args.map((p) => p.toJSON()),
56
+ };
57
+ }
58
+ }
59
+ /**
60
+ * Represents a fact, a rule with no body (always true).
61
+ *
62
+ * @example
63
+ * human(socrates).
64
+ * @category Logic
65
+ */
66
+ export class Fact extends ASTNode {
67
+ /** @hidden */
68
+ patterns;
69
+ /** @hidden */
70
+ identifier;
71
+ constructor(identifier, patterns, loc) {
72
+ super(loc);
73
+ this.identifier = identifier;
74
+ this.patterns = patterns;
75
+ }
76
+ accept(visitor) {
77
+ return visitor.visitFact?.(this);
78
+ }
79
+ toJSON() {
80
+ return {
81
+ type: "Fact",
82
+ identifier: this.identifier.toJSON(),
83
+ patterns: this.patterns.map((p) => p.toJSON()),
84
+ };
85
+ }
86
+ }
87
+ /**
88
+ * Represents a query or goal to be proven.
89
+ *
90
+ * @example
91
+ * ?- human(X).
92
+ * @category Logic
93
+ */
94
+ export class Query extends ASTNode {
95
+ /** @hidden */
96
+ expression;
97
+ constructor(expression, loc) {
98
+ super(loc);
99
+ this.expression = expression;
100
+ }
101
+ accept(visitor) {
102
+ return visitor.visitQuery?.(this);
103
+ }
104
+ toJSON() {
105
+ return {
106
+ type: "Query",
107
+ expression: this.expression.toJSON(),
108
+ };
109
+ }
110
+ }
111
+ /**
112
+ * Represents an existential quantification.
113
+ * @category Logic
114
+ */
115
+ export class Exist extends ASTNode {
116
+ /** @hidden */
117
+ patterns;
118
+ /** @hidden */
119
+ identifier;
120
+ constructor(identifier, patterns, loc) {
121
+ super(loc);
122
+ this.identifier = identifier;
123
+ this.patterns = patterns;
124
+ }
125
+ accept(visitor) {
126
+ return visitor.visitExist?.(this);
127
+ }
128
+ toJSON() {
129
+ return {
130
+ type: "Exist",
131
+ identifier: this.identifier.toJSON(),
132
+ patterns: this.patterns.map((p) => p.toJSON()),
133
+ };
134
+ }
135
+ }
136
+ /**
137
+ * Represents logical negation or failure-as-negation.
138
+ * @category Logic
139
+ */
140
+ export class Not extends ASTNode {
141
+ /** @hidden */
142
+ expressions;
143
+ constructor(expressions, loc) {
144
+ super(loc);
145
+ this.expressions = expressions;
146
+ }
147
+ accept(visitor) {
148
+ return visitor.visitNot?.(this);
149
+ }
150
+ toJSON() {
151
+ return {
152
+ type: "Not",
153
+ expressions: this.expressions.map((expr) => expr.toJSON()),
154
+ };
155
+ }
156
+ }
157
+ /**
158
+ * Represents a higher-order predicate finding all solutions (e.g., findall/3).
159
+ * @category Logic
160
+ */
161
+ export class Findall extends ASTNode {
162
+ /** @hidden */
163
+ bag;
164
+ /** @hidden */
165
+ goal;
166
+ /** @hidden */
167
+ template;
168
+ constructor(template, goal, bag, loc) {
169
+ super(loc);
170
+ this.template = template;
171
+ this.goal = goal;
172
+ this.bag = bag;
173
+ }
174
+ accept(visitor) {
175
+ return visitor.visitFindall?.(this);
176
+ }
177
+ toJSON() {
178
+ return {
179
+ type: "Findall",
180
+ template: this.template.toJSON(),
181
+ goal: this.goal.toJSON(),
182
+ bag: this.bag.toJSON(),
183
+ };
184
+ }
185
+ }
186
+ /**
187
+ * Represents a universal quantification or forall predicate.
188
+ * @category Logic
189
+ */
190
+ export class Forall extends ASTNode {
191
+ /** @hidden */
192
+ action;
193
+ /** @hidden */
194
+ condition;
195
+ constructor(condition, action, loc) {
196
+ super(loc);
197
+ this.condition = condition;
198
+ this.action = action;
199
+ }
200
+ accept(visitor) {
201
+ return visitor.visitForall?.(this);
202
+ }
203
+ toJSON() {
204
+ return {
205
+ type: "Forall",
206
+ condition: this.condition.toJSON(),
207
+ action: this.action.toJSON(),
208
+ };
209
+ }
210
+ }
211
+ /**
212
+ * Represents a single goal within a logical rule or query.
213
+ * @category Logic
214
+ */
215
+ export class Goal extends ASTNode {
216
+ /** @hidden */
217
+ args;
218
+ /** @hidden */
219
+ identifier;
220
+ constructor(identifier, args, loc) {
221
+ super(loc);
222
+ this.identifier = identifier;
223
+ this.args = args;
224
+ }
225
+ accept(visitor) {
226
+ return visitor.visitGoal?.(this);
227
+ }
228
+ toJSON() {
229
+ return {
230
+ type: "Goal",
231
+ identifier: this.identifier.toJSON(),
232
+ arguments: this.args.map((arg) => arg.toJSON()),
233
+ };
234
+ }
235
+ }
236
+ export const isRuntimePredicate = (prim) => {
237
+ return (typeof prim === "object" &&
238
+ "kind" in prim &&
239
+ (prim.kind === "Fact" || prim.kind === "Rule"));
240
+ };