diff --git a/v.v b/v.v index 563e41d54b..c98089da43 100644 --- a/v.v +++ b/v.v @@ -8,6 +8,7 @@ import ( benchmark os filepath + //v.types // time ) diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index 26e2f8f33e..fb1bc6c302 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -9,6 +9,10 @@ import ( strings filepath compiler.x64 + //v.table + //v.parser + //v.gen + //v.types ) pub const ( @@ -25,7 +29,8 @@ enum BuildMode { } const ( - supported_platforms = ['windows', 'mac', 'macos', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku'] + supported_platforms = ['windows', 'mac', 'macos', 'linux', 'freebsd', 'openbsd', 'netbsd', + 'dragonfly', 'android', 'js', 'solaris', 'haiku'] ) enum OS { @@ -373,6 +378,43 @@ pub fn (v mut V) compile() { v.cc() } +/* +pub fn (v mut V) compile2() { + if os.user_os() != 'windows' && v.pref.ccompiler == 'msvc' { + verror('Cannot build with msvc on ${os.user_os()}') + } + //cgen.genln('// Generated by V') + println('compile2()') + if v.pref.is_verbose { + println('all .v files before:') + println(v.files) + } + v.add_v_files_to_compile() + if v.pref.is_verbose { + println('all .v files:') + println(v.files) + } + table := &table.Table{} + files := parser.parse_files(v.files, table) + c := gen.cgen(files) + println('out: $v.out_name_c') + os.write_file(v.out_name_c, c) + /* + cgen.genln(c_builtin_types) + + if !v.pref.is_bare { + cgen.genln(c_headers) + } + else { + cgen.genln(bare_c_headers) + } + } + */ + v.cc() + +} +*/ + pub fn (v mut V) compile_x64() { $if !linux { println('v -x64 can only generate Linux binaries for now') @@ -513,7 +555,7 @@ pub fn (v mut V) generate_main() { else if v.v_fmt_file=='' && !v.pref.is_repl { verror('function `main` is not declared in the main module') } - } + } else if v.pref.is_test { if v.table.main_exists() { verror('test files cannot have function `main`') diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 37c33d7e57..e27a4aefda 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -8,10 +8,10 @@ import ( v.types ) -pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | +pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit -pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt | AssignStmt | +pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt | AssignStmt | ForStmt | StructDecl // Stand-alone expression in a statement list. pub struct ExprStmt { @@ -123,7 +123,7 @@ pub: typ types.Type } -pub struct Program { +pub struct File { pub: stmts []Stmt } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 4569881157..6a9fc3e409 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -10,15 +10,17 @@ struct Gen { out strings.Builder } -pub fn cgen(program ast.Program) string { +pub fn cgen(files []ast.File) string { mut g := Gen{ out: strings.new_builder(100) } - for stmt in program.stmts { - g.stmt(stmt) - g.writeln('') + for file in files { + for stmt in file.stmts { + g.stmt(stmt) + g.writeln('') + } } - return (g.out.str()) + return g.out.str() } pub fn (g &Gen) save() {} diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index 6cb1717b8d..a86a74ad4e 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -25,7 +25,7 @@ fn test_c_files() { } table := &table.Table{} program := parser.parse_file(text, table) - res := gen.cgen(program) + res := gen.cgen([program]) if compare_texts(res, ctext) { eprintln('${i}... ' + term.green('OK')) } diff --git a/vlib/v/gen/jsgen.v b/vlib/v/gen/jsgen.v index a59289e420..dd816e290f 100644 --- a/vlib/v/gen/jsgen.v +++ b/vlib/v/gen/jsgen.v @@ -10,7 +10,7 @@ struct JsGen { out strings.Builder } -pub fn jsgen(program ast.Program) string { +pub fn jsgen(program ast.File) string { mut g := JsGen{ out: strings.new_builder(100) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 8e6317fe02..0a7bce0716 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -10,6 +10,7 @@ import ( v.table v.types term + os ) struct Parser { @@ -57,7 +58,7 @@ pub fn (p mut Parser) get_type() types.Type { } } -pub fn parse_file(text string, table &table.Table) ast.Program { +pub fn parse_file(text string, table &table.Table) ast.File { mut stmts := []ast.Stmt mut p := Parser{ scanner: scanner.new_scanner(text) @@ -76,11 +77,40 @@ pub fn parse_file(text string, table &table.Table) ast.Program { } // println('nr stmts = $stmts.len') // println(stmts[0]) - return ast.Program{ + return ast.File{ stmts: stmts } } +pub fn parse_files(paths []string, table &table.Table) []ast.File { + mut files := []ast.File + for path in paths { + mut stmts := []ast.Stmt + text := os.read_file(path) or { panic(err) } + mut p := Parser{ + scanner: scanner.new_scanner(text) + table: table + } + 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() + } + // println('nr stmts = $stmts.len') + // println(stmts[0]) + files << ast.File{ + stmts: stmts + } + } + return files +} + pub fn (p mut Parser) read_first_token() { // need to call next() twice to get peek token and current token p.next() @@ -452,8 +482,8 @@ fn (p mut Parser) parse_number_literal() (ast.Expr,types.Type) { return node,typ } -fn (p mut Parser) module_decl() ast.Stmt { - // p.check(.key_module) +fn (p mut Parser) module_decl() ast.Module { + p.check(.key_module) p.next() return ast.Module{} } @@ -520,7 +550,6 @@ fn (p mut Parser) fn_decl() ast.FnDecl { name: name args: args }) - // p.check(.rcbr) stmts := p.parse_block() return ast.FnDecl{ name: name @@ -531,7 +560,6 @@ fn (p mut Parser) fn_decl() ast.FnDecl { } fn (p mut Parser) return_stmt() ast.Return { - // println('return st') p.next() expr,t := p.expr(0) if !types.check(p.return_type, t) { @@ -568,7 +596,7 @@ fn (p mut Parser) var_decl() ast.VarDecl { return ast.VarDecl{ name: name expr: expr // p.expr(token.lowest_prec) - + typ: t } } diff --git a/vlib/v/parser/parser_test.v b/vlib/v/parser/parser_test.v index a11f67ee22..b386e93433 100644 --- a/vlib/v/parser/parser_test.v +++ b/vlib/v/parser/parser_test.v @@ -76,7 +76,7 @@ fn test_parse_expr() { for s in input { e << parse_stmt(s, table) } - program := ast.Program{ + program := ast.File{ stmts: e } res := gen.cgen(program)