cgen: simplify match_expr_classic() (#10424)

pull/10444/head
yuyi 2021-06-13 11:27:31 +08:00 committed by GitHub
parent 5ee1ded3fb
commit a6eba7a9b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 59 deletions

View File

@ -4234,45 +4234,38 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str
if i > 0 { if i > 0 {
g.write(' || ') g.write(' || ')
} }
if type_sym.kind == .string { match type_sym.kind {
if expr is ast.StringLiteral && (expr as ast.StringLiteral).val == '' { .array {
g.write('${cond_var}.len == 0') ptr_typ := g.gen_array_equality_fn(node.cond_type)
} else { g.write('${ptr_typ}_arr_eq($cond_var, ')
g.write('string__eq(')
g.write(cond_var)
g.write(', ')
g.expr(expr) g.expr(expr)
g.write(')') g.write(')')
} }
} else if type_sym.kind == .array { .array_fixed {
ptr_typ := g.gen_array_equality_fn(node.cond_type)
g.write('${ptr_typ}_arr_eq(')
g.write(cond_var)
g.write(', ')
g.expr(expr)
g.write(')')
} else if type_sym.kind == .array_fixed {
ptr_typ := g.gen_fixed_array_equality_fn(node.cond_type) ptr_typ := g.gen_fixed_array_equality_fn(node.cond_type)
g.write('${ptr_typ}_arr_eq(') g.write('${ptr_typ}_arr_eq($cond_var, ')
g.write(cond_var)
g.write(', ')
g.expr(expr) g.expr(expr)
g.write(')') g.write(')')
} else if type_sym.kind == .map { }
.map {
ptr_typ := g.gen_map_equality_fn(node.cond_type) ptr_typ := g.gen_map_equality_fn(node.cond_type)
g.write('${ptr_typ}_map_eq(') g.write('${ptr_typ}_map_eq($cond_var, ')
g.write(cond_var)
g.write(', ')
g.expr(expr) g.expr(expr)
g.write(')') g.write(')')
} else if type_sym.kind == .struct_ { }
.string {
g.write('string__eq($cond_var, ')
g.expr(expr)
g.write(')')
}
.struct_ {
ptr_typ := g.gen_struct_equality_fn(node.cond_type) ptr_typ := g.gen_struct_equality_fn(node.cond_type)
g.write('${ptr_typ}_struct_eq(') g.write('${ptr_typ}_struct_eq($cond_var, ')
g.write(cond_var)
g.write(', ')
g.expr(expr) g.expr(expr)
g.write(')') g.write(')')
} else if expr is ast.RangeExpr { }
else {
if expr is ast.RangeExpr {
// if type is unsigned and low is 0, check is unneeded // if type is unsigned and low is 0, check is unneeded
mut skip_low := false mut skip_low := false
if expr.low is ast.IntegerLiteral { if expr.low is ast.IntegerLiteral {
@ -4283,23 +4276,21 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str
} }
g.write('(') g.write('(')
if !skip_low { if !skip_low {
g.write(cond_var) g.write('$cond_var >= ')
g.write(' >= ')
g.expr(expr.low) g.expr(expr.low)
g.write(' && ') g.write(' && ')
} }
g.write(cond_var) g.write('$cond_var <= ')
g.write(' <= ')
g.expr(expr.high) g.expr(expr.high)
g.write(')') g.write(')')
} else { } else {
g.write(cond_var) g.write('$cond_var == (')
g.write(' == ')
g.write('(')
g.expr(expr) g.expr(expr)
g.write(')') g.write(')')
} }
} }
}
}
if is_expr && tmp_var.len == 0 { if is_expr && tmp_var.len == 0 {
g.write(') ? ') g.write(') ? ')
} else { } else {