ast: MatchExpr: expr_type => cond_type

pull/4055/head
Alexander Medvednikov 2020-03-18 12:23:32 +01:00
parent 256a93e6e6
commit af289da844
3 changed files with 11 additions and 10 deletions

View File

@ -354,7 +354,7 @@ pub:
mut: mut:
is_expr bool // returns a value is_expr bool // returns a value
return_type table.Type return_type table.Type
expr_type table.Type // type of `x` in `match x {` cond_type table.Type // type of `x` in `match x {`
is_sum_type bool is_sum_type bool
} }

View File

@ -790,15 +790,15 @@ 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 { pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
node.is_expr = c.expected_type != table.void_type node.is_expr = c.expected_type != table.void_type
expr_type := c.expr(node.cond) cond_type := c.expr(node.cond)
if expr_type == 0 { if cond_type == 0 {
c.error('match 0 expr type', node.pos) c.error('match 0 cond type', node.pos)
} }
c.expected_type = expr_type c.expected_type = cond_type
mut ret_type := table.void_type mut ret_type := table.void_type
for branch in node.branches { for branch in node.branches {
for expr in branch.exprs { for expr in branch.exprs {
c.expected_type = expr_type c.expected_type = cond_type
typ := c.expr(expr) typ := c.expr(expr)
typ_sym := c.table.get_type_symbol(typ) typ_sym := c.table.get_type_symbol(typ)
// TODO: // TODO:
@ -826,7 +826,7 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
// node.expected_type = c.expected_type // node.expected_type = c.expected_type
// } // }
node.return_type = ret_type node.return_type = ret_type
node.expr_type = expr_type node.cond_type = cond_type
// println('!m $expr_type') // println('!m $expr_type')
return ret_type return ret_type
} }

View File

@ -970,7 +970,7 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) {
fn (g mut Gen) match_expr(node ast.MatchExpr) { fn (g mut Gen) match_expr(node ast.MatchExpr) {
// println('match expr typ=$it.expr_type') // println('match expr typ=$it.expr_type')
// TODO // TODO
if node.expr_type == 0 { if node.cond_type == 0 {
g.writeln('// match 0') g.writeln('// match 0')
return return
} }
@ -979,7 +979,7 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
g.inside_ternary = true g.inside_ternary = true
// g.write('/* EM ret type=${g.typ(node.return_type)} */') // g.write('/* EM ret type=${g.typ(node.return_type)} */')
} }
type_sym := g.table.get_type_symbol(node.expr_type) type_sym := g.table.get_type_symbol(node.cond_type)
mut tmp := '' mut tmp := ''
if type_sym.kind != .void { if type_sym.kind != .void {
tmp = g.new_tmp_var() tmp = g.new_tmp_var()
@ -1278,7 +1278,8 @@ fn (g mut Gen) return_statement(it ast.Return) {
if table.type_is_optional(g.fn_decl.return_type) { if table.type_is_optional(g.fn_decl.return_type) {
mut is_none := false mut is_none := false
mut is_error := false mut is_error := false
match it.exprs[0] { expr0 := it.exprs[0]
match expr0 {
ast.None { ast.None {
is_none = true is_none = true
} }