From 5481f226ddcfb09c21def22f298b01bc86ff101a Mon Sep 17 00:00:00 2001 From: Vinicius Rangel Date: Thu, 17 Oct 2019 00:52:58 -0300 Subject: [PATCH] parser: Infinite multi-dimensional array --- vlib/compiler/comptime.v | 5 ++++- vlib/compiler/parser.v | 31 ++++++++----------------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/vlib/compiler/comptime.v b/vlib/compiler/comptime.v index 496667a28c..49e6981a8e 100644 --- a/vlib/compiler/comptime.v +++ b/vlib/compiler/comptime.v @@ -273,7 +273,10 @@ fn (p mut Parser) gen_array_str(typ Type) { }) elm_type := typ.name.right(6) 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.error('cant print ${elm_type}[], unhandled print of ${elm_type}') } diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 82d3bab25e..3a8fbd5cff 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -987,35 +987,22 @@ fn (p mut Parser) get_type() string { return f.typ_str() } // arrays ([]int) - mut is_arr := false - mut is_arr2 := false// [][]int TODO remove this and allow unlimited levels of arrays + mut arr_level := 0 is_question := p.tok == .question if is_question { p.check(.question) } - if p.tok == .lsbr { + for p.tok == .lsbr { p.check(.lsbr) // [10]int if p.tok == .number { - typ = '[$p.lit]' + typ += '[$p.lit]' p.next() } else { - is_arr = true + arr_level++ } 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 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) } // Register an []array type - if is_arr2 { - typ = 'array_array_$typ' - p.register_array(typ) - } - else if is_arr { - typ = 'array_$typ' + if arr_level > 0 { // p.log('ARR TYPE="$typ" run=$p.pass') // We come across "[]User" etc ? + for i := 0; i < arr_level; i++ { + typ = 'array_$typ' + } p.register_array(typ) } p.next()