Skip to content

Commit

Permalink
map init
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Jul 29, 2024
1 parent 07bd216 commit 8b4a0c0
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 14 deletions.
6 changes: 6 additions & 0 deletions array_map.v
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ fn (mut app App) array_init(c CompositeLit) {
}

fn (mut app App) map_init(node CompositeLit) {
app.genln('{')
for elt in node.elts {
kv := elt as KeyValueExpr
app.key_value_expr(kv)
}
app.genln('}')
}
3 changes: 2 additions & 1 deletion ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ struct UnaryExpr {
}

struct KeyValueExpr {
key Ident @[json: 'Key']
// key Ident @[json: 'Key']
key Expr @[json: 'Key']
value Expr @[json: 'Value']
node_type_str string @[json: '_type']
}
Expand Down
15 changes: 11 additions & 4 deletions expr.v
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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.

Expand All @@ -10,7 +12,7 @@ fn (mut app App) expr(expr Expr) {
app.binary_expr(expr)
}
Ident {
app.gen(expr.name.camel_to_snake()) // to_lower()) // TODO ?
app.gen(go2v_ident(expr.name))
}
CallExpr {
app.call_expr(expr)
Expand Down Expand Up @@ -99,9 +101,14 @@ fn (mut app App) paren_expr(p ParenExpr) {
app.gen(')')
}

fn (mut app App) key_value_expr(k KeyValueExpr) {
app.gen('\t${k.key.name}: ')
app.expr(k.value)
fn (mut app App) key_value_expr(expr KeyValueExpr) {
if expr.key is Ident {
app.gen('\t${expr.key.name}: ')
} else {
app.expr(expr.key)
app.gen(': ')
}
app.expr(expr.value)
app.genln('')
}

Expand Down
7 changes: 0 additions & 7 deletions stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,3 @@ fn (mut app App) return_stmt(node ReturnStmt) {
}
}
}

fn go2v_type(typ string) string {
if typ == 'byte' {
return 'u8'
}
return typ
}
4 changes: 3 additions & 1 deletion struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ fn (mut app App) struct_decl(spec Spec) {
}
for field in spec.typ.fields.list {
type_name := app.type_or_ident(field.typ)
app.genln('\t${field.names.map(it.name).join(', ')} ${type_name}')
for n in field.names {
app.genln('\t${go2v_ident(n.name)} ${go2v_type(type_name)}')
}
}
app.genln('}\n')
}
Expand Down
2 changes: 1 addition & 1 deletion tests/map/map.vv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module main

struct Vertex {
mut:
pub mut:
lat f64
long f64
}
Expand Down
18 changes: 18 additions & 0 deletions util.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module main

fn go2v_type(typ string) string {
match typ {
'byte' {
return 'u8'
}
'float64' {
return 'f64'
}
else {}
}
return typ
}

fn go2v_ident(ident string) string {
return ident.camel_to_snake() // to_lower()) // TODO ?
}

0 comments on commit 8b4a0c0

Please sign in to comment.