From dbc285f2b10077a4c4c402f0dcd0559be0278701 Mon Sep 17 00:00:00 2001 From: nyash-codex Date: Fri, 31 Oct 2025 20:22:43 +0900 Subject: [PATCH] hako(parser): add index operator support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ParserExprBox: support postfix [expr] and lower to Method(recv, "get", [index]) - ParserStmtBox: support index assignment IDENT [expr] = expr → Expr(Method(recv, "set", [idx, val])) - Note: lowering uses existing Method → MIR emit path (int args). Phase‑1 canaries use int indices/values. --- .../compiler/parser/expr/parser_expr_box.hako | 11 ++++++- .../compiler/parser/stmt/parser_stmt_box.hako | 30 +++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lang/src/compiler/parser/expr/parser_expr_box.hako b/lang/src/compiler/parser/expr/parser_expr_box.hako index 776202ce..6d596a99 100644 --- a/lang/src/compiler/parser/expr/parser_expr_box.hako +++ b/lang/src/compiler/parser/expr/parser_expr_box.hako @@ -168,6 +168,16 @@ static box ParserExprBox { k = ctx.skip_ws(src, k) if src.substring(k, k+1) == ")" { k = k + 1 } node = "{\"type\":\"Method\",\"recv\":" + node + ",\"method\":\"" + mname + "\",\"args\":" + args_json2 + "}" + } else if tch == "[" { + // Index access: node[ index ] → Method(recv=node, method="get", args=[index]) + k = k + 1 + k = ctx.skip_ws(src, k) + local idx_json = ctx.parse_expr2(src, k) + k = ctx.gpos_get() + k = ctx.skip_ws(src, k) + if src.substring(k, k+1) == "]" { k = k + 1 } + local args_idx = "[" + idx_json + "]" + node = "{\\\"type\\\":\\\"Method\\\",\\\"recv\\\":" + node + ",\\\"method\\\":\\\"get\\\",\\\"args\\\":" + args_idx + "}" } else { cont2 = 0 } @@ -352,4 +362,3 @@ static box ParserExprBox { return out + "@" + ctx.i2s(j) } } - diff --git a/lang/src/compiler/parser/stmt/parser_stmt_box.hako b/lang/src/compiler/parser/stmt/parser_stmt_box.hako index db415f7b..f3db3747 100644 --- a/lang/src/compiler/parser/stmt/parser_stmt_box.hako +++ b/lang/src/compiler/parser/stmt/parser_stmt_box.hako @@ -16,13 +16,40 @@ static box ParserStmtBox { return me.parse_using(src, j, stmt_start, ctx) } - // assignment: IDENT '=' expr + // assignment: IDENT '[' expr ']' '=' expr or IDENT '=' expr if j < src.size() && ctx.is_alpha(src.substring(j, j+1)) { local idp0 = ctx.read_ident2(src, j) local at0 = idp0.lastIndexOf("@") if at0 > 0 { local name0 = idp0.substring(0, at0) local k0 = ctx.to_int(idp0.substring(at0+1, idp0.size())) + // Case A: index assignment arr[expr] = value + { + local kA = ctx.skip_ws(src, k0) + if kA < src.size() && src.substring(kA, kA+1) == "[" { + kA = kA + 1 + kA = ctx.skip_ws(src, kA) + // parse index expression + local idx_json = ctx.parse_expr2(src, kA) + kA = ctx.gpos_get() + kA = ctx.skip_ws(src, kA) + if kA < src.size() && src.substring(kA, kA+1) == "]" { kA = kA + 1 } + kA = ctx.skip_ws(src, kA) + if kA < src.size() && src.substring(kA, kA+1) == "=" { + // parse RHS + kA = kA + 1 + kA = ctx.skip_ws(src, kA) + local rhs_json = ctx.parse_expr2(src, kA) + kA = ctx.gpos_get() + // Build Method set(name[idx], rhs) as Expr statement + local recv = "{\\\"type\\\":\\\"Var\\\",\\\"name\\\":\"" + name0 + "\"}" + local args = "[" + idx_json + "," + rhs_json + "]" + local method = "{\\\"type\\\":\\\"Method\\\",\\\"recv\\\":" + recv + ",\\\"method\\\":\\\"set\\\",\\\"args\\\":" + args + "}" + ctx.gpos_set(kA) + return "{\"type\":\"Expr\",\"expr\":" + method + "}" + } + } + } k0 = ctx.skip_ws(src, k0) if k0 < src.size() && src.substring(k0, k0+1) == "=" { local eq_two = "=" @@ -199,4 +226,3 @@ static box ParserStmtBox { return "" } } -