From c14c81ace68375fe535920d1715af55b75b8081c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 7 Mar 2020 04:45:35 +0100 Subject: [PATCH] cgen: fix -> --- vlib/v/ast/ast.v | 26 ++++++++++++++------------ vlib/v/checker/checker.v | 5 +++-- vlib/v/gen/cgen.v | 9 ++++++++- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 2660eab63a..1a78c776f7 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -8,15 +8,15 @@ import ( v.table ) -pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | -FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | -AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | -CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | +pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | +FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | +AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | +CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | ConcatExpr | Type | AsCast -pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | -ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | -HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | +pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | +ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | +HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | LineComment | MultiLineComment | AssertStmt | UnsafeStmt // pub type Type = StructType | ArrayType // pub struct StructType { @@ -65,9 +65,11 @@ pub: // `foo.bar` pub struct SelectorExpr { pub: - pos token.Position - expr Expr - field string + pos token.Position + expr Expr + field string +mut: + expr_type table.Type } // module declaration @@ -279,7 +281,7 @@ pub: pos token.Position left Expr right Expr - mut: +mut: left_type table.Type right_type table.Type } @@ -413,7 +415,7 @@ pub: pub struct AsCast { pub: expr Expr - typ table.Type + typ table.Type } // e.g. `[unsafe_fn]` diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1feb19a7d6..4b57abd07b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -296,8 +296,9 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr) 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) + selector_expr.expr_type = typ typ_sym := c.table.get_type_symbol(typ) field_name := selector_expr.field 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) } ast.SelectorExpr { - return c.selector_expr(it) + return c.selector_expr(mut it) } ast.SizeOf { return table.int_type diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 5daf7de4d7..428aca081a 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -310,6 +310,7 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) { if it.is_method { styp := g.typ(it.receiver.typ) g.write('$styp $it.receiver.name') + // TODO mut g.definitions.write('$styp $it.receiver.name') if it.args.len > 0 { g.write(', ') @@ -347,6 +348,7 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) { g.definitions.writeln(');') } for stmt in it.stmts { + // g.write('\t') g.stmt(stmt) } if is_main { @@ -609,7 +611,12 @@ fn (g mut Gen) expr(node ast.Expr) { } ast.SelectorExpr { g.expr(it.expr) - g.write('.') + if table.type_nr_muls(it.expr_type) > 0 { + g.write('->') + } + else { + g.write('.') + } g.write(it.field) } ast.Type {