-
Notifications
You must be signed in to change notification settings - Fork 19
/
expr.v
225 lines (208 loc) · 3.99 KB
/
expr.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
221
222
223
224
225
// 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) expr(expr Expr) {
match expr {
InvalidExpr {
print_backtrace()
eprintln('> invalid expression encountered')
}
BasicLit {
app.basic_lit(expr)
}
BinaryExpr {
app.binary_expr(expr)
}
Ident {
app.ident(expr)
}
CallExpr {
app.call_expr(expr)
}
SelectorExpr {
app.selector_expr(expr)
}
CompositeLit {
app.composite_lit(expr)
}
KeyValueExpr {
app.key_value_expr(expr)
}
IndexExpr {
app.index_expr(expr)
}
MapType {
app.map_type(expr)
}
ParenExpr {
app.paren_expr(expr)
}
UnaryExpr {
app.unary_expr(expr)
}
ArrayType {
app.array_type(expr)
}
StarExpr {
app.star_expr(expr)
}
FuncLit {
app.func_lit(expr)
}
Ellipsis {}
SliceExpr {
app.slice_expr(expr)
}
FuncType {
app.func_type(expr)
}
TypeAssertExpr {
app.type_assert_expr(expr)
}
}
}
fn (mut app App) basic_lit(l BasicLit) {
if l.kind == 'CHAR' {
app.gen(quoted_lit(l.value, '`'))
} else if l.kind == 'STRING' {
app.gen(quoted_lit(l.value, "'"))
} else {
app.gen(l.value)
}
}
fn quoted_lit(s string, quote string) string {
mut quote2 := quote
go_quote := s[0]
mut no_quotes := s[1..s.len - 1]
// Use "" quotes if the string literal contains '
if quote2 == "'" && no_quotes.contains("'") && !no_quotes.contains('"') {
// no_quotes = no_quotes.replace(
quote2 = '"'
}
if s.contains('\\"') {
quote2 = '"'
}
mut prefix := ''
if go_quote == `\`` {
prefix = 'r'
}
return '${prefix}${quote2}${no_quotes}${quote2}'
}
fn (mut app App) selector_expr(s SelectorExpr) {
force_upper := app.force_upper // save force upper for `mod.ForceUpper`
app.force_upper = false
app.expr(s.x)
app.gen('.')
app.force_upper = force_upper
app.gen(app.go2v_ident(s.sel.name))
}
fn (mut app App) index_expr(s IndexExpr) {
app.expr(s.x)
app.gen('[')
app.expr(s.index)
app.gen(']')
}
fn (mut app App) binary_expr(b BinaryExpr) {
app.expr(b.x)
if b.op == '\u0026^' {
app.gen('&~')
} else {
app.gen(b.op)
}
app.expr(b.y)
}
fn (mut app App) unary_expr(u UnaryExpr) {
if u.op == '^' {
// In Go bitwise NOT is ^x
// In V it's ~x, ^ is only used for XOR: x^b
app.gen('~')
} else if u.op != '+' {
app.gen(u.op)
}
app.expr(u.x)
}
fn (mut app App) paren_expr(p ParenExpr) {
app.gen('(')
app.expr(p.x)
app.gen(')')
}
fn (mut app App) type_assert_expr(t TypeAssertExpr) {
// TODO more?
app.expr(t.x)
}
fn (mut app App) key_value_expr(expr KeyValueExpr) {
if expr.key is Ident {
app.gen('\t${app.go2v_ident(expr.key.name)}: ')
} else {
app.expr(expr.key)
app.gen(': ')
}
app.expr(expr.value)
}
fn (mut app App) array_type(node ArrayType) {
match node.elt {
Ident {
app.gen('[]${go2v_type(node.elt.name)}')
}
StarExpr {
app.gen('[]')
app.star_expr(node.elt)
// app.gen('[]&${node.elt.name}')
}
SelectorExpr {
app.gen('[]')
app.force_upper = true
app.selector_expr(node.elt)
app.force_upper = false
}
FuncType {
app.gen('[]')
app.func_type(node.elt)
}
ArrayType {
app.gen('[]')
app.array_type(node.elt)
}
else {
app.gen('UNKNOWN ELT ${node.elt.type_name()}')
}
}
}
fn (mut app App) map_type(node MapType) {
app.force_upper = true
app.gen('map[')
app.force_upper = true
app.expr(node.key)
app.gen(']')
app.force_upper = true
app.expr(node.val)
app.force_upper = false
}
fn (mut app App) chan_type(node ChanType) {
app.gen('chan ')
app.expr(node.value)
}
fn (mut app App) star_expr(node StarExpr) {
if app.no_star {
app.no_star = false
} else {
app.gen('&')
}
app.expr(node.x)
}
fn (mut app App) slice_expr(node SliceExpr) {
app.expr(node.x)
app.gen('[')
if node.low is InvalidExpr {
} else {
app.expr(node.low)
}
app.gen('..')
if node.high is InvalidExpr {
} else {
app.expr(node.high)
}
app.gen(']')
}
fn (mut app App) ident(node Ident) {
app.gen(go2v_type(app.go2v_ident(node.name)))
}