-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathswitch.v
More file actions
207 lines (196 loc) · 5.02 KB
/
switch.v
File metadata and controls
207 lines (196 loc) · 5.02 KB
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
// 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) switch_stmt(switch_stmt SwitchStmt) {
// Switch with no condition (tag) is just a bunch of if-elseif's
if switch_stmt.tag is InvalidExpr {
for i, stmt in switch_stmt.body.list {
if i > 0 {
app.gen('else ')
}
case_clause := stmt as CaseClause
if case_clause.list.len == 0 {
// default:
} else if case_clause.list.len > 0 {
app.gen('if ')
app.expr(case_clause.list[0])
}
app.genln('{')
app.stmt_list(case_clause.body)
app.genln('}')
}
return
}
if switch_stmt.init.lhs.len > 0 {
app.assign_stmt(switch_stmt.init, false)
}
if switch_stmt.body.list.len == 1 {
app.gen('if ')
app.expr(switch_stmt.tag)
case_clause := switch_stmt.body.list[0] as CaseClause
if case_clause.list.len == 1 {
app.gen(' == ')
app.expr(case_clause.list[0])
app.genln('{')
app.stmt_list(case_clause.body)
app.genln('}')
} else {
app.gen(' in [')
for i, x in case_clause.list {
if i > 0 {
app.gen(',')
}
app.expr(x)
}
app.genln('] {')
app.stmt_list(case_clause.body)
app.genln('}')
}
return
}
app.gen('match ')
app.expr(switch_stmt.tag)
app.genln('{')
for stmt in switch_stmt.body.list {
case_clause := stmt as CaseClause
for i, x in case_clause.list {
// Check if this is an enum value and add . prefix
if x is Ident {
v_name := app.go2v_ident(x.name)
if v_name in app.enum_values {
app.gen('.${v_name}')
} else {
app.expr(x)
}
} else {
app.expr(x)
}
if i < case_clause.list.len - 1 {
app.gen(',')
}
}
if case_clause.list.len == 0 {
// Default case - check if it only contains a panic (defensive default)
// If so, skip it since V will complain about exhaustive match with else
if app.is_defensive_panic_only(case_clause.body) {
continue
}
app.gen('else ')
}
app.genln('{')
app.stmt_list(case_clause.body)
app.genln('}')
}
app.genln('}')
}
fn (mut app App) type_switch_stmt(node TypeSwitchStmt) {
// Handle the assignment part first (e.g., `e := x.(type)` => `mut e := x`)
// Get the source expression from the RHS TypeAssertExpr
mut switch_expr := Expr(InvalidExpr{})
mut has_assignment := false
mut assigned_var_name := ''
// node.assign can be either AssignStmt or ExprStmt
match node.assign {
AssignStmt {
if node.assign.rhs.len > 0 {
rhs0 := node.assign.rhs[0]
if rhs0 is TypeAssertExpr {
switch_expr = rhs0.x
}
}
has_assignment = node.assign.lhs.len > 0
}
ExprStmt {
// Type switch without assignment: switch s.(type)
if node.assign.x is TypeAssertExpr {
switch_expr = (node.assign.x as TypeAssertExpr).x
}
has_assignment = false
}
else {}
}
// Generate the assignment if there's an lhs
if has_assignment {
assign := node.assign as AssignStmt
lhs0 := assign.lhs[0]
if lhs0 is Ident {
mut lhs_name := app.go2v_ident(lhs0.name)
// Handle shadowing - rename if the name already exists
lhs_name = app.unique_name_anti_shadow(lhs_name)
app.cur_fn_names[lhs_name] = true // Track the variable for shadowing
assigned_var_name = lhs_name
app.gen('mut ')
app.gen(lhs_name)
app.gen(' := ')
app.expr(switch_expr)
app.genln('')
}
}
app.gen('match ')
// Use the assigned variable name only for complex expressions (to avoid struct literal issues in match)
// For simple Ident expressions, use the original to maintain expected behavior
if assigned_var_name != '' && switch_expr !is Ident {
app.gen(assigned_var_name)
} else {
app.expr(switch_expr)
}
app.genln('.type_name() {')
for stmt in node.body.list {
case_clause := stmt as CaseClause
for i, x in case_clause.list {
// Type cases should be converted to string names
app.gen(app.type_case_pattern(x))
if i < case_clause.list.len - 1 {
app.gen(',')
}
}
if case_clause.list.len == 0 {
app.gen('else ')
}
app.genln('{')
app.stmt_list(case_clause.body)
app.genln('}')
}
app.genln('}')
}
// Check if a statement list only contains a panic call (defensive default case)
// These are typically used in exhaustive switches and should be skipped in V
fn (app App) is_defensive_panic_only(stmts []Stmt) bool {
if stmts.len != 1 {
return false
}
// Check if it's a panic call
match stmts[0] {
ExprStmt {
expr := stmts[0] as ExprStmt
if expr.x is CallExpr {
call := expr.x as CallExpr
if call.fun is Ident {
fn_name := (call.fun as Ident).name
return fn_name == 'panic'
}
}
}
else {}
}
return false
}
// Helper to extract the type name from a case pattern in type switch
fn (app App) type_case_pattern(x Expr) string {
match x {
Ident {
// Simple type like `string` or `int`
return "'${go2v_type(x.name)}'"
}
StarExpr {
// Pointer type like `*Foo`
return app.type_case_pattern(x.x)
}
SelectorExpr {
// Qualified type like `pkg.Type`
return "'${x.sel.name}'"
}
else {
return "'UNKNOWN_TYPE'"
}
}
}