File tree 9 files changed +46
-7
lines changed
9 files changed +46
-7
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ type Decls = FuncDecl | GenDecl
7
7
8
8
type Expr = InvalidExpr
9
9
| ArrayType
10
+ | FuncType
10
11
| BasicLit
11
12
| BinaryExpr
12
13
| CallExpr
@@ -325,6 +326,7 @@ fn (e Expr) node_type() string {
325
326
StarExpr { return e.node_type }
326
327
SliceExpr { return e.node_type }
327
328
UnaryExpr { return e.node_type }
329
+ FuncType { return e.node_type }
328
330
}
329
331
return 'unknown node type'
330
332
}
Original file line number Diff line number Diff line change @@ -52,6 +52,9 @@ fn (mut app App) expr(expr Expr) {
52
52
SliceExpr {
53
53
app.slice_expr (expr)
54
54
}
55
+ FuncType {
56
+ app.func_type (expr)
57
+ }
55
58
}
56
59
}
57
60
@@ -144,6 +147,10 @@ fn (mut app App) array_type(node ArrayType) {
144
147
app.selector_expr (node.elt)
145
148
app.force_upper = false
146
149
}
150
+ FuncType {
151
+ app.gen ('[]' )
152
+ app.func_type (node.elt)
153
+ }
147
154
else {
148
155
app.gen ('UNKNOWN ELT ${node.elt.type_name()} ' )
149
156
}
Original file line number Diff line number Diff line change @@ -150,6 +150,8 @@ fn (mut app App) make_call(call CallExpr) {
150
150
app.gen (', cap: ' )
151
151
app.expr (call.args[2 ])
152
152
app.gen (' }' )
153
+ } else {
154
+ app.gen ('{}' )
153
155
}
154
156
}
155
157
@@ -188,6 +190,8 @@ fn (mut app App) handle_fmt_call(fn_name string, _ []Expr) {
188
190
'sprintf' {
189
191
app.gen ('strconv.v_sprintf' )
190
192
}
191
- else {}
193
+ else {
194
+ app.gen ('strconv.' + fn_name)
195
+ }
192
196
}
193
197
}
Original file line number Diff line number Diff line change 2
2
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
3
3
4
4
fn (mut app App) func_decl (decl FuncDecl) {
5
+ app.genln ('' )
5
6
app.comments (decl.doc)
6
7
method_name := decl.name.name.to_lower ()
7
8
// Capital? Then it's public in Go
Original file line number Diff line number Diff line change @@ -119,7 +119,7 @@ fn (mut app App) translate_file(go_file_path string) {
119
119
v_path := go_file_path.replace ('.go' , '.v' )
120
120
os.write_file (v_path, generated_v_code) or { panic (err) }
121
121
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} ' )
123
123
}
124
124
125
125
fn (mut app App) run_test (subdir string , test_name string ) ! {
Original file line number Diff line number Diff line change @@ -157,7 +157,9 @@ fn (mut app App) decl_stmt(d DeclStmt) {
157
157
app.gen (' := ' )
158
158
mut kind := 'int'
159
159
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)
161
163
continue
162
164
}
163
165
value := spec.values[0 ]
@@ -191,15 +193,19 @@ fn (mut app App) decl_stmt(d DeclStmt) {
191
193
192
194
fn (mut app App) defer_stmt (node DeferStmt) {
193
195
// print_backtrace()
194
- app.gen ('defer' )
196
+ app.gen ('defer ' )
195
197
// `defer fn() { ... } ()
196
198
// empty function, just generate `defer { ... }` in V
199
+
197
200
if node.call is CallExpr && node.call.args.len == 0 {
198
201
if node.call.fun is FuncLit {
199
202
func_lit := node.call.fun as FuncLit
200
203
app.block_stmt (func_lit.body)
201
204
} 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()}')
203
209
}
204
210
} else {
205
211
app.genln ('{' )
@@ -216,6 +222,7 @@ fn (mut app App) return_stmt(node ReturnStmt) {
216
222
app.gen (',' )
217
223
}
218
224
}
225
+ app.genln ('' )
219
226
}
220
227
221
228
// continue break etc
Original file line number Diff line number Diff line change @@ -191,7 +191,10 @@ fn (mut app App) struct_init(c CompositeLit) {
191
191
typ := c.typ
192
192
match typ {
193
193
Ident {
194
- app.genln ('${typ.name} {' )
194
+ app.gen ('${typ.name} {' )
195
+ if c.elts.len > 0 {
196
+ app.genln ('' )
197
+ }
195
198
for elt in c.elts {
196
199
app.expr (elt)
197
200
app.genln ('' )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ fn (mut app App) go2v_ident(ident string) string {
67
67
*/
68
68
}
69
69
70
- const v_keywords_which_are_not_go_keywords = ['match' ]
70
+ const v_keywords_which_are_not_go_keywords = ['match' , 'lock' , 'fn' ]
71
71
72
72
fn go2v_ident2 (ident string ) string {
73
73
x := ident.camel_to_snake () // to_lower()) // TODO ?
You can’t perform that action at this time.
0 commit comments