mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-15 08:55:04 +01:00
start implementing module res
This commit is contained in:
parent
2da011caf4
commit
cbbda39688
9 changed files with 389 additions and 95 deletions
|
|
@ -5,6 +5,7 @@ import {
|
|||
Identifier,
|
||||
ImportDef,
|
||||
Item,
|
||||
ModItem,
|
||||
Resolution,
|
||||
StringLiteral,
|
||||
Ty,
|
||||
|
|
@ -14,7 +15,7 @@ import {
|
|||
} from "./ast";
|
||||
|
||||
export function printAst(ast: Ast): string {
|
||||
return ast.items.map(printItem).join("\n");
|
||||
return ast.rootItems.map(printItem).join("\n");
|
||||
}
|
||||
|
||||
function printStringLiteral(lit: StringLiteral): string {
|
||||
|
|
@ -22,15 +23,20 @@ function printStringLiteral(lit: StringLiteral): string {
|
|||
}
|
||||
|
||||
function printItem(item: Item): string {
|
||||
const id = `/*${item.id}*/ `;
|
||||
|
||||
switch (item.kind) {
|
||||
case "function": {
|
||||
return printFunction(item.node);
|
||||
return id + printFunction(item.node);
|
||||
}
|
||||
case "type": {
|
||||
return printTypeDef(item.node);
|
||||
return id + printTypeDef(item.node);
|
||||
}
|
||||
case "import": {
|
||||
return printImportDef(item.node);
|
||||
return id + printImportDef(item.node);
|
||||
}
|
||||
case "mod": {
|
||||
return id +printMod(item.node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -65,6 +71,17 @@ function printImportDef(def: ImportDef): string {
|
|||
)}(${args})${ret};`;
|
||||
}
|
||||
|
||||
function printMod(mod: ModItem): string {
|
||||
switch (mod.modKind.kind) {
|
||||
case "inline":
|
||||
return `mod ${mod.name} (\n${mod.modKind.contents
|
||||
.map(printItem)
|
||||
.join("\n ")});`;
|
||||
case "extern":
|
||||
return `extern mod ${mod.name};`;
|
||||
}
|
||||
}
|
||||
|
||||
function printExpr(expr: Expr, indent: number): string {
|
||||
switch (expr.kind) {
|
||||
case "empty": {
|
||||
|
|
@ -117,6 +134,9 @@ function printExpr(expr: Expr, indent: number): string {
|
|||
case "ident": {
|
||||
return printIdent(expr.value);
|
||||
}
|
||||
case "path": {
|
||||
return `<${expr.segments.join(".")}>${printRes(expr.res)}`;
|
||||
}
|
||||
case "binary": {
|
||||
return `${printExpr(expr.lhs, indent)} ${expr.binaryKind} ${printExpr(
|
||||
expr.rhs,
|
||||
|
|
@ -185,18 +205,19 @@ 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`;
|
||||
}
|
||||
function printRes(res: Resolution): string {
|
||||
switch (res.kind) {
|
||||
case "local":
|
||||
return `#${res.index}`;
|
||||
case "item":
|
||||
return `#G${res.id}`;
|
||||
case "builtin": {
|
||||
return `#B`;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function printIdent(ident: Identifier): string {
|
||||
const res = ident.res ? printRes(ident.res) : "";
|
||||
return `${ident.name}${res}`;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue