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,70 +4234,61 @@ 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) 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.expr(expr)
g.write(', ') g.write(')')
g.expr(expr) }
g.write(')') .map {
} else if type_sym.kind == .array_fixed { ptr_typ := g.gen_map_equality_fn(node.cond_type)
ptr_typ := g.gen_fixed_array_equality_fn(node.cond_type) g.write('${ptr_typ}_map_eq($cond_var, ')
g.write('${ptr_typ}_arr_eq(') g.expr(expr)
g.write(cond_var) g.write(')')
g.write(', ') }
g.expr(expr) .string {
g.write(')') g.write('string__eq($cond_var, ')
} else if type_sym.kind == .map { g.expr(expr)
ptr_typ := g.gen_map_equality_fn(node.cond_type) g.write(')')
g.write('${ptr_typ}_map_eq(') }
g.write(cond_var) .struct_ {
g.write(', ') ptr_typ := g.gen_struct_equality_fn(node.cond_type)
g.expr(expr) g.write('${ptr_typ}_struct_eq($cond_var, ')
g.write(')') g.expr(expr)
} else if type_sym.kind == .struct_ { g.write(')')
ptr_typ := g.gen_struct_equality_fn(node.cond_type) }
g.write('${ptr_typ}_struct_eq(') else {
g.write(cond_var) if expr is ast.RangeExpr {
g.write(', ') // if type is unsigned and low is 0, check is unneeded
g.expr(expr) mut skip_low := false
g.write(')') if expr.low is ast.IntegerLiteral {
} else if expr is ast.RangeExpr { if node.cond_type in [ast.u16_type, ast.u32_type, ast.u64_type]
// if type is unsigned and low is 0, check is unneeded && expr.low.val == '0' {
mut skip_low := false skip_low = true
if expr.low is ast.IntegerLiteral { }
if node.cond_type in [ast.u16_type, ast.u32_type, ast.u64_type] }
&& expr.low.val == '0' { g.write('(')
skip_low = true if !skip_low {
g.write('$cond_var >= ')
g.expr(expr.low)
g.write(' && ')
}
g.write('$cond_var <= ')
g.expr(expr.high)
g.write(')')
} else {
g.write('$cond_var == (')
g.expr(expr)
g.write(')')
} }
} }
g.write('(')
if !skip_low {
g.write(cond_var)
g.write(' >= ')
g.expr(expr.low)
g.write(' && ')
}
g.write(cond_var)
g.write(' <= ')
g.expr(expr.high)
g.write(')')
} else {
g.write(cond_var)
g.write(' == ')
g.write('(')
g.expr(expr)
g.write(')')
} }
} }
if is_expr && tmp_var.len == 0 { if is_expr && tmp_var.len == 0 {