register fn args; remove dup code

pull/3311/head
Alexander Medvednikov 2020-01-02 08:37:41 +01:00
parent 460b35137a
commit a6a9958cfc
3 changed files with 8 additions and 31 deletions

2
v2.v
View File

@ -14,7 +14,7 @@ cdefs = '
typedef int bool; typedef int bool;
typedef struct { char* str; } string; typedef struct { char* str; } string;
typedef double f64; typedef double f64;
string tos3(char* s) { return (string){ .str = s }; } string tos3(char* s) { return (string){ .str = s, len = strlen(s) }; }
') ')
fn main() { fn main() {

View File

@ -70,10 +70,12 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
for p.tok.kind != .rpar { for p.tok.kind != .rpar {
arg_name := p.check_name() arg_name := p.check_name()
typ := p.parse_type() typ := p.parse_type()
args << table.Var{ arg := table.Var{
name: arg_name name: arg_name
typ: typ typ: typ
} }
args << arg
p.table.register_var(arg)
ast_args << ast.Arg{ ast_args << ast.Arg{
typ: typ typ: typ
name: arg_name name: arg_name
@ -103,12 +105,12 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
} }
pub fn (p &Parser) check_fn_calls() { pub fn (p &Parser) check_fn_calls() {
println('CHEKC FN CALLS') println('check fn calls')
for call in p.table.unknown_calls { for call in p.table.unknown_calls {
f := p.table.find_fn(call.name) or { f := p.table.find_fn(call.name) or {
p.error_at_line('unknown function `$call.name`', call.tok.line_nr) p.error_at_line('unknown function `$call.name`', call.tok.line_nr)
return return
} }
println(call.name) println(f.name)
} }
} }

View File

@ -35,6 +35,7 @@ pub fn parse_stmt(text string, table &table.Table) ast.Stmt {
} }
pub fn parse_file(path string, table &table.Table) ast.File { pub fn parse_file(path string, table &table.Table) ast.File {
println('parse file "$path"')
text := os.read_file(path) or { text := os.read_file(path) or {
panic(err) panic(err)
} }
@ -66,33 +67,7 @@ pub fn parse_file(path string, table &table.Table) ast.File {
pub fn parse_files(paths []string, table &table.Table) []ast.File { pub fn parse_files(paths []string, table &table.Table) []ast.File {
mut files := []ast.File mut files := []ast.File
for path in paths { for path in paths {
println('parse file "$path"') files << parse_file(path, table)
mut stmts := []ast.Stmt
text := os.read_file(path) or {
panic(err)
}
mut p := Parser{
scanner: scanner.new_scanner(text)
table: table
file_name: path
}
p.read_first_token()
for {
// res := s.scan()
if p.tok.kind == .eof {
break
}
// println('expr at ' + p.tok.str())
s := p.stmt()
// println(s)
stmts << s // p.stmt()
}
p.check_fn_calls()
// println('nr stmts = $stmts.len')
// println(stmts[0])
files << ast.File{
stmts: stmts
}
} }
return files return files
} }