hako(parser): add index operator support
- 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.
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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 ""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user