clean up map initialization
parent
a5f5b52204
commit
e446f988f7
|
@ -1,3 +1,8 @@
|
||||||
|
## V 0.1.18
|
||||||
|
|
||||||
|
- Map initialization syntax: `m := { ‘foo’: ‘bar’, ‘baz’: ‘kek’ }`
|
||||||
|
|
||||||
|
|
||||||
## V 0.1.17
|
## V 0.1.17
|
||||||
*29 Jul 2019*
|
*29 Jul 2019*
|
||||||
- `vweb` module for developing web apps in V.
|
- `vweb` module for developing web apps in V.
|
||||||
|
|
|
@ -2243,8 +2243,8 @@ fn (p mut Parser) factor() string {
|
||||||
case Token.lcbr:
|
case Token.lcbr:
|
||||||
// `m := { 'one': 1 }`
|
// `m := { 'one': 1 }`
|
||||||
if p.peek() == .str {
|
if p.peek() == .str {
|
||||||
return p.map_init()
|
return p.map_init()
|
||||||
}
|
}
|
||||||
// { user | name :'new name' }
|
// { user | name :'new name' }
|
||||||
return p.assoc()
|
return p.assoc()
|
||||||
case Token.key_if:
|
case Token.key_if:
|
||||||
|
@ -2422,17 +2422,14 @@ fn (p mut Parser) string_expr() {
|
||||||
// m := { 'one': 1 }
|
// m := { 'one': 1 }
|
||||||
fn (p mut Parser) map_init() string {
|
fn (p mut Parser) map_init() string {
|
||||||
// m := { 'one': 1, 'two': 2 }
|
// m := { 'one': 1, 'two': 2 }
|
||||||
mut keys := []string // ['one', 'two']
|
mut keys_gen := '' // (string[]){tos2("one"), tos2("two")}
|
||||||
mut keys_gen := '' // ' (string[]){tos2("one"), tos2("two")}
|
mut vals_gen := '' // (int[]){1, 2}
|
||||||
mut vals := []string // [1, 2]
|
|
||||||
mut vals_gen := ''
|
|
||||||
mut val_type := '' // 'int'
|
mut val_type := '' // 'int'
|
||||||
if p.tok == .lcbr {
|
if p.tok == .lcbr {
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
mut i := 0
|
mut i := 0
|
||||||
for {
|
for {
|
||||||
key := p.lit
|
key := p.lit
|
||||||
keys << key
|
|
||||||
keys_gen += 'tos2("$key"), '
|
keys_gen += 'tos2("$key"), '
|
||||||
p.check(.str)
|
p.check(.str)
|
||||||
p.check(.colon)
|
p.check(.colon)
|
||||||
|
@ -2448,7 +2445,6 @@ fn (p mut Parser) map_init() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val_expr := p.cgen.end_tmp()
|
val_expr := p.cgen.end_tmp()
|
||||||
vals << val_expr
|
|
||||||
vals_gen += '$val_expr, '
|
vals_gen += '$val_expr, '
|
||||||
if p.tok == .rcbr {
|
if p.tok == .rcbr {
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
|
@ -2458,7 +2454,7 @@ fn (p mut Parser) map_init() string {
|
||||||
p.check(.comma)
|
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 } )')
|
'(string[]){ $keys_gen }, ($val_type []){ $vals_gen } )')
|
||||||
typ := 'map_$val_type'
|
typ := 'map_$val_type'
|
||||||
return typ
|
return typ
|
||||||
|
|
|
@ -61,7 +61,7 @@ fn test_map() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_map_init() {
|
fn test_map_init() {
|
||||||
m := { 'one': 1, 'two': 2, 'jooo': 'sdf' }
|
m := { 'one': 1, 'two': 2 }
|
||||||
assert m['one'] == 1
|
assert m['one'] == 1
|
||||||
assert m['two'] == 2
|
assert m['two'] == 2
|
||||||
assert m['three'] == 0
|
assert m['three'] == 0
|
||||||
|
|
Loading…
Reference in New Issue