yukigo 0.1.0 → 0.2.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 (144) hide show
  1. package/.mocharc.json +3 -3
  2. package/CHANGELOG.md +26 -0
  3. package/README.md +193 -199
  4. package/dist/analyzer/GraphBuilder.d.ts +29 -0
  5. package/dist/analyzer/GraphBuilder.js +99 -0
  6. package/dist/analyzer/index.d.ts +11 -23
  7. package/dist/analyzer/index.js +100 -58
  8. package/dist/analyzer/inspections/functional/functional.d.ts +44 -0
  9. package/dist/analyzer/inspections/functional/functional.js +149 -0
  10. package/dist/analyzer/inspections/functional/smells.d.ts +16 -0
  11. package/dist/analyzer/inspections/functional/smells.js +98 -0
  12. package/dist/analyzer/inspections/{generic.d.ts → generic/generic.d.ts} +70 -43
  13. package/dist/analyzer/inspections/generic/generic.js +604 -0
  14. package/dist/analyzer/inspections/generic/smells.d.ts +61 -0
  15. package/dist/analyzer/inspections/generic/smells.js +349 -0
  16. package/dist/analyzer/inspections/imperative/imperative.d.ts +35 -0
  17. package/dist/analyzer/inspections/imperative/imperative.js +109 -0
  18. package/dist/analyzer/inspections/imperative/smells.d.ts +16 -0
  19. package/dist/analyzer/inspections/imperative/smells.js +58 -0
  20. package/dist/analyzer/inspections/logic/logic.d.ts +32 -0
  21. package/dist/analyzer/inspections/logic/logic.js +96 -0
  22. package/dist/analyzer/inspections/logic/smells.d.ts +15 -0
  23. package/dist/analyzer/inspections/logic/smells.js +60 -0
  24. package/dist/analyzer/inspections/object/object.d.ts +88 -0
  25. package/dist/analyzer/inspections/object/object.js +319 -0
  26. package/dist/analyzer/inspections/object/smells.d.ts +30 -0
  27. package/dist/analyzer/inspections/object/smells.js +135 -0
  28. package/dist/analyzer/utils.d.ts +26 -4
  29. package/dist/analyzer/utils.js +71 -13
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.js +1 -0
  32. package/dist/interpreter/components/EnvBuilder.d.ts +9 -5
  33. package/dist/interpreter/components/EnvBuilder.js +100 -30
  34. package/dist/interpreter/components/Operations.d.ts +4 -4
  35. package/dist/interpreter/components/Operations.js +17 -2
  36. package/dist/interpreter/components/PatternMatcher.d.ts +47 -17
  37. package/dist/interpreter/components/PatternMatcher.js +264 -119
  38. package/dist/interpreter/components/RuntimeContext.d.ts +35 -0
  39. package/dist/interpreter/components/RuntimeContext.js +93 -0
  40. package/dist/interpreter/components/TestRunner.d.ts +18 -0
  41. package/dist/interpreter/components/TestRunner.js +103 -0
  42. package/dist/interpreter/components/Visitor.d.ts +63 -57
  43. package/dist/interpreter/components/Visitor.js +508 -173
  44. package/dist/interpreter/components/logic/LogicEngine.d.ts +29 -0
  45. package/dist/interpreter/components/logic/LogicEngine.js +259 -0
  46. package/dist/interpreter/components/logic/LogicResolver.d.ts +53 -0
  47. package/dist/interpreter/components/logic/LogicResolver.js +471 -0
  48. package/dist/interpreter/components/logic/LogicTranslator.d.ts +14 -0
  49. package/dist/interpreter/components/logic/LogicTranslator.js +99 -0
  50. package/dist/interpreter/components/runtimes/FunctionRuntime.d.ts +12 -0
  51. package/dist/interpreter/components/runtimes/FunctionRuntime.js +147 -0
  52. package/dist/interpreter/components/runtimes/LazyRuntime.d.ts +19 -0
  53. package/dist/interpreter/components/runtimes/LazyRuntime.js +269 -0
  54. package/dist/interpreter/components/runtimes/ObjectRuntime.d.ts +35 -0
  55. package/dist/interpreter/components/runtimes/ObjectRuntime.js +126 -0
  56. package/dist/interpreter/entities.d.ts +105 -0
  57. package/dist/interpreter/entities.js +96 -0
  58. package/dist/interpreter/errors.d.ts +1 -1
  59. package/dist/interpreter/index.d.ts +4 -12
  60. package/dist/interpreter/index.js +10 -13
  61. package/dist/interpreter/trampoline.d.ts +17 -0
  62. package/dist/interpreter/trampoline.js +38 -0
  63. package/dist/interpreter/utils.d.ts +4 -7
  64. package/dist/interpreter/utils.js +25 -17
  65. package/dist/tester/index.d.ts +25 -0
  66. package/dist/tester/index.js +108 -0
  67. package/dist/utils/helpers.d.ts +0 -4
  68. package/dist/utils/helpers.js +20 -24
  69. package/package.json +2 -2
  70. package/src/analyzer/GraphBuilder.ts +142 -0
  71. package/src/analyzer/index.ts +185 -132
  72. package/src/analyzer/inspections/functional/functional.ts +121 -0
  73. package/src/analyzer/inspections/functional/smells.ts +102 -0
  74. package/src/analyzer/inspections/{generic.ts → generic/generic.ts} +581 -499
  75. package/src/analyzer/inspections/generic/smells.ts +365 -0
  76. package/src/analyzer/inspections/imperative/imperative.ts +101 -0
  77. package/src/analyzer/inspections/imperative/smells.ts +54 -0
  78. package/src/analyzer/inspections/logic/logic.ts +90 -0
  79. package/src/analyzer/inspections/logic/smells.ts +54 -0
  80. package/src/analyzer/inspections/{object.ts → object/object.ts} +264 -282
  81. package/src/analyzer/inspections/object/smells.ts +144 -0
  82. package/src/analyzer/utils.ts +109 -26
  83. package/src/index.ts +3 -2
  84. package/src/interpreter/components/EnvBuilder.ts +202 -97
  85. package/src/interpreter/components/Operations.ts +99 -81
  86. package/src/interpreter/components/PatternMatcher.ts +475 -254
  87. package/src/interpreter/components/RuntimeContext.ts +119 -0
  88. package/src/interpreter/components/TestRunner.ts +151 -0
  89. package/src/interpreter/components/Visitor.ts +1065 -493
  90. package/src/interpreter/components/logic/LogicEngine.ts +519 -0
  91. package/src/interpreter/components/logic/LogicResolver.ts +858 -0
  92. package/src/interpreter/components/logic/LogicTranslator.ts +149 -0
  93. package/src/interpreter/components/runtimes/FunctionRuntime.ts +227 -0
  94. package/src/interpreter/components/runtimes/LazyRuntime.ts +334 -0
  95. package/src/interpreter/components/runtimes/ObjectRuntime.ts +224 -0
  96. package/src/interpreter/errors.ts +47 -47
  97. package/src/interpreter/index.ts +52 -59
  98. package/src/interpreter/trampoline.ts +71 -0
  99. package/src/interpreter/utils.ts +84 -79
  100. package/src/tester/index.ts +128 -0
  101. package/src/utils/helpers.ts +67 -73
  102. package/tests/analyzer/functional.spec.ts +207 -221
  103. package/tests/analyzer/generic.spec.ts +178 -100
  104. package/tests/analyzer/helpers.spec.ts +83 -83
  105. package/tests/analyzer/logic.spec.ts +237 -292
  106. package/tests/analyzer/oop.spec.ts +323 -338
  107. package/tests/analyzer/transitive.spec.ts +166 -0
  108. package/tests/interpreter/EnvBuilder.spec.ts +183 -178
  109. package/tests/interpreter/FunctionRuntime.spec.ts +223 -234
  110. package/tests/interpreter/LazyRuntime.spec.ts +225 -190
  111. package/tests/interpreter/LogicEngine.spec.ts +327 -194
  112. package/tests/interpreter/LogicSubstitution.spec.ts +80 -0
  113. package/tests/interpreter/ObjectRuntime.spec.ts +606 -0
  114. package/tests/interpreter/Operations.spec.ts +220 -220
  115. package/tests/interpreter/PatternSystem.spec.ts +213 -189
  116. package/tests/interpreter/Tests.spec.ts +122 -0
  117. package/tests/interpreter/interpreter.spec.ts +991 -937
  118. package/tests/tester/Tester.spec.ts +153 -0
  119. package/tsconfig.build.json +15 -7
  120. package/tsconfig.json +25 -17
  121. package/dist/analyzer/inspections/functional.d.ts +0 -46
  122. package/dist/analyzer/inspections/functional.js +0 -123
  123. package/dist/analyzer/inspections/generic.js +0 -427
  124. package/dist/analyzer/inspections/imperative.d.ts +0 -37
  125. package/dist/analyzer/inspections/imperative.js +0 -105
  126. package/dist/analyzer/inspections/logic.d.ts +0 -49
  127. package/dist/analyzer/inspections/logic.js +0 -140
  128. package/dist/analyzer/inspections/object.d.ts +0 -83
  129. package/dist/analyzer/inspections/object.js +0 -235
  130. package/dist/interpreter/components/FunctionRuntime.d.ts +0 -8
  131. package/dist/interpreter/components/FunctionRuntime.js +0 -52
  132. package/dist/interpreter/components/LazyRuntime.d.ts +0 -7
  133. package/dist/interpreter/components/LazyRuntime.js +0 -75
  134. package/dist/interpreter/components/LogicEngine.d.ts +0 -21
  135. package/dist/interpreter/components/LogicEngine.js +0 -152
  136. package/dist/interpreter/components/LogicResolver.d.ts +0 -11
  137. package/dist/interpreter/components/LogicResolver.js +0 -87
  138. package/src/analyzer/inspections/functional.ts +0 -159
  139. package/src/analyzer/inspections/imperative.ts +0 -129
  140. package/src/analyzer/inspections/logic.ts +0 -166
  141. package/src/interpreter/components/FunctionRuntime.ts +0 -79
  142. package/src/interpreter/components/LazyRuntime.ts +0 -97
  143. package/src/interpreter/components/LogicEngine.ts +0 -227
  144. package/src/interpreter/components/LogicResolver.ts +0 -130
@@ -1,16 +1,74 @@
1
- import { StopTraversalException } from "@yukigo/ast";
2
- export function executeVisitor(node, visitor) {
3
- let passes = false;
4
- try {
5
- node.accept(visitor);
6
- }
7
- catch (e) {
8
- if (e instanceof StopTraversalException) {
9
- passes = true;
10
- }
11
- else {
12
- throw e;
1
+ import { TraverseVisitor, } from "yukigo-ast";
2
+ export class ScopedVisitor extends TraverseVisitor {
3
+ binding;
4
+ inScope;
5
+ constructor(binding) {
6
+ super();
7
+ this.setBinding(binding);
8
+ }
9
+ setBinding(binding) {
10
+ this.binding = binding;
11
+ this.inScope = !binding;
12
+ }
13
+ visitVariable(node) {
14
+ this.manageScope(node, () => super.visitVariable(node));
15
+ }
16
+ visitFunction(node) {
17
+ this.manageScope(node, () => super.visitFunction(node));
18
+ }
19
+ visitClass(node) {
20
+ this.manageScope(node, () => super.visitClass(node));
21
+ }
22
+ visitObject(node) {
23
+ this.manageScope(node, () => super.visitObject(node));
24
+ }
25
+ visitMethod(node) {
26
+ this.manageScope(node, () => super.visitMethod(node));
27
+ }
28
+ visitRule(node) {
29
+ this.manageScope(node, () => super.visitRule(node));
30
+ }
31
+ visitFact(node) {
32
+ this.manageScope(node, () => super.visitFact(node));
33
+ }
34
+ visitProcedure(node) {
35
+ this.manageScope(node, () => super.visitProcedure(node));
36
+ }
37
+ manageScope(node, traverse) {
38
+ const isTarget = node.identifier.value === this.binding;
39
+ if (isTarget)
40
+ this.inScope = true;
41
+ traverse();
42
+ if (isTarget)
43
+ this.inScope = false;
44
+ }
45
+ }
46
+ export function AutoScoped(constructor) {
47
+ const proto = constructor.prototype;
48
+ const methodNames = Object.getOwnPropertyNames(proto);
49
+ for (const name of methodNames) {
50
+ if (isValidVisitMethod(name)) {
51
+ const originalMethod = proto[name];
52
+ // replace the method on the prototype
53
+ proto[name] = function (node) {
54
+ if (!this.inScope)
55
+ return;
56
+ // run the inspection logic if in scope
57
+ return originalMethod.call(this, node);
58
+ };
13
59
  }
14
60
  }
15
- return passes;
16
61
  }
62
+ function isScopeBoundary(name) {
63
+ return [
64
+ "visitFunction",
65
+ "visitMethod",
66
+ "visitProcedure",
67
+ "visitRule",
68
+ "visitFact",
69
+ "visitVariable",
70
+ "visitClass",
71
+ "visitObject",
72
+ ].includes(name);
73
+ }
74
+ const isValidVisitMethod = (name) => name.startsWith("visit") && name !== "constructor" && !isScopeBoundary(name);
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./interpreter/index.js";
2
2
  export * from "./analyzer/index.js";
3
+ export * from "./tester/index.js";
3
4
  export * from "./utils/helpers.js";
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./interpreter/index.js";
2
2
  export * from "./analyzer/index.js";
3
+ export * from "./tester/index.js";
3
4
  export * from "./utils/helpers.js";
@@ -1,16 +1,20 @@
1
- import { AST, ASTNode, Fact, Function, Rule, TraverseVisitor } from "@yukigo/ast";
2
- import { EnvStack } from "../index.js";
1
+ import { AST, ASTNode, Class, Fact, Function, Rule, TraverseVisitor, Object, Variable, Sequence } from "yukigo-ast";
2
+ import { RuntimeContext } from "./RuntimeContext.js";
3
3
  /**
4
4
  * Builds the initial environment by collecting all top-level function declarations.
5
5
  * Each function captures a closure of the environment at its definition time,
6
6
  * allowing recursion by including itself in the closure.
7
7
  */
8
8
  export declare class EnvBuilderVisitor extends TraverseVisitor {
9
- private env;
10
- constructor(baseEnv?: EnvStack);
11
- build(ast: AST): EnvStack;
9
+ private ctx;
10
+ constructor(ctx: RuntimeContext);
11
+ build(ast: AST): void;
12
+ visitSequence(node: Sequence): void;
12
13
  visitFunction(node: Function): void;
14
+ visitClass(node: Class): void;
15
+ visitObject(node: Object): void;
13
16
  visitFact(node: Fact): void;
14
17
  visitRule(node: Rule): void;
18
+ visitVariable(node: Variable): void;
15
19
  visit(node: ASTNode): void;
16
20
  }
@@ -1,55 +1,100 @@
1
- import { isRuntimePredicate, TraverseVisitor, } from "@yukigo/ast";
2
- import { define } from "../utils.js";
1
+ import { isRuntimePredicate, TraverseVisitor, } from "yukigo-ast";
2
+ import { InterpreterVisitor } from "./Visitor.js";
3
+ import { idContinuation, trampoline } from "../trampoline.js";
4
+ import { InterpreterError } from "../errors.js";
3
5
  /**
4
6
  * Builds the initial environment by collecting all top-level function declarations.
5
7
  * Each function captures a closure of the environment at its definition time,
6
8
  * allowing recursion by including itself in the closure.
7
9
  */
8
10
  export class EnvBuilderVisitor extends TraverseVisitor {
9
- env;
10
- constructor(baseEnv) {
11
+ ctx;
12
+ constructor(ctx) {
11
13
  super();
12
- this.env = baseEnv ?? [new Map()];
14
+ this.ctx = ctx;
13
15
  }
14
16
  build(ast) {
15
17
  for (const node of ast)
16
18
  node.accept(this);
17
- return this.env;
19
+ }
20
+ visitSequence(node) {
21
+ for (const stmt of node.statements)
22
+ stmt.accept(this);
18
23
  }
19
24
  visitFunction(node) {
20
25
  const name = node.identifier.value;
26
+ if (this.ctx.config.debug)
27
+ console.log(`[EnvBuilder] Defining function: ${name}`);
21
28
  if (node.equations.length === 0)
22
29
  throw new Error(`Function ${name} has no equations`);
23
30
  const arity = node.equations[0].patterns.length;
24
- for (const eq of node.equations) {
25
- if (eq.patterns.length !== arity)
26
- throw new Error(`All equations of ${name} must have the same arity`);
27
- }
31
+ if (node.equations.some((eq) => eq.patterns.length !== arity))
32
+ throw new Error(`All equations of ${name} must have the same arity`);
28
33
  let placeholder;
29
- define(this.env, name, placeholder);
34
+ this.ctx.define(name, placeholder);
30
35
  const equations = node.equations.map((eq) => ({
31
36
  patterns: eq.patterns,
32
37
  body: eq.body,
33
38
  }));
34
39
  const runtimeFunc = {
40
+ type: "Function",
35
41
  identifier: name,
36
42
  arity,
37
43
  equations,
44
+ closure: this.ctx.env,
45
+ };
46
+ this.ctx.define(name, runtimeFunc);
47
+ }
48
+ visitClass(node) {
49
+ const identifier = node.identifier.value;
50
+ if (this.ctx.config.debug)
51
+ console.log(`[EnvBuilder] Defining class: ${identifier}`);
52
+ const superclass = node.extendsSymbol?.value;
53
+ const mixins = node.includes.map((symbol) => symbol.value);
54
+ const collector = new OOPCollector();
55
+ node.expression.accept(collector);
56
+ const fields = collector.collectedFields;
57
+ const methods = collector.collectedMethods;
58
+ const runtimeClass = {
59
+ type: "Class",
60
+ identifier,
61
+ fields,
62
+ methods,
63
+ superclass,
64
+ mixins,
65
+ };
66
+ this.ctx.define(identifier, runtimeClass);
67
+ }
68
+ visitObject(node) {
69
+ const identifier = node.identifier.value;
70
+ if (this.ctx.config.debug)
71
+ console.log(`[EnvBuilder] Defining object: ${identifier}`);
72
+ const collector = new OOPCollector();
73
+ node.expression.accept(collector);
74
+ const fields = collector.collectedFields;
75
+ const methods = collector.collectedMethods;
76
+ const runtimeObject = {
77
+ type: "Object",
78
+ identifier,
79
+ className: "",
80
+ fields,
81
+ methods,
38
82
  };
39
- define(this.env, name, runtimeFunc);
83
+ this.ctx.define(identifier, runtimeObject);
40
84
  }
41
85
  visitFact(node) {
42
86
  const identifier = node.identifier.value;
43
- const runtimeValue = this.env[0].get(identifier);
44
- if (isRuntimePredicate(runtimeValue) && runtimeValue.kind === "Fact") {
45
- this.env[0].set(identifier, {
46
- ...runtimeValue,
47
- equations: [...runtimeValue.equations, node],
48
- });
87
+ if (this.ctx.config.debug)
88
+ console.log(`[EnvBuilder] Defining fact: ${identifier}`);
89
+ try {
90
+ const runtimeValue = this.ctx.lookup(identifier);
91
+ if (!isRuntimePredicate(runtimeValue))
92
+ throw new InterpreterError("EnvBuilder", `"${identifier}" is not a predicate. Maybe there is something else defined as "${identifier}"?`);
93
+ runtimeValue.equations.push(node);
49
94
  }
50
- else {
51
- this.env[0].set(identifier, {
52
- kind: "Fact",
95
+ catch (error) {
96
+ this.ctx.define(identifier, {
97
+ kind: "Predicate",
53
98
  identifier,
54
99
  equations: [node],
55
100
  });
@@ -57,22 +102,47 @@ export class EnvBuilderVisitor extends TraverseVisitor {
57
102
  }
58
103
  visitRule(node) {
59
104
  const identifier = node.identifier.value;
60
- const runtimeValue = this.env[0].get(identifier);
61
- if (isRuntimePredicate(runtimeValue) && runtimeValue.kind === "Rule") {
62
- this.env[0].set(identifier, {
63
- ...runtimeValue,
64
- equations: [...runtimeValue.equations, node],
65
- });
105
+ if (this.ctx.config.debug)
106
+ console.log(`[EnvBuilder] Defining rule: ${identifier}`);
107
+ try {
108
+ const runtimeValue = this.ctx.lookup(identifier);
109
+ if (!isRuntimePredicate(runtimeValue))
110
+ throw new InterpreterError("EnvBuilder", `"${identifier}" is not a predicate. Maybe there is something else defined as "${identifier}"?`);
111
+ runtimeValue.equations.push(node);
66
112
  }
67
- else {
68
- this.env[0].set(identifier, {
69
- kind: "Rule",
113
+ catch (error) {
114
+ this.ctx.define(identifier, {
115
+ kind: "Predicate",
70
116
  identifier,
71
117
  equations: [node],
72
118
  });
73
119
  }
74
120
  }
121
+ visitVariable(node) {
122
+ const identifier = node.identifier.value;
123
+ if (this.ctx.config.debug)
124
+ console.log(`[EnvBuilder] Defining variable: ${identifier}`);
125
+ const interpreter = new InterpreterVisitor(this.ctx);
126
+ const cps = node.expression.accept(interpreter);
127
+ this.ctx.define(identifier, trampoline(cps(idContinuation)));
128
+ }
75
129
  visit(node) {
76
130
  return node.accept(this);
77
131
  }
78
132
  }
133
+ class OOPCollector extends TraverseVisitor {
134
+ collectedMethods = new Map();
135
+ collectedFields = new Map();
136
+ visitMethod(node) {
137
+ const runtimeMethod = {
138
+ type: "Function",
139
+ identifier: node.identifier.value,
140
+ arity: node.equations[0].patterns.length,
141
+ equations: node.equations,
142
+ };
143
+ this.collectedMethods.set(node.identifier.value, runtimeMethod);
144
+ }
145
+ visitAttribute(node) {
146
+ this.collectedFields.set(node.identifier.value, InterpreterVisitor.evaluateLiteral(node.expression));
147
+ }
148
+ }
@@ -1,4 +1,4 @@
1
- import { PrimitiveValue } from "@yukigo/ast";
1
+ import { PrimitiveValue } from "yukigo-ast";
2
2
  export type UnaryOp<T, R = T> = (x: T) => R;
3
3
  export type BinaryOp<T, R = T> = (x: T, y: T) => R;
4
4
  export type OperatorTable<T> = Record<string, T>;
@@ -6,9 +6,9 @@ export declare const ArithmeticBinaryTable: OperatorTable<BinaryOp<number>>;
6
6
  export declare const ComparisonOperationTable: OperatorTable<BinaryOp<boolean>>;
7
7
  export declare const LogicalBinaryTable: OperatorTable<(l: boolean, r: () => boolean) => boolean>;
8
8
  export declare const BitwiseBinaryTable: OperatorTable<BinaryOp<number>>;
9
- export declare const StringOperationTable: OperatorTable<BinaryOp<string>>;
9
+ export declare const StringOperationTable: OperatorTable<BinaryOp<any, string>>;
10
10
  export declare const BitwiseUnaryTable: OperatorTable<UnaryOp<number>>;
11
11
  export declare const LogicalUnaryTable: OperatorTable<UnaryOp<boolean>>;
12
- export declare const ListBinaryTable: OperatorTable<BinaryOp<PrimitiveValue[]>>;
12
+ export declare const ListBinaryTable: OperatorTable<BinaryOp<any, PrimitiveValue>>;
13
13
  export declare const ListUnaryTable: OperatorTable<UnaryOp<PrimitiveValue[], PrimitiveValue>>;
14
- export declare const ArithmeticUnaryTable: OperatorTable<UnaryOp<number>>;
14
+ export declare const ArithmeticUnaryTable: OperatorTable<UnaryOp<number, number | string>>;
@@ -34,7 +34,13 @@ export const BitwiseBinaryTable = {
34
34
  BitwiseXor: (a, b) => a ^ b,
35
35
  };
36
36
  export const StringOperationTable = {
37
- Concat: (a, b) => `${a + b}`,
37
+ Concat: (a, b) => {
38
+ if ((typeof a !== "string" && typeof a !== "number") ||
39
+ (typeof b !== "string" && typeof b !== "number")) {
40
+ throw new Error(`String Concatenation: operands must be strings or numbers, got ${typeof a} and ${typeof b}`);
41
+ }
42
+ return String(a) + String(b);
43
+ },
38
44
  };
39
45
  export const BitwiseUnaryTable = {
40
46
  BitwiseNot: (a) => ~a,
@@ -43,7 +49,15 @@ export const LogicalUnaryTable = {
43
49
  Negation: (a) => !a,
44
50
  };
45
51
  export const ListBinaryTable = {
46
- Concat: (a, b) => a.concat(b),
52
+ Concat: (a, b) => {
53
+ const res = a.concat(b);
54
+ if (typeof a === "string" && typeof b === "string") {
55
+ const hasNonString = res.some((c) => typeof c !== "string");
56
+ if (!hasNonString)
57
+ return res.join("");
58
+ }
59
+ return res;
60
+ },
47
61
  };
48
62
  export const ListUnaryTable = {
49
63
  Size: (a) => a.length,
@@ -66,4 +80,5 @@ export const ArithmeticUnaryTable = {
66
80
  Floor: (a) => Math.floor(a),
67
81
  Negation: (a) => a * -1,
68
82
  Sqrt: (a) => Math.sqrt(a),
83
+ ToString: (a) => String(a),
69
84
  };
@@ -1,5 +1,33 @@
1
- import { ApplicationPattern, AsPattern, ASTNode, ConsPattern, ConstructorPattern, FunctorPattern, ListPattern, LiteralPattern, PrimitiveValue, TuplePattern, UnionPattern, VariablePattern, Visitor, WildcardPattern } from "@yukigo/ast";
1
+ import { ApplicationPattern, AsPattern, ASTNode, ConsPattern, ConstructorPattern, Expression, FunctorPattern, LazyList, ListPattern, LiteralPattern, PrimitiveValue, TuplePattern, UnionPattern, VariablePattern, Visitor, WildcardPattern, TypePattern, EnvStack } from "yukigo-ast";
2
2
  import { Bindings } from "../index.js";
3
+ import { CPSThunk } from "../trampoline.js";
4
+ import { RuntimeContext } from "./RuntimeContext.js";
5
+ import { ExpressionEvaluator } from "../utils.js";
6
+ declare class SharedSequence {
7
+ private cache;
8
+ private source;
9
+ private isDone;
10
+ constructor(generatorFactory: () => Generator<PrimitiveValue, void, unknown>);
11
+ get(index: number): {
12
+ value: PrimitiveValue | null;
13
+ done: boolean;
14
+ };
15
+ }
16
+ export interface InternalConsState {
17
+ readonly head: PrimitiveValue;
18
+ readonly tailExpr: Expression;
19
+ readonly evaluator: ExpressionEvaluator;
20
+ readonly capturedEnv: EnvStack;
21
+ realizedTail?: PrimitiveValue;
22
+ }
23
+ export interface MemoizedLazyList extends LazyList {
24
+ _sequence: SharedSequence;
25
+ _offset: number;
26
+ _consState?: InternalConsState;
27
+ toJSON: () => any;
28
+ }
29
+ export declare function isMemoizedList(list: unknown): list is MemoizedLazyList;
30
+ export declare function createMemoizedStream(genFactory: () => Generator<PrimitiveValue, void, unknown>, sequence?: SharedSequence, offset?: number): MemoizedLazyList;
3
31
  export declare class PatternResolver implements Visitor<string> {
4
32
  visitVariablePattern(node: VariablePattern): string;
5
33
  visitWildcardPattern(node: WildcardPattern): string;
@@ -11,6 +39,7 @@ export declare class PatternResolver implements Visitor<string> {
11
39
  visitFunctorPattern(node: FunctorPattern): string;
12
40
  visitApplicationPattern(node: ApplicationPattern): string;
13
41
  visitAsPattern(node: AsPattern): string;
42
+ visitTypePattern(node: TypePattern): string;
14
43
  visit(node: ASTNode): string;
15
44
  }
16
45
  /**
@@ -18,24 +47,25 @@ export declare class PatternResolver implements Visitor<string> {
18
47
  * Updates `bindings` when variables are bound successfully.
19
48
  * Returns true if the pattern matches, false otherwise.
20
49
  */
21
- export declare class PatternMatcher implements Visitor<boolean> {
50
+ export declare class PatternMatcher implements Visitor<CPSThunk<boolean>> {
22
51
  private value;
23
52
  private bindings;
24
- constructor(value: PrimitiveValue, bindings: Bindings);
25
- visitVariablePattern(node: VariablePattern): boolean;
26
- visitWildcardPattern(node: WildcardPattern): boolean;
27
- visitLiteralPattern(node: LiteralPattern): boolean;
28
- visitTuplePattern(node: TuplePattern): boolean;
29
- visitListPattern(node: ListPattern): boolean;
53
+ private ctx;
54
+ constructor(value: PrimitiveValue, bindings: Bindings, ctx: RuntimeContext);
55
+ visitVariablePattern(node: VariablePattern): CPSThunk<boolean>;
56
+ visitWildcardPattern(node: WildcardPattern): CPSThunk<boolean>;
57
+ visitLiteralPattern(node: LiteralPattern): CPSThunk<boolean>;
58
+ visitTuplePattern(node: TuplePattern): CPSThunk<boolean>;
59
+ visitListPattern(node: ListPattern): CPSThunk<boolean>;
30
60
  private matchList;
31
- visitConsPattern(node: ConsPattern): boolean;
61
+ visitConsPattern(node: ConsPattern): CPSThunk<boolean>;
62
+ visitTypePattern(node: TypePattern): CPSThunk<boolean>;
32
63
  private resolveCons;
33
- visitConstructorPattern(node: ConstructorPattern): boolean;
34
- visitFunctorPattern(node: FunctorPattern): boolean;
35
- visitApplicationPattern(node: ApplicationPattern): boolean;
36
- visitAsPattern(node: AsPattern): boolean;
37
- visitUnionPattern(node: UnionPattern): boolean;
38
- visit(node: ASTNode): boolean;
39
- private deepEqual;
40
- private realize;
64
+ visitConstructorPattern(node: ConstructorPattern): CPSThunk<boolean>;
65
+ visitFunctorPattern(node: FunctorPattern): CPSThunk<boolean>;
66
+ visitApplicationPattern(node: ApplicationPattern): CPSThunk<boolean>;
67
+ visitAsPattern(node: AsPattern): CPSThunk<boolean>;
68
+ visitUnionPattern(node: UnionPattern): CPSThunk<boolean>;
69
+ visit(node: ASTNode): CPSThunk<boolean>;
41
70
  }
71
+ export {};