Skip to content

Commit

Permalink
make ast structs more closely match JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
JalonSolov committed Aug 29, 2024
1 parent a72245f commit 7fb8e40
Show file tree
Hide file tree
Showing 23 changed files with 284 additions and 276 deletions.
121 changes: 72 additions & 49 deletions array_map.v
Original file line number Diff line number Diff line change
@@ -1,60 +1,83 @@
module main
// 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) array_init(c CompositeLit) {
mut have_len := false
mut len_val := ''
mut is_fixed := false
if c.typ.len is BasicLit {
is_fixed = c.typ.len.value != ''
have_len = c.typ.len.value != ''
len_val = c.typ.len.value
} else if c.typ.len is Ellipsis {
is_fixed = true
}
// No elements, just `[]bool{}` (specify type)
if c.elts.len == 0 {
app.gen('[')
if have_len {
app.gen(len_val)
}
app.gen(']')
app.gen(go2v_type(c.typ.elt.name))
app.gen('{}')
} else {
// [1,2,3]
app.gen('[')
elt_name := go2v_type(c.typ.elt.name)
for i, elt in c.elts {
if i == 0 && elt_name != '' && elt_name != 'string' && !elt_name.starts_with_capital() {
// specify type in the first element
// [u8(1), 2, 3]
app.gen('${elt_name}(')
app.expr(elt)
app.gen(')')
} else {
app.expr(elt)
typ := c.typ
match typ {
ArrayType {
mut have_len := false
mut len_val := ''
mut is_fixed := false
if typ.len is BasicLit {
is_fixed = typ.len.value != ''
have_len = typ.len.value != ''
len_val = typ.len.value
} else if typ.len is Ellipsis {
is_fixed = true
}
if i < c.elts.len - 1 {
app.gen(',')
mut elt_name := ''
match typ.elt {
Ident {
elt_name = go2v_type(typ.elt.name)
}
StarExpr {
x := typ.elt.x
match x {
Ident {
elt_name = go2v_type(x.name)
}
else {}
}
}
else {}
}
}
if have_len {
diff := len_val.int() - c.elts.len
if diff > 0 {
for _ in 0 .. diff {
app.gen(',')
match elt_name {
'isize', 'usize' { app.gen('0') }
'string' { app.gen("''") }
else { app.gen('unknown element type??') }
// No elements, just `[]bool{}` (specify type)
if c.elts.len == 0 {
app.gen('[')
if have_len {
app.gen(len_val)
}
app.gen(']')
app.gen(elt_name)
app.gen('{}')
} else {
// [1,2,3]
app.gen('[')
for i, elt in c.elts {
if i == 0 && elt_name != '' && elt_name != 'string'
&& !elt_name.starts_with_capital() {
// specify type in the first element
// [u8(1), 2, 3]
app.gen('${elt_name}(')
app.expr(elt)
app.gen(')')
} else {
app.expr(elt)
}
if i < c.elts.len - 1 {
app.gen(',')
}
}
if have_len {
diff := len_val.int() - c.elts.len
if diff > 0 {
for _ in 0 .. diff {
app.gen(',')
match elt_name {
'isize', 'usize' { app.gen('0') }
'string' { app.gen("''") }
else { app.gen('unknown element type??') }
}
}
}
}
app.gen(']')
if is_fixed {
app.gen('!')
}
}
}
app.gen(']')
if is_fixed {
app.gen('!')
}
else {}
}
}

Expand Down
23 changes: 22 additions & 1 deletion assign_stmt.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// 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) assign_stmt(assign AssignStmt, no_mut bool) {
// app.genln('//assign_stmt')
for l_idx, lhs_expr in assign.lhs {
if l_idx == 0 {
match lhs_expr {
Expand All @@ -26,10 +28,29 @@ fn (mut app App) assign_stmt(assign AssignStmt, no_mut bool) {
//
app.gen(assign.tok)
for r_idx, rhs_expr in assign.rhs {
mut needs_close_paren := false
if r_idx == 0 {
rhs := rhs_expr
match rhs {
BasicLit {
if rhs.kind.is_upper() {
v_kind := go2v_type(rhs.kind.to_lower())
if v_kind != 'int' && v_kind != 'string' {
app.gen('${v_kind}(')
needs_close_paren = true
}
}
}
else {}
}
}
if r_idx > 0 {
app.gen(', ')
}
app.expr(rhs_expr)
if needs_close_paren {
app.gen(')')
}
}
app.genln('')
}
Expand Down
Loading

0 comments on commit 7fb8e40

Please sign in to comment.