From 83552a0d5834f412e06c19ef4025cb7c7b9619c1 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 26 Apr 2020 17:52:27 +0200 Subject: [PATCH] array: replace make() with the new init syntax --- vlib/builtin/array.v | 12 ------------ vlib/builtin/array_test.v | 4 ++++ vlib/crypto/rand/rand.v | 3 +-- vlib/strings/builder.c.v | 7 +++++-- vlib/v/checker/checker.v | 22 +++++++++++++--------- vlib/v/token/token.v | 3 +-- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index a3f654ba3c..e18828b710 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -26,18 +26,6 @@ fn __new_array(mylen int, cap int, elm_size int) array { return arr } -// TODO -pub fn make(len int, cap int, elm_size int) array { - return __new_array(len, cap, elm_size) -} - -/* -struct Foo { - a []string - b [][]string -} -*/ - // Private function, used by V (`nums := [1, 2, 3]`) fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array { cap_ := if cap == 0 { 1 } else { cap } diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index c5ee360db7..ad7ae7f627 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -551,6 +551,7 @@ fn test_array_str() { numbers := [1, 2, 3] assert numbers == [1,2,3] numbers2 := [numbers, [4, 5, 6]] // dup str() bug + _=numbers2 assert true assert numbers.str() == '[1, 2, 3]' // QTODO @@ -727,4 +728,7 @@ fn test_array_with_cap() { a4 := []int{cap:10, len:1 } assert a4.len == 1 assert a4.cap == 10 + a5 := []int{len:1, cap:10} + assert a5.len == 1 + assert a5.cap == 10 } diff --git a/vlib/crypto/rand/rand.v b/vlib/crypto/rand/rand.v index c3bc04aa27..4c5fef5ecc 100644 --- a/vlib/crypto/rand/rand.v +++ b/vlib/crypto/rand/rand.v @@ -11,8 +11,7 @@ const ( // NOTE: temp until we have []bytes(buff) fn c_array_to_bytes_tmp(len int, buffer voidptr) []byte { - mut arr := []byte{} - arr = make(len, 1, 1) + mut arr := []byte{len:len, cap:1} arr.data = buffer /* diff --git a/vlib/strings/builder.c.v b/vlib/strings/builder.c.v index 22751c4067..e6d0563377 100644 --- a/vlib/strings/builder.c.v +++ b/vlib/strings/builder.c.v @@ -13,7 +13,8 @@ pub mut: pub fn new_builder(initial_size int) Builder { return Builder{ - buf: make(0, initial_size, 1) + //buf: make(0, initial_size) + buf: []byte{cap: initial_size} initial_size: initial_size } } @@ -86,7 +87,9 @@ pub fn (b mut Builder) free() { unsafe{ free(b.buf.data) } - b.buf = make(0, b.initial_size, 1) + // QTODO checker bug + s := b.initial_size + b.buf = []byte{cap: s} b.len = 0 } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 40e3add85b..0ac65ba75e 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -361,15 +361,9 @@ pub fn (mut c Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type { if left.kind == .array { // `array << elm` match infix_expr.left { - ast.Ident { - - } - ast.SelectorExpr { - - } - else { - println('typeof: ${typeof(infix_expr.left)}') - } + ast.Ident {} + ast.SelectorExpr {} + else { println('typeof: ${typeof(infix_expr.left)}') } } // the expressions have different types (array_x and x) if c.table.check(c.table.value_type(left_type), right_type) { @@ -1047,6 +1041,16 @@ pub fn (mut c Checker) array_init(array_init mut ast.ArrayInit) table.Type { } // a = [] if array_init.exprs.len == 0 { + if array_init.has_cap { + if c.expr(array_init.cap_expr) != table.int_type { + c.error('array cap needs to be an int', array_init.pos) + } + } + if array_init.has_len { + if c.expr(array_init.len_expr) != table.int_type { + c.error('array len needs to be an int', array_init.pos) + } + } type_sym := c.table.get_type_symbol(c.expected_type) if type_sym.kind != .array { c.error('array_init: no type specified (maybe: `[]Type` instead of `[]`)', array_init.pos) diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 0d1aa128fe..f688064bfc 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -314,8 +314,7 @@ pub enum Precedence { } pub fn build_precedences() []Precedence { - mut p := []Precedence{} - p = make(100, 100, sizeof(Precedence)) + mut p := []Precedence{len:100, cap:100} p[Kind.assign] = .assign p[Kind.eq] = .eq p[Kind.ne] = .eq