cgen: fix multiple matches in one expr (#12516)

pull/12519/head
yuyi 2021-11-19 19:52:28 +08:00 committed by GitHub
parent 80a4ff9900
commit c2eb909c9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -4444,6 +4444,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
} else {
g.match_expr_classic(node, is_expr, cond_var, tmp_var)
}
g.set_current_pos_as_last_stmt_pos()
g.write(cur_line)
if need_tmp_var {
g.write('$tmp_var')

View File

@ -0,0 +1,26 @@
fn hex_to_bytes(s string) ?[]byte {
mut ret := []byte{cap: s.len}
for read := 0; read < s.len; read += 2 {
high := s[read]
low := s[read + 1]
ret << match high {
48...57 { (high - 48) << 4 }
65...70 { (high - 65) << 4 }
97...102 { (high - 97) << 4 }
else { panic('impossible') }
} + match low {
48...57 { low - 48 }
65...70 { low - 65 }
97...102 { low - 97 }
else { panic('impossible') }
}
}
return ret
}
fn test_multiple_matchs_in_one_expr() {
ret := hex_to_bytes('FFFF') or { 'error'.bytes() }
println(ret)
assert '$ret' == '[U, U]'
}