From 284627e0d206eb0838321c15a4188555d7810458 Mon Sep 17 00:00:00 2001 From: sfja Date: Fri, 13 Mar 2026 00:23:33 +0100 Subject: [PATCH] add array initializer syntax --- src/middle.ts | 9 +++++++++ src/mir_interpreter.ts | 19 ++++++++++++++++++- tests/{_array.ethlang => array.ethlang} | 0 3 files changed, 27 insertions(+), 1 deletion(-) rename tests/{_array.ethlang => array.ethlang} (100%) diff --git a/src/middle.ts b/src/middle.ts index 4aa10e6..1152fa2 100644 --- a/src/middle.ts +++ b/src/middle.ts @@ -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[] } diff --git a/src/mir_interpreter.ts b/src/mir_interpreter.ts index f5369c4..bb74c4a 100644 --- a/src/mir_interpreter.ts +++ b/src/mir_interpreter.ts @@ -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 ``; + 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 }; diff --git a/tests/_array.ethlang b/tests/array.ethlang similarity index 100% rename from tests/_array.ethlang rename to tests/array.ethlang