parser: fix script and repl error

pull/4312/head
yuyi 2020-04-09 18:46:16 +08:00 committed by GitHub
parent 384d401af0
commit 87df587f5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 3 deletions

View File

@ -320,9 +320,18 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
return p.comment()
}
else {
// #printf("");
p.error('bad top level statement ' + p.tok.str())
return ast.Stmt{}
if p.pref.is_script && !p.pref.is_test {
p.scanner.text = 'fn main() {' + p.scanner.text + '}'
p.scanner.is_started = false
p.scanner.pos = 0
p.next()
p.next()
return p.top_stmt()
}
else {
p.error('bad top level statement ' + p.tok.str())
return ast.Stmt{}
}
}
}
}