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,107 @@
1
+ import {
2
+ YukigoPrimitive,
3
+ ASTNode,
4
+ BooleanPrimitive,
5
+ NumberPrimitive,
6
+ ListPrimitive,
7
+ CharPrimitive,
8
+ StringPrimitive,
9
+ NilPrimitive,
10
+ SymbolPrimitive,
11
+ Primitive,
12
+ } from "./generics.js";
13
+ import {
14
+ ApplicationPattern,
15
+ AsPattern,
16
+ ConsPattern,
17
+ ConstructorPattern,
18
+ FunctorPattern,
19
+ ListPattern,
20
+ LiteralPattern,
21
+ Pattern,
22
+ TuplePattern,
23
+ UnionPattern,
24
+ VariablePattern,
25
+ WildcardPattern,
26
+ } from "./patterns.js";
27
+
28
+ /* type Visitor = {
29
+ [key: string]: (node: ASTNode, parent?: ASTNode) => boolean | void;
30
+ }; */
31
+
32
+ /* export function traverse(ast: AST | ASTNode, visitor: Visitor) {
33
+ function runVisitor(node: ASTNode, parent?: ASTNode) {
34
+ if (!node || typeof node !== "object" || !("type" in node)) {
35
+ return false;
36
+ }
37
+
38
+ let stop = false;
39
+
40
+ // Check for specific node type visitor
41
+ if (visitor[node.type]) {
42
+ stop = visitor[node.type](node, parent) === true;
43
+ }
44
+
45
+ // Check for wildcard visitor
46
+ if (!stop && visitor["*"]) {
47
+ stop = visitor["*"](node, parent) === true;
48
+ }
49
+
50
+ if (stop) return true;
51
+
52
+ // Recursively visit children based on their type
53
+ for (const key in node) {
54
+ if (!node.hasOwnProperty(key)) continue;
55
+
56
+ const value = node[key];
57
+
58
+ if (!value) return false;
59
+
60
+ const items = Array.isArray(value) ? value : [value];
61
+
62
+ for (const item of items) {
63
+ if (typeof item === "object" && item.type && runVisitor(item, node)) {
64
+ return true;
65
+ }
66
+ }
67
+ }
68
+ return false;
69
+ }
70
+
71
+ // Handle the initial AST array or a single node
72
+ if (Array.isArray(ast)) {
73
+ for (const node of ast) {
74
+ if (runVisitor(node)) break;
75
+ }
76
+ } else {
77
+ runVisitor(ast);
78
+ }
79
+ } */
80
+
81
+ // disgusting...
82
+ export function isYukigoPrimitive(node: ASTNode): node is Primitive {
83
+ return (
84
+ node instanceof NumberPrimitive ||
85
+ node instanceof BooleanPrimitive ||
86
+ node instanceof ListPrimitive ||
87
+ node instanceof CharPrimitive ||
88
+ node instanceof StringPrimitive ||
89
+ node instanceof NilPrimitive ||
90
+ node instanceof SymbolPrimitive
91
+ );
92
+ }
93
+ export function isPattern(node: ASTNode): node is Pattern {
94
+ return (
95
+ node instanceof VariablePattern ||
96
+ node instanceof LiteralPattern ||
97
+ node instanceof ApplicationPattern ||
98
+ node instanceof TuplePattern ||
99
+ node instanceof ListPattern ||
100
+ node instanceof FunctorPattern ||
101
+ node instanceof AsPattern ||
102
+ node instanceof WildcardPattern ||
103
+ node instanceof ConstructorPattern ||
104
+ node instanceof ConsPattern ||
105
+ node instanceof UnionPattern
106
+ );
107
+ }
@@ -0,0 +1,515 @@
1
+ import { Visitor } from "../visitor.js";
2
+ import { ASTNode, Expression, SourceLocation } from "./generics.js";
3
+
4
+ export type ArithmeticBinaryOperator =
5
+ | "Plus"
6
+ | "Minus"
7
+ | "Multiply"
8
+ | "Divide"
9
+ | "Modulo"
10
+ | "Power"
11
+ | "Max"
12
+ | "Min";
13
+ export type ArithmeticUnaryOperator =
14
+ | "Round"
15
+ | "Absolute"
16
+ | "Ceil"
17
+ | "Floor"
18
+ | "Negation"
19
+ | "Sqrt";
20
+
21
+ export type ComparisonOperatorType =
22
+ | "Equal"
23
+ | "NotEqual"
24
+ | "Same"
25
+ | "NotSame"
26
+ | "Similar"
27
+ | "NotSimilar"
28
+ | "GreaterOrEqualThan"
29
+ | "GreaterThan"
30
+ | "LessOrEqualThan"
31
+ | "LessThan";
32
+
33
+ export type LogicalBinaryOperator = "And" | "Or";
34
+ export type LogicalUnaryOperator = "Negation";
35
+
36
+ export type BitwiseBinaryOperator =
37
+ | "BitwiseOr"
38
+ | "BitwiseAnd"
39
+ | "BitwiseLeftShift"
40
+ | "BitwiseRightShift"
41
+ | "BitwiseUnsignedRightShift"
42
+ | "BitwiseXor";
43
+
44
+ export type BitwiseUnaryOperator = "BitwiseNot";
45
+
46
+ export type ListBinaryOperator =
47
+ | "Push"
48
+ | "Concat"
49
+ | "Inject"
50
+ | "Gather"
51
+ | "Collect"
52
+ | "AllSatisfy"
53
+ | "AnySatisfy"
54
+ | "Select"
55
+ | "Detect"
56
+ | "GetAt"
57
+ | "Count"
58
+ | "Slice"
59
+ | "SetAt";
60
+
61
+ export type ListUnaryOperator = "Size" | "DetectMax" | "DetectMin" | "Flatten";
62
+
63
+ export type UnifyOperator = "Unify";
64
+
65
+ export type AssignOperator = "Assign";
66
+
67
+ export type StringBinaryOperator = "Concat";
68
+
69
+ export type UnaryOperator =
70
+ | ArithmeticUnaryOperator
71
+ | LogicalUnaryOperator
72
+ | ListUnaryOperator;
73
+
74
+ export type BinaryOperator =
75
+ | ArithmeticBinaryOperator
76
+ | ComparisonOperatorType
77
+ | LogicalBinaryOperator
78
+ | BitwiseBinaryOperator
79
+ | BitwiseUnaryOperator
80
+ | AssignOperator
81
+ | UnifyOperator
82
+ | StringBinaryOperator
83
+ | ListBinaryOperator;
84
+
85
+ export type Operator = UnaryOperator | BinaryOperator;
86
+
87
+ /**
88
+ * Represents a unary arithmetic operation like negation (-x).
89
+ * @category Operators
90
+ */
91
+ export class ArithmeticUnaryOperation extends ASTNode {
92
+ /** @hidden */
93
+ public operand: Expression;
94
+ /** @hidden */
95
+ public operator: ArithmeticUnaryOperator;
96
+
97
+ constructor(
98
+ operator: ArithmeticUnaryOperator,
99
+ operand: Expression,
100
+ loc?: SourceLocation
101
+ ) {
102
+ super(loc);
103
+ this.operator = operator;
104
+ this.operand = operand;
105
+ }
106
+ public accept<R>(visitor: Visitor<R>): R {
107
+ return visitor.visitArithmeticUnaryOperation?.(this);
108
+ }
109
+ public toJSON() {
110
+ return {
111
+ type: "ArithmeticUnaryOperation",
112
+ operator: this.operator,
113
+ operand: this.operand,
114
+ };
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Represents a binary arithmetic operation like addition or multiplication.
120
+ * @category Operators
121
+ */
122
+ export class ArithmeticBinaryOperation extends ASTNode {
123
+ /** @hidden */
124
+ public right: Expression;
125
+ /** @hidden */
126
+ public left: Expression;
127
+ /** @hidden */
128
+ public operator: ArithmeticBinaryOperator;
129
+
130
+ constructor(
131
+ operator: ArithmeticBinaryOperator,
132
+ left: Expression,
133
+ right: Expression,
134
+ loc?: SourceLocation
135
+ ) {
136
+ super(loc);
137
+ this.operator = operator;
138
+ this.left = left;
139
+ this.right = right;
140
+ }
141
+ public accept<R>(visitor: Visitor<R>): R {
142
+ return visitor.visitArithmeticBinaryOperation?.(this);
143
+ }
144
+ public toJSON() {
145
+ return {
146
+ type: "ArithmeticBinaryOperation",
147
+ operator: this.operator,
148
+ left: this.left,
149
+ right: this.right,
150
+ };
151
+ }
152
+ }
153
+ /**
154
+ * Represents a unary operation on a list, such as getting the head or tail.
155
+ * @category Operators
156
+ */
157
+ export class ListUnaryOperation extends ASTNode {
158
+ /** @hidden */
159
+ public operand: Expression;
160
+ /** @hidden */
161
+ public operator: ListUnaryOperator;
162
+
163
+ constructor(
164
+ operator: ListUnaryOperator,
165
+ operand: Expression,
166
+ loc?: SourceLocation
167
+ ) {
168
+ super(loc);
169
+ this.operator = operator;
170
+ this.operand = operand;
171
+ }
172
+ public accept<R>(visitor: Visitor<R>): R {
173
+ return visitor.visitListUnaryOperation?.(this);
174
+ }
175
+ public toJSON() {
176
+ return {
177
+ type: "ListUnaryOperation",
178
+ operator: this.operator,
179
+ operand: this.operand,
180
+ };
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Represents a binary operation on lists, such as concatenation.
186
+ * @category Operators
187
+ */
188
+ export class ListBinaryOperation extends ASTNode {
189
+ /** @hidden */
190
+ public right: Expression;
191
+ /** @hidden */
192
+ public left: Expression;
193
+ /** @hidden */
194
+ public operator: ListBinaryOperator;
195
+
196
+ constructor(
197
+ operator: ListBinaryOperator,
198
+ left: Expression,
199
+ right: Expression,
200
+ loc?: SourceLocation
201
+ ) {
202
+ super(loc);
203
+ this.operator = operator;
204
+ this.left = left;
205
+ this.right = right;
206
+ }
207
+ public accept<R>(visitor: Visitor<R>): R {
208
+ return visitor.visitListBinaryOperation?.(this);
209
+ }
210
+ public toJSON() {
211
+ return {
212
+ type: "ListBinaryOperation",
213
+ operator: this.operator,
214
+ left: this.left,
215
+ right: this.right,
216
+ };
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Represents a comparison between two values (e.g., >, <, ==).
222
+ * @category Operators
223
+ */
224
+ export class ComparisonOperation extends ASTNode {
225
+ /** @hidden */
226
+ public right: Expression;
227
+ /** @hidden */
228
+ public left: Expression;
229
+ /** @hidden */
230
+ public operator: ComparisonOperatorType;
231
+
232
+ constructor(
233
+ operator: ComparisonOperatorType,
234
+ left: Expression,
235
+ right: Expression,
236
+ loc?: SourceLocation
237
+ ) {
238
+ super(loc);
239
+ this.operator = operator;
240
+ this.left = left;
241
+ this.right = right;
242
+ }
243
+ public accept<R>(visitor: Visitor<R>): R {
244
+ return visitor.visitComparisonOperation?.(this);
245
+ }
246
+ public toJSON() {
247
+ return {
248
+ type: "ComparisonOperation",
249
+ operator: this.operator,
250
+ left: this.left,
251
+ right: this.right,
252
+ };
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Represents a binary logical operation like AND or OR.
258
+ * @category Operators
259
+ */
260
+ export class LogicalBinaryOperation extends ASTNode {
261
+ /** @hidden */
262
+ public right: Expression;
263
+ /** @hidden */
264
+ public left: Expression;
265
+ /** @hidden */
266
+ public operator: LogicalBinaryOperator;
267
+
268
+ constructor(
269
+ operator: LogicalBinaryOperator,
270
+ left: Expression,
271
+ right: Expression,
272
+ loc?: SourceLocation
273
+ ) {
274
+ super(loc);
275
+ this.operator = operator;
276
+ this.left = left;
277
+ this.right = right;
278
+ }
279
+ public accept<R>(visitor: Visitor<R>): R {
280
+ return visitor.visitLogicalBinaryOperation?.(this);
281
+ }
282
+ public toJSON() {
283
+ return {
284
+ type: "LogicalBinaryOperation",
285
+ operator: this.operator,
286
+ left: this.left,
287
+ right: this.right,
288
+ };
289
+ }
290
+ }
291
+ /**
292
+ * Represents a unary logical operation like NOT.
293
+ * @category Operators
294
+ */
295
+ export class LogicalUnaryOperation extends ASTNode {
296
+ /** @hidden */
297
+ public operand: Expression;
298
+ /** @hidden */
299
+ public operator: LogicalUnaryOperator;
300
+
301
+ constructor(
302
+ operator: LogicalUnaryOperator,
303
+ operand: Expression,
304
+ loc?: SourceLocation
305
+ ) {
306
+ super(loc);
307
+ this.operator = operator;
308
+ this.operand = operand;
309
+ }
310
+ public accept<R>(visitor: Visitor<R>): R {
311
+ return visitor.visitLogicalUnaryOperation?.(this);
312
+ }
313
+ public toJSON() {
314
+ return {
315
+ type: "LogicalUnaryOperation",
316
+ operator: this.operator,
317
+ operand: this.operand,
318
+ };
319
+ }
320
+ }
321
+
322
+ /**
323
+ * Represents a binary bitwise operation like AND, OR, XOR.
324
+ * @category Operators
325
+ */
326
+ export class BitwiseBinaryOperation extends ASTNode {
327
+ /** @hidden */
328
+ public right: Expression;
329
+ /** @hidden */
330
+ public left: Expression;
331
+ /** @hidden */
332
+ public operator: BitwiseBinaryOperator;
333
+
334
+ constructor(
335
+ operator: BitwiseBinaryOperator,
336
+ left: Expression,
337
+ right: Expression,
338
+ loc?: SourceLocation
339
+ ) {
340
+ super(loc);
341
+ this.operator = operator;
342
+ this.left = left;
343
+ this.right = right;
344
+ }
345
+ public accept<R>(visitor: Visitor<R>): R {
346
+ return visitor.visitBitwiseBinaryOperation?.(this);
347
+ }
348
+ public toJSON() {
349
+ return {
350
+ type: "BitwiseBinaryOperation",
351
+ operator: this.operator,
352
+ left: this.left,
353
+ right: this.right,
354
+ };
355
+ }
356
+ }
357
+ /**
358
+ * Represents a unary bitwise operation like NOT (complement).
359
+ * @category Operators
360
+ */
361
+ export class BitwiseUnaryOperation extends ASTNode {
362
+ /** @hidden */
363
+ public operand: Expression;
364
+ /** @hidden */
365
+ public operator: BitwiseUnaryOperator;
366
+
367
+ constructor(
368
+ operator: BitwiseUnaryOperator,
369
+ operand: Expression,
370
+ loc?: SourceLocation
371
+ ) {
372
+ super(loc);
373
+ this.operator = operator;
374
+ this.operand = operand;
375
+ }
376
+ public accept<R>(visitor: Visitor<R>): R {
377
+ return visitor.visitBitwiseUnaryOperation?.(this);
378
+ }
379
+ public toJSON() {
380
+ return {
381
+ type: "BitwiseUnaryOperation",
382
+ operator: this.operator,
383
+ operand: this.operand,
384
+ };
385
+ }
386
+ }
387
+
388
+ /**
389
+ * Represents an operation specific to string manipulation.
390
+ * @category Operators
391
+ */
392
+ export class StringOperation extends ASTNode {
393
+ /** @hidden */
394
+ public right: Expression;
395
+ /** @hidden */
396
+ public left: Expression;
397
+ /** @hidden */
398
+ public operator: StringBinaryOperator;
399
+
400
+ constructor(
401
+ operator: StringBinaryOperator,
402
+ left: Expression,
403
+ right: Expression,
404
+ loc?: SourceLocation
405
+ ) {
406
+ super(loc);
407
+ this.operator = operator;
408
+ this.left = left;
409
+ this.right = right;
410
+ }
411
+ public accept<R>(visitor: Visitor<R>): R {
412
+ return visitor.visitStringOperation?.(this);
413
+ }
414
+ public toJSON() {
415
+ return {
416
+ type: "StringOperation",
417
+ operator: this.operator,
418
+ left: this.left,
419
+ right: this.right,
420
+ };
421
+ }
422
+ }
423
+
424
+ /**
425
+ * Represents a unification operation (=), fundamental to logic programming.
426
+ *
427
+ * @example
428
+ * X = 5
429
+ * @category Logic
430
+ */
431
+ export class UnifyOperation extends ASTNode {
432
+ /** @hidden */
433
+ public right: Expression;
434
+ /** @hidden */
435
+ public left: Expression;
436
+ /** @hidden */
437
+ public operator: UnifyOperator;
438
+
439
+ constructor(
440
+ operator: UnifyOperator,
441
+ left: Expression,
442
+ right: Expression,
443
+ loc?: SourceLocation
444
+ ) {
445
+ super(loc);
446
+ this.operator = operator;
447
+ this.left = left;
448
+ this.right = right;
449
+ }
450
+ public accept<R>(visitor: Visitor<R>): R {
451
+ return visitor.visitUnifyOperation?.(this);
452
+ }
453
+ public toJSON() {
454
+ return {
455
+ type: "UnifyOperation",
456
+ operator: this.operator,
457
+ left: this.left,
458
+ right: this.right,
459
+ };
460
+ }
461
+ }
462
+
463
+ /**
464
+ * Represents an in-place assignment operation (e.g., +=, -=).
465
+ * @category Operators
466
+ */
467
+ export class AssignOperation extends ASTNode {
468
+ /** @hidden */
469
+ public right: Expression;
470
+ /** @hidden */
471
+ public left: Expression;
472
+ /** @hidden */
473
+ public operator: AssignOperator;
474
+
475
+ constructor(
476
+ operator: AssignOperator,
477
+ left: Expression,
478
+ right: Expression,
479
+ loc?: SourceLocation
480
+ ) {
481
+ super(loc);
482
+ this.operator = operator;
483
+ this.left = left;
484
+ this.right = right;
485
+ }
486
+ public accept<R>(visitor: Visitor<R>): R {
487
+ return visitor.visitAssignOperation?.(this);
488
+ }
489
+ public toJSON() {
490
+ return {
491
+ type: "AssignOperation",
492
+ operator: this.operator,
493
+ left: this.left,
494
+ right: this.right,
495
+ };
496
+ }
497
+ }
498
+
499
+ export type BinaryOperation =
500
+ | ArithmeticBinaryOperation
501
+ | StringOperation
502
+ | ListBinaryOperation
503
+ | LogicalBinaryOperation
504
+ | ComparisonOperation
505
+ | UnifyOperation
506
+ | AssignOperation
507
+ | BitwiseBinaryOperation;
508
+
509
+ export type UnaryOperation =
510
+ | ArithmeticUnaryOperation
511
+ | ListUnaryOperation
512
+ | LogicalUnaryOperation
513
+ | BitwiseUnaryOperation;
514
+
515
+ export type Operation = BinaryOperation | UnaryOperation;