yukigo 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.
- package/.mocharc.json +4 -0
- package/CHANGELOG.md +6 -0
- package/README.md +199 -0
- package/dist/analyzer/index.d.ts +71 -0
- package/dist/analyzer/index.js +110 -0
- package/dist/analyzer/inspections/functional.d.ts +46 -0
- package/dist/analyzer/inspections/functional.js +123 -0
- package/dist/analyzer/inspections/generic.d.ts +151 -0
- package/dist/analyzer/inspections/generic.js +427 -0
- package/dist/analyzer/inspections/imperative.d.ts +37 -0
- package/dist/analyzer/inspections/imperative.js +105 -0
- package/dist/analyzer/inspections/logic.d.ts +49 -0
- package/dist/analyzer/inspections/logic.js +140 -0
- package/dist/analyzer/inspections/object.d.ts +83 -0
- package/dist/analyzer/inspections/object.js +235 -0
- package/dist/analyzer/utils.d.ts +4 -0
- package/dist/analyzer/utils.js +16 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interpreter/components/EnvBuilder.d.ts +16 -0
- package/dist/interpreter/components/EnvBuilder.js +78 -0
- package/dist/interpreter/components/FunctionRuntime.d.ts +8 -0
- package/dist/interpreter/components/FunctionRuntime.js +52 -0
- package/dist/interpreter/components/LazyRuntime.d.ts +7 -0
- package/dist/interpreter/components/LazyRuntime.js +75 -0
- package/dist/interpreter/components/LogicEngine.d.ts +21 -0
- package/dist/interpreter/components/LogicEngine.js +152 -0
- package/dist/interpreter/components/LogicResolver.d.ts +11 -0
- package/dist/interpreter/components/LogicResolver.js +87 -0
- package/dist/interpreter/components/Operations.d.ts +14 -0
- package/dist/interpreter/components/Operations.js +69 -0
- package/dist/interpreter/components/PatternMatcher.d.ts +41 -0
- package/dist/interpreter/components/PatternMatcher.js +206 -0
- package/dist/interpreter/components/Visitor.d.ts +64 -0
- package/dist/interpreter/components/Visitor.js +299 -0
- package/dist/interpreter/errors.d.ts +19 -0
- package/dist/interpreter/errors.js +36 -0
- package/dist/interpreter/index.d.ts +32 -0
- package/dist/interpreter/index.js +44 -0
- package/dist/interpreter/utils.d.ts +14 -0
- package/dist/interpreter/utils.js +57 -0
- package/dist/utils/helpers.d.ts +14 -0
- package/dist/utils/helpers.js +51 -0
- package/package.json +30 -0
- package/src/analyzer/index.ts +132 -0
- package/src/analyzer/inspections/functional.ts +159 -0
- package/src/analyzer/inspections/generic.ts +499 -0
- package/src/analyzer/inspections/imperative.ts +129 -0
- package/src/analyzer/inspections/logic.ts +166 -0
- package/src/analyzer/inspections/object.ts +282 -0
- package/src/analyzer/utils.ts +26 -0
- package/src/index.ts +3 -0
- package/src/interpreter/components/EnvBuilder.ts +97 -0
- package/src/interpreter/components/FunctionRuntime.ts +79 -0
- package/src/interpreter/components/LazyRuntime.ts +97 -0
- package/src/interpreter/components/LogicEngine.ts +227 -0
- package/src/interpreter/components/LogicResolver.ts +130 -0
- package/src/interpreter/components/Operations.ts +81 -0
- package/src/interpreter/components/PatternMatcher.ts +254 -0
- package/src/interpreter/components/Visitor.ts +493 -0
- package/src/interpreter/errors.ts +47 -0
- package/src/interpreter/index.ts +59 -0
- package/src/interpreter/utils.ts +79 -0
- package/src/utils/helpers.ts +73 -0
- package/tests/analyzer/functional.spec.ts +221 -0
- package/tests/analyzer/generic.spec.ts +100 -0
- package/tests/analyzer/helpers.spec.ts +83 -0
- package/tests/analyzer/logic.spec.ts +292 -0
- package/tests/analyzer/oop.spec.ts +338 -0
- package/tests/interpreter/EnvBuilder.spec.ts +178 -0
- package/tests/interpreter/FunctionRuntime.spec.ts +234 -0
- package/tests/interpreter/LazyRuntime.spec.ts +190 -0
- package/tests/interpreter/LogicEngine.spec.ts +194 -0
- package/tests/interpreter/Operations.spec.ts +220 -0
- package/tests/interpreter/PatternSystem.spec.ts +189 -0
- package/tests/interpreter/interpreter.spec.ts +937 -0
- package/tsconfig.build.json +7 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { StopTraversalException, TraverseVisitor, } from "@yukigo/ast";
|
|
2
|
+
import { executeVisitor } from "../utils.js";
|
|
3
|
+
export class BindingVisitor extends TraverseVisitor {
|
|
4
|
+
targetBinding;
|
|
5
|
+
isInsideTargetScope = false; // this flag helps to check nested functions inside the targetBinding scope
|
|
6
|
+
constructor(binding) {
|
|
7
|
+
super();
|
|
8
|
+
this.targetBinding = binding;
|
|
9
|
+
}
|
|
10
|
+
visitProcedure(node) {
|
|
11
|
+
const currentFunctionName = node.identifier.value;
|
|
12
|
+
// if not inside scope then is top-level
|
|
13
|
+
if (!this.isInsideTargetScope) {
|
|
14
|
+
if (!this.targetBinding || currentFunctionName === this.targetBinding) {
|
|
15
|
+
this.isInsideTargetScope = true;
|
|
16
|
+
this.traverseCollection(node.equations);
|
|
17
|
+
this.isInsideTargetScope = false;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
// if inside the scope of targetBinding then traverse
|
|
23
|
+
this.traverseCollection(node.equations);
|
|
24
|
+
}
|
|
25
|
+
visitFunction(node) {
|
|
26
|
+
const currentFunctionName = node.identifier.value;
|
|
27
|
+
// if not inside scope then is top-level
|
|
28
|
+
if (!this.isInsideTargetScope) {
|
|
29
|
+
if (!this.targetBinding || currentFunctionName === this.targetBinding) {
|
|
30
|
+
this.isInsideTargetScope = true;
|
|
31
|
+
this.traverseCollection(node.equations);
|
|
32
|
+
this.isInsideTargetScope = false;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
// if inside the scope of targetBinding then traverse
|
|
38
|
+
this.traverseCollection(node.equations);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export class DeclaresEnumeration extends TraverseVisitor {
|
|
42
|
+
enumName;
|
|
43
|
+
constructor(enumName) {
|
|
44
|
+
super();
|
|
45
|
+
this.enumName = enumName;
|
|
46
|
+
}
|
|
47
|
+
visitEnumeration(node) {
|
|
48
|
+
if (node.identifier.value === this.enumName)
|
|
49
|
+
throw StopTraversalException;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export class DeclaresProcedure extends TraverseVisitor {
|
|
53
|
+
procedureName;
|
|
54
|
+
constructor(procedureName) {
|
|
55
|
+
super();
|
|
56
|
+
this.procedureName = procedureName;
|
|
57
|
+
}
|
|
58
|
+
visitProcedure(node) {
|
|
59
|
+
if (node.identifier.value === this.procedureName)
|
|
60
|
+
throw StopTraversalException;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export class UsesForLoop extends BindingVisitor {
|
|
64
|
+
visitForLoop(node) {
|
|
65
|
+
throw StopTraversalException;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export class UsesWhile extends BindingVisitor {
|
|
69
|
+
visitWhile(node) {
|
|
70
|
+
throw StopTraversalException;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export class UsesRepeat extends BindingVisitor {
|
|
74
|
+
visitRepeat(node) {
|
|
75
|
+
throw StopTraversalException;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export class UsesLoop extends BindingVisitor {
|
|
79
|
+
visitForLoop(node) {
|
|
80
|
+
throw StopTraversalException;
|
|
81
|
+
}
|
|
82
|
+
visitWhile(node) {
|
|
83
|
+
throw StopTraversalException;
|
|
84
|
+
}
|
|
85
|
+
visitRepeat(node) {
|
|
86
|
+
throw StopTraversalException;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export class UsesSwitch extends BindingVisitor {
|
|
90
|
+
visitSwitch(node) {
|
|
91
|
+
throw StopTraversalException;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
export const imperativeInspections = {
|
|
95
|
+
DeclaresEnumeration: (node, args) => executeVisitor(node, new DeclaresEnumeration(args[0])),
|
|
96
|
+
DeclaresProcedure: (node, args) => executeVisitor(node, new DeclaresProcedure(args[0])),
|
|
97
|
+
UsesForEach: (node, args) => {
|
|
98
|
+
throw Error("Inspection not implemented");
|
|
99
|
+
},
|
|
100
|
+
UsesForLoop: (node, args, binding) => executeVisitor(node, new UsesForLoop(binding)),
|
|
101
|
+
UsesRepeat: (node, args, binding) => executeVisitor(node, new UsesRepeat(binding)),
|
|
102
|
+
UsesWhile: (node, args, binding) => executeVisitor(node, new UsesWhile(binding)),
|
|
103
|
+
UsesLoop: (node, args, binding) => executeVisitor(node, new UsesLoop(binding)),
|
|
104
|
+
UsesSwitch: (node, args, binding) => executeVisitor(node, new UsesSwitch(binding)),
|
|
105
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Exist, Fact, Findall, Forall, Not, Rule, TraverseVisitor, UnifyOperation } from "@yukigo/ast";
|
|
2
|
+
import { InspectionMap } from "../utils.js";
|
|
3
|
+
export declare abstract class DeclaresBinding extends TraverseVisitor {
|
|
4
|
+
protected readonly targetBinding: string;
|
|
5
|
+
constructor(binding: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class DeclaresFact extends DeclaresBinding {
|
|
8
|
+
visitFact(node: Fact): void;
|
|
9
|
+
}
|
|
10
|
+
export declare class DeclaresRule extends DeclaresBinding {
|
|
11
|
+
visitRule(node: Rule): void;
|
|
12
|
+
}
|
|
13
|
+
export declare class DeclaresPredicate extends DeclaresBinding {
|
|
14
|
+
visitFact(node: Fact): void;
|
|
15
|
+
visitRule(node: Rule): void;
|
|
16
|
+
}
|
|
17
|
+
export declare class PredicateVisitor extends TraverseVisitor {
|
|
18
|
+
private readonly targetBinding;
|
|
19
|
+
protected isInsideTargetScope: boolean;
|
|
20
|
+
constructor(binding: string);
|
|
21
|
+
visitFact(node: Fact): void;
|
|
22
|
+
visitRule(node: Rule): void;
|
|
23
|
+
}
|
|
24
|
+
export declare class UsesFindall extends TraverseVisitor {
|
|
25
|
+
visitFindall(node: Findall): void;
|
|
26
|
+
}
|
|
27
|
+
export declare class UsesForall extends TraverseVisitor {
|
|
28
|
+
visitForall(node: Forall): void;
|
|
29
|
+
}
|
|
30
|
+
export declare class UsesNot extends TraverseVisitor {
|
|
31
|
+
visitNot(node: Not): void;
|
|
32
|
+
visitExist(node: Exist): void;
|
|
33
|
+
}
|
|
34
|
+
export declare class UsesUnificationOperator extends PredicateVisitor {
|
|
35
|
+
visitUnifyOperation(node: UnifyOperation): void;
|
|
36
|
+
}
|
|
37
|
+
export declare class UsesCut extends PredicateVisitor {
|
|
38
|
+
visitExist(node: Exist): void;
|
|
39
|
+
}
|
|
40
|
+
export declare class UsesFail extends PredicateVisitor {
|
|
41
|
+
visitExist(node: Exist): void;
|
|
42
|
+
}
|
|
43
|
+
export declare class HasRedundantReduction extends TraverseVisitor {
|
|
44
|
+
private readonly targetBinding;
|
|
45
|
+
protected isInsideTargetScope: boolean;
|
|
46
|
+
constructor(binding: string);
|
|
47
|
+
visitRule(node: Rule): void;
|
|
48
|
+
}
|
|
49
|
+
export declare const logicInspections: InspectionMap;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { AssignOperation, Exist, StopTraversalException, SymbolPrimitive, TraverseVisitor, } from "@yukigo/ast";
|
|
2
|
+
import { executeVisitor } from "../utils.js";
|
|
3
|
+
import { isYukigoPrimitive } from "@yukigo/ast";
|
|
4
|
+
export class DeclaresBinding extends TraverseVisitor {
|
|
5
|
+
targetBinding;
|
|
6
|
+
constructor(binding) {
|
|
7
|
+
super();
|
|
8
|
+
this.targetBinding = binding;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class DeclaresFact extends DeclaresBinding {
|
|
12
|
+
visitFact(node) {
|
|
13
|
+
const bindingName = node.identifier.value;
|
|
14
|
+
if (!this.targetBinding || bindingName === this.targetBinding)
|
|
15
|
+
throw new StopTraversalException();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export class DeclaresRule extends DeclaresBinding {
|
|
19
|
+
visitRule(node) {
|
|
20
|
+
const bindingName = node.identifier.value;
|
|
21
|
+
if (!this.targetBinding || bindingName === this.targetBinding)
|
|
22
|
+
throw new StopTraversalException();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class DeclaresPredicate extends DeclaresBinding {
|
|
26
|
+
visitFact(node) {
|
|
27
|
+
new DeclaresFact(this.targetBinding).visitFact(node);
|
|
28
|
+
}
|
|
29
|
+
visitRule(node) {
|
|
30
|
+
new DeclaresRule(this.targetBinding).visitRule(node);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class PredicateVisitor extends TraverseVisitor {
|
|
34
|
+
targetBinding;
|
|
35
|
+
isInsideTargetScope = false; // this flag helps to check nested functions inside the targetBinding scope
|
|
36
|
+
constructor(binding) {
|
|
37
|
+
super();
|
|
38
|
+
this.targetBinding = binding;
|
|
39
|
+
}
|
|
40
|
+
visitFact(node) {
|
|
41
|
+
const nodeName = node.identifier.value;
|
|
42
|
+
if (!this.isInsideTargetScope) {
|
|
43
|
+
if (nodeName === this.targetBinding) {
|
|
44
|
+
this.isInsideTargetScope = true;
|
|
45
|
+
this.traverseCollection(node.patterns);
|
|
46
|
+
this.isInsideTargetScope = false;
|
|
47
|
+
}
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.traverseCollection(node.patterns);
|
|
51
|
+
}
|
|
52
|
+
visitRule(node) {
|
|
53
|
+
const nodeName = node.identifier.value;
|
|
54
|
+
if (!this.isInsideTargetScope) {
|
|
55
|
+
if (nodeName === this.targetBinding) {
|
|
56
|
+
this.isInsideTargetScope = true;
|
|
57
|
+
this.traverseCollection(node.expressions);
|
|
58
|
+
this.isInsideTargetScope = false;
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.traverseCollection(node.expressions);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export class UsesFindall extends TraverseVisitor {
|
|
66
|
+
visitFindall(node) {
|
|
67
|
+
throw new StopTraversalException();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export class UsesForall extends TraverseVisitor {
|
|
71
|
+
visitForall(node) {
|
|
72
|
+
throw new StopTraversalException();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export class UsesNot extends TraverseVisitor {
|
|
76
|
+
visitNot(node) {
|
|
77
|
+
throw new StopTraversalException();
|
|
78
|
+
}
|
|
79
|
+
visitExist(node) {
|
|
80
|
+
if (node.identifier.value === "not")
|
|
81
|
+
throw new StopTraversalException();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export class UsesUnificationOperator extends PredicateVisitor {
|
|
85
|
+
visitUnifyOperation(node) {
|
|
86
|
+
throw new StopTraversalException();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export class UsesCut extends PredicateVisitor {
|
|
90
|
+
visitExist(node) {
|
|
91
|
+
if (node.identifier.value === "!")
|
|
92
|
+
throw new StopTraversalException();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export class UsesFail extends PredicateVisitor {
|
|
96
|
+
visitExist(node) {
|
|
97
|
+
if (node.identifier.value === "fail")
|
|
98
|
+
throw new StopTraversalException();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
export class HasRedundantReduction extends TraverseVisitor {
|
|
102
|
+
targetBinding;
|
|
103
|
+
isInsideTargetScope = false;
|
|
104
|
+
constructor(binding) {
|
|
105
|
+
super();
|
|
106
|
+
this.targetBinding = binding;
|
|
107
|
+
}
|
|
108
|
+
visitRule(node) {
|
|
109
|
+
// Only inspect the specified rule if a binding is provided
|
|
110
|
+
if (this.targetBinding && node.identifier.value !== this.targetBinding)
|
|
111
|
+
return;
|
|
112
|
+
// Check each unification in the rule's body
|
|
113
|
+
for (const bodyElement of node.expressions) {
|
|
114
|
+
if (bodyElement instanceof AssignOperation) {
|
|
115
|
+
const left = bodyElement.left;
|
|
116
|
+
const right = bodyElement.right;
|
|
117
|
+
if (!(left instanceof SymbolPrimitive))
|
|
118
|
+
continue;
|
|
119
|
+
const redundantReductionParameters = isYukigoPrimitive(right);
|
|
120
|
+
const redundantReductionFunctors = right instanceof Exist;
|
|
121
|
+
// Check if reduction is redundant
|
|
122
|
+
const isRedundant = redundantReductionParameters || redundantReductionFunctors;
|
|
123
|
+
if (isRedundant)
|
|
124
|
+
throw new StopTraversalException();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export const logicInspections = {
|
|
130
|
+
DeclaresFact: (node, args, binding) => executeVisitor(node, new DeclaresFact(binding)),
|
|
131
|
+
DeclaresRule: (node, args, binding) => executeVisitor(node, new DeclaresRule(binding)),
|
|
132
|
+
DeclaresPredicate: (node, args, binding) => executeVisitor(node, new DeclaresPredicate(binding)),
|
|
133
|
+
UsesFindall: (node, args, binding) => executeVisitor(node, new UsesFindall()),
|
|
134
|
+
UsesForall: (node, args, binding) => executeVisitor(node, new UsesForall()),
|
|
135
|
+
UsesNot: (node, args, binding) => executeVisitor(node, new UsesNot()),
|
|
136
|
+
UsesUnificationOperator: (node, args, binding) => executeVisitor(node, new UsesUnificationOperator(binding)),
|
|
137
|
+
UsesCut: (node, args, binding) => executeVisitor(node, new UsesCut(binding)),
|
|
138
|
+
UsesFail: (node, args, binding) => executeVisitor(node, new UsesFail(binding)),
|
|
139
|
+
HasRedundantReduction: (node, args, binding) => executeVisitor(node, new HasRedundantReduction(binding)),
|
|
140
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Attribute, Class, Include, Interface, Method, New, Object, PrimitiveMethod, Send, TraverseVisitor } from "@yukigo/ast";
|
|
2
|
+
import { InspectionMap } from "../utils.js";
|
|
3
|
+
export declare class DeclaresAttribute extends TraverseVisitor {
|
|
4
|
+
private attributeName;
|
|
5
|
+
constructor(attributeName: string);
|
|
6
|
+
visitAttribute(node: Attribute): void;
|
|
7
|
+
}
|
|
8
|
+
export declare class DeclaresClass extends TraverseVisitor {
|
|
9
|
+
private className;
|
|
10
|
+
constructor(className: string);
|
|
11
|
+
visitClass(node: Class): void;
|
|
12
|
+
}
|
|
13
|
+
export declare class DeclaresInterface extends TraverseVisitor {
|
|
14
|
+
private interfaceName;
|
|
15
|
+
constructor(interfaceName: string);
|
|
16
|
+
visitInterface(node: Interface): void;
|
|
17
|
+
}
|
|
18
|
+
export declare class DeclaresMethod extends TraverseVisitor {
|
|
19
|
+
private methodName;
|
|
20
|
+
constructor(methodName: string);
|
|
21
|
+
visitMethod(node: Method): void;
|
|
22
|
+
}
|
|
23
|
+
export declare class DeclaresObject extends TraverseVisitor {
|
|
24
|
+
private objectName;
|
|
25
|
+
constructor(objectName: string);
|
|
26
|
+
visitObject(node: Object): void;
|
|
27
|
+
}
|
|
28
|
+
export declare class DeclaresPrimitive extends TraverseVisitor {
|
|
29
|
+
private operatorName;
|
|
30
|
+
constructor(operatorName: string);
|
|
31
|
+
visitPrimitiveMethod(node: PrimitiveMethod): void;
|
|
32
|
+
}
|
|
33
|
+
export declare class DeclaresSuperclass extends TraverseVisitor {
|
|
34
|
+
private superclassName;
|
|
35
|
+
constructor(superclassName: string);
|
|
36
|
+
visitClass(node: Class): void;
|
|
37
|
+
}
|
|
38
|
+
export declare class Implements extends TraverseVisitor {
|
|
39
|
+
private interfaceName;
|
|
40
|
+
constructor(interfaceName: string);
|
|
41
|
+
visitClass(node: Class): void;
|
|
42
|
+
}
|
|
43
|
+
export declare class IncludeMixin extends TraverseVisitor {
|
|
44
|
+
private mixinsName;
|
|
45
|
+
constructor(mixinsName: string);
|
|
46
|
+
visitInclude(node: Include): void;
|
|
47
|
+
}
|
|
48
|
+
export declare class Instantiates extends TraverseVisitor {
|
|
49
|
+
private className;
|
|
50
|
+
constructor(className: string);
|
|
51
|
+
visitNew(node: New): void;
|
|
52
|
+
}
|
|
53
|
+
export declare class UsesDynamicPolymorphism extends TraverseVisitor {
|
|
54
|
+
private selectorName;
|
|
55
|
+
private count;
|
|
56
|
+
constructor(selectorName: string);
|
|
57
|
+
visitMethod(node: Method): void;
|
|
58
|
+
}
|
|
59
|
+
export declare class UsesInheritance extends TraverseVisitor {
|
|
60
|
+
visitClass(node: Class): void;
|
|
61
|
+
visitInterface(node: Interface): void;
|
|
62
|
+
}
|
|
63
|
+
export declare class UsesMixins extends TraverseVisitor {
|
|
64
|
+
visitInclude(node: Include): void;
|
|
65
|
+
}
|
|
66
|
+
export declare class UsesObjectComposition extends TraverseVisitor {
|
|
67
|
+
visitAttribute(node: Attribute): void;
|
|
68
|
+
}
|
|
69
|
+
export declare class UsesStaticMethodOverload extends TraverseVisitor {
|
|
70
|
+
private scopes;
|
|
71
|
+
visitClass(node: Class): void;
|
|
72
|
+
visitObject(node: Object): void;
|
|
73
|
+
visitMethod(node: Method): void;
|
|
74
|
+
}
|
|
75
|
+
export declare class UsesDynamicMethodOverload extends TraverseVisitor {
|
|
76
|
+
visitMethod(node: Method): void;
|
|
77
|
+
}
|
|
78
|
+
export declare class UsesTemplateMethod extends TraverseVisitor {
|
|
79
|
+
private abstractMethodsStack;
|
|
80
|
+
visitClass(node: Class): void;
|
|
81
|
+
visitSend(node: Send): void;
|
|
82
|
+
}
|
|
83
|
+
export declare const objectInspections: InspectionMap;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { New, Self, StopTraversalException, SymbolPrimitive, TraverseVisitor, } from "@yukigo/ast";
|
|
2
|
+
import { executeVisitor } from "../utils.js";
|
|
3
|
+
export class DeclaresAttribute extends TraverseVisitor {
|
|
4
|
+
attributeName;
|
|
5
|
+
constructor(attributeName) {
|
|
6
|
+
super();
|
|
7
|
+
this.attributeName = attributeName;
|
|
8
|
+
}
|
|
9
|
+
visitAttribute(node) {
|
|
10
|
+
if (node.identifier.value === this.attributeName)
|
|
11
|
+
throw new StopTraversalException();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class DeclaresClass extends TraverseVisitor {
|
|
15
|
+
className;
|
|
16
|
+
constructor(className) {
|
|
17
|
+
super();
|
|
18
|
+
this.className = className;
|
|
19
|
+
}
|
|
20
|
+
visitClass(node) {
|
|
21
|
+
if (node.identifier.value === this.className)
|
|
22
|
+
throw new StopTraversalException();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class DeclaresInterface extends TraverseVisitor {
|
|
26
|
+
interfaceName;
|
|
27
|
+
constructor(interfaceName) {
|
|
28
|
+
super();
|
|
29
|
+
this.interfaceName = interfaceName;
|
|
30
|
+
}
|
|
31
|
+
visitInterface(node) {
|
|
32
|
+
if (node.identifier.value === this.interfaceName)
|
|
33
|
+
throw new StopTraversalException();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export class DeclaresMethod extends TraverseVisitor {
|
|
37
|
+
methodName;
|
|
38
|
+
constructor(methodName) {
|
|
39
|
+
super();
|
|
40
|
+
this.methodName = methodName;
|
|
41
|
+
}
|
|
42
|
+
visitMethod(node) {
|
|
43
|
+
if (node.identifier.value === this.methodName)
|
|
44
|
+
throw new StopTraversalException();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export class DeclaresObject extends TraverseVisitor {
|
|
48
|
+
objectName;
|
|
49
|
+
constructor(objectName) {
|
|
50
|
+
super();
|
|
51
|
+
this.objectName = objectName;
|
|
52
|
+
}
|
|
53
|
+
visitObject(node) {
|
|
54
|
+
if (node.identifier.value === this.objectName)
|
|
55
|
+
throw new StopTraversalException();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export class DeclaresPrimitive extends TraverseVisitor {
|
|
59
|
+
operatorName;
|
|
60
|
+
constructor(operatorName) {
|
|
61
|
+
super();
|
|
62
|
+
this.operatorName = operatorName;
|
|
63
|
+
}
|
|
64
|
+
visitPrimitiveMethod(node) {
|
|
65
|
+
if (node.operator === this.operatorName)
|
|
66
|
+
throw new StopTraversalException();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export class DeclaresSuperclass extends TraverseVisitor {
|
|
70
|
+
superclassName;
|
|
71
|
+
constructor(superclassName) {
|
|
72
|
+
super();
|
|
73
|
+
this.superclassName = superclassName;
|
|
74
|
+
}
|
|
75
|
+
visitClass(node) {
|
|
76
|
+
if (node.extendsSymbol && node.extendsSymbol.value === this.superclassName)
|
|
77
|
+
throw new StopTraversalException();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export class Implements extends TraverseVisitor {
|
|
81
|
+
interfaceName;
|
|
82
|
+
constructor(interfaceName) {
|
|
83
|
+
super();
|
|
84
|
+
this.interfaceName = interfaceName;
|
|
85
|
+
}
|
|
86
|
+
visitClass(node) {
|
|
87
|
+
if (node.implementsNode &&
|
|
88
|
+
node.implementsNode.identifier.value === this.interfaceName)
|
|
89
|
+
throw new StopTraversalException();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export class IncludeMixin extends TraverseVisitor {
|
|
93
|
+
mixinsName;
|
|
94
|
+
constructor(mixinsName) {
|
|
95
|
+
super();
|
|
96
|
+
this.mixinsName = mixinsName;
|
|
97
|
+
}
|
|
98
|
+
visitInclude(node) {
|
|
99
|
+
if (node.identifier.value === this.mixinsName)
|
|
100
|
+
throw new StopTraversalException();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export class Instantiates extends TraverseVisitor {
|
|
104
|
+
className;
|
|
105
|
+
constructor(className) {
|
|
106
|
+
super();
|
|
107
|
+
this.className = className;
|
|
108
|
+
}
|
|
109
|
+
visitNew(node) {
|
|
110
|
+
if (node.identifier.value === this.className)
|
|
111
|
+
throw new StopTraversalException();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export class UsesDynamicPolymorphism extends TraverseVisitor {
|
|
115
|
+
selectorName;
|
|
116
|
+
count = 0;
|
|
117
|
+
constructor(selectorName) {
|
|
118
|
+
super();
|
|
119
|
+
this.selectorName = selectorName;
|
|
120
|
+
}
|
|
121
|
+
visitMethod(node) {
|
|
122
|
+
if (node.identifier.value === this.selectorName) {
|
|
123
|
+
this.count++;
|
|
124
|
+
if (this.count >= 2)
|
|
125
|
+
throw new StopTraversalException();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export class UsesInheritance extends TraverseVisitor {
|
|
130
|
+
visitClass(node) {
|
|
131
|
+
if (node.extendsSymbol)
|
|
132
|
+
throw new StopTraversalException();
|
|
133
|
+
}
|
|
134
|
+
visitInterface(node) {
|
|
135
|
+
if (node.extendsSymbol && node.extendsSymbol.length > 0)
|
|
136
|
+
throw new StopTraversalException();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
export class UsesMixins extends TraverseVisitor {
|
|
140
|
+
visitInclude(node) {
|
|
141
|
+
throw new StopTraversalException();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export class UsesObjectComposition extends TraverseVisitor {
|
|
145
|
+
visitAttribute(node) {
|
|
146
|
+
if (node.expression instanceof New)
|
|
147
|
+
throw new StopTraversalException();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
export class UsesStaticMethodOverload extends TraverseVisitor {
|
|
151
|
+
scopes = [];
|
|
152
|
+
visitClass(node) {
|
|
153
|
+
this.scopes.push(new Set());
|
|
154
|
+
node.expression.accept(this);
|
|
155
|
+
this.scopes.pop();
|
|
156
|
+
}
|
|
157
|
+
visitObject(node) {
|
|
158
|
+
this.scopes.push(new Set());
|
|
159
|
+
node.expression.accept(this);
|
|
160
|
+
this.scopes.pop();
|
|
161
|
+
}
|
|
162
|
+
visitMethod(node) {
|
|
163
|
+
const currentScope = this.scopes[0];
|
|
164
|
+
const methodName = node.identifier.value;
|
|
165
|
+
if (currentScope.has(methodName))
|
|
166
|
+
throw new StopTraversalException();
|
|
167
|
+
currentScope.add(methodName);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
export class UsesDynamicMethodOverload extends TraverseVisitor {
|
|
171
|
+
visitMethod(node) {
|
|
172
|
+
if (node.equations.length > 1)
|
|
173
|
+
throw new StopTraversalException();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
class AbstractMethodCollector extends TraverseVisitor {
|
|
177
|
+
abstractMethods = new Set();
|
|
178
|
+
visitMethod(node) {
|
|
179
|
+
if (node.getMetadata("isAbstract") === true)
|
|
180
|
+
this.abstractMethods.add(node.identifier.value);
|
|
181
|
+
}
|
|
182
|
+
// stop propagation to not mix scopes
|
|
183
|
+
visitClass(node) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
visitObject(node) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
export class UsesTemplateMethod extends TraverseVisitor {
|
|
191
|
+
abstractMethodsStack = [];
|
|
192
|
+
visitClass(node) {
|
|
193
|
+
const collector = new AbstractMethodCollector();
|
|
194
|
+
if (node.expression)
|
|
195
|
+
executeVisitor(node.expression, collector);
|
|
196
|
+
this.abstractMethodsStack.push(collector.abstractMethods);
|
|
197
|
+
node.expression.accept(this);
|
|
198
|
+
this.abstractMethodsStack.pop();
|
|
199
|
+
}
|
|
200
|
+
visitSend(node) {
|
|
201
|
+
if (node.receiver instanceof Self) {
|
|
202
|
+
if (this.abstractMethodsStack.length === 0)
|
|
203
|
+
return;
|
|
204
|
+
const currentAbstractMethods = this.abstractMethodsStack[0];
|
|
205
|
+
// This doesnt match if message is complex expression
|
|
206
|
+
if (!(node.selector instanceof SymbolPrimitive))
|
|
207
|
+
return;
|
|
208
|
+
const selectorName = node.selector.value;
|
|
209
|
+
const isMessageAbstract = currentAbstractMethods.has(selectorName);
|
|
210
|
+
if (isMessageAbstract)
|
|
211
|
+
throw new StopTraversalException();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
export const objectInspections = {
|
|
216
|
+
DeclaresAttribute: (node, args) => executeVisitor(node, new DeclaresAttribute(args[0])),
|
|
217
|
+
DeclaresClass: (node, args) => executeVisitor(node, new DeclaresClass(args[0])),
|
|
218
|
+
DeclaresInterface: (node, args) => executeVisitor(node, new DeclaresInterface(args[0])),
|
|
219
|
+
DeclaresMethod: (node, args) => executeVisitor(node, new DeclaresMethod(args[0])),
|
|
220
|
+
DeclaresObject: (node, args) => executeVisitor(node, new DeclaresObject(args[0])),
|
|
221
|
+
DeclaresPrimitive: (node, args) => executeVisitor(node, new DeclaresPrimitive(args[0])),
|
|
222
|
+
DeclaresSuperclass: (node, args) => executeVisitor(node, new DeclaresSuperclass(args[0])),
|
|
223
|
+
Implements: (node, args) => executeVisitor(node, new Implements(args[0])),
|
|
224
|
+
Include: (node, args) => executeVisitor(node, new IncludeMixin(args[0])),
|
|
225
|
+
Inherits: (node, args) => executeVisitor(node, new DeclaresSuperclass(args[0])),
|
|
226
|
+
Instantiates: (node, args) => executeVisitor(node, new Instantiates(args[0])),
|
|
227
|
+
UsesDynamicPolymorphism: (node, args) => executeVisitor(node, new UsesDynamicPolymorphism(args[0])),
|
|
228
|
+
UsesInheritance: (node, args) => executeVisitor(node, new UsesInheritance()),
|
|
229
|
+
UsesMixins: (node, args) => executeVisitor(node, new UsesMixins()),
|
|
230
|
+
UsesObjectComposition: (node, args) => executeVisitor(node, new UsesObjectComposition()),
|
|
231
|
+
UsesStaticMethodOverload: (node, args) => executeVisitor(node, new UsesStaticMethodOverload()),
|
|
232
|
+
UsesDynamicMethodOverload: (node, args) => executeVisitor(node, new UsesDynamicMethodOverload()),
|
|
233
|
+
UsesTemplateMethod: (node, args) => executeVisitor(node, new UsesTemplateMethod()),
|
|
234
|
+
UsesStaticPolymorphism: (node, args) => executeVisitor(node, new UsesStaticMethodOverload()),
|
|
235
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ASTNode, TraverseVisitor } from "@yukigo/ast";
|
|
2
|
+
export type InspectionHandler = (node: ASTNode, args: string[], binding?: string) => boolean;
|
|
3
|
+
export type InspectionMap = Record<string, InspectionHandler>;
|
|
4
|
+
export declare function executeVisitor(node: ASTNode, visitor: TraverseVisitor): boolean;
|
|
@@ -0,0 +1,16 @@
|
|
|
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;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return passes;
|
|
16
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AST, ASTNode, Fact, Function, Rule, TraverseVisitor } from "@yukigo/ast";
|
|
2
|
+
import { EnvStack } from "../index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Builds the initial environment by collecting all top-level function declarations.
|
|
5
|
+
* Each function captures a closure of the environment at its definition time,
|
|
6
|
+
* allowing recursion by including itself in the closure.
|
|
7
|
+
*/
|
|
8
|
+
export declare class EnvBuilderVisitor extends TraverseVisitor {
|
|
9
|
+
private env;
|
|
10
|
+
constructor(baseEnv?: EnvStack);
|
|
11
|
+
build(ast: AST): EnvStack;
|
|
12
|
+
visitFunction(node: Function): void;
|
|
13
|
+
visitFact(node: Fact): void;
|
|
14
|
+
visitRule(node: Rule): void;
|
|
15
|
+
visit(node: ASTNode): void;
|
|
16
|
+
}
|