Skip to content

Commit

Permalink
FuncType arrays; fmt.xxx; zero value for maps and arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Sep 26, 2024
1 parent 587e0ae commit e02e20a
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Decls = FuncDecl | GenDecl

type Expr = InvalidExpr
| ArrayType
| FuncType
| BasicLit
| BinaryExpr
| CallExpr
Expand Down Expand Up @@ -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'
}
7 changes: 7 additions & 0 deletions expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ fn (mut app App) expr(expr Expr) {
SliceExpr {
app.slice_expr(expr)
}
FuncType {
app.func_type(expr)
}
}
}

Expand Down Expand Up @@ -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()}')
}
Expand Down
6 changes: 5 additions & 1 deletion fn_call.v
Original file line number Diff line number Diff line change
Expand Up @@ -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('{}')
}
}

Expand Down Expand Up @@ -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)
}
}
}
1 change: 1 addition & 0 deletions fn_decl.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) ! {
Expand Down
13 changes: 10 additions & 3 deletions stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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('{')
Expand All @@ -216,6 +222,7 @@ fn (mut app App) return_stmt(node ReturnStmt) {
app.gen(',')
}
}
app.genln('')
}

// continue break etc
Expand Down
5 changes: 4 additions & 1 deletion struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
Expand Down
15 changes: 15 additions & 0 deletions type.v
Original file line number Diff line number Diff line change
@@ -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')
}
}
2 changes: 1 addition & 1 deletion util.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand Down

0 comments on commit e02e20a

Please sign in to comment.