clean up map initialization

pull/1454/head
Alexander Medvednikov 2019-08-03 10:26:36 +02:00
parent a5f5b52204
commit e446f988f7
3 changed files with 11 additions and 10 deletions

View File

@ -1,3 +1,8 @@
## V 0.1.18
- Map initialization syntax: `m := { foo: bar, baz: kek }`
## V 0.1.17
*29 Jul 2019*
- `vweb` module for developing web apps in V.

View File

@ -2243,8 +2243,8 @@ fn (p mut Parser) factor() string {
case Token.lcbr:
// `m := { 'one': 1 }`
if p.peek() == .str {
return p.map_init()
}
return p.map_init()
}
// { user | name :'new name' }
return p.assoc()
case Token.key_if:
@ -2422,17 +2422,14 @@ fn (p mut Parser) string_expr() {
// m := { 'one': 1 }
fn (p mut Parser) map_init() string {
// m := { 'one': 1, 'two': 2 }
mut keys := []string // ['one', 'two']
mut keys_gen := '' // ' (string[]){tos2("one"), tos2("two")}
mut vals := []string // [1, 2]
mut vals_gen := ''
mut keys_gen := '' // (string[]){tos2("one"), tos2("two")}
mut vals_gen := '' // (int[]){1, 2}
mut val_type := '' // 'int'
if p.tok == .lcbr {
p.check(.lcbr)
mut i := 0
for {
key := p.lit
keys << key
keys_gen += 'tos2("$key"), '
p.check(.str)
p.check(.colon)
@ -2448,7 +2445,6 @@ fn (p mut Parser) map_init() string {
}
}
val_expr := p.cgen.end_tmp()
vals << val_expr
vals_gen += '$val_expr, '
if p.tok == .rcbr {
p.check(.rcbr)
@ -2458,7 +2454,7 @@ fn (p mut Parser) map_init() string {
p.check(.comma)
}
}
p.gen('new_map_init($keys.len, sizeof($val_type), ' +
p.gen('new_map_init($i, sizeof($val_type), ' +
'(string[]){ $keys_gen }, ($val_type []){ $vals_gen } )')
typ := 'map_$val_type'
return typ

View File

@ -61,7 +61,7 @@ fn test_map() {
}
fn test_map_init() {
m := { 'one': 1, 'two': 2, 'jooo': 'sdf' }
m := { 'one': 1, 'two': 2 }
assert m['one'] == 1
assert m['two'] == 2
assert m['three'] == 0