Skip to content

Commit e02e20a

Browse files
committed
FuncType arrays; fmt.xxx; zero value for maps and arrays
1 parent 587e0ae commit e02e20a

File tree

9 files changed

+46
-7
lines changed

9 files changed

+46
-7
lines changed

ast.v

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Decls = FuncDecl | GenDecl
77

88
type Expr = InvalidExpr
99
| ArrayType
10+
| FuncType
1011
| BasicLit
1112
| BinaryExpr
1213
| CallExpr
@@ -325,6 +326,7 @@ fn (e Expr) node_type() string {
325326
StarExpr { return e.node_type }
326327
SliceExpr { return e.node_type }
327328
UnaryExpr { return e.node_type }
329+
FuncType { return e.node_type }
328330
}
329331
return 'unknown node type'
330332
}

expr.v

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ fn (mut app App) expr(expr Expr) {
5252
SliceExpr {
5353
app.slice_expr(expr)
5454
}
55+
FuncType {
56+
app.func_type(expr)
57+
}
5558
}
5659
}
5760

@@ -144,6 +147,10 @@ fn (mut app App) array_type(node ArrayType) {
144147
app.selector_expr(node.elt)
145148
app.force_upper = false
146149
}
150+
FuncType {
151+
app.gen('[]')
152+
app.func_type(node.elt)
153+
}
147154
else {
148155
app.gen('UNKNOWN ELT ${node.elt.type_name()}')
149156
}

fn_call.v

+5-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ fn (mut app App) make_call(call CallExpr) {
150150
app.gen(', cap: ')
151151
app.expr(call.args[2])
152152
app.gen(' }')
153+
} else {
154+
app.gen('{}')
153155
}
154156
}
155157

@@ -188,6 +190,8 @@ fn (mut app App) handle_fmt_call(fn_name string, _ []Expr) {
188190
'sprintf' {
189191
app.gen('strconv.v_sprintf')
190192
}
191-
else {}
193+
else {
194+
app.gen('strconv.' + fn_name)
195+
}
192196
}
193197
}

fn_decl.v

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
33

44
fn (mut app App) func_decl(decl FuncDecl) {
5+
app.genln('')
56
app.comments(decl.doc)
67
method_name := decl.name.name.to_lower()
78
// Capital? Then it's public in Go

main.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn (mut app App) translate_file(go_file_path string) {
119119
v_path := go_file_path.replace('.go', '.v')
120120
os.write_file(v_path, generated_v_code) or { panic(err) }
121121
println('${v_path} has been successfully generated')
122-
os.system('v fmt -w ${v_path}')
122+
os.system('v -translated-go fmt -w ${v_path}')
123123
}
124124

125125
fn (mut app App) run_test(subdir string, test_name string) ! {

stmt.v

+10-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ fn (mut app App) decl_stmt(d DeclStmt) {
157157
app.gen(' := ')
158158
mut kind := 'int'
159159
if spec.values.len == 0 {
160-
app.genln('NO SPEC VALUES')
160+
// app.genln('NO SPEC VALUES')
161+
// `var x int` declaration without initialization
162+
app.gen_zero_value(spec.typ)
161163
continue
162164
}
163165
value := spec.values[0]
@@ -191,15 +193,19 @@ fn (mut app App) decl_stmt(d DeclStmt) {
191193

192194
fn (mut app App) defer_stmt(node DeferStmt) {
193195
// print_backtrace()
194-
app.gen('defer')
196+
app.gen('defer ')
195197
// `defer fn() { ... } ()
196198
// empty function, just generate `defer { ... }` in V
199+
197200
if node.call is CallExpr && node.call.args.len == 0 {
198201
if node.call.fun is FuncLit {
199202
func_lit := node.call.fun as FuncLit
200203
app.block_stmt(func_lit.body)
201204
} else {
202-
app.genln('// UNKNOWN node.call.fun ${typeof(node.call.fun).name}')
205+
app.genln('{')
206+
app.expr(node.call.fun) // TODO broken no () after foo.bar
207+
app.genln('}')
208+
// app.genln('// UNKNOWN node.call.fun ${node.call.fun.type_name()}')
203209
}
204210
} else {
205211
app.genln('{')
@@ -216,6 +222,7 @@ fn (mut app App) return_stmt(node ReturnStmt) {
216222
app.gen(',')
217223
}
218224
}
225+
app.genln('')
219226
}
220227

221228
// continue break etc

struct.v

+4-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ fn (mut app App) struct_init(c CompositeLit) {
191191
typ := c.typ
192192
match typ {
193193
Ident {
194-
app.genln('${typ.name}{')
194+
app.gen('${typ.name}{')
195+
if c.elts.len > 0 {
196+
app.genln('')
197+
}
195198
for elt in c.elts {
196199
app.expr(elt)
197200
app.genln('')

type.v

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2024 Alexander Medvednikov. All rights reserved.
2+
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
3+
module main
4+
5+
fn (mut app App) gen_zero_value(t Type) {
6+
if t is MapType {
7+
app.map_type(t)
8+
app.gen('{}')
9+
} else if t is ArrayType {
10+
app.array_type(t)
11+
app.gen('{}')
12+
} else {
13+
app.gen('0')
14+
}
15+
}

util.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn (mut app App) go2v_ident(ident string) string {
6767
*/
6868
}
6969

70-
const v_keywords_which_are_not_go_keywords = ['match']
70+
const v_keywords_which_are_not_go_keywords = ['match', 'lock', 'fn']
7171

7272
fn go2v_ident2(ident string) string {
7373
x := ident.camel_to_snake() // to_lower()) // TODO ?

0 commit comments

Comments
 (0)