cgen: fix ptr indexing

pull/3988/head
Alexander Medvednikov 2020-03-11 05:25:11 +01:00
parent 9e14850bbf
commit 75db3e4416
4 changed files with 12 additions and 3 deletions

View File

@ -328,6 +328,7 @@ fn (m map) exists(key string) bool {
pub fn (m &map) keys() []string { pub fn (m &map) keys() []string {
mut keys := [''].repeat(m.size) mut keys := [''].repeat(m.size)
//mut keys := []string{len: m.size}
if m.value_bytes == 0 { if m.value_bytes == 0 {
return keys return keys
} }

View File

@ -311,7 +311,9 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
pub fn (c mut Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.Type { pub fn (c mut Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.Type {
typ := c.expr(selector_expr.expr) typ := c.expr(selector_expr.expr)
selector_expr.expr_type = typ selector_expr.expr_type = typ
// if selector_expr.field == 'size' {
// println('sel expr line_nr=$selector_expr.pos.line_nr typ=$selector_expr.expr_type') // println('sel expr line_nr=$selector_expr.pos.line_nr typ=$selector_expr.expr_type')
// }
typ_sym := c.table.get_type_symbol(typ) typ_sym := c.table.get_type_symbol(typ)
field_name := selector_expr.field field_name := selector_expr.field
if field := typ_sym.find_field(field_name) { if field := typ_sym.find_field(field_name) {
@ -880,6 +882,11 @@ pub fn (c mut Checker) index_expr(node mut ast.IndexExpr) table.Type {
} }
return table.byte_type return table.byte_type
} }
else if table.type_is_ptr(typ) {
// byte* => byte
// bytes[0] is a byte, not byte*
return table.type_deref(typ)
}
// else { // else {
// return table.int_type // return table.int_type
// } // }

View File

@ -590,11 +590,9 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('/* guard */') g.write('/* guard */')
} }
ast.IndexExpr { ast.IndexExpr {
//
g.index_expr(it) g.index_expr(it)
} }
ast.InfixExpr { ast.InfixExpr {
// sdf
g.infix_expr(it) g.infix_expr(it)
} }
ast.IntegerLiteral { ast.IntegerLiteral {
@ -763,8 +761,12 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('->') g.write('->')
} }
else { else {
// g.write('. /*typ= $it.expr_type */') // ${g.typ(it.expr_type)} /')
g.write('.') g.write('.')
} }
if it.expr_type == 0 {
verror('cgen: SelectorExpr typ=0 field=$it.field')
}
g.write(it.field) g.write(it.field)
} }
ast.Type { ast.Type {

View File

@ -82,7 +82,6 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
p.next() p.next()
rec_name = p.check_name() rec_name = p.check_name()
if p.tok.kind == .key_mut { if p.tok.kind == .key_mut {
// p.next()
rec_mut = true rec_mut = true
} }
rec_type = p.parse_type() rec_type = p.parse_type()