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")) {
|
if (expr.is("IntExpr")) {
|
||||||
return this.pushInst(Ty.Int, "Int", { value: expr.kind.value });
|
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")) {
|
if (expr.is("CallExpr")) {
|
||||||
const ty = this.checker.check(expr);
|
const ty = this.checker.check(expr);
|
||||||
const args = expr.kind.args
|
const args = expr.kind.args
|
||||||
@ -407,6 +413,8 @@ export class Inst {
|
|||||||
case "Int":
|
case "Int":
|
||||||
case "Bool":
|
case "Bool":
|
||||||
return ` ${k.value}`;
|
return ` ${k.value}`;
|
||||||
|
case "Array":
|
||||||
|
return ` [${k.values.map(r).join(", ")}]`;
|
||||||
case "Fn":
|
case "Fn":
|
||||||
return ` ${k.fn.stmt.kind.ident}`;
|
return ` ${k.fn.stmt.kind.ident}`;
|
||||||
case "Param":
|
case "Param":
|
||||||
@ -461,6 +469,7 @@ export type InstKind =
|
|||||||
| { tag: "Void" }
|
| { tag: "Void" }
|
||||||
| { tag: "Int"; value: number }
|
| { tag: "Int"; value: number }
|
||||||
| { tag: "Bool"; value: boolean }
|
| { tag: "Bool"; value: boolean }
|
||||||
|
| { tag: "Array"; values: Inst[] }
|
||||||
| { tag: "Fn"; fn: Fn }
|
| { tag: "Fn"; fn: Fn }
|
||||||
| { tag: "Param"; idx: number }
|
| { tag: "Param"; idx: number }
|
||||||
| { tag: "Call"; callee: Inst; args: Inst[] }
|
| { tag: "Call"; callee: Inst; args: Inst[] }
|
||||||
|
|||||||
@ -24,6 +24,19 @@ export class FnInterpreter {
|
|||||||
case "Void":
|
case "Void":
|
||||||
case "Int":
|
case "Int":
|
||||||
case "Bool":
|
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":
|
case "Fn":
|
||||||
this.regs.set(inst, new Val(k));
|
this.regs.set(inst, new Val(k));
|
||||||
break;
|
break;
|
||||||
@ -201,7 +214,7 @@ class Val {
|
|||||||
|
|
||||||
static Void = new Val({ tag: "Void" });
|
static Void = new Val({ tag: "Void" });
|
||||||
|
|
||||||
pretty() {
|
pretty(): string {
|
||||||
const k = this.kind;
|
const k = this.kind;
|
||||||
switch (k.tag) {
|
switch (k.tag) {
|
||||||
case "Null":
|
case "Null":
|
||||||
@ -213,11 +226,14 @@ class Val {
|
|||||||
return `${k.value}`;
|
return `${k.value}`;
|
||||||
case "Ptr":
|
case "Ptr":
|
||||||
return `<pointer>`;
|
return `<pointer>`;
|
||||||
|
case "Array":
|
||||||
|
return `[${k.values.map((v) => v.pretty()).join(", ")}]`;
|
||||||
case "Fn":
|
case "Fn":
|
||||||
return `<${k.fn.ty.pretty()}>`;
|
return `<${k.fn.ty.pretty()}>`;
|
||||||
default:
|
default:
|
||||||
k satisfies never;
|
k satisfies never;
|
||||||
}
|
}
|
||||||
|
throw new Error();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,4 +243,5 @@ type ValKind =
|
|||||||
| { tag: "Int"; value: number }
|
| { tag: "Int"; value: number }
|
||||||
| { tag: "Bool"; value: boolean }
|
| { tag: "Bool"; value: boolean }
|
||||||
| { tag: "Ptr"; mutable: boolean; value: Val }
|
| { tag: "Ptr"; mutable: boolean; value: Val }
|
||||||
|
| { tag: "Array"; values: Val[] }
|
||||||
| { tag: "Fn"; fn: mir.Fn };
|
| { tag: "Fn"; fn: mir.Fn };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user