mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
repl: fix import xxx with no fn main error
This commit is contained in:
parent
4d04e88679
commit
de272654f1
|
@ -1,5 +0,0 @@
|
||||||
vlib/v/checker/tests/no_fn_main.v:1:1: error: function `main` must be declared in the main module
|
|
||||||
1 | fn no_main() {
|
|
||||||
| ^
|
|
||||||
2 | println('Hello world !')
|
|
||||||
3 | }
|
|
|
@ -1,3 +0,0 @@
|
||||||
fn no_main() {
|
|
||||||
println('Hello world !')
|
|
||||||
}
|
|
|
@ -401,3 +401,18 @@ fn (mut p Parser) fn_redefinition_error(name string) {
|
||||||
*/
|
*/
|
||||||
p.error('redefinition of function `$name`')
|
p.error('redefinition of function `$name`')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn have_fn_main(stmts []ast.Stmt) bool {
|
||||||
|
mut has_main_fn := false
|
||||||
|
for stmt in stmts {
|
||||||
|
match stmt {
|
||||||
|
ast.FnDecl {
|
||||||
|
if it.name == 'main' {
|
||||||
|
has_main_fn = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return has_main_fn
|
||||||
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
|
||||||
return p.stmt()
|
return p.stmt()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_file(path string, table &table.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences, global_scope &ast.Scope) ast.File {
|
pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences, global_scope &ast.Scope) ast.File {
|
||||||
// println('parse_file("$path")')
|
// println('parse_file("$path")')
|
||||||
// text := os.read_file(path) or {
|
// text := os.read_file(path) or {
|
||||||
// panic(err)
|
// panic(err)
|
||||||
|
@ -71,7 +71,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
|
||||||
mut stmts := []ast.Stmt{}
|
mut stmts := []ast.Stmt{}
|
||||||
mut p := Parser{
|
mut p := Parser{
|
||||||
scanner: scanner.new_scanner_file(path, comments_mode)
|
scanner: scanner.new_scanner_file(path, comments_mode)
|
||||||
table: table
|
table: b_table
|
||||||
file_name: path
|
file_name: path
|
||||||
file_name_dir: os.dir(path)
|
file_name_dir: os.dir(path)
|
||||||
pref: pref
|
pref: pref
|
||||||
|
@ -102,9 +102,13 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
|
||||||
*/
|
*/
|
||||||
// TODO: import only mode
|
// TODO: import only mode
|
||||||
for {
|
for {
|
||||||
// res := s.scan()
|
|
||||||
if p.tok.kind == .eof {
|
if p.tok.kind == .eof {
|
||||||
// println('EOF, breaking')
|
if p.pref.is_script && !p.pref.is_test && p.mod == 'main' && !have_fn_main(stmts) {
|
||||||
|
stmts << ast.FnDecl {
|
||||||
|
name: 'main'
|
||||||
|
return_type: table.void_type
|
||||||
|
}
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// println('stmt at ' + p.tok.str())
|
// println('stmt at ' + p.tok.str())
|
||||||
|
@ -350,7 +354,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if p.pref.is_script && !p.pref.is_test {
|
if p.pref.is_script && !p.pref.is_test {
|
||||||
p.scanner.add_fn_main_and_rescan()
|
p.scanner.add_fn_main_and_rescan(p.tok.pos-1)
|
||||||
p.read_first_token()
|
p.read_first_token()
|
||||||
return p.top_stmt()
|
return p.top_stmt()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -75,12 +75,17 @@ pub fn new_scanner(text string, comments_mode CommentsMode) &Scanner {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (s &Scanner) add_fn_main_and_rescan() {
|
pub fn (s &Scanner) add_fn_main_and_rescan(pos int) {
|
||||||
s.text = 'fn main() {' + s.text + '}'
|
if pos > 0 {
|
||||||
s.is_started = false
|
s.text = s.text[..pos] + 'fn main() {' + s.text[pos..] + '}'
|
||||||
s.pos = 0
|
s.pos = pos
|
||||||
s.line_nr = 0
|
s.is_started = false
|
||||||
s.last_nl_pos = 0
|
} else {
|
||||||
|
s.text = 'fn main() {' + s.text + '}'
|
||||||
|
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 {
|
fn (s &Scanner) new_token(tok_kind token.Kind, lit string, len int) token.Token {
|
||||||
|
|
7
vlib/v/tests/repl/array.repl
Normal file
7
vlib/v/tests/repl/array.repl
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
mut a := [1, 2, 3]
|
||||||
|
b := [4, 5]
|
||||||
|
a << b
|
||||||
|
a = a.filter(it%2==0)
|
||||||
|
a
|
||||||
|
===output===
|
||||||
|
[2, 4]
|
4
vlib/v/tests/repl/import.repl
Normal file
4
vlib/v/tests/repl/import.repl
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
import time
|
||||||
|
time.now().unix_time() > 160000
|
||||||
|
===output===
|
||||||
|
true
|
Loading…
Reference in New Issue
Block a user