parser: add checks for if statement

pull/3410/head
Swastik Baranwal 2020-01-11 17:51:35 +05:30 committed by Alexander Medvednikov
parent 06f581e146
commit e3db628d0c
1 changed files with 11 additions and 6 deletions

View File

@ -18,7 +18,6 @@ fn (p mut Parser) match_statement(is_expr bool) string {
}
is_sum_type := typ in p.table.sum_types
mut sum_child_type := ''
// is it safe to use p.cgen.insert_before ???
tmp_var := p.get_tmp()
p.cgen.insert_before('$typ $tmp_var = $expr;')
@ -142,7 +141,8 @@ fn (p mut Parser) match_statement(is_expr bool) string {
name: 'it'
typ: sum_child_type
})
} else {
}
else {
p.check_types(p.bool_expression(), typ)
}
p.expected_type = ''
@ -221,6 +221,12 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
}
p.next()
p.fspace()
if p.tok == .name && p.peek() == .assign {
p.error('cannot assign on if-else statement')
}
if p.tok == .name && (p.peek() == .inc || p.peek() == .dec) {
p.error('`${p.peek().str()}` is a statement')
}
// `if a := opt() { }` syntax
if p.tok == .name && p.peek() == .decl_assign {
p.check_not_reserved()
@ -352,4 +358,3 @@ fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
}
return typ
}