Skip to content

Commit 0ba0fbc

Browse files
authored
make ast structs more closely match JSON (#129)
1 parent a72245f commit 0ba0fbc

File tree

23 files changed

+284
-276
lines changed

23 files changed

+284
-276
lines changed

array_map.v

+72-49
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,83 @@
1-
module main
1+
// Copyright (c) 2024 Alexander Medvednikov. All rights reserved.
2+
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
23

34
fn (mut app App) array_init(c CompositeLit) {
4-
mut have_len := false
5-
mut len_val := ''
6-
mut is_fixed := false
7-
if c.typ.len is BasicLit {
8-
is_fixed = c.typ.len.value != ''
9-
have_len = c.typ.len.value != ''
10-
len_val = c.typ.len.value
11-
} else if c.typ.len is Ellipsis {
12-
is_fixed = true
13-
}
14-
// No elements, just `[]bool{}` (specify type)
15-
if c.elts.len == 0 {
16-
app.gen('[')
17-
if have_len {
18-
app.gen(len_val)
19-
}
20-
app.gen(']')
21-
app.gen(go2v_type(c.typ.elt.name))
22-
app.gen('{}')
23-
} else {
24-
// [1,2,3]
25-
app.gen('[')
26-
elt_name := go2v_type(c.typ.elt.name)
27-
for i, elt in c.elts {
28-
if i == 0 && elt_name != '' && elt_name != 'string' && !elt_name.starts_with_capital() {
29-
// specify type in the first element
30-
// [u8(1), 2, 3]
31-
app.gen('${elt_name}(')
32-
app.expr(elt)
33-
app.gen(')')
34-
} else {
35-
app.expr(elt)
5+
typ := c.typ
6+
match typ {
7+
ArrayType {
8+
mut have_len := false
9+
mut len_val := ''
10+
mut is_fixed := false
11+
if typ.len is BasicLit {
12+
is_fixed = typ.len.value != ''
13+
have_len = typ.len.value != ''
14+
len_val = typ.len.value
15+
} else if typ.len is Ellipsis {
16+
is_fixed = true
3617
}
37-
if i < c.elts.len - 1 {
38-
app.gen(',')
18+
mut elt_name := ''
19+
match typ.elt {
20+
Ident {
21+
elt_name = go2v_type(typ.elt.name)
22+
}
23+
StarExpr {
24+
x := typ.elt.x
25+
match x {
26+
Ident {
27+
elt_name = go2v_type(x.name)
28+
}
29+
else {}
30+
}
31+
}
32+
else {}
3933
}
40-
}
41-
if have_len {
42-
diff := len_val.int() - c.elts.len
43-
if diff > 0 {
44-
for _ in 0 .. diff {
45-
app.gen(',')
46-
match elt_name {
47-
'isize', 'usize' { app.gen('0') }
48-
'string' { app.gen("''") }
49-
else { app.gen('unknown element type??') }
34+
// No elements, just `[]bool{}` (specify type)
35+
if c.elts.len == 0 {
36+
app.gen('[')
37+
if have_len {
38+
app.gen(len_val)
39+
}
40+
app.gen(']')
41+
app.gen(elt_name)
42+
app.gen('{}')
43+
} else {
44+
// [1,2,3]
45+
app.gen('[')
46+
for i, elt in c.elts {
47+
if i == 0 && elt_name != '' && elt_name != 'string'
48+
&& !elt_name.starts_with_capital() {
49+
// specify type in the first element
50+
// [u8(1), 2, 3]
51+
app.gen('${elt_name}(')
52+
app.expr(elt)
53+
app.gen(')')
54+
} else {
55+
app.expr(elt)
56+
}
57+
if i < c.elts.len - 1 {
58+
app.gen(',')
5059
}
5160
}
61+
if have_len {
62+
diff := len_val.int() - c.elts.len
63+
if diff > 0 {
64+
for _ in 0 .. diff {
65+
app.gen(',')
66+
match elt_name {
67+
'isize', 'usize' { app.gen('0') }
68+
'string' { app.gen("''") }
69+
else { app.gen('unknown element type??') }
70+
}
71+
}
72+
}
73+
}
74+
app.gen(']')
75+
if is_fixed {
76+
app.gen('!')
77+
}
5278
}
5379
}
54-
app.gen(']')
55-
if is_fixed {
56-
app.gen('!')
57-
}
80+
else {}
5881
}
5982
}
6083

assign_stmt.v

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// Copyright (c) 2024 Alexander Medvednikov. All rights reserved.
2+
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
3+
14
fn (mut app App) assign_stmt(assign AssignStmt, no_mut bool) {
2-
// app.genln('//assign_stmt')
35
for l_idx, lhs_expr in assign.lhs {
46
if l_idx == 0 {
57
match lhs_expr {
@@ -26,10 +28,29 @@ fn (mut app App) assign_stmt(assign AssignStmt, no_mut bool) {
2628
//
2729
app.gen(assign.tok)
2830
for r_idx, rhs_expr in assign.rhs {
31+
mut needs_close_paren := false
32+
if r_idx == 0 {
33+
rhs := rhs_expr
34+
match rhs {
35+
BasicLit {
36+
if rhs.kind.is_upper() {
37+
v_kind := go2v_type(rhs.kind.to_lower())
38+
if v_kind != 'int' && v_kind != 'string' {
39+
app.gen('${v_kind}(')
40+
needs_close_paren = true
41+
}
42+
}
43+
}
44+
else {}
45+
}
46+
}
2947
if r_idx > 0 {
3048
app.gen(', ')
3149
}
3250
app.expr(rhs_expr)
51+
if needs_close_paren {
52+
app.gen(')')
53+
}
3354
}
3455
app.genln('')
3556
}

0 commit comments

Comments
 (0)