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

parser: Infinite multi-dimensional array

This commit is contained in:
Vinicius Rangel 2019-10-17 00:52:58 -03:00 committed by Alexander Medvednikov
parent 5faa7e7861
commit 5481f226dd
2 changed files with 12 additions and 24 deletions

View File

@ -273,7 +273,10 @@ fn (p mut Parser) gen_array_str(typ Type) {
}) })
elm_type := typ.name.right(6) elm_type := typ.name.right(6)
elm_type2 := p.table.find_type(elm_type) elm_type2 := p.table.find_type(elm_type)
if p.typ_to_fmt(elm_type, 0) == '' && is_array := elm_type.starts_with('array_')
if is_array {
p.gen_array_str(elm_type2)
} else if p.typ_to_fmt(elm_type, 0) == '' &&
!p.table.type_has_method(elm_type2, 'str') { !p.table.type_has_method(elm_type2, 'str') {
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}') p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
} }

View File

@ -987,35 +987,22 @@ fn (p mut Parser) get_type() string {
return f.typ_str() return f.typ_str()
} }
// arrays ([]int) // arrays ([]int)
mut is_arr := false mut arr_level := 0
mut is_arr2 := false// [][]int TODO remove this and allow unlimited levels of arrays
is_question := p.tok == .question is_question := p.tok == .question
if is_question { if is_question {
p.check(.question) p.check(.question)
} }
if p.tok == .lsbr { for p.tok == .lsbr {
p.check(.lsbr) p.check(.lsbr)
// [10]int // [10]int
if p.tok == .number { if p.tok == .number {
typ = '[$p.lit]' typ += '[$p.lit]'
p.next() p.next()
} }
else { else {
is_arr = true arr_level++
} }
p.check(.rsbr) p.check(.rsbr)
// [10][3]int
if p.tok == .lsbr {
p.next()
if p.tok == .number {
typ += '[$p.lit]'
p.check(.number)
}
else {
is_arr2 = true
}
p.check(.rsbr)
}
} }
// map[string]int // map[string]int
if !p.builtin_mod && p.tok == .name && p.lit == 'map' { if !p.builtin_mod && p.tok == .name && p.lit == 'map' {
@ -1101,14 +1088,12 @@ fn (p mut Parser) get_type() string {
typ += strings.repeat(`*`, nr_muls) typ += strings.repeat(`*`, nr_muls)
} }
// Register an []array type // Register an []array type
if is_arr2 { if arr_level > 0 {
typ = 'array_array_$typ'
p.register_array(typ)
}
else if is_arr {
typ = 'array_$typ'
// p.log('ARR TYPE="$typ" run=$p.pass') // p.log('ARR TYPE="$typ" run=$p.pass')
// We come across "[]User" etc ? // We come across "[]User" etc ?
for i := 0; i < arr_level; i++ {
typ = 'array_$typ'
}
p.register_array(typ) p.register_array(typ)
} }
p.next() p.next()