From e446f988f7ecea5d980e4780086af5be2cb5c169 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 3 Aug 2019 10:26:36 +0200 Subject: [PATCH] clean up map initialization --- CHANGELOG.md | 5 +++++ compiler/parser.v | 14 +++++--------- vlib/builtin/map_test.v | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef670e58eb..23b2bef77c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/compiler/parser.v b/compiler/parser.v index 6b6f19b0be..a7d2129b8f 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -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 diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index c973f91401..57d4378abd 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -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