From c514f0b67237ced4f1e6d88044c433a8a6f070b6 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 18 Mar 2020 16:07:52 +0100 Subject: [PATCH] cgen: automatic dereference and match fix --- vlib/v/ast/ast.v | 18 +++++++++--------- vlib/v/checker/checker.v | 3 ++- vlib/v/gen/cgen.v | 13 +++++++++---- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 660312ba2d..f1378280e6 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -347,16 +347,16 @@ mut: pub struct MatchExpr { pub: - tok_kind token.Kind - cond Expr - branches []MatchBranch - pos token.Position + tok_kind token.Kind + cond Expr + branches []MatchBranch + pos token.Position mut: - is_expr bool // returns a value - return_type table.Type - cond_type table.Type // type of `x` in `match x {` - // expected_type table.Type // for debugging only - is_sum_type bool + is_expr bool // returns a value + return_type table.Type + cond_type table.Type // type of `x` in `match x {` + expected_type table.Type // for debugging only + is_sum_type bool } pub struct MatchBranch { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 215ef45a0b..99576fd095 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -456,6 +456,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) { }) } } + c.expected_type = table.void_type } pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type { @@ -839,7 +840,7 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type { pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type { node.is_expr = c.expected_type != table.void_type - // node.expected_type = c.expected_type + node.expected_type = c.expected_type cond_type := c.expr(node.cond) if cond_type == 0 { c.error('match 0 cond type', node.pos) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index d7ec38f5a8..57898c3188 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -286,7 +286,7 @@ fn (g mut Gen) stmt(node ast.Stmt) { g.stmts(it.stmts) g.writeln('}') } - //TODO: + // TODO: else {} } ast.ForStmt { @@ -1054,11 +1054,12 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) { g.writeln(') {') } } - if node.is_sum_type && branch.exprs.len > 0 { + // g.writeln('/* M sum_type=$node.is_sum_type is_expr=$node.is_expr exp_type=${g.typ(node.expected_type)}*/') + if node.is_sum_type && branch.exprs.len > 0 && !node.is_expr { // The first node in expr is an ast.Type // Use it to generate `it` variable. - fe := branch.exprs[0] - match fe { + first_expr := branch.exprs[0] + match first_expr { ast.Type { it_type := g.typ(it.typ) // g.writeln('$it_type* it = ($it_type*)${tmp}.obj; // ST it') @@ -1302,6 +1303,10 @@ fn (g mut Gen) return_statement(it ast.Return) { } // g.write('/*OPTIONAL*/') } + if !table.type_is_ptr(g.fn_decl.return_type) && table.type_is_ptr(it.types[0]) { + // Automatic Dereference + g.write('*') + } g.expr_with_cast(it.types[0], g.fn_decl.return_type, it.exprs[0]) } g.writeln(';')