array of pointers support

pull/3130/head
BigBlack 2019-12-18 08:26:04 +08:00 committed by Alexander Medvednikov
parent f00f9fbf5a
commit 67cf7f18e6
5 changed files with 27 additions and 15 deletions

View File

@ -3,6 +3,19 @@ const (
A = 8
)
fn test_pointer() {
mut arr := []*int
a := 1
b := 2
c := 3
arr << &a
arr << &b
arr << &c
assert *arr[0] == 1
arr[1] = &c
assert *arr[1] == 3
}
fn test_ints() {
mut a := [1, 5, 2, 3]
assert a.len == 4

View File

@ -416,7 +416,7 @@ fn (p mut Parser) expression() string {
// a << 7 => int tmp = 7; array_push(&a, &tmp);
// _PUSH(&a, expression(), tmp, string)
tmp := p.get_tmp()
tmp_typ := typ[6..]// skip "array_"
tmp_typ := typ[6..].replace('_ptr','*')// skip "array_"
p.check_space(.left_shift)
// Get the value we are pushing
p.gen(', (')

View File

@ -89,7 +89,7 @@ fn (p mut Parser) for_st() {
p.gen_for_varg_header(i, expr, typ, val)
}
else if is_arr {
typ = typ[6..]
typ = typ[6..].replace('_ptr','*')
p.gen_for_header(i, tmp, typ, val)
}
else if is_map {
@ -174,7 +174,7 @@ fn (p mut Parser) for_st() {
p.gen_for_range_header(i, range_end, tmp, typ, val)
}
else if is_arr {
typ = typ[6..]// all after `array_`
typ = typ[6..].replace('_ptr','*')// all after `array_`
p.gen_for_header(i, tmp, typ, val)
}
else if is_str {

View File

@ -676,11 +676,7 @@ fn (p mut Parser) gen_array_push(ph int, typ, expr_type, tmp, elm_type string) {
// Don't dereference if it's already a mutable array argument (`fn foo(mut []int)`)
push_call := if typ.contains('*'){'_PUSH('} else { '_PUSH(&'}
p.cgen.set_placeholder(ph, push_call)
if elm_type.ends_with('*') {
p.gen('), $tmp, ${elm_type[..elm_type.len - 1]})')
} else {
p.gen('), $tmp, $elm_type)')
}
p.gen('), $tmp, $elm_type)')
}
}

View File

@ -2090,7 +2090,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
}
if is_arr {
if is_arr0 {
typ = typ[6..]
typ = typ[6..].replace('_ptr', '*')
}
p.gen_array_at(typ, is_arr0, fn_ph)
}
@ -2203,6 +2203,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
// }
// `m[key]`. no =, just a getter
else if (is_map || is_arr || (is_str && !p.builtin_mod)) && is_indexer {
typ = typ.replace('_ptr','*')
p.index_get(typ, fn_ph, IndexConfig{
is_arr: is_arr
is_map: is_map
@ -2496,16 +2497,16 @@ fn (p mut Parser) array_init() string {
p.check(.rsbr)
// type after `]`? (e.g. "[]string")
exp_array := p.expected_type.starts_with('array_')
if p.tok != .name && p.tok != .lsbr && i == 0 && !exp_array {
p.error('specify array type: `[]typ` instead of `[]`')
}
if p.tok == .name && i == 0 &&
if p.tok != .name && p.tok != .mul && p.tok != .lsbr && i == 0 && !exp_array {
p.error('specify array type: `[]typ` instead of `[]`')
}
if i == 0 && (p.tok == .name || p.tok == .mul) &&
p.tokens[p.token_idx-2].line_nr == p.tokens[p.token_idx-1].line_nr { // TODO
// vals.len == 0 {
if exp_array {
p.error('no need to specify the full array type here, use `[]` instead of `[]${p.expected_type[6..]}`')
}
typ = p.get_type()
typ = p.get_type().replace('*','_ptr')
} else if exp_array && i == 0 {
// allow `known_array = []`
typ = p.expected_type[6..]
@ -2536,7 +2537,9 @@ fn (p mut Parser) array_init() string {
// if ptr {
// typ += '_ptr"
// }
p.gen_array_init(typ, no_alloc, new_arr_ph, i)
real := typ.replace('_ptr','*')
p.gen_array_init(real, no_alloc, new_arr_ph, i)
typ = 'array_$typ'
p.register_array(typ)
return typ