Skip to content

Commit 1c52ef3

Browse files
authored
start keeping track of struct names (#136)
* start keeping track of struct names * fix fmt * fix fmt again
1 parent 6081da5 commit 1c52ef3

File tree

8 files changed

+45
-79
lines changed

8 files changed

+45
-79
lines changed

array_map.v

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ fn (mut app App) array_init(c CompositeLit) {
8282
}
8383

8484
fn (mut app App) map_init(node CompositeLit) {
85+
app.expr(node.typ)
8586
app.genln('{')
8687
for elt in node.elts {
8788
kv := elt as KeyValueExpr

expr.v

+10-15
Original file line numberDiff line numberDiff line change
@@ -157,27 +157,26 @@ fn (mut app App) key_value_expr(expr KeyValueExpr) {
157157

158158
fn (mut app App) array_type(node ArrayType) {
159159
match node.elt {
160-
Ident {
161-
app.gen('[]${go2v_type(node.elt.name)}')
160+
ArrayType {
161+
app.gen('[]')
162+
app.array_type(node.elt)
162163
}
163-
StarExpr {
164+
FuncType {
164165
app.gen('[]')
165-
app.star_expr(node.elt)
166-
// app.gen('[]&${node.elt.name}')
166+
app.func_type(node.elt)
167+
}
168+
Ident {
169+
app.gen('[]${go2v_type(node.elt.name)}')
167170
}
168171
SelectorExpr {
169172
app.gen('[]')
170173
app.force_upper = true
171174
app.selector_expr(node.elt)
172175
app.force_upper = false
173176
}
174-
FuncType {
175-
app.gen('[]')
176-
app.func_type(node.elt)
177-
}
178-
ArrayType {
177+
StarExpr {
179178
app.gen('[]')
180-
app.array_type(node.elt)
179+
app.star_expr(node.elt)
181180
}
182181
else {
183182
app.gen('UNKNOWN ELT ${node.elt.type_name()}')
@@ -186,14 +185,10 @@ fn (mut app App) array_type(node ArrayType) {
186185
}
187186

188187
fn (mut app App) map_type(node MapType) {
189-
app.force_upper = true
190188
app.gen('map[')
191-
app.force_upper = true
192189
app.expr(node.key)
193190
app.gen(']')
194-
app.force_upper = true
195191
app.expr(node.val)
196-
app.force_upper = false
197192
}
198193

199194
fn (mut app App) chan_type(node ChanType) {

main.v

+12-11
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ import v.util.diff
1010

1111
struct App {
1212
mut:
13-
sb strings.Builder
14-
is_fn_call bool // for lowercase idents
15-
tests_ok bool = true
16-
skip_first_arg bool // for `strings.Replace(s...)` => `s.replace(...)`
17-
force_upper bool // for `field Type` in struct decl, `mod.UpperCase` types etc
18-
no_star bool // To skip & in StarExpr in type matches (interfaces)
19-
type_decl_name string
20-
is_enum_decl bool
21-
is_mut_recv bool // so that `mut f Foo` is generated instead of `mut f &Foo`
22-
cur_fn_names map[string]bool // for fixing shadowing
23-
running_test bool // disables shadowing for now
13+
sb strings.Builder
14+
is_fn_call bool // for lowercase idents
15+
tests_ok bool = true
16+
skip_first_arg bool // for `strings.Replace(s...)` => `s.replace(...)`
17+
force_upper bool // for `field Type` in struct decl, `mod.UpperCase` types etc
18+
no_star bool // To skip & in StarExpr in type matches (interfaces)
19+
type_decl_name string
20+
is_enum_decl bool
21+
is_mut_recv bool // so that `mut f Foo` is generated instead of `mut f &Foo`
22+
cur_fn_names map[string]bool // for fixing shadowing
23+
running_test bool // disables shadowing for now
24+
struct_or_alias []string // skip camel_to_snake for these, but force capitalize
2425
}
2526

2627
fn (mut app App) genln(s string) {

stmt.v

+16-33
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,37 @@ fn (mut app App) stmt_list(list []Stmt) {
99
}
1010

1111
fn (mut app App) stmt(stmt Stmt) {
12-
// app.genln('TYPE = ${stmt.node_type} ${stmt is ExprStmt}')
13-
// println('FOR STMT')
14-
// println(stmt)
1512
match stmt {
1613
AssignStmt {
1714
// println('GOT A')
1815
app.assign_stmt(stmt, false) // no_mut:false
1916
}
20-
// have to keep track of variable names which match outer scope, so they can be renamed in inner...
2117
BlockStmt {
18+
// have to keep track of variable names which match outer scope, so they can be renamed in inner...
2219
app.block_stmt(stmt)
2320
}
2421
BranchStmt {
2522
app.branch_stmt(stmt)
2623
}
24+
DeclStmt {
25+
app.decl_stmt(stmt)
26+
}
27+
DeferStmt {
28+
app.defer_stmt(stmt)
29+
}
2730
ExprStmt {
2831
// app.genln('expr stmt')
2932
app.expr_stmt(stmt)
3033
}
31-
SwitchStmt {
32-
app.switch_stmt(stmt)
34+
ForStmt {
35+
app.for_stmt(stmt)
3336
}
34-
TypeSwitchStmt {
35-
app.type_switch_stmt(stmt)
37+
GoStmt {
38+
app.go_stmt(stmt)
3639
}
3740
IfStmt {
3841
app.if_stmt(stmt)
3942
}
40-
ForStmt {
41-
app.for_stmt(stmt)
42-
}
43-
DeclStmt {
44-
app.decl_stmt(stmt)
45-
}
4643
IncDecStmt {
4744
app.inc_dec_stmt(stmt)
4845
}
@@ -52,11 +49,11 @@ fn (mut app App) stmt(stmt Stmt) {
5249
ReturnStmt {
5350
app.return_stmt(stmt)
5451
}
55-
DeferStmt {
56-
app.defer_stmt(stmt)
52+
SwitchStmt {
53+
app.switch_stmt(stmt)
5754
}
58-
GoStmt {
59-
app.go_stmt(stmt)
55+
TypeSwitchStmt {
56+
app.type_switch_stmt(stmt)
6057
}
6158
else {
6259
app.genln('\t// unhandled in stmt: ${stmt}')
@@ -75,8 +72,6 @@ fn (mut app App) go_stmt(stmt GoStmt) {
7572

7673
fn (mut app App) block_stmt(body BlockStmt) {
7774
app.genln('{')
78-
// println('LIST=')
79-
// println(body.list)
8075
app.stmt_list(body.list)
8176
app.genln('}')
8277
}
@@ -87,10 +82,8 @@ fn (mut app App) if_stmt(node IfStmt) {
8782
}
8883

8984
app.gen('if ')
90-
// app.genln('/*forceu=${app.force_upper}*/')
9185
app.expr(node.cond)
9286
app.block_stmt(node.body)
93-
// else if ... {
9487
if node.else_ is IfStmt {
9588
app.genln('else')
9689
if node.else_.init.tok != '' {
@@ -102,20 +95,14 @@ fn (mut app App) if_stmt(node IfStmt) {
10295
if node.else_.init.tok != '' {
10396
app.genln('}')
10497
}
105-
}
106-
// else {
107-
else if node.else_ is BlockStmt {
98+
} else if node.else_ is BlockStmt {
10899
app.genln('else')
109100
app.block_stmt(node.else_)
110101
}
111102
}
112103

113104
fn (mut app App) for_stmt(f ForStmt) {
114105
app.gen('for ')
115-
// println(f)
116-
// for {}
117-
// if f.cond == unsafe { nil } {
118-
//}
119106

120107
init_empty := f.init.node_type == '' // f.init is InvalidStmt // f.init.node_type == ''
121108
cond_empty := f.cond.node_type() == ''
@@ -148,15 +135,13 @@ fn (mut app App) range_stmt(node RangeStmt) {
148135
app.gen('_ ')
149136
} else {
150137
key_name := app.unique_name_anti_shadow(app.go2v_ident(node.key.name))
151-
// app.gen(app.go2v_ident(node.key.name))
152138
app.gen(key_name)
153139
app.cur_fn_names[key_name] = true
154140
app.gen(', ')
155141
if node.value.name == '' {
156142
app.gen(' _ ')
157143
} else {
158144
value_name := app.unique_name_anti_shadow(app.go2v_ident(node.value.name))
159-
// app.gen(app.go2v_ident(node.value.name))
160145
app.gen(value_name)
161146
app.cur_fn_names[value_name] = true
162147
}
@@ -173,7 +158,6 @@ fn (mut app App) inc_dec_stmt(i IncDecStmt) {
173158
}
174159

175160
fn (mut app App) decl_stmt(d DeclStmt) {
176-
// app.genln('//decl_stmt')
177161
match d.decl {
178162
GenDecl {
179163
if d.decl.tok == 'var' {
@@ -234,7 +218,6 @@ fn (mut app App) decl_stmt(d DeclStmt) {
234218
}
235219

236220
fn (mut app App) defer_stmt(node DeferStmt) {
237-
// print_backtrace()
238221
app.gen('defer ')
239222
// `defer fn() { ... } ()
240223
// empty function, just generate `defer { ... }` in V

struct.v

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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) gen_decl(decl GenDecl) {
5-
// app.genln('// gen_decl')
6-
// mut needs_closer := false
75
app.comments(decl.doc)
86
for spec in decl.specs {
97
match spec {
@@ -16,6 +14,7 @@ fn (mut app App) gen_decl(decl GenDecl) {
1614
app.interface_decl(spec.name.name, spec.typ)
1715
}
1816
StructType {
17+
app.struct_or_alias << spec.name.name
1918
app.struct_decl(spec.name.name, spec.typ)
2019
}
2120
else {
@@ -51,7 +50,7 @@ fn (mut app App) type_decl(spec TypeSpec) {
5150
// Remember the type name for the upcoming const (enum) handler if it's an enum
5251
name := spec.name.name
5352
app.type_decl_name = name
54-
// TODO figure out how to diffirentiate between enums and type aliases
53+
// TODO figure out how to differentiate between enums and type aliases
5554
if name == 'EnumTest' {
5655
return
5756
}
@@ -172,10 +171,6 @@ fn (mut app App) interface_decl(interface_name string, spec InterfaceType) {
172171
// []int{1,2,3}
173172
// map[string]int{"foo":1}
174173
fn (mut app App) composite_lit(c CompositeLit) {
175-
// if c.typ.name != '' {
176-
// app.struct_init(c)
177-
// return
178-
// }
179174
match c.typ {
180175
ArrayType {
181176
app.array_init(c)
@@ -193,7 +188,6 @@ fn (mut app App) composite_lit(c CompositeLit) {
193188
app.map_init(c) // loops thru each key_value_expr TODO method needs renaming
194189
}
195190
else {
196-
// app.gen('// UNHANDLED CompositeLit type ${typeof(c.typ).name}')
197191
app.genln('// UNHANDLED CompositeLit type ${c.typ.type_name()} strtyp="${c.typ}"')
198192
}
199193
}
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module main
22

33
struct Vertex {
4-
mut:
4+
pub mut:
55
lat f64
66
long f64
77
}
88

99
fn main() {
10-
mut l := map[string]Vertex{}
10+
mut l := map[string]Vertex{
11+
}
1112
}

util.v

+1-10
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ fn go2v_type(typ string) string {
5151
}
5252

5353
fn (mut app App) go2v_ident(ident string) string {
54-
// println('ident=${ident} force_upper=${app.force_upper}')
55-
if app.force_upper {
54+
if app.force_upper || ident in app.struct_or_alias {
5655
app.force_upper = false
5756
if ident in v_keywords_which_are_not_go_keywords {
5857
return ident + '_'
@@ -63,14 +62,6 @@ fn (mut app App) go2v_ident(ident string) string {
6362
return ident.capitalize()
6463
}
6564
return go2v_ident2(ident)
66-
/*
67-
return if ident[0].is_capital() {
68-
// println('is cap')
69-
ident
70-
} else {
71-
go2v_ident2(ident)
72-
}
73-
*/
7465
}
7566

7667
const v_keywords_which_are_not_go_keywords = ['match', 'lock', 'fn', 'enum', 'in', 'as']

0 commit comments

Comments
 (0)