parser: parse_file(path)

pull/3293/head
Alexander Medvednikov 2020-01-01 10:15:05 +01:00
parent 87cff0386c
commit 96d02849aa
4 changed files with 16 additions and 8 deletions

View File

@ -98,14 +98,14 @@ pub fn create(path string) ?File {
$if linux { $if linux {
//$if linux_or_macos { //$if linux_or_macos {
mut fd := 0 mut fd := 0
println('creat SYS') //println('creat SYS')
$if macos { $if macos {
fd = C.syscall(sys_open_nocancel, path.str, 0x601, 0x1b6) fd = C.syscall(sys_open_nocancel, path.str, 0x601, 0x1b6)
} }
$else { $else {
fd = C.syscall(sys_creat, path.str, 511) fd = C.syscall(sys_creat, path.str, 511)
} }
println('fd=$fd') //println('fd=$fd')
if fd == -1 { if fd == -1 {
return error('failed to create file "$path"') return error('failed to create file "$path"')
} }

View File

@ -17,14 +17,12 @@ fn test_c_files() {
vexe := os.getenv('VEXE') vexe := os.getenv('VEXE')
vroot := filepath.dir(vexe) vroot := filepath.dir(vexe)
for i in 1 .. nr_tests + 1 { for i in 1 .. nr_tests + 1 {
text := os.read_file('$vroot/vlib/v/gen/tests/${i}.vv') or { path := '$vroot/vlib/v/gen/tests/${i}.vv'
panic(err)
}
ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or { ctext := os.read_file('$vroot/vlib/v/gen/tests/${i}.c') or {
panic(err) panic(err)
} }
table := &table.Table{} table := &table.Table{}
program := parser.parse_file(text, table) program := parser.parse_file(path, table)
res := gen.cgen([program]) res := gen.cgen([program])
if compare_texts(res, ctext) { if compare_texts(res, ctext) {
eprintln('${i}... ' + term.green('OK')) eprintln('${i}... ' + term.green('OK'))

View File

@ -4,7 +4,9 @@ fn function1() int {
return 0 return 0
} }
fn foo(a int) {} fn foo(a int) {
end()
}
struct User { struct User {
name string name string
@ -57,5 +59,9 @@ fn init_array() {
} }
fn end() {
}
fn main() { fn main() {
} }

View File

@ -62,11 +62,15 @@ pub fn (p mut Parser) get_type() types.Type {
} }
} }
pub fn parse_file(text string, table &table.Table) ast.File { pub fn parse_file(path string, table &table.Table) ast.File {
text := os.read_file(path) or {
panic(err)
}
mut stmts := []ast.Stmt mut stmts := []ast.Stmt
mut p := Parser{ mut p := Parser{
scanner: scanner.new_scanner(text) scanner: scanner.new_scanner(text)
table: table table: table
file_name: path
} }
p.read_first_token() p.read_first_token()
for { for {