name resolution

This commit is contained in:
nora 2023-07-23 17:32:34 +02:00
parent cc2a9aeca8
commit 35f1c92e36
6 changed files with 449 additions and 60 deletions

View file

@ -1,6 +1,14 @@
import { Expr, FunctionDef, Item, Type } from "./ast";
import {
Ast,
Expr,
FunctionDef,
Identifier,
Item,
Resolution,
Type,
} from "./ast";
export function printAst(ast: Item[]): string {
export function printAst(ast: Ast): string {
return ast.map(printItem).join("\n");
}
@ -67,7 +75,7 @@ function printExpr(expr: Expr, indent: number): string {
}
}
case "ident": {
return expr.value;
return printIdent(expr.value);
}
case "binary": {
return `${printExpr(expr.lhs, indent)} ${expr.binaryKind} ${printExpr(
@ -107,7 +115,7 @@ function printExpr(expr: Expr, indent: number): string {
function printType(type: Type): string {
switch (type.kind) {
case "ident":
return type.value;
return printIdent(type.value);
case "list":
return `[${printType(type.elem)}]`;
case "tuple":
@ -115,6 +123,22 @@ function printType(type: Type): string {
}
}
function printIdent(ident: Identifier): string {
const printRes = (res: Resolution): string => {
switch (res.kind) {
case "local":
return `#${res.index}`;
case "item":
return `#G${res.index}`;
case "builtin": {
return `#B`;
}
}
};
const res = ident.res ? printRes(ident.res) : "";
return `${ident.name}${res}`;
}
function linebreak(indent: number): string {
return `\n${ind(indent)}`;
}