1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

map: allow array values

This commit is contained in:
Alexander Medvednikov 2019-08-16 18:44:35 +03:00
parent 54b3c4d2c5
commit 94b900bd0a
6 changed files with 58 additions and 47 deletions

View File

@ -67,8 +67,8 @@ pub fn(graph mut ModDepGraph) add(mod string, deps []string) {
}
pub fn(graph &ModDepGraph) resolve() *ModDepGraph {
mut node_names := map[string]ModDepGraphNode{}
mut node_deps := map[string]DepSet{}
mut node_names := map[string]ModDepGraphNode
mut node_deps := map[string]DepSet
for _, node in graph.nodes {
node_names[node.name] = node

View File

@ -883,8 +883,8 @@ fn (p mut Parser) get_type() string {
p.error('maps only support string keys for now')
}
p.check(.rsbr)
val_type := p.check_name()
typ= 'map_$val_type'
val_type := p.get_type()// p.check_name()
typ = 'map_$val_type'
p.register_map(typ)
return typ
}
@ -2603,15 +2603,19 @@ fn (p mut Parser) map_init() string {
p.error('only string key maps allowed for now')
}
p.check(.rsbr)
val_type = p.check_name()
if !p.table.known_type(val_type) {
p.error('map init unknown type "$val_type"')
}
val_type = p.get_type()/// p.check_name()
//if !p.table.known_type(val_type) {
//p.error('map init unknown type "$val_type"')
//}
typ := 'map_$val_type'
p.register_map(typ)
p.gen('new_map(1, sizeof($val_type))')
p.check(.lcbr)
p.check(.rcbr)
if p.tok == .lcbr {
p.check(.lcbr)
p.check(.rcbr)
println('warning: $p.file_name:$p.scanner.line_nr ' +
'initializaing maps no longer requires `{}`')
}
return typ
}

View File

@ -137,8 +137,8 @@ fn is_primitive_type(typ string) bool {
fn new_table(obfuscate bool) *Table {
mut t := &Table {
obf_ids: map[string]int{}
fns: map[string]Fn{}
obf_ids: map[string]int
fns: map[string]Fn
//generic_fns: map[string]GenTable{}
generic_fns: []GenTable
obfuscate: obfuscate
@ -818,7 +818,7 @@ fn (table &Table) qualify_module(mod string, file_path string) string {
fn new_file_import_table(file_path string) *FileImportTable {
return &FileImportTable{
file_path: file_path
imports: map[string]string{}
imports: map[string]string
}
}

View File

@ -114,7 +114,7 @@ enum Token {
// build_keys genereates a map with keywords' string values:
// Keywords['return'] == .key_return
fn build_keys() map[string]int {
mut res := map[string]int{}
mut res := map[string]int
for t := int(Token.keyword_beg) + 1; t < int(Token.keyword_end); t++ {
key := TokenStr[t]
res[key] = int(t)
@ -127,11 +127,11 @@ fn build_token_str() []string {
mut s := [''; NrTokens]
s[Token.keyword_beg] = ''
s[Token.keyword_end] = ''
s[Token.eof] = '.eof'
s[Token.name] = '.name'
s[Token.number] = '.number'
s[Token.eof] = 'eof'
s[Token.name] = 'name'
s[Token.number] = 'number'
s[Token.str] = 'STR'
s[Token.chartoken] = '.chartoken'
s[Token.chartoken] = 'char'
s[Token.plus] = '+'
s[Token.minus] = '-'
s[Token.mul] = '*'
@ -198,7 +198,6 @@ fn build_token_str() []string {
s[Token.key_type] = 'type'
s[Token.key_for] = 'for'
s[Token.key_switch] = 'switch'
//Tokens[MATCH] = 'match'
s[Token.key_case] = 'case'
s[Token.func] = 'fn'
s[Token.key_true] = 'true'
@ -207,7 +206,7 @@ fn build_token_str() []string {
s[Token.key_break] = 'break'
s[Token.key_import] = 'import'
s[Token.key_embed] = 'embed'
//Tokens[TYP.eof] = 'typeof'
//Tokens[key_typeof] = 'typeof'
s[Token.key_default] = 'default'
s[Token.key_enum] = 'enum'
s[Token.key_interface] = 'interface'

View File

@ -14,7 +14,6 @@ pub:
len int
cap int
element_size int
}
// Private function, used by V (`nums := []int`)

View File

@ -14,7 +14,7 @@ fn (a mut A) set(key string, val int) {
}
fn test_map() {
mut m := map[string]int{}
mut m := map[string]int
assert m.size == 0
m['hi'] = 80
m['hello'] = 101
@ -43,13 +43,13 @@ fn test_map() {
assert m.keys().len == 1
assert m.keys()[0] == 'hello'
////
mut users := map[string]User{}
mut users := map[string]User
users['1'] = User{'Peter'}
peter := users['1']
assert peter.name == 'Peter'
mut a := A{
m: map[string]int{}
users: map[string]User{}
m: map[string]int
users: map[string]User
}
a.users['Bob'] = User{'Bob'}
q := a.users['Bob']
@ -73,7 +73,7 @@ fn test_string_map() {
fn test_large_map() {
//ticks := time.ticks()
mut nums := map[string]int{}
mut nums := map[string]int
N := 30 * 1000
for i := 0; i < N; i++ {
key := i.str()
@ -86,71 +86,80 @@ fn test_large_map() {
}
fn test_various_map_value() {
mut m1 := map[string]int{}
mut m1 := map[string]int
m1['test'] = 1
assert m1['test'] == 1
mut m2 := map[string]string{}
mut m2 := map[string]string
m2['test'] = 'test'
assert m2['test'] == 'test'
mut m3 := map[string]i8{}
mut m3 := map[string]i8
m3['test'] = i8(0)
assert m3['test'] == i8(0)
mut m4 := map[string]i16{}
mut m4 := map[string]i16
m4['test'] = i16(0)
assert m4['test'] == i16(0)
mut m5 := map[string]i32{}
mut m5 := map[string]i32
m5['test'] = i32(0)
assert m5['test'] == i32(0)
mut m6 := map[string]u8{}
mut m6 := map[string]u8
m6['test'] = u8(0)
assert m6['test'] == u8(0)
mut m7 := map[string]u16{}
mut m7 := map[string]u16
m7['test'] = u16(0)
assert m7['test'] == u16(0)
mut m8 := map[string]u32{}
mut m8 := map[string]u32
m8['test'] = u32(0)
assert m8['test'] == u32(0)
mut m9 := map[string]bool{}
mut m9 := map[string]bool
m9['test'] = true
assert m9['test'] == true
mut m10 := map[string]byte{}
mut m10 := map[string]byte
m10['test'] = byte(0)
assert m10['test'] == byte(0)
mut m11 := map[string]f32{}
mut m11 := map[string]f32
m11['test'] = f32(0.0)
assert m11['test'] == f32(0.0)
mut m12 := map[string]f64{}
mut m12 := map[string]f64
m12['test'] = f64(0.0)
assert m12['test'] == f64(0.0)
mut m13 := map[string]rune{}
mut m13 := map[string]rune
m13['test'] = rune(0)
assert m13['test'] == rune(0)
mut m14 := map[string]voidptr{}
m14['test'] = voidptr(0)
assert m14['test'] == voidptr(0)
//mut m14 := map[string]voidptr
//m14['test'] = voidptr(0)
//assert m14['test'] == voidptr(0)
mut m15 := map[string]byteptr{}
m15['test'] = byteptr(0)
assert m15['test'] == byteptr(0)
//mut m15 := map[string]byteptr
//m15['test'] = byteptr(0)
//assert m15['test'] == byteptr(0)
mut m16 := map[string]i64{}
mut m16 := map[string]i64
m16['test'] = i64(0)
assert m16['test'] == i64(0)
mut m17 := map[string]u64{}
mut m17 := map[string]u64
m17['test'] = u64(0)
assert m17['test'] == u64(0)
}
fn test_string_arr() {
mut m := map[string][]string
m['a'] = ['one', 'two']
assert m['a'].len == 2
assert m['a'][0] == 'one'
assert m['a'][1] == 'two'
}