mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-15 00:45:04 +01:00
name resolution
This commit is contained in:
parent
cc2a9aeca8
commit
35f1c92e36
6 changed files with 449 additions and 60 deletions
|
|
@ -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)}`;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue