parser: fix comptime if script mode (fix #6419) (#14578)

master
yuyi 2022-06-01 18:27:27 +08:00 committed by GitHub
parent 5a2c271bd4
commit 786045c7da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 34 deletions

View File

@ -667,10 +667,15 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
return p.error('unexpected eof') return p.error('unexpected eof')
} }
if_expr := p.if_expr(true) if_expr := p.if_expr(true)
return ast.ExprStmt{ cur_stmt := ast.ExprStmt{
expr: if_expr expr: if_expr
pos: if_expr.pos pos: if_expr.pos
} }
if comptime_if_expr_contains_top_stmt(if_expr) {
return cur_stmt
} else {
return p.other_stmts(cur_stmt)
}
} }
.hash { .hash {
return p.hash() return p.hash()
@ -688,6 +693,40 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
return p.comment_stmt() return p.comment_stmt()
} }
else { else {
return p.other_stmts(ast.empty_stmt())
}
}
if p.should_abort {
break
}
}
// TODO remove dummy return statement
// the compiler complains if it's not there
return ast.empty_stmt()
}
fn comptime_if_expr_contains_top_stmt(if_expr ast.IfExpr) bool {
for branch in if_expr.branches {
for stmt in branch.stmts {
if stmt is ast.ExprStmt {
if stmt.expr is ast.IfExpr {
if !comptime_if_expr_contains_top_stmt(stmt.expr) {
return false
}
} else if stmt.expr is ast.CallExpr {
return false
}
} else if stmt is ast.AssignStmt {
return false
} else if stmt is ast.HashStmt {
return true
}
}
}
return true
}
fn (mut p Parser) other_stmts(cur_stmt ast.Stmt) ast.Stmt {
p.inside_fn = true p.inside_fn = true
if p.pref.is_script && !p.pref.is_test { if p.pref.is_script && !p.pref.is_test {
p.script_mode = true p.script_mode = true
@ -699,6 +738,9 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
p.open_scope() p.open_scope()
mut stmts := []ast.Stmt{} mut stmts := []ast.Stmt{}
if cur_stmt != ast.empty_stmt() {
stmts << cur_stmt
}
for p.tok.kind != .eof { for p.tok.kind != .eof {
stmts << p.stmt(false) stmts << p.stmt(false)
} }
@ -722,15 +764,6 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
return p.error('bad top level statement ' + p.tok.str()) return p.error('bad top level statement ' + p.tok.str())
} }
} }
}
if p.should_abort {
break
}
}
// TODO remove dummy return statement
// the compiler complains if it's not there
return ast.empty_stmt()
}
// TODO [if vfmt] // TODO [if vfmt]
pub fn (mut p Parser) check_comment() ast.Comment { pub fn (mut p Parser) check_comment() ast.Comment {

View File

@ -0,0 +1 @@
others

View File

@ -0,0 +1,5 @@
$if vinix {
println('vinix')
} $else {
println('others')
}