add array initializer syntax
All checks were successful
Check / Explore-Gitea-Actions (push) Successful in 8s
All checks were successful
Check / Explore-Gitea-Actions (push) Successful in 8s
This commit is contained in:
parent
6620dbcace
commit
284627e0d2
@ -166,6 +166,12 @@ class FnLowerer {
|
||||
if (expr.is("IntExpr")) {
|
||||
return this.pushInst(Ty.Int, "Int", { value: expr.kind.value });
|
||||
}
|
||||
if (expr.is("ArrayExpr")) {
|
||||
const ty = this.checker.check(expr);
|
||||
const values = expr.kind.values
|
||||
.map((value) => this.lowerExpr(value));
|
||||
return this.pushInst(ty, "Array", { values });
|
||||
}
|
||||
if (expr.is("CallExpr")) {
|
||||
const ty = this.checker.check(expr);
|
||||
const args = expr.kind.args
|
||||
@ -407,6 +413,8 @@ export class Inst {
|
||||
case "Int":
|
||||
case "Bool":
|
||||
return ` ${k.value}`;
|
||||
case "Array":
|
||||
return ` [${k.values.map(r).join(", ")}]`;
|
||||
case "Fn":
|
||||
return ` ${k.fn.stmt.kind.ident}`;
|
||||
case "Param":
|
||||
@ -461,6 +469,7 @@ export type InstKind =
|
||||
| { tag: "Void" }
|
||||
| { tag: "Int"; value: number }
|
||||
| { tag: "Bool"; value: boolean }
|
||||
| { tag: "Array"; values: Inst[] }
|
||||
| { tag: "Fn"; fn: Fn }
|
||||
| { tag: "Param"; idx: number }
|
||||
| { tag: "Call"; callee: Inst; args: Inst[] }
|
||||
|
||||
@ -24,6 +24,19 @@ export class FnInterpreter {
|
||||
case "Void":
|
||||
case "Int":
|
||||
case "Bool":
|
||||
this.regs.set(inst, new Val(k));
|
||||
break;
|
||||
case "Array":
|
||||
this.regs.set(
|
||||
inst,
|
||||
new Val({
|
||||
tag: "Array",
|
||||
values: k.values.map((inst) =>
|
||||
this.regs.get(inst)!
|
||||
),
|
||||
}),
|
||||
);
|
||||
break;
|
||||
case "Fn":
|
||||
this.regs.set(inst, new Val(k));
|
||||
break;
|
||||
@ -201,7 +214,7 @@ class Val {
|
||||
|
||||
static Void = new Val({ tag: "Void" });
|
||||
|
||||
pretty() {
|
||||
pretty(): string {
|
||||
const k = this.kind;
|
||||
switch (k.tag) {
|
||||
case "Null":
|
||||
@ -213,11 +226,14 @@ class Val {
|
||||
return `${k.value}`;
|
||||
case "Ptr":
|
||||
return `<pointer>`;
|
||||
case "Array":
|
||||
return `[${k.values.map((v) => v.pretty()).join(", ")}]`;
|
||||
case "Fn":
|
||||
return `<${k.fn.ty.pretty()}>`;
|
||||
default:
|
||||
k satisfies never;
|
||||
}
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
|
||||
@ -227,4 +243,5 @@ type ValKind =
|
||||
| { tag: "Int"; value: number }
|
||||
| { tag: "Bool"; value: boolean }
|
||||
| { tag: "Ptr"; mutable: boolean; value: Val }
|
||||
| { tag: "Array"; values: Val[] }
|
||||
| { tag: "Fn"; fn: mir.Fn };
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user