Skip to content

Commit

Permalink
array_map.v
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Jul 29, 2024
1 parent b0c8374 commit 07bd216
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 55 deletions.
63 changes: 63 additions & 0 deletions array_map.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module main

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(c.typ.elt.name)
app.gen('{}')
} else {
// [1,2,3]
app.gen('[')
for i, elt in c.elts {
elt_name := go2v_type(c.typ.elt.name)
if i == 0 && is_fixed && elt_name != '' && elt_name != 'string' && elt_name != 'int' {
// 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 {
elt_name := go2v_type(c.typ.elt.name)
diff := len_val.int() - c.elts.len
if diff > 0 {
for _ in 0 .. diff {
app.gen(',')
match elt_name {
'int' { app.gen('0') }
'string' { app.gen("''") }
else { app.gen('unknown element type??') }
}
}
}
}
app.gen(']')
if is_fixed {
app.gen('!')
}
}
}

fn (mut app App) map_init(node CompositeLit) {
}
1 change: 1 addition & 0 deletions main.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const passing_tests = [
'array_fixed_size',
'defer',
'defer_multiple',
'map',
]

struct App {
Expand Down
63 changes: 8 additions & 55 deletions struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,62 +33,15 @@ fn (mut app App) composite_lit(c CompositeLit) {
app.struct_init(c)
return
}
if c.typ.node_type_str == 'ArrayType' {
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
match c.typ.node_type_str {
'ArrayType' {
app.array_init(c)
}
// No elements, just `[]bool{}` (specify type)
if c.elts.len == 0 {
app.gen('[')
if have_len {
app.gen(len_val)
}
app.gen(']')
app.gen(c.typ.elt.name)
app.gen('{}')
} else {
// [1,2,3]
app.gen('[')
for i, elt in c.elts {
elt_name := go2v_type(c.typ.elt.name)
if i == 0 && is_fixed && elt_name != '' && elt_name != 'string' && elt_name != 'int' {
// 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 {
elt_name := go2v_type(c.typ.elt.name)
diff := len_val.int() - c.elts.len
if diff > 0 {
for _ in 0 .. diff {
app.gen(',')
match elt_name {
'int' { app.gen('0') }
'string' { app.gen("''") }
else { app.gen('unknown element type??') }
}
}
}
}
app.gen(']')
if is_fixed {
app.gen('!')
}
'MapType' {
app.map_init(c)
}
else {
app.gen('// UNHANDLED CompositeLit type')
}
}
}
Expand Down

0 comments on commit 07bd216

Please sign in to comment.