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

cgen: fix ->

This commit is contained in:
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

@ -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]`

View File

@ -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

View File

@ -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 {