diff --git a/ast.v b/ast.v index 10a5936..098fb79 100644 --- a/ast.v +++ b/ast.v @@ -7,6 +7,7 @@ type Decls = FuncDecl | GenDecl type Expr = InvalidExpr | ArrayType + | FuncType | BasicLit | BinaryExpr | CallExpr @@ -325,6 +326,7 @@ fn (e Expr) node_type() string { StarExpr { return e.node_type } SliceExpr { return e.node_type } UnaryExpr { return e.node_type } + FuncType { return e.node_type } } return 'unknown node type' } diff --git a/expr.v b/expr.v index f9100c9..15d1cf8 100644 --- a/expr.v +++ b/expr.v @@ -52,6 +52,9 @@ fn (mut app App) expr(expr Expr) { SliceExpr { app.slice_expr(expr) } + FuncType { + app.func_type(expr) + } } } @@ -144,6 +147,10 @@ fn (mut app App) array_type(node ArrayType) { app.selector_expr(node.elt) app.force_upper = false } + FuncType { + app.gen('[]') + app.func_type(node.elt) + } else { app.gen('UNKNOWN ELT ${node.elt.type_name()}') } diff --git a/fn_call.v b/fn_call.v index eb96b91..64ab3de 100644 --- a/fn_call.v +++ b/fn_call.v @@ -150,6 +150,8 @@ fn (mut app App) make_call(call CallExpr) { app.gen(', cap: ') app.expr(call.args[2]) app.gen(' }') + } else { + app.gen('{}') } } @@ -188,6 +190,8 @@ fn (mut app App) handle_fmt_call(fn_name string, _ []Expr) { 'sprintf' { app.gen('strconv.v_sprintf') } - else {} + else { + app.gen('strconv.' + fn_name) + } } } diff --git a/fn_decl.v b/fn_decl.v index 046f5b3..5a3e2d7 100644 --- a/fn_decl.v +++ b/fn_decl.v @@ -2,6 +2,7 @@ // Use of this source code is governed by a GPL license that can be found in the LICENSE file. fn (mut app App) func_decl(decl FuncDecl) { + app.genln('') app.comments(decl.doc) method_name := decl.name.name.to_lower() // Capital? Then it's public in Go diff --git a/main.v b/main.v index d09ccd3..d33c3c4 100644 --- a/main.v +++ b/main.v @@ -119,7 +119,7 @@ fn (mut app App) translate_file(go_file_path string) { v_path := go_file_path.replace('.go', '.v') os.write_file(v_path, generated_v_code) or { panic(err) } println('${v_path} has been successfully generated') - os.system('v fmt -w ${v_path}') + os.system('v -translated-go fmt -w ${v_path}') } fn (mut app App) run_test(subdir string, test_name string) ! { diff --git a/stmt.v b/stmt.v index d5b840a..834042d 100644 --- a/stmt.v +++ b/stmt.v @@ -157,7 +157,9 @@ fn (mut app App) decl_stmt(d DeclStmt) { app.gen(' := ') mut kind := 'int' if spec.values.len == 0 { - app.genln('NO SPEC VALUES') + // app.genln('NO SPEC VALUES') + // `var x int` declaration without initialization + app.gen_zero_value(spec.typ) continue } value := spec.values[0] @@ -191,15 +193,19 @@ fn (mut app App) decl_stmt(d DeclStmt) { fn (mut app App) defer_stmt(node DeferStmt) { // print_backtrace() - app.gen('defer') + app.gen('defer ') // `defer fn() { ... } () // empty function, just generate `defer { ... }` in V + if node.call is CallExpr && node.call.args.len == 0 { if node.call.fun is FuncLit { func_lit := node.call.fun as FuncLit app.block_stmt(func_lit.body) } else { - app.genln('// UNKNOWN node.call.fun ${typeof(node.call.fun).name}') + app.genln('{') + app.expr(node.call.fun) // TODO broken no () after foo.bar + app.genln('}') + // app.genln('// UNKNOWN node.call.fun ${node.call.fun.type_name()}') } } else { app.genln('{') @@ -216,6 +222,7 @@ fn (mut app App) return_stmt(node ReturnStmt) { app.gen(',') } } + app.genln('') } // continue break etc diff --git a/struct.v b/struct.v index e8212c3..d6a563e 100644 --- a/struct.v +++ b/struct.v @@ -191,7 +191,10 @@ fn (mut app App) struct_init(c CompositeLit) { typ := c.typ match typ { Ident { - app.genln('${typ.name}{') + app.gen('${typ.name}{') + if c.elts.len > 0 { + app.genln('') + } for elt in c.elts { app.expr(elt) app.genln('') diff --git a/type.v b/type.v new file mode 100644 index 0000000..e869132 --- /dev/null +++ b/type.v @@ -0,0 +1,15 @@ +// Copyright (c) 2024 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by a GPL license that can be found in the LICENSE file. +module main + +fn (mut app App) gen_zero_value(t Type) { + if t is MapType { + app.map_type(t) + app.gen('{}') + } else if t is ArrayType { + app.array_type(t) + app.gen('{}') + } else { + app.gen('0') + } +} diff --git a/util.v b/util.v index fb1ab56..3ee28f8 100644 --- a/util.v +++ b/util.v @@ -67,7 +67,7 @@ fn (mut app App) go2v_ident(ident string) string { */ } -const v_keywords_which_are_not_go_keywords = ['match'] +const v_keywords_which_are_not_go_keywords = ['match', 'lock', 'fn'] fn go2v_ident2(ident string) string { x := ident.camel_to_snake() // to_lower()) // TODO ?