if-statement optional braces when single statement
This commit is contained in:
@@ -1184,6 +1184,27 @@ parse_block :: proc(parser: ^Parser) -> []ast.Stmt_Id {
|
||||
return body[:]
|
||||
}
|
||||
|
||||
// parse_branch_body parses an `if` then/else body: a braced block (possibly on
|
||||
// a following line) or a single brace-less statement (milestone 16). For the
|
||||
// then-branch (`validate_condition`), a brace-less body requires the condition
|
||||
// to be parenthesized unless it is a function call; the `else` keyword and the
|
||||
// unwrap `|...|` already delimit, so those positions skip the check.
|
||||
parse_branch_body :: proc(parser: ^Parser, validate_condition: bool, condition: ast.Expr_Id, has_captures: bool) -> []ast.Stmt_Id {
|
||||
skip_newlines(parser)
|
||||
if current(parser).kind == .Left_Brace {
|
||||
return parse_block(parser)
|
||||
}
|
||||
if validate_condition && !has_captures && condition != ast.INVALID_EXPR && int(condition) < len(parser.module.exprs) {
|
||||
expr := parser.module.exprs[condition]
|
||||
if !expr.parenthesized && expr.kind != .Call {
|
||||
source.add(parser.diagnostics, expr.span, "a brace-less 'if' body requires the condition to be parenthesized unless it is a function call")
|
||||
}
|
||||
}
|
||||
single := make([]ast.Stmt_Id, 1, parser.module.allocator)
|
||||
single[0] = parse_statement(parser)
|
||||
return single
|
||||
}
|
||||
|
||||
parse_if :: proc(parser: ^Parser) -> ast.Stmt_Id {
|
||||
start := advance(parser) // consume 'if'
|
||||
skip_newlines(parser)
|
||||
@@ -1226,8 +1247,7 @@ parse_if :: proc(parser: ^Parser) -> ast.Stmt_Id {
|
||||
source.add(parser.diagnostics, current(parser).span, "expected '|' to close unwrap captures")
|
||||
}
|
||||
}
|
||||
skip_newlines(parser)
|
||||
then_body := parse_block(parser)
|
||||
then_body := parse_branch_body(parser, true, condition, len(captures) > 0)
|
||||
else_body: []ast.Stmt_Id = nil
|
||||
saved_cursor := parser.cursor
|
||||
skip_newlines(parser)
|
||||
@@ -1240,7 +1260,7 @@ parse_if :: proc(parser: ^Parser) -> ast.Stmt_Id {
|
||||
single[0] = nested
|
||||
else_body = single
|
||||
} else {
|
||||
else_body = parse_block(parser)
|
||||
else_body = parse_branch_body(parser, false, ast.INVALID_EXPR, false)
|
||||
}
|
||||
} else {
|
||||
parser.cursor = saved_cursor
|
||||
|
||||
Reference in New Issue
Block a user