mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-15 00:45:04 +01:00
start structs
This commit is contained in:
parent
f6f6673721
commit
b52abed441
8 changed files with 214 additions and 62 deletions
|
|
@ -7,6 +7,7 @@ import {
|
|||
Resolution,
|
||||
Ty,
|
||||
Type,
|
||||
TypeDef,
|
||||
tyIsUnit,
|
||||
} from "./ast";
|
||||
|
||||
|
|
@ -19,6 +20,9 @@ function printItem(item: Item): string {
|
|||
case "function": {
|
||||
return printFunction(item.node);
|
||||
}
|
||||
case "type": {
|
||||
return printTypeDef(item.node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -27,7 +31,18 @@ function printFunction(func: FunctionDef): string {
|
|||
.map(({ name, type }) => `${name}: ${printType(type)}`)
|
||||
.join(", ");
|
||||
const ret = func.returnType ? `: ${printType(func.returnType)}` : "";
|
||||
return `function ${func.name}(${args})${ret} = ${printExpr(func.body, 0)}`;
|
||||
return `function ${func.name}(${args})${ret} = ${printExpr(func.body, 0)};`;
|
||||
}
|
||||
|
||||
function printTypeDef(type: TypeDef): string {
|
||||
const fields = type.fields.map(
|
||||
({ name, type }) => `${ind(1)}${name.name}: ${printType(type)},`
|
||||
);
|
||||
|
||||
const fieldPart =
|
||||
type.fields.length === 0 ? "()" : `(\n${fields.join("\n")}\n)`;
|
||||
|
||||
return `type ${type.name} = ${fieldPart};`;
|
||||
}
|
||||
|
||||
function printExpr(expr: Expr, indent: number): string {
|
||||
|
|
@ -165,6 +180,9 @@ export function printTy(ty: Ty): string {
|
|||
case "var": {
|
||||
return `?${ty.index}`;
|
||||
}
|
||||
case "struct": {
|
||||
return ty.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue