parser: optimize no main file process

pull/4880/head
yuyi 2020-05-13 20:02:04 +08:00 committed by GitHub
parent 60dd7a7471
commit b1511ce995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 18 deletions

View File

@ -0,0 +1,3 @@
vlib/v/checker/tests/no_main_println_err.v:1:5: error: too few arguments in call to `println` (0 instead of 1)
1 | println()
| ~~~~~~~~~

View File

@ -0,0 +1 @@
println()

View File

@ -364,9 +364,16 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
}
else {
if p.pref.is_script && !p.pref.is_test {
p.scanner.add_fn_main_and_rescan(p.tok.pos - 1)
p.read_first_token()
return p.top_stmt()
mut stmts := []ast.Stmt{}
for p.tok.kind != .eof {
stmts << p.stmt()
}
return ast.FnDecl{
name: 'main'
stmts: stmts
file: p.file_name
return_type: table.void_type
}
} else {
p.error('bad top level statement ' + p.tok.str())
return ast.Stmt{}

View File

@ -75,21 +75,6 @@ pub fn new_scanner(text string, comments_mode CommentsMode) &Scanner {
return s
}
pub fn (s &Scanner) add_fn_main_and_rescan(pos int) {
// NB: the text may have ended in // comment, which would hide the ending }
// To avoid that, we need a \n right before it.
if pos > 0 {
s.text = s.text[..pos] + 'fn main() {' + s.text[pos..] + '\n}'
s.pos = pos
s.is_started = false
} else {
s.text = 'fn main() {' + s.text + '\n}'
s.pos = 0
s.line_nr = 0
s.is_started = false
}
}
fn (s &Scanner) new_token(tok_kind token.Kind, lit string, len int) token.Token {
return token.Token{
kind: tok_kind