Start lowering

This commit is contained in:
nora 2023-07-26 13:20:32 +02:00
parent 8b424c0add
commit 87f081a4fe
5 changed files with 33 additions and 25 deletions

View file

@ -1,36 +1,35 @@
import {
Ast,
binaryExprPrecedenceClass,
COMPARISON_KINDS,
DEFAULT_FOLDER,
EQUALITY_KINDS,
Expr,
ExprBinary,
ExprUnary,
foldAst,
Folder,
Identifier,
LOGICAL_KINDS,
Resolution,
Ty,
TY_BOOL,
TY_INT,
TY_STRING,
TY_UNIT,
TyFn,
Type,
binaryExprPrecedenceClass,
fold_ast,
} from "./ast";
import { CompilerError, Span } from "./error";
import { printTy } from "./printer";
const TY_UNIT: Ty = { kind: "tuple", elems: [] };
const TY_STRING: Ty = { kind: "string" };
const TY_BOOL: Ty = { kind: "bool" };
const TY_INT: Ty = { kind: "int" };
function builtinAsTy(name: string, span: Span): Ty {
switch (name) {
case "String": {
return TY_STRING;
}
case "Int": {
return TY_INT;
return TY_BOOL;
}
case "Bool": {
return TY_BOOL;
@ -105,7 +104,7 @@ export function typeck(ast: Ast): Ast {
const item = ast[index];
switch (item.kind) {
case "function": {
const args = item.node.args.map((arg) => lowerAstTy(arg.type));
const args = item.node.params.map((arg) => lowerAstTy(arg.type));
const returnTy: Ty = item.node.returnType
? lowerAstTy(item.node.returnType)
: TY_UNIT;
@ -153,7 +152,7 @@ export function typeck(ast: Ast): Ast {
kind: "function",
node: {
name: item.node.name,
args: item.node.args.map((arg, i) => ({
params: item.node.params.map((arg, i) => ({
...arg,
type: { ...arg.type, ty: fnTy.params[i] },
})),
@ -162,15 +161,14 @@ export function typeck(ast: Ast): Ast {
},
span: item.span,
id: item.id,
ty: fnTy,
};
}
}
},
};
const withTypes = fold_ast(ast, checker);
return withTypes;
return foldAst(ast, checker);
}
type TyVarRes =