Skip to content

Commit d3fa603

Browse files
committed
use the pythonic "elif" syntax for the zkDSL
1 parent 0bea730 commit d3fa603

File tree

17 files changed

+50
-51
lines changed

17 files changed

+50
-51
lines changed

crates/lean_compiler/src/grammar.pest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ assignment_target_list = { ("(" ~ simple_target_list ~ ")") | simple_target_list
7676
simple_target_list = { assignment_target ~ ("," ~ assignment_target)* ~ ","? }
7777
assignment_target = { (identifier ~ mut_annotation) | array_access_expr | identifier }
7878

79-
if_statement = { "if" ~ condition ~ ":" ~ newline ~ statement* ~ end_block ~ else_if_clause* ~ else_clause? }
79+
if_statement = { "if" ~ condition ~ ":" ~ newline ~ statement* ~ end_block ~ elif_clause* ~ else_clause? }
8080

8181
condition = { assumed_bool_expr | "(" ~ comparison ~ ")" | comparison }
8282

@@ -86,7 +86,7 @@ assumed_bool_expr = { "!!assume_bool" ~ "(" ~ expression ~ ")" }
8686
comparison = { add_expr ~ comparison_op ~ add_expr }
8787
comparison_op = { "==" | "!=" | "<=" | "<" }
8888

89-
else_if_clause = { "else" ~ "if" ~ condition ~ ":" ~ newline ~ statement* ~ end_block }
89+
elif_clause = { "elif" ~ condition ~ ":" ~ newline ~ statement* ~ end_block }
9090

9191
else_clause = { "else" ~ ":" ~ newline ~ statement* ~ end_block }
9292

crates/lean_compiler/src/parser/parsers/statement.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,25 @@ impl Parse<Line> for IfStatementParser {
5757
let condition = ConditionParser.parse(next_inner_pair(&mut inner, "if condition")?, ctx)?;
5858

5959
let mut then_branch: Vec<Line> = Vec::new();
60-
let mut else_if_branches: Vec<(Condition, Vec<Line>, SourceLineNumber)> = Vec::new();
60+
let mut elif_branches: Vec<(Condition, Vec<Line>, SourceLineNumber)> = Vec::new();
6161
let mut else_branch: Vec<Line> = Vec::new();
6262

6363
for item in inner {
6464
match item.as_rule() {
6565
Rule::statement => {
6666
Self::add_statement_with_location(&mut then_branch, item, ctx)?;
6767
}
68-
Rule::else_if_clause => {
68+
Rule::elif_clause => {
6969
let line_number = item.line_col().0;
7070
let mut inner = item.into_inner();
71-
let else_if_condition =
72-
ConditionParser.parse(next_inner_pair(&mut inner, "else if condition")?, ctx)?;
73-
let mut else_if_branch = Vec::new();
74-
for else_if_item in inner {
75-
if else_if_item.as_rule() == Rule::statement {
76-
Self::add_statement_with_location(&mut else_if_branch, else_if_item, ctx)?;
71+
let elif_condition = ConditionParser.parse(next_inner_pair(&mut inner, "elif condition")?, ctx)?;
72+
let mut elif_branch = Vec::new();
73+
for elif_item in inner {
74+
if elif_item.as_rule() == Rule::statement {
75+
Self::add_statement_with_location(&mut elif_branch, elif_item, ctx)?;
7776
}
7877
}
79-
else_if_branches.push((else_if_condition, else_if_branch, line_number));
78+
elif_branches.push((elif_condition, elif_branch, line_number));
8079
}
8180
Rule::else_clause => {
8281
for else_item in item.into_inner() {
@@ -92,10 +91,10 @@ impl Parse<Line> for IfStatementParser {
9291
let mut outer_else_branch = Vec::new();
9392
let mut inner_else_branch = &mut outer_else_branch;
9493

95-
for (else_if_condition, else_if_branch, line_number) in else_if_branches.into_iter() {
94+
for (elif_condition, elif_branch, line_number) in elif_branches.into_iter() {
9695
inner_else_branch.push(Line::IfCondition {
97-
condition: else_if_condition,
98-
then_branch: else_if_branch,
96+
condition: elif_condition,
97+
then_branch: elif_branch,
9998
else_branch: Vec::new(),
10099
location: SourceLocation {
101100
file_id: ctx.current_file_id,

crates/lean_compiler/tests/test_data/perf_constant_if_with_conditions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def main():
5858
# Chain of if-else-if with constants
5959
if A == 1:
6060
result = result + 5000
61-
else if A == 2:
61+
elif A == 2:
6262
result = result + 6000
63-
else if A == 10:
63+
elif A == 10:
6464
result = result + 64
6565
else:
6666
result = result + 7000

crates/lean_compiler/tests/test_data/program_110.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def main():
5252
outer_sel = 1
5353
if outer_sel == 0:
5454
p = p + 100
55-
else if outer_sel == 1:
55+
elif outer_sel == 1:
5656
inner_sel = 2
5757
match inner_sel:
5858
case 0:

crates/lean_compiler/tests/test_data/program_111.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ def state_machine_step(current_state, phase):
9090
result = 1
9191
else:
9292
result = current_state + 1000
93-
else if phase == 1:
93+
elif phase == 1:
9494
result = current_state + 11
95-
else if phase == 2:
95+
elif phase == 2:
9696
result = current_state + 3
97-
else if phase == 3:
97+
elif phase == 3:
9898
result = current_state * 10
9999
else:
100100
result = current_state + 1

crates/lean_compiler/tests/test_data/program_112.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def main():
2323
if phase == 0:
2424
counter = 0
2525
flag = 100
26-
else if phase == 1:
26+
elif phase == 1:
2727
counter = 10
2828
flag = 200
2929
else:

crates/lean_compiler/tests/test_data/program_114.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def main():
3636
if mode == 0:
3737
fwd_x = 1
3838
fwd_y = 1
39-
else if mode == 1:
39+
elif mode == 1:
4040
fwd_x = 10
4141
fwd_y = 10
4242
else:
@@ -86,7 +86,7 @@ def complex_nested_compute(outer, inner, depth):
8686

8787
if outer == 0:
8888
result = 100
89-
else if outer == 1:
89+
elif outer == 1:
9090
if inner == 0:
9191
result = 110
9292
else:
@@ -95,18 +95,18 @@ def complex_nested_compute(outer, inner, depth):
9595
if inner == 0:
9696
if depth == 0:
9797
result = 200
98-
else if depth == 1:
98+
elif depth == 1:
9999
result = 210
100-
else if depth == 2:
100+
elif depth == 2:
101101
result = 220
102102
else:
103103
result = 230
104104
else:
105105
if depth == 0:
106106
result = 250
107-
else if depth == 1:
107+
elif depth == 1:
108108
result = 260
109-
else if depth == 2:
109+
elif depth == 2:
110110
result = 270
111111
else:
112112
result = 280

crates/lean_compiler/tests/test_data/program_118.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ def long_else_if_chain(n):
7272
result: Mut = 0
7373
if n == 0:
7474
result = 1
75-
else if n == 1:
75+
elif n == 1:
7676
result = 2
77-
else if n == 2:
77+
elif n == 2:
7878
result = 3
79-
else if n == 3:
79+
elif n == 3:
8080
result = 4
81-
else if n == 4:
81+
elif n == 4:
8282
result = 5
8383
return result

crates/lean_compiler/tests/test_data/program_12.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def fill_array(arr):
1111
for i in range(0, N):
1212
if i == 0:
1313
arr[i] = 10
14-
else if i == 1:
14+
elif i == 1:
1515
arr[i] = 20
16-
else if i == 2:
16+
elif i == 2:
1717
arr[i] = 30
1818
else:
1919
i_plus_one = i + 1

crates/lean_compiler/tests/test_data/program_137.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ def main():
66
for i in range(0, 6):
77
if i == 0:
88
result += 1
9-
else if i == 1:
9+
elif i == 1:
1010
result += 2
11-
else if i == 2:
11+
elif i == 2:
1212
result += 4
13-
else if i == 3:
13+
elif i == 3:
1414
result += 8
15-
else if i == 4:
15+
elif i == 4:
1616
result += 16
1717
else:
1818
result += 32

0 commit comments

Comments
 (0)