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

v2: more array init & index expr fixes

This commit is contained in:
joe-conigliaro 2020-02-20 00:34:44 +11:00
parent 478c48cd6f
commit e4179c0008
2 changed files with 27 additions and 25 deletions

View File

@ -225,12 +225,15 @@ pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type {
c.error('expected array element with type `$elem_type_sym.name`', array_init.pos) c.error('expected array element with type `$elem_type_sym.name`', array_init.pos)
} }
} }
// idx := if is_fixed { p.table.find_or_register_array_fixed(val_type, fixed_size, 1) } else { p.table.find_or_register_array(val_type, 1) } // only inits if know types like []string set the type in parser
is_fixed := false // as the rest could be result of expression, so do it here
fixed_size := 1 if array_init.typ == 0 {
idx := if is_fixed { c.table.find_or_register_array_fixed(elem_type, fixed_size, 1) } else { c.table.find_or_register_array(elem_type, 1) } is_fixed := false
array_type := table.new_type(idx) fixed_size := 1
array_init.typ = array_type idx := if is_fixed { c.table.find_or_register_array_fixed(elem_type, fixed_size, 1) } else { c.table.find_or_register_array(elem_type, 1) }
array_type := table.new_type(idx)
array_init.typ = array_type
}
return array_init.typ return array_init.typ
} }
@ -536,7 +539,7 @@ pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
typ = info.elem_type typ = info.elem_type
} }
*/ */
mut typ := c.expr(node.left) typ := c.expr(node.left)
mut is_range := false // TODO is_range := node.index is ast.RangeExpr mut is_range := false // TODO is_range := node.index is ast.RangeExpr
match node.index { match node.index {
ast.RangeExpr { ast.RangeExpr {
@ -544,20 +547,19 @@ pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
} }
else {} else {}
} }
typ_sym := c.table.get_type_symbol(typ)
if !is_range { if !is_range {
index_type := c.expr(node.index) index_type := c.expr(node.index)
// if index_type.typ.kind != .int {
if table.type_idx(index_type) != table.int_type_idx { if table.type_idx(index_type) != table.int_type_idx {
index_type_sym := c.table.get_type_symbol(index_type) index_type_sym := c.table.get_type_symbol(index_type)
c.error('non-integer index (type `$index_type_sym.name`)', node.pos) c.error('non-integer index (type `$index_type_sym.name`)', node.pos)
} }
typ_sym := c.table.get_type_symbol(typ)
if typ_sym.kind == .array { if typ_sym.kind == .array {
// Check index type // Check index type
info := typ_sym.info as table.Array info := typ_sym.info as table.Array
return info.elem_type return info.elem_type
} }
if typ_sym.kind == .array_fixed { else if typ_sym.kind == .array_fixed {
info := typ_sym.info as table.ArrayFixed info := typ_sym.info as table.ArrayFixed
return info.elem_type return info.elem_type
} }
@ -565,9 +567,12 @@ pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
info := typ_sym.info as table.Map info := typ_sym.info as table.Map
return info.value_type return info.value_type
} }
else { else if typ_sym.kind in [.byteptr, .string] {
typ = table.int_type return table.byte_type
} }
//else {
// return table.int_type
//}
} }
return typ return typ
} }

View File

@ -634,7 +634,6 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
node = p.if_expr() node = p.if_expr()
} }
.lsbr { .lsbr {
// node,typ = p.array_init()
node = p.array_init() node = p.array_init()
} }
.key_none { .key_none {
@ -1105,9 +1104,7 @@ fn (p mut Parser) string_expr() (ast.Expr,table.Type) {
return node,table.string_type return node,table.string_type
} }
// fn (p mut Parser) array_init() (ast.Expr,table.Type) { fn (p mut Parser) array_init() ast.ArrayInit {
fn (p mut Parser) array_init() ast.Expr {
mut node := ast.Expr{}
p.check(.lsbr) p.check(.lsbr)
// `[]` - empty array with an automatically deduced type // `[]` - empty array with an automatically deduced type
// p.warn('array_init() exp=$p.expected_type') // p.warn('array_init() exp=$p.expected_type')
@ -1130,7 +1127,7 @@ fn (p mut Parser) array_init() ast.Expr {
} }
*/ */
mut val_type := table.void_type mut array_type := table.Type(0)
mut exprs := []ast.Expr mut exprs := []ast.Expr
// mut is_fixed := false // mut is_fixed := false
// mut fixed_size := 0 // mut fixed_size := 0
@ -1140,7 +1137,11 @@ fn (p mut Parser) array_init() ast.Expr {
p.check(.rsbr) p.check(.rsbr)
// []string // []string
if p.tok.kind == .name && p.tok.line_nr == line_nr { if p.tok.kind == .name && p.tok.line_nr == line_nr {
val_type = p.parse_type() val_type := p.parse_type()
// this is set here becasue its a known type, others could be the
// result of expr so we do those in checker
idx := p.table.find_or_register_array(val_type, 1)
array_type = table.new_type(idx)
} }
// [] // []
else { else {
@ -1151,11 +1152,8 @@ fn (p mut Parser) array_init() ast.Expr {
else { else {
// [1,2,3] // [1,2,3]
for i := 0; p.tok.kind != .rsbr; i++ { for i := 0; p.tok.kind != .rsbr; i++ {
expr,typ := p.expr(0) expr,_ := p.expr(0)
exprs << expr exprs << expr
if i == 0 {
val_type = typ
}
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.check(.comma)
} }
@ -1181,12 +1179,11 @@ fn (p mut Parser) array_init() ast.Expr {
} }
// idx := if is_fixed { p.table.find_or_register_array_fixed(val_type, fixed_size, 1) } else { p.table.find_or_register_array(val_type, 1) } // idx := if is_fixed { p.table.find_or_register_array_fixed(val_type, fixed_size, 1) } else { p.table.find_or_register_array(val_type, 1) }
// array_type := table.new_type(idx) // array_type := table.new_type(idx)
node = ast.ArrayInit{ return ast.ArrayInit{
// typ: array_type typ: array_type
exprs: exprs exprs: exprs
pos: p.tok.position() pos: p.tok.position()
} }
return node
} }
fn (p mut Parser) parse_number_literal() (ast.Expr,table.Type) { fn (p mut Parser) parse_number_literal() (ast.Expr,table.Type) {