From bec42e8dd387a6ba2d2543a1fc422a0974f2ae3a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 24 Mar 2021 12:34:06 +0000 Subject: [PATCH] Improve core:odin/ast ast.Range_Stmt to use generic number of `vals` rather than the fixed two to aid with parsing errors --- core/odin/ast/ast.odin | 3 +-- core/odin/ast/clone.odin | 3 +-- core/odin/ast/walk.odin | 9 ++++----- core/odin/parser/parser.odin | 16 ++-------------- 4 files changed, 8 insertions(+), 23 deletions(-) diff --git a/core/odin/ast/ast.odin b/core/odin/ast/ast.odin index f5eab84cc..e7411ebcd 100644 --- a/core/odin/ast/ast.odin +++ b/core/odin/ast/ast.odin @@ -402,8 +402,7 @@ Range_Stmt :: struct { using node: Stmt, label: ^Expr, for_pos: tokenizer.Pos, - val0: ^Expr, - val1: ^Expr, + vals: []^Expr, in_pos: tokenizer.Pos, expr: ^Expr, body: ^Stmt, diff --git a/core/odin/ast/clone.odin b/core/odin/ast/clone.odin index 295074f8c..e9344e0b4 100644 --- a/core/odin/ast/clone.odin +++ b/core/odin/ast/clone.odin @@ -192,8 +192,7 @@ clone_node :: proc(node: ^Node) -> ^Node { r.body = clone(r.body); case Range_Stmt: r.label = auto_cast clone(r.label); - r.val0 = clone(r.val0); - r.val1 = clone(r.val1); + r.vals = clone(r.vals); r.expr = clone(r.expr); r.body = clone(r.body); case Case_Clause: diff --git a/core/odin/ast/walk.odin b/core/odin/ast/walk.odin index 0a0b57006..9f60b7e7d 100644 --- a/core/odin/ast/walk.odin +++ b/core/odin/ast/walk.odin @@ -209,11 +209,10 @@ walk :: proc(v: ^Visitor, node: ^Node) { if n.label != nil { walk(v, n.label); } - if n.val0 != nil { - walk(v, n.val0); - } - if n.val1 != nil { - walk(v, n.val1); + for val in n.vals { + if val != nil { + walk(v, val); + } } walk(v, n.expr); walk(v, n.body); diff --git a/core/odin/parser/parser.odin b/core/odin/parser/parser.odin index 82f6c8ddf..db5d7ade2 100644 --- a/core/odin/parser/parser.odin +++ b/core/odin/parser/parser.odin @@ -738,18 +738,7 @@ parse_for_stmt :: proc(p: ^Parser) -> ^ast.Stmt { if is_range { assign_stmt := cond.derived.(ast.Assign_Stmt); - val0, val1: ^ast.Expr; - - switch len(assign_stmt.lhs) { - case 1: - val0 = assign_stmt.lhs[0]; - case 2: - val0 = assign_stmt.lhs[0]; - val1 = assign_stmt.lhs[1]; - case: - error(p, cond.pos, "expected either 1 or 2 identifiers"); - return ast.new(ast.Bad_Stmt, tok.pos, body.end); - } + vals := assign_stmt.lhs[:]; rhs: ^ast.Expr; if len(assign_stmt.rhs) > 0 { @@ -758,8 +747,7 @@ parse_for_stmt :: proc(p: ^Parser) -> ^ast.Stmt { range_stmt := ast.new(ast.Range_Stmt, tok.pos, body.end); range_stmt.for_pos = tok.pos; - range_stmt.val0 = val0; - range_stmt.val1 = val1; + range_stmt.vals = vals; range_stmt.in_pos = assign_stmt.op.pos; range_stmt.expr = rhs; range_stmt.body = body;