Skip to content

Commit c453993

Browse files
committed
shadowing fixes; arr.clone(); chan type; type assert; type switch; bundler.go
1 parent 20feea4 commit c453993

File tree

15 files changed

+12093
-22
lines changed

15 files changed

+12093
-22
lines changed

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
- name: Run tests
4343
run: v -g test .
4444

45-
- name: Run copmplex tests
45+
- name: Run complex tests
4646
run: |
4747
v run . complex_tests/esbuild/api_impl
48+
v run . complex_tests/esbuild/resolver

assign_stmt.v

+57-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
// Copyright (c) 2024 Alexander Medvednikov. All rights reserved.
22
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
3-
import rand
3+
4+
fn (mut app App) unique_name_anti_shadow(n string) string {
5+
if n == '_' {
6+
return '_'
7+
}
8+
if app.running_test {
9+
return n
10+
}
11+
if n !in app.cur_fn_names {
12+
return n
13+
}
14+
// Increase the i in `name_i` until it's unique.
15+
mut i := 1
16+
mut res := ''
17+
for {
18+
res = '${n}_${i}'
19+
20+
if res !in app.cur_fn_names {
21+
break
22+
}
23+
i++
24+
if i > 100 {
25+
panic('100 levels of shadowing, that cannot be real!')
26+
}
27+
}
28+
// res := n + rand.intn(10000) or { 0 }.str() // LOL fix this
29+
return res
30+
}
431

532
fn (mut app App) assign_stmt(assign AssignStmt, no_mut bool) {
633
for l_idx, lhs_expr in assign.lhs {
@@ -24,15 +51,20 @@ fn (mut app App) assign_stmt(assign AssignStmt, no_mut bool) {
2451
// Handle shadowing
2552
mut n := lhs_expr.name
2653
if assign.tok == ':=' && n != '_' && n in app.cur_fn_names {
27-
n += rand.intn(10000) or { 0 }.str() // LOL fix this
54+
n = app.unique_name_anti_shadow(n)
2855
}
29-
3056
app.cur_fn_names[n] = true
57+
3158
new_ident := Ident{
3259
...lhs_expr
3360
name: n
3461
}
35-
app.ident(new_ident) // lhs_expr)
62+
// app.ident(app.go2v_ident(new_ident))
63+
app.ident(new_ident)
64+
} else if lhs_expr is StarExpr {
65+
// Can't use star_expr(), since it generates &
66+
app.gen('*')
67+
app.expr(lhs_expr.x)
3668
} else {
3769
app.expr(lhs_expr)
3870
}
@@ -43,7 +75,9 @@ fn (mut app App) assign_stmt(assign AssignStmt, no_mut bool) {
4375
}
4476
//
4577
app.gen(assign.tok)
78+
// app.gen('/*F*/')
4679
for r_idx, rhs_expr in assign.rhs {
80+
// app.genln('/* ${rhs_expr} */')
4781
// app.gen('ridx=${r_idx}')
4882
mut needs_close_paren := false
4983
if r_idx == 0 {
@@ -73,20 +107,37 @@ fn (mut app App) assign_stmt(assign AssignStmt, no_mut bool) {
73107
}
74108

75109
fn (mut app App) check_and_handle_append(assign AssignStmt) bool {
110+
if assign.rhs.len == 0 {
111+
app.genln('// append no rhs')
112+
return false
113+
}
76114
first_rhs := assign.rhs[0]
77115
if first_rhs is CallExpr {
78116
fun := first_rhs.fun
79117
if fun is Ident {
80118
if fun.name == 'append' {
81-
app.gen_append(first_rhs.args)
119+
app.gen_append(first_rhs.args, assign.tok)
82120
return true
83121
}
84122
}
85123
}
86124
return false
87125
}
88126

89-
fn (mut app App) gen_append(args []Expr) {
127+
fn (mut app App) gen_append(args []Expr, assign_tok string) {
128+
// Handle special case `mut x := arr.clone()`
129+
// In Go it's
130+
// `append([]Foo{}, foo...)`
131+
132+
arg0 := args[0]
133+
if arg0 is CompositeLit && arg0.typ is ArrayType {
134+
app.gen(' ${assign_tok} ')
135+
app.expr(args[1])
136+
app.gen('.')
137+
app.genln('clone()')
138+
return
139+
}
140+
90141
app.gen(' << ')
91142
if args.len == 2 {
92143
app.expr(args[1])

ast.v

+27-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ type Expr = InvalidExpr
2323
| SliceExpr
2424
| StarExpr
2525
| UnaryExpr
26+
| TypeAssertExpr
2627

27-
type Stmt = AssignStmt
28+
type Stmt = InvalidStmt
29+
| AssignStmt
2830
| BlockStmt
2931
| BranchStmt
3032
| CaseClause
@@ -37,10 +39,13 @@ type Stmt = AssignStmt
3739
| RangeStmt
3840
| ReturnStmt
3941
| SwitchStmt
42+
| TypeSwitchStmt
4043
| GoStmt
4144

4245
struct InvalidExpr {}
4346

47+
struct InvalidStmt {}
48+
4449
type Specs = ImportSpec | TypeSpec | ValueSpec
4550

4651
type Type = InvalidExpr
@@ -52,6 +57,7 @@ type Type = InvalidExpr
5257
| Ident
5358
| StarExpr
5459
| SelectorExpr
60+
| ChanType
5561

5662
struct GoFile {
5763
package_name Ident @[json: 'Name']
@@ -105,6 +111,12 @@ struct MapType {
105111
val Expr @[json: 'Value']
106112
}
107113

114+
struct ChanType {
115+
node_type string @[json: '_type']
116+
dir string @[json: 'Dir']
117+
value Expr @[json: 'Value']
118+
}
119+
108120
struct Ellipsis {
109121
node_type string @[json: '_type']
110122
elt Ident @[json: 'Elt']
@@ -140,6 +152,13 @@ struct SwitchStmt {
140152
body BlockStmt @[json: 'Body']
141153
}
142154

155+
struct TypeSwitchStmt {
156+
node_type string @[json: '_type']
157+
assign AssignStmt @[json: 'Assign']
158+
// tag Expr @[json: 'Tag']
159+
body BlockStmt @[json: 'Body']
160+
}
161+
143162
struct CaseClause {
144163
node_type string @[json: '_type']
145164
list []Expr @[json: 'List']
@@ -278,6 +297,12 @@ struct SliceExpr {
278297
high Expr @[json: 'High']
279298
}
280299

300+
struct TypeAssertExpr {
301+
node_type string @[json: '_type']
302+
x Expr @[json: 'X']
303+
typ Type @[json: 'Type']
304+
}
305+
281306
struct StarExpr {
282307
node_type string @[json: '_type']
283308
x Expr @[json: 'X']
@@ -335,6 +360,7 @@ fn (e Expr) node_type() string {
335360
SliceExpr { return e.node_type }
336361
UnaryExpr { return e.node_type }
337362
FuncType { return e.node_type }
363+
TypeAssertExpr { return e.node_type }
338364
}
339365
return 'unknown node type'
340366
}

0 commit comments

Comments
 (0)