Skip to content

Commit 35bbb54

Browse files
authored
cgen: fix match codegen with option (fix #25394) (#25399)
1 parent ad9b3a0 commit 35bbb54

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

vlib/v/gen/c/match.v

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str
435435
mut has_goto := false
436436
for j, branch in node.branches {
437437
is_last := j == node.branches.len - 1
438+
reset_if = branch.exprs.any(g.match_must_reset_if(it))
439+
if reset_if {
440+
g.writeln('')
441+
g.set_current_pos_as_last_stmt_pos()
442+
}
438443
if branch.is_else || (use_ternary && is_last) {
439444
if node.branches.len > 1 {
440445
if use_ternary {
@@ -469,7 +474,6 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str
469474
g.write_v_source_line_info(branch)
470475
g.write('if (')
471476
}
472-
reset_if = branch.exprs.any(g.match_must_reset_if(it))
473477
for i, expr in branch.exprs {
474478
if i > 0 {
475479
g.write(' || ')
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module main
2+
3+
type SecondTokenizerValue = []rune | Keyword
4+
5+
enum AdvancedTokenType {
6+
identifier
7+
keyword
8+
newline
9+
}
10+
11+
struct SecondTokenizerToken {
12+
type AdvancedTokenType
13+
value ?SecondTokenizerValue
14+
}
15+
16+
enum Keyword {
17+
module
18+
import
19+
}
20+
21+
fn test_main() {
22+
mut tokens := []SecondTokenizerToken{}
23+
tokens << SecondTokenizerToken{
24+
type: AdvancedTokenType.keyword
25+
value: Keyword.module
26+
}
27+
build_ast(tokens) or { assert err.msg() == 'Handling module' }
28+
}
29+
30+
struct RootAST {
31+
mut:
32+
module []rune
33+
}
34+
35+
fn build_ast(tokens []SecondTokenizerToken) !RootAST {
36+
for _ in 0 .. 1 {
37+
token := tokens[0]
38+
match true {
39+
false {
40+
return error('Expected `module` keyword at the start of the file, but got `${token.type}`')
41+
}
42+
token.type == .keyword && token.value == ?SecondTokenizerValue(Keyword.module) {
43+
return error('Handling module')
44+
}
45+
else {
46+
continue
47+
}
48+
}
49+
}
50+
51+
return error('Not implemented')
52+
}

0 commit comments

Comments
 (0)