compiler: struct to mir

This commit is contained in:
SimonFJ20 2025-02-17 09:22:17 +01:00
parent 1f357fbb00
commit 5b109a5432
4 changed files with 31 additions and 1 deletions

View File

@ -436,7 +436,7 @@ export class Checker {
notCovered
.keys()
.toArray()
.map((field) => `'${field}'`)
.map((field) => `'${this.ctx.identText(field.ident)}'`)
.join(", ")
} not covered`,
expr.span,

View File

@ -273,7 +273,9 @@ export class FnLowerer {
case "group":
case "array":
case "repeat":
return todo(k.tag);
case "struct":
return this.lowerStructExpr(expr, k);
case "ref":
case "deref":
case "elem":
@ -322,6 +324,13 @@ export class FnLowerer {
exhausted(re.kind);
}
private lowerStructExpr(expr: ast.Expr, kind: ast.StructExpr): RVal {
const ty = this.ch.exprTy(expr);
const fields = kind.fields
.map((field) => this.lowerExprToOperand(field.expr));
return { tag: "struct", ty, fields };
}
private lowerCallExpr(expr: ast.Expr, kind: ast.CallExpr): RVal {
const args = kind.args.map((arg) => this.lowerExprToOperand(arg));
const func = this.lowerExprToOperand(kind.expr);

View File

@ -85,6 +85,7 @@ export type RVal =
| { tag: "ptr"; place: Place; mut: boolean }
| { tag: "binary"; binaryType: BinaryType; left: Operand; right: Operand }
| { tag: "unary"; unaryType: UnaryType; operand: Operand }
| { tag: "struct"; ty: Ty; fields: Operand[] }
| { tag: "call"; func: Operand; args: Operand[] };
export type BinaryType =

View File

@ -165,6 +165,26 @@ export class MirFnStringifyer {
}
case "unary":
return todo(rval.tag);
case "struct": {
const tyk = rval.ty.kind;
if (tyk.tag === "struct") {
const datak = tyk.kind.data.kind;
if (datak.tag !== "struct") {
throw new Error();
}
const name = tyk.item.ident.text;
const fields = rval.fields
.map((field, idx) =>
`${datak.fields[idx].ident!.text}: ${
this.operand(field)
}`
)
.join(", ");
return `${name} { ${fields} }`;
} else {
return todo();
}
}
case "call":
return `call ${this.operand(rval.func)}(${
rval.args.map((arg) => this.operand(arg)).join(", ")