@@ -10,29 +10,29 @@ fun replace_by_expr(if_stmt, ctx) =
10
10
{
11
11
val is_return = simple_return(if_stmt.f_then_stmts);
12
12
val then_stmt = if_stmt.f_then_stmts[1];
13
- val else_stmt = if_stmt.f_else_part.f_stmts[1];
14
- val if_expr = new ParenExpr(new IfExpr(
15
- f_cond_expr = if_stmt.f_cond_expr,
16
- f_then_expr = if is_return then then_stmt.f_return_expr else then_stmt.f_expr,
17
- f_else_expr = if is_return then else_stmt.f_return_expr else else_stmt.f_expr,
18
- f_alternatives=new ElsifExprPartList([
19
- {
20
- val stmt = part.f_stmts[1];
21
- new ElsifExprPart(
22
- f_cond_expr = part.f_cond_expr,
23
- f_then_expr = if is_return then stmt.f_return_expr else stmt.f_expr
24
- )
25
- }
26
- for part in if_stmt.f_alternatives.children
27
- ].to_list)
28
- ));
13
+ val expr_index = if is_return then 1 else 2;
14
+ val template_and_rule = (
15
+ if is_return
16
+ then ("return (if {} then {} {} else {});", "return_stmt_rule")
17
+ else (then_stmt.f_dest.text & " := (if {} then {} {} else {});", "assignment_stmt_rule")
18
+ );
29
19
ctx.replace(
30
20
if_stmt,
31
- if is_return
32
- then new ReturnStmt(if_expr)
33
- else new AssignStmt(
34
- f_dest = then_stmt.f_dest,
35
- f_expr = if_expr
21
+ ctx.create_from_template(
22
+ template_and_rule[1],
23
+ template_and_rule[2],
24
+ [
25
+ if_stmt.f_cond_expr,
26
+ then_stmt[expr_index],
27
+ new ElsifExprPartList([
28
+ new ElsifExprPart(
29
+ f_cond_expr = part.f_cond_expr,
30
+ f_then_expr = part.f_stmts[1][expr_index]
31
+ )
32
+ for part in if_stmt.f_alternatives.children
33
+ ].to_list),
34
+ if_stmt.f_else_part.f_stmts[1][expr_index]
35
+ ]
36
36
)
37
37
)
38
38
}
0 commit comments