-
Notifications
You must be signed in to change notification settings - Fork 19
/
struct.v
220 lines (210 loc) · 4.65 KB
/
struct.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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.
fn (mut app App) gen_decl(decl GenDecl) {
// app.genln('// gen_decl')
// mut needs_closer := false
app.comments(decl.doc)
for spec in decl.specs {
match spec {
ImportSpec {
app.import_spec(spec)
}
TypeSpec {
match spec.typ {
InterfaceType {
app.interface_decl(spec.name.name, spec.typ)
}
StructType {
app.struct_decl(spec.name.name, spec.typ)
}
else {
app.type_decl(spec)
}
}
}
ValueSpec {
if spec.typ is Ident {
// needs_closer = true
}
match decl.tok {
'var' {
// app.genln('// ValueSpec global')
app.global_decl(spec)
}
else {
// app.genln('// VA const')
app.const_decl(spec)
}
}
}
}
}
// if needs_closer {
if app.is_enum_decl {
app.genln('}')
}
app.is_enum_decl = false
}
fn (mut app App) type_decl(spec TypeSpec) {
// Remember the type name for the upcoming const (enum) handler if it's an enum
name := spec.name.name
app.type_decl_name = name
// TODO figure out how to diffirentiate between enums and type aliases
if name == 'EnumTest' {
return
}
// Generate actual type alias
app.gen('type ${name} = ')
app.typ(spec.typ)
app.genln('')
}
fn (mut app App) global_decl(spec ValueSpec) {
for name in spec.names {
app.gen('__global ${name.name} ')
if spec.typ is InvalidExpr {
// No type means `var x = foo.bar()`
// Eval the expression
app.gen(' = ')
// TODO multiple values?
if spec.values.len > 0 {
app.expr(spec.values[0])
}
} else {
app.typ(spec.typ)
app.genln('')
}
}
}
fn (mut app App) const_decl(spec ValueSpec) {
// Handle iota (V enuma)
if spec.values.len > 0 {
first_val := spec.values[0]
if first_val is Ident {
if first_val.name == 'iota' {
app.is_enum_decl = true
app.genln('enum ${app.type_decl_name} {')
}
}
}
for i, name in spec.names {
if !app.is_enum_decl && name.name.starts_with_capital() {
app.gen('pub ')
}
n := app.go2v_ident(name.name)
if app.is_enum_decl {
if n != 'iota' {
app.genln(n)
continue
}
} else {
app.gen('const ${n} = ')
}
if i < spec.values.len && !app.is_enum_decl {
app.expr(spec.values[i])
}
app.genln('')
}
}
const master_module_path = 'github.com.evanw.esbuild.internal' // TODO hardcoded
fn (mut app App) import_spec(spec ImportSpec) {
name := spec.path.value.replace('"', '').replace('/', '.')
// Skip modules that don't exist in V (fmt, strings etc)
if name in nonexistent_modules {
return
}
if name.starts_with(master_module_path) {
n := name.replace(master_module_path, '')
app.genln('import ${n[1..]} // local module')
return
}
// TODO a temp hack
if name.starts_with('github') {
return
}
app.genln('import ${name}')
}
fn (mut app App) struct_decl(struct_name string, spec StructType) {
app.genln('struct ${struct_name} {')
if spec.fields.list.len > 0 {
app.genln('pub mut:')
}
for field in spec.fields.list {
app.comments(field.doc)
for n in field.names {
app.gen('\t')
app.gen(app.go2v_ident(n.name))
app.gen(' ')
app.force_upper = true
app.typ(field.typ)
if field.typ in [StarExpr, FuncType] {
app.gen(' = unsafe { nil }')
}
app.genln('')
}
}
app.genln('}\n')
}
fn (mut app App) interface_decl(interface_name string, spec InterfaceType) {
app.genln('interface ${interface_name} {')
for field in spec.methods.list {
app.comments(field.doc)
for n in field.names {
app.gen('\t')
app.gen(app.go2v_ident(n.name))
app.force_upper = true
app.typ(field.typ)
app.genln('')
}
}
app.genln('}\n')
}
// Foo{bar:baz}
// config.Foo{bar:baz}
// []int{1,2,3}
// map[string]int{"foo":1}
fn (mut app App) composite_lit(c CompositeLit) {
// if c.typ.name != '' {
// app.struct_init(c)
// return
// }
match c.typ {
ArrayType {
app.array_init(c)
}
Ident {
app.struct_init(c)
}
MapType {
app.map_init(c)
}
SelectorExpr {
app.force_upper = true
app.selector_expr(c.typ)
app.force_upper = false
app.map_init(c) // loops thru each key_value_expr TODO method needs renaming
}
else {
// app.gen('// UNHANDLED CompositeLit type ${typeof(c.typ).name}')
app.genln('// UNHANDLED CompositeLit type ${c.typ.type_name()} strtyp="${c.typ}"')
}
}
}
fn (mut app App) struct_init(c CompositeLit) {
typ := c.typ
match typ {
Ident {
app.force_upper = true
n := app.go2v_ident(typ.name)
app.gen('${n}{')
if c.elts.len > 0 {
app.genln('')
}
for elt in c.elts {
app.expr(elt)
app.genln('')
}
app.gen('}')
}
else {}
}
}