v2: make index_expr return array array element type
parent
9610821884
commit
d25a0e30dc
|
@ -31,6 +31,11 @@ int main() {
|
|||
array_string g = new_array_from_c_array(2, 2, sizeof(array_string), {
|
||||
testb(1), tos3("hello"),
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,13 @@ fn main() {
|
|||
mut g := [testb(1),'hello']
|
||||
|
||||
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) {
|
||||
|
|
|
@ -635,7 +635,10 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
|||
node,typ = p.dot_expr(node, typ)
|
||||
}
|
||||
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 {
|
||||
p.next()
|
||||
|
@ -672,7 +675,7 @@ fn (p mut Parser) prefix_expr() (ast.Expr,table.Type) {
|
|||
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]`
|
||||
p.next() // [
|
||||
if p.tok.kind == .dotdot {
|
||||
|
@ -687,7 +690,7 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
|
|||
low: ast.Expr{}
|
||||
high: high
|
||||
}
|
||||
}
|
||||
},left_type // TODO: return correct type
|
||||
}
|
||||
expr,_ := p.expr(0) // `[expr]` or `[expr..]`
|
||||
if p.tok.kind == .dotdot {
|
||||
|
@ -705,7 +708,14 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
|
|||
low: expr
|
||||
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]
|
||||
p.check(.rsbr)
|
||||
|
@ -713,7 +723,7 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
|
|||
left: left
|
||||
index: expr
|
||||
pos: p.tok.position()
|
||||
}
|
||||
},typ
|
||||
}
|
||||
|
||||
fn (p mut Parser) filter(typ table.Type) {
|
||||
|
@ -858,7 +868,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||
}
|
||||
}
|
||||
// `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()
|
||||
if p.tok.kind == .comma {
|
||||
p.check(.comma)
|
||||
|
@ -873,6 +883,10 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||
// arr_expr
|
||||
_,arr_typ := p.expr(0)
|
||||
// array / map
|
||||
if table.type_idx(arr_typ) == table.string_type_idx {
|
||||
elem_type = table.byte_type
|
||||
}
|
||||
else {
|
||||
arr_typ_sym := p.table.get_type_symbol(arr_typ)
|
||||
match arr_typ_sym.info {
|
||||
table.Array {
|
||||
|
@ -887,6 +901,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||
// p.error('cannot loop over type: $elem_type_sym.name')
|
||||
}
|
||||
}
|
||||
}
|
||||
// 0 .. 10
|
||||
// start := p.tok.lit.int()
|
||||
if p.tok.kind == .dotdot {
|
||||
|
|
Loading…
Reference in New Issue