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

array: replace make() with the new init syntax

This commit is contained in:
Alexander Medvednikov 2020-04-26 17:52:27 +02:00
parent f23948010a
commit 83552a0d58
6 changed files with 24 additions and 27 deletions

View File

@ -26,18 +26,6 @@ fn __new_array(mylen int, cap int, elm_size int) array {
return arr 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]`) // Private function, used by V (`nums := [1, 2, 3]`)
fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array { fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
cap_ := if cap == 0 { 1 } else { cap } cap_ := if cap == 0 { 1 } else { cap }

View File

@ -551,6 +551,7 @@ fn test_array_str() {
numbers := [1, 2, 3] numbers := [1, 2, 3]
assert numbers == [1,2,3] assert numbers == [1,2,3]
numbers2 := [numbers, [4, 5, 6]] // dup str() bug numbers2 := [numbers, [4, 5, 6]] // dup str() bug
_=numbers2
assert true assert true
assert numbers.str() == '[1, 2, 3]' assert numbers.str() == '[1, 2, 3]'
// QTODO // QTODO
@ -727,4 +728,7 @@ fn test_array_with_cap() {
a4 := []int{cap:10, len:1 } a4 := []int{cap:10, len:1 }
assert a4.len == 1 assert a4.len == 1
assert a4.cap == 10 assert a4.cap == 10
a5 := []int{len:1, cap:10}
assert a5.len == 1
assert a5.cap == 10
} }

View File

@ -11,8 +11,7 @@ const (
// NOTE: temp until we have []bytes(buff) // NOTE: temp until we have []bytes(buff)
fn c_array_to_bytes_tmp(len int, buffer voidptr) []byte { fn c_array_to_bytes_tmp(len int, buffer voidptr) []byte {
mut arr := []byte{} mut arr := []byte{len:len, cap:1}
arr = make(len, 1, 1)
arr.data = buffer arr.data = buffer
/* /*

View File

@ -13,7 +13,8 @@ pub mut:
pub fn new_builder(initial_size int) Builder { pub fn new_builder(initial_size int) Builder {
return Builder{ return Builder{
buf: make(0, initial_size, 1) //buf: make(0, initial_size)
buf: []byte{cap: initial_size}
initial_size: initial_size initial_size: initial_size
} }
} }
@ -86,7 +87,9 @@ pub fn (b mut Builder) free() {
unsafe{ unsafe{
free(b.buf.data) 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 b.len = 0
} }

View File

@ -361,15 +361,9 @@ pub fn (mut c Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
if left.kind == .array { if left.kind == .array {
// `array << elm` // `array << elm`
match infix_expr.left { match infix_expr.left {
ast.Ident { ast.Ident {}
ast.SelectorExpr {}
} else { println('typeof: ${typeof(infix_expr.left)}') }
ast.SelectorExpr {
}
else {
println('typeof: ${typeof(infix_expr.left)}')
}
} }
// the expressions have different types (array_x and x) // the expressions have different types (array_x and x)
if c.table.check(c.table.value_type(left_type), right_type) { 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 = [] // a = []
if array_init.exprs.len == 0 { 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) type_sym := c.table.get_type_symbol(c.expected_type)
if type_sym.kind != .array { if type_sym.kind != .array {
c.error('array_init: no type specified (maybe: `[]Type` instead of `[]`)', array_init.pos) c.error('array_init: no type specified (maybe: `[]Type` instead of `[]`)', array_init.pos)

View File

@ -314,8 +314,7 @@ pub enum Precedence {
} }
pub fn build_precedences() []Precedence { pub fn build_precedences() []Precedence {
mut p := []Precedence{} mut p := []Precedence{len:100, cap:100}
p = make(100, 100, sizeof(Precedence))
p[Kind.assign] = .assign p[Kind.assign] = .assign
p[Kind.eq] = .eq p[Kind.eq] = .eq
p[Kind.ne] = .eq p[Kind.ne] = .eq