cgen: fix ->

pull/3951/head
Alexander Medvednikov 2020-03-07 04:45:35 +01:00
parent f5a8d883d2
commit c14c81ace6
3 changed files with 25 additions and 15 deletions

View File

@ -65,9 +65,11 @@ pub:
// `foo.bar` // `foo.bar`
pub struct SelectorExpr { pub struct SelectorExpr {
pub: pub:
pos token.Position pos token.Position
expr Expr expr Expr
field string field string
mut:
expr_type table.Type
} }
// module declaration // module declaration
@ -279,7 +281,7 @@ pub:
pos token.Position pos token.Position
left Expr left Expr
right Expr right Expr
mut: mut:
left_type table.Type left_type table.Type
right_type table.Type right_type table.Type
} }
@ -413,7 +415,7 @@ pub:
pub struct AsCast { pub struct AsCast {
pub: pub:
expr Expr expr Expr
typ table.Type typ table.Type
} }
// e.g. `[unsafe_fn]` // e.g. `[unsafe_fn]`

View File

@ -296,8 +296,9 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
return table.void_type return table.void_type
} }
pub fn (c mut Checker) selector_expr(selector_expr 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
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) {
@ -598,7 +599,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
return c.expr(it.expr) return c.expr(it.expr)
} }
ast.SelectorExpr { ast.SelectorExpr {
return c.selector_expr(it) return c.selector_expr(mut it)
} }
ast.SizeOf { ast.SizeOf {
return table.int_type return table.int_type

View File

@ -310,6 +310,7 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
if it.is_method { if it.is_method {
styp := g.typ(it.receiver.typ) styp := g.typ(it.receiver.typ)
g.write('$styp $it.receiver.name') g.write('$styp $it.receiver.name')
// TODO mut
g.definitions.write('$styp $it.receiver.name') g.definitions.write('$styp $it.receiver.name')
if it.args.len > 0 { if it.args.len > 0 {
g.write(', ') g.write(', ')
@ -347,6 +348,7 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
g.definitions.writeln(');') g.definitions.writeln(');')
} }
for stmt in it.stmts { for stmt in it.stmts {
// g.write('\t')
g.stmt(stmt) g.stmt(stmt)
} }
if is_main { if is_main {
@ -609,7 +611,12 @@ fn (g mut Gen) expr(node ast.Expr) {
} }
ast.SelectorExpr { ast.SelectorExpr {
g.expr(it.expr) g.expr(it.expr)
g.write('.') if table.type_nr_muls(it.expr_type) > 0 {
g.write('->')
}
else {
g.write('.')
}
g.write(it.field) g.write(it.field)
} }
ast.Type { ast.Type {