mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 14:48:47 +00:00
Merge pull request #2696 from hwchen/hwchen/fix-parse-call-expr-ellipses
core:odin/parser allow args after varargs in parse_call_expr
This commit is contained in:
@@ -2941,9 +2941,9 @@ parse_call_expr :: proc(p: ^Parser, operand: ^ast.Expr) -> ^ast.Expr {
|
|||||||
p.expr_level += 1
|
p.expr_level += 1
|
||||||
open := expect_token(p, .Open_Paren)
|
open := expect_token(p, .Open_Paren)
|
||||||
|
|
||||||
|
seen_ellipsis := false
|
||||||
for p.curr_tok.kind != .Close_Paren &&
|
for p.curr_tok.kind != .Close_Paren &&
|
||||||
p.curr_tok.kind != .EOF &&
|
p.curr_tok.kind != .EOF {
|
||||||
ellipsis.pos.line == 0 {
|
|
||||||
|
|
||||||
if p.curr_tok.kind == .Comma {
|
if p.curr_tok.kind == .Comma {
|
||||||
error(p, p.curr_tok.pos, "expected an expression not ,")
|
error(p, p.curr_tok.pos, "expected an expression not ,")
|
||||||
@@ -2972,10 +2972,16 @@ parse_call_expr :: proc(p: ^Parser, operand: ^ast.Expr) -> ^ast.Expr {
|
|||||||
fv.value = value
|
fv.value = value
|
||||||
|
|
||||||
arg = fv
|
arg = fv
|
||||||
|
} else if seen_ellipsis {
|
||||||
|
error(p, arg.pos, "Positional arguments are not allowed after '..'")
|
||||||
}
|
}
|
||||||
|
|
||||||
append(&args, arg)
|
append(&args, arg)
|
||||||
|
|
||||||
|
if ellipsis.pos.line != 0 {
|
||||||
|
seen_ellipsis = true
|
||||||
|
}
|
||||||
|
|
||||||
if !allow_token(p, .Comma) {
|
if !allow_token(p, .Comma) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -459,6 +459,27 @@ named_proc_return_parameters :: proc() {
|
|||||||
fmt.println("foo2 =", foo2()) // 567 321
|
fmt.println("foo2 =", foo2()) // 567 321
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variadic_procedures :: proc() {
|
||||||
|
fmt.println("\n# variadic procedures")
|
||||||
|
sum :: proc(nums: ..int, init_value:= 0) -> (result: int) {
|
||||||
|
result = init_value
|
||||||
|
for n in nums {
|
||||||
|
result += n
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.println("sum(()) =", sum())
|
||||||
|
fmt.println("sum(1, 2) =", sum(1, 2))
|
||||||
|
fmt.println("sum(1, 2, 3, 4, 5) =", sum(1, 2, 3, 4, 5))
|
||||||
|
fmt.println("sum(1, 2, 3, 4, 5, init_value = 5) =", sum(1, 2, 3, 4, 5, init_value = 5))
|
||||||
|
|
||||||
|
// pass a slice as varargs
|
||||||
|
odds := []int{1, 3, 5}
|
||||||
|
fmt.println("odds =", odds)
|
||||||
|
fmt.println("sum(..odds) =", sum(..odds))
|
||||||
|
fmt.println("sum(..odds, init_value = 5) =", sum(..odds, init_value = 5))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
explicit_procedure_overloading :: proc() {
|
explicit_procedure_overloading :: proc() {
|
||||||
fmt.println("\n# explicit procedure overloading")
|
fmt.println("\n# explicit procedure overloading")
|
||||||
@@ -2462,6 +2483,7 @@ main :: proc() {
|
|||||||
the_basics()
|
the_basics()
|
||||||
control_flow()
|
control_flow()
|
||||||
named_proc_return_parameters()
|
named_proc_return_parameters()
|
||||||
|
variadic_procedures()
|
||||||
explicit_procedure_overloading()
|
explicit_procedure_overloading()
|
||||||
struct_type()
|
struct_type()
|
||||||
union_type()
|
union_type()
|
||||||
|
|||||||
Reference in New Issue
Block a user