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 {
g.write(' || ')
}
if type_sym.kind == .string {
if expr is ast.StringLiteral && (expr as ast.StringLiteral).val == '' {
g.write('${cond_var}.len == 0')
} else {
g.write('string__eq(')
g.write(cond_var)
g.write(', ')
match type_sym.kind {
.array {
ptr_typ := g.gen_array_equality_fn(node.cond_type)
g.write('${ptr_typ}_arr_eq($cond_var, ')
g.expr(expr)
g.write(')')
}
} else if type_sym.kind == .array {
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 {
.array_fixed {
ptr_typ := g.gen_fixed_array_equality_fn(node.cond_type)
g.write('${ptr_typ}_arr_eq(')
g.write(cond_var)
g.write(', ')
g.write('${ptr_typ}_arr_eq($cond_var, ')
g.expr(expr)
g.write(')')
} else if type_sym.kind == .map {
}
.map {
ptr_typ := g.gen_map_equality_fn(node.cond_type)
g.write('${ptr_typ}_map_eq(')
g.write(cond_var)
g.write(', ')
g.write('${ptr_typ}_map_eq($cond_var, ')
g.expr(expr)
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)
g.write('${ptr_typ}_struct_eq(')
g.write(cond_var)
g.write(', ')
g.write('${ptr_typ}_struct_eq($cond_var, ')
g.expr(expr)
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
mut skip_low := false
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('(')
if !skip_low {
g.write(cond_var)
g.write(' >= ')
g.write('$cond_var >= ')
g.expr(expr.low)
g.write(' && ')
}
g.write(cond_var)
g.write(' <= ')
g.write('$cond_var <= ')
g.expr(expr.high)
g.write(')')
} else {
g.write(cond_var)
g.write(' == ')
g.write('(')
g.write('$cond_var == (')
g.expr(expr)
g.write(')')
}
}
}
}
if is_expr && tmp_var.len == 0 {
g.write(') ? ')
} else {