checker: check fn main(); vfmt: fix for{}, module and module comments

pull/4340/head
Alexander Medvednikov 2020-04-10 22:27:51 +02:00
parent fdac3a1f2e
commit b95a47b5a7
6 changed files with 55 additions and 32 deletions

View File

@ -103,7 +103,7 @@ fn main() {
}
else {}
}
if command in ['run', 'build'] || command.ends_with('.v') || os.exists(command) {
if command in ['run', 'build-module'] || command.ends_with('.v') || os.exists(command) {
//println('command')
//println(prefs.path)
builder.compile(command, prefs)
@ -179,7 +179,7 @@ fn parse_args(args []string) (&pref.Preferences, string) {
res.path = args[command_pos+1]
res.run_args = args[command_pos+1..]
}
if command == 'build' {
if command == 'build-module' {
res.build_mode = .build_module
res.path = args[command_pos+1]
}

View File

@ -60,6 +60,17 @@ pub fn (c mut Checker) check_files(ast_files []ast.File) {
for file in ast_files {
c.check(file)
}
// Make sure fn main is defined in non lib builds
if c.pref.build_mode == .build_module {
return
}
for i, f in c.table.fns {
if f.name == 'main' {
return
}
}
eprintln('function `main` is undeclared in the main module')
exit(1)
}
pub fn (c mut Checker) struct_init(struct_init mut ast.StructInit) table.Type {

View File

@ -36,7 +36,7 @@ pub fn fmt(file ast.File, table &table.Table) string {
file: file
}
f.cur_mod = 'main'
for stmt in file.stmts {
for i, stmt in file.stmts {
f.stmt(stmt)
}
// for comment in file.comments { println('$comment.line_nr $comment.text') }
@ -279,7 +279,11 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
ast.ForStmt {
f.write('for ')
f.expr(it.cond)
if it.is_inf {
f.writeln('{')
} else {
f.writeln(' {')
}
f.stmts(it.stmts)
f.writeln('}')
}

View File

@ -466,6 +466,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
ast.InterfaceDecl {
g.writeln('// interface')
}
ast.Module {}
ast.Return {
if g.defer_stmts.len > 0 {
g.write_defer_stmts()

View File

@ -68,7 +68,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
scanner: scanner.new_scanner_file(path, comments_mode)
table: table
file_name: path
file_name_dir: os.dir( path )
file_name_dir: os.dir(path)
pref: pref
scope: &ast.Scope{
start_pos: 0
@ -78,12 +78,16 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
}
// comments_mode: comments_mode
p.read_first_token()
// module decl
module_decl := if p.tok.kind == .key_module { p.module_decl() } else { ast.Module{
name: 'main'
} }
p.mod = module_decl.name
p.builtin_mod = p.mod == 'builtin'
for p.tok.kind == .comment {
mut stmt := ast.Stmt{} // TODO sum type << bug
com := p.comment()
stmt = com
stmts << stmt
}
mut mstmt := ast.Stmt{}
module_decl := p.module_decl()
mstmt = module_decl
stmts << mstmt
// imports
/*
mut imports := []ast.Import
@ -250,6 +254,10 @@ fn (p mut Parser) check_name() string {
}
pub fn (p mut Parser) top_stmt() ast.Stmt {
if p.fileis('mmm.v') {
print('!!stmt()')
println(p.tok.kind.str())
}
match p.tok.kind {
.key_pub {
match p.peek_tok.kind {
@ -283,9 +291,6 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
.key_interface {
return p.interface_decl()
}
.key_module {
return p.module_decl()
}
.key_import {
node := p.import_stmt()
p.ast_imports << node
@ -859,7 +864,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
left: node
right: right
op: tok.kind
pos:pos
pos: pos
}
} else if p.tok.kind.is_infix() {
node = p.infix_expr(node)
@ -1348,7 +1353,7 @@ fn (p mut Parser) array_init() ast.ArrayInit {
last_pos := p.tok.position()
len := last_pos.pos - first_pos.pos
pos := token.Position{
line_nr: first_pos.line_nr,
line_nr: first_pos.line_nr
pos: first_pos.pos
len: len
}
@ -1403,10 +1408,14 @@ fn (p mut Parser) parse_number_literal() ast.Expr {
}
fn (p mut Parser) module_decl() ast.Module {
mut name := 'main'
if p.tok.kind == .key_module {
p.check(.key_module)
mod := p.check_name()
full_mod := p.table.qualify_module(mod, p.file_name)
name = p.check_name()
}
full_mod := p.table.qualify_module(name, p.file_name)
p.mod = full_mod
p.builtin_mod = p.mod == 'builtin'
return ast.Module{
name: full_mod
}
@ -1918,8 +1927,6 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
return ast.EnumDecl{
name: name
is_pub: is_pub
// vals: vals
// default_exprs: default_exprs
fields: fields
}
}