v2: make index_expr return array array element type

pull/3721/head
joe-conigliaro 2020-02-11 22:59:40 +11:00 committed by GitHub
parent 9610821884
commit d25a0e30dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 21 deletions

View File

@ -31,7 +31,12 @@ int main() {
array_string g = new_array_from_c_array(2, 2, sizeof(array_string), { array_string g = new_array_from_c_array(2, 2, sizeof(array_string), {
testb(1), tos3("hello"), testb(1), tos3("hello"),
}); });
return 0; array_Foo arr_foo = new_array_from_c_array(1, 1, sizeof(array_Foo), {
a,
});
Foo af_idx_el = arr_foo[0];
string foo_a = af_idx_el.a;
return 0;
} }
multi_return_int_string mr_test() { multi_return_int_string mr_test() {

View File

@ -32,6 +32,13 @@ fn main() {
mut g := [testb(1),'hello'] mut g := [testb(1),'hello']
m1, m2 := mr_test() m1, m2 := mr_test()
//mut arr_foo := []Foo
arr_foo := [a]
//arr_foo << a // TODO
af_idx_el := arr_foo[0]
foo_a := af_idx_el.a
} }
fn mr_test() (int, string) { fn mr_test() (int, string) {

View File

@ -635,7 +635,10 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
node,typ = p.dot_expr(node, typ) node,typ = p.dot_expr(node, typ)
} }
else if p.tok.kind == .lsbr { else if p.tok.kind == .lsbr {
node = p.index_expr(node) // , typ) //node = p.index_expr(node) // , typ)
ie_node,ie_typ := p.index_expr(node, typ)
node = ie_node
typ = ie_typ
} }
else if p.tok.kind == .key_as { else if p.tok.kind == .key_as {
p.next() p.next()
@ -672,7 +675,7 @@ fn (p mut Parser) prefix_expr() (ast.Expr,table.Type) {
return expr,typ return expr,typ
} }
fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr { fn (p mut Parser) index_expr(left ast.Expr, left_type table.Type) (ast.IndexExpr,table.Type) {
// left == `a` in `a[0]` // left == `a` in `a[0]`
p.next() // [ p.next() // [
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {
@ -687,7 +690,7 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
low: ast.Expr{} low: ast.Expr{}
high: high high: high
} }
} },left_type // TODO: return correct type
} }
expr,_ := p.expr(0) // `[expr]` or `[expr..]` expr,_ := p.expr(0) // `[expr]` or `[expr..]`
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {
@ -705,7 +708,14 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
low: expr low: expr
high: high high: high
} }
} },left_type // TODO: return correct type
}
// get the element type
mut typ := left_type
left_type_sym := p.table.get_type_symbol(left_type)
if left_type_sym.kind == .array {
info := left_type_sym.info as table.Array
typ = info.elem_type
} }
// [expr] // [expr]
p.check(.rsbr) p.check(.rsbr)
@ -713,7 +723,7 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
left: left left: left
index: expr index: expr
pos: p.tok.position() pos: p.tok.position()
} },typ
} }
fn (p mut Parser) filter(typ table.Type) { fn (p mut Parser) filter(typ table.Type) {
@ -858,8 +868,8 @@ fn (p mut Parser) for_statement() ast.Stmt {
} }
} }
// `for i in vals`, `for i in start .. end` // `for i in vals`, `for i in start .. end`
else if p.peek_tok.kind == .key_in || p.peek_tok.kind == .comma { else if p.peek_tok.kind in [.key_in, .comma] {
var_name := p.check_name() var_name := p.check_name()
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.check(.comma)
val_name := p.check_name() val_name := p.check_name()
@ -873,20 +883,25 @@ fn (p mut Parser) for_statement() ast.Stmt {
// arr_expr // arr_expr
_,arr_typ := p.expr(0) _,arr_typ := p.expr(0)
// array / map // array / map
arr_typ_sym := p.table.get_type_symbol(arr_typ) if table.type_idx(arr_typ) == table.string_type_idx {
match arr_typ_sym.info { elem_type = table.byte_type
table.Array { }
elem_type = it.elem_type else {
arr_typ_sym := p.table.get_type_symbol(arr_typ)
match arr_typ_sym.info {
table.Array {
elem_type = it.elem_type
}
table.Map {
elem_type = it.value_type
}
else {
println(1)
// elem_type_sym := p.table.get_type_symbol(elem_type)
// p.error('cannot loop over type: $elem_type_sym.name')
}
} }
table.Map { }
elem_type = it.value_type
}
else {
println(1)
// elem_type_sym := p.table.get_type_symbol(elem_type)
// p.error('cannot loop over type: $elem_type_sym.name')
}
}
// 0 .. 10 // 0 .. 10
// start := p.tok.lit.int() // start := p.tok.lit.int()
if p.tok.kind == .dotdot { if p.tok.kind == .dotdot {