This commit is contained in:
nora 2023-07-30 16:54:04 +02:00
parent 50e82066c9
commit 6d2a2fe474
9 changed files with 443 additions and 98 deletions

View file

@ -3,8 +3,10 @@ import {
Expr,
FunctionDef,
Identifier,
ImportDef,
Item,
Resolution,
StringLiteral,
Ty,
Type,
TypeDef,
@ -15,6 +17,10 @@ export function printAst(ast: Ast): string {
return ast.items.map(printItem).join("\n");
}
function printStringLiteral(lit: StringLiteral): string {
return `"${lit.value}"`;
}
function printItem(item: Item): string {
switch (item.kind) {
case "function": {
@ -23,6 +29,9 @@ function printItem(item: Item): string {
case "type": {
return printTypeDef(item.node);
}
case "import": {
return printImportDef(item.node);
}
}
}
@ -45,6 +54,17 @@ function printTypeDef(type: TypeDef): string {
return `type ${type.name} = ${fieldPart};`;
}
function printImportDef(def: ImportDef): string {
const args = def.params
.map(({ name, type }) => `${name}: ${printType(type)}`)
.join(", ");
const ret = def.returnType ? `: ${printType(def.returnType)}` : "";
return `import ${printStringLiteral(def.module)} ${printStringLiteral(
def.func
)}(${args})${ret};`;
}
function printExpr(expr: Expr, indent: number): string {
switch (expr.kind) {
case "empty": {
@ -84,10 +104,10 @@ function printExpr(expr: Expr, indent: number): string {
case "literal": {
switch (expr.value.kind) {
case "str": {
return `"${expr.value.value}"`;
return printStringLiteral(expr.value);
}
case "int": {
return `${expr.value.value}`;
return `${expr.value.value}_${expr.value.type}`;
}
}
}