yukigo-haskell-parser 0.1.0 → 0.1.2
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 +3 -3
- package/CHANGELOG.md +6 -0
- package/README.md +10 -10
- package/dist/index.d.ts +13 -2
- package/dist/index.js +112 -32
- package/dist/index.js.map +1 -1
- package/dist/parser/grammar.js +220 -231
- package/dist/parser/grammar.js.map +1 -1
- package/dist/parser/lexer.d.ts +70 -15
- package/dist/parser/lexer.js +259 -50
- package/dist/parser/lexer.js.map +1 -1
- package/dist/prelude.d.ts +1 -1
- package/dist/prelude.js +280 -204
- package/dist/prelude.js.map +1 -1
- package/dist/typechecker/DeclarationCollector.d.ts +6 -1
- package/dist/typechecker/DeclarationCollector.js +39 -0
- package/dist/typechecker/DeclarationCollector.js.map +1 -1
- package/dist/typechecker/TypeBuilder.d.ts +1 -1
- package/dist/typechecker/TypeBuilder.js +6 -2
- package/dist/typechecker/TypeBuilder.js.map +1 -1
- package/dist/typechecker/checker.d.ts +15 -4
- package/dist/typechecker/checker.js +108 -25
- package/dist/typechecker/checker.js.map +1 -1
- package/dist/typechecker/core.d.ts +4 -0
- package/dist/typechecker/core.js +45 -19
- package/dist/typechecker/core.js.map +1 -1
- package/dist/typechecker/inference.d.ts +5 -1
- package/dist/typechecker/inference.js +95 -21
- package/dist/typechecker/inference.js.map +1 -1
- package/dist/utils/helpers.d.ts +1 -1
- package/dist/utils/helpers.js +1 -1
- package/dist/utils/helpers.js.map +1 -1
- package/dist/utils/types.d.ts +1 -1
- package/dist/utils/types.js +7 -0
- package/dist/utils/types.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +192 -55
- package/src/parser/grammar.ne +519 -463
- package/src/parser/grammar.ts +230 -239
- package/src/parser/lexer.ts +353 -77
- package/src/prelude.ts +282 -206
- package/src/typechecker/DeclarationCollector.ts +157 -104
- package/src/typechecker/TypeBuilder.ts +150 -148
- package/src/typechecker/checker.ts +501 -395
- package/src/typechecker/core.ts +192 -162
- package/src/typechecker/inference.ts +1421 -1327
- package/src/utils/helpers.ts +29 -29
- package/src/utils/types.ts +63 -56
- package/tests/hspec.spec.ts +92 -0
- package/tests/lexer.spec.ts +175 -0
- package/tests/parser.spec.ts +829 -823
- package/tests/prelude.spec.ts +17 -22
- package/tests/typechecker.spec.ts +327 -310
- package/tsconfig.json +26 -17
- package/tsconfig.tsbuildinfo +1 -0
- package/dist/parser/layoutPreprocessor.d.ts +0 -1
- package/dist/parser/layoutPreprocessor.js +0 -22
- package/dist/parser/layoutPreprocessor.js.map +0 -1
- package/dist/parser/preprocessor.d.ts +0 -28
- package/dist/parser/preprocessor.js +0 -453
- package/dist/parser/preprocessor.js.map +0 -1
- package/src/parser/layoutPreprocessor.ts +0 -21
- package/src/parser/preprocessor.ne +0 -375
- package/src/parser/preprocessor.ts +0 -502
- package/tests/preprocessor.spec.ts +0 -69
package/src/index.ts
CHANGED
|
@@ -1,55 +1,192 @@
|
|
|
1
|
-
import grammar from "./parser/grammar.js";
|
|
2
|
-
import nearley from "nearley";
|
|
3
|
-
import { groupFunctionDeclarations } from "./utils/helpers.js";
|
|
4
|
-
import { TypeChecker } from "./typechecker/checker.js";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
1
|
+
import grammar from "./parser/grammar.js";
|
|
2
|
+
import nearley from "nearley";
|
|
3
|
+
import { groupFunctionDeclarations } from "./utils/helpers.js";
|
|
4
|
+
import { TypeChecker } from "./typechecker/checker.js";
|
|
5
|
+
import {
|
|
6
|
+
ArithmeticUnaryOperation,
|
|
7
|
+
AST,
|
|
8
|
+
Equation,
|
|
9
|
+
Expression,
|
|
10
|
+
Function,
|
|
11
|
+
Instance,
|
|
12
|
+
ListType,
|
|
13
|
+
Return,
|
|
14
|
+
Sequence,
|
|
15
|
+
SimpleType,
|
|
16
|
+
StringOperation,
|
|
17
|
+
StringPrimitive,
|
|
18
|
+
SymbolPrimitive,
|
|
19
|
+
TypePattern,
|
|
20
|
+
UnguardedBody,
|
|
21
|
+
VariablePattern,
|
|
22
|
+
YukigoParser,
|
|
23
|
+
} from "yukigo-ast";
|
|
24
|
+
import { preludeCode } from "./prelude.js";
|
|
25
|
+
import { typeMappings } from "./utils/types.js";
|
|
26
|
+
import { Token } from "moo";
|
|
27
|
+
|
|
28
|
+
class UnexpectedToken extends Error {
|
|
29
|
+
constructor(token: Token) {
|
|
30
|
+
super(
|
|
31
|
+
`Parser: Unexpected '${token.type}' token '${token.value}' at line ${token.line} col ${token.col}.`,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
class AmbiguityError extends Error {
|
|
36
|
+
constructor(amountAST: number) {
|
|
37
|
+
super(
|
|
38
|
+
`Parser: Too much ambiguity. ${amountAST} ASTs parsed. Output not generated.`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
class TypeError extends Error {
|
|
43
|
+
constructor(errors: string[]) {
|
|
44
|
+
super(`Found type errors.\n\t-${errors.join("\n\t-")}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type HaskellConfig = {
|
|
49
|
+
typecheck: boolean;
|
|
50
|
+
includePrims: boolean;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const HaskellDefaultConfig = {
|
|
54
|
+
typecheck: true,
|
|
55
|
+
includePrims: true,
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export class YukigoHaskellParser implements YukigoParser {
|
|
59
|
+
public errors: string[] = [];
|
|
60
|
+
private prelude: AST;
|
|
61
|
+
private config: HaskellConfig;
|
|
62
|
+
private checker?: TypeChecker;
|
|
63
|
+
|
|
64
|
+
constructor(
|
|
65
|
+
prelude: string = preludeCode,
|
|
66
|
+
config: HaskellConfig = HaskellDefaultConfig,
|
|
67
|
+
) {
|
|
68
|
+
this.errors = [];
|
|
69
|
+
this.prelude = this.feedParser(prelude);
|
|
70
|
+
this.config = config;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private preprocessor(code: string): string {
|
|
74
|
+
return code.replace(/Exception\.evaluate/g, "evaluate");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public parse(code: string): AST {
|
|
78
|
+
const processedCode = this.preprocessor(code);
|
|
79
|
+
const result = this.feedParser(processedCode);
|
|
80
|
+
const fullAst = this.prelude.concat(result);
|
|
81
|
+
|
|
82
|
+
const makePrim = (name: string) => new Function(new SymbolPrimitive(name), [
|
|
83
|
+
new Equation(
|
|
84
|
+
[new VariablePattern(new SymbolPrimitive("x"))],
|
|
85
|
+
new UnguardedBody(new Sequence([new Return(new ArithmeticUnaryOperation("ToString", new SymbolPrimitive("x")))]))
|
|
86
|
+
)
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
const prims = [
|
|
90
|
+
makePrim("primShow"),
|
|
91
|
+
makePrim("primShowChar"),
|
|
92
|
+
makePrim("primShowString"),
|
|
93
|
+
makePrim("primShowList")
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
const resolveYukigoType = (t: any): any => {
|
|
97
|
+
if (t instanceof SimpleType) {
|
|
98
|
+
const mapped = typeMappings[t.value];
|
|
99
|
+
if (mapped) return new SimpleType(mapped, t.constraints, t.loc);
|
|
100
|
+
return t;
|
|
101
|
+
}
|
|
102
|
+
if (t instanceof ListType) return t;
|
|
103
|
+
return t;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const primShowString = new Function(new SymbolPrimitive("primShowString"), [
|
|
107
|
+
new Equation(
|
|
108
|
+
[new VariablePattern(new SymbolPrimitive("s"))],
|
|
109
|
+
new UnguardedBody(new Sequence([
|
|
110
|
+
new Return(
|
|
111
|
+
new StringOperation(
|
|
112
|
+
"Concat",
|
|
113
|
+
new StringPrimitive("\""),
|
|
114
|
+
new StringOperation("Concat", new SymbolPrimitive("s"), new StringPrimitive("\""))
|
|
115
|
+
)
|
|
116
|
+
)
|
|
117
|
+
]))
|
|
118
|
+
)
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
// Transform Instance nodes into Function nodes with TypePatterns
|
|
122
|
+
const transformedAst: AST = this.config.includePrims ? [...prims, primShowString] : [];
|
|
123
|
+
for (const node of fullAst) {
|
|
124
|
+
if (node instanceof Instance) {
|
|
125
|
+
const instanceNode = node as Instance;
|
|
126
|
+
const yukigoType = resolveYukigoType(instanceNode.type);
|
|
127
|
+
|
|
128
|
+
for (const func of instanceNode.functions) {
|
|
129
|
+
const overloadedEquations = func.equations.map((eq) => {
|
|
130
|
+
const firstPattern = eq.patterns[0];
|
|
131
|
+
const typePattern = new TypePattern(
|
|
132
|
+
yukigoType,
|
|
133
|
+
firstPattern
|
|
134
|
+
);
|
|
135
|
+
return new Equation(
|
|
136
|
+
[typePattern, ...eq.patterns.slice(1)],
|
|
137
|
+
eq.body,
|
|
138
|
+
eq.returnExpr,
|
|
139
|
+
eq.loc
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
transformedAst.unshift(
|
|
143
|
+
new Function(func.identifier, overloadedEquations, func.loc)
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
transformedAst.push(node);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const ast = groupFunctionDeclarations(transformedAst);
|
|
152
|
+
if (this.config.typecheck) {
|
|
153
|
+
this.checker = new TypeChecker();
|
|
154
|
+
const errors = this.checker.check(ast);
|
|
155
|
+
if (errors.length > 0) {
|
|
156
|
+
this.errors.push(...errors);
|
|
157
|
+
throw new TypeError(errors);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return ast;
|
|
161
|
+
}
|
|
162
|
+
public parseExpression(code: string): Expression {
|
|
163
|
+
const processedCode = this.preprocessor(code);
|
|
164
|
+
const expr = this.feedParser(processedCode)[0];
|
|
165
|
+
return expr;
|
|
166
|
+
}
|
|
167
|
+
private feedParser(code: string): any {
|
|
168
|
+
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
|
|
169
|
+
try {
|
|
170
|
+
parser.feed(code);
|
|
171
|
+
parser.finish();
|
|
172
|
+
} catch (error) {
|
|
173
|
+
if ("token" in error && error.token) throw new UnexpectedToken(error.token);
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
const { results } = parser;
|
|
177
|
+
if (results.length > 1) throw new AmbiguityError(results.length);
|
|
178
|
+
if (results.length == 0) return [];
|
|
179
|
+
return results[0];
|
|
180
|
+
}
|
|
181
|
+
public typeOfExpression(code: string): string {
|
|
182
|
+
if (!this.config.typecheck || !this.checker)
|
|
183
|
+
throw new Error("Type checking not initialized. Did you load a file?");
|
|
184
|
+
|
|
185
|
+
const expr = this.parseExpression(code);
|
|
186
|
+
return this.checker.inferExpression(expr);
|
|
187
|
+
}
|
|
188
|
+
public getKnownSymbols(): string[] {
|
|
189
|
+
if (!this.config.typecheck || !this.checker) return [];
|
|
190
|
+
return this.checker.getKnownSymbols();
|
|
191
|
+
}
|
|
192
|
+
}
|