Skip to content

Commit a2400e9

Browse files
committed
parser: mv statement parser
1 parent d9895f2 commit a2400e9

File tree

2 files changed

+69
-57
lines changed

2 files changed

+69
-57
lines changed

crates/leanVm/src/parser/mod.rs

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use crate::{
88
bytecode::precompiles::PRECOMPILES,
99
constant::F,
1010
intermediate_bytecode::HighLevelOperation,
11-
lang::{
12-
Boolean, ConstExpression, ConstantValue, Expression, Line, Program, SimpleExpr,
13-
},
11+
lang::{Boolean, ConstExpression, ConstantValue, Expression, Line, Program, SimpleExpr},
1412
};
1513

1614
pub mod error;
@@ -21,6 +19,8 @@ pub mod program;
2119
pub use program::*;
2220
pub mod function;
2321
pub(crate) use function::*;
22+
pub mod statement;
23+
pub(crate) use statement::*;
2424

2525
#[derive(Parser, Debug)]
2626
#[grammar = "grammar.pest"]
@@ -64,60 +64,6 @@ fn parse_parameter(pair: Pair<'_, Rule>) -> Result<(String, bool), ParseError> {
6464
Ok((first.as_str().to_string(), false))
6565
}
6666

67-
fn parse_statement(
68-
pair: Pair<'_, Rule>,
69-
constants: &BTreeMap<String, usize>,
70-
trash_var_count: &mut usize,
71-
) -> Result<Line, ParseError> {
72-
let inner = pair.into_inner().next().unwrap();
73-
74-
match inner.as_rule() {
75-
Rule::single_assignment => parse_assignment(inner, constants),
76-
Rule::array_assign => parse_array_assign(inner, constants),
77-
Rule::if_statement => parse_if_statement(inner, constants, trash_var_count),
78-
Rule::for_statement => parse_for_statement(inner, constants, trash_var_count),
79-
Rule::return_statement => parse_return_statement(inner, constants),
80-
Rule::function_call => parse_function_call(&inner, constants, trash_var_count),
81-
Rule::assert_eq_statement => parse_assert_eq(inner, constants),
82-
Rule::assert_not_eq_statement => parse_assert_not_eq(inner, constants),
83-
Rule::break_statement => Ok(Line::Break),
84-
Rule::continue_statement => todo!("Continue statement not implemented yet"),
85-
_ => Err(ParseError::SemanticError("Unknown statement".to_string())),
86-
}
87-
}
88-
89-
fn parse_if_statement(
90-
pair: Pair<'_, Rule>,
91-
constants: &BTreeMap<String, usize>,
92-
trash_var_count: &mut usize,
93-
) -> Result<Line, ParseError> {
94-
let mut inner = pair.into_inner();
95-
let condition = parse_condition(inner.next().unwrap(), constants)?;
96-
97-
let mut then_branch = Vec::new();
98-
let mut else_branch = Vec::new();
99-
100-
for item in inner {
101-
match item.as_rule() {
102-
Rule::statement => then_branch.push(parse_statement(item, constants, trash_var_count)?),
103-
Rule::else_clause => {
104-
for else_item in item.into_inner() {
105-
if else_item.as_rule() == Rule::statement {
106-
else_branch.push(parse_statement(else_item, constants, trash_var_count)?);
107-
}
108-
}
109-
}
110-
_ => {}
111-
}
112-
}
113-
114-
Ok(Line::IfCondition {
115-
condition,
116-
then_branch,
117-
else_branch,
118-
})
119-
}
120-
12167
fn parse_assignment(
12268
pair: Pair<'_, Rule>,
12369
constants: &BTreeMap<String, usize>,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::collections::BTreeMap;
2+
3+
use pest::iterators::Pair;
4+
5+
use super::{
6+
ParseError, Rule, parse_assert_eq, parse_assert_not_eq, parse_assignment, parse_for_statement,
7+
parse_function_call,
8+
};
9+
use crate::{
10+
lang::Line,
11+
parser::{parse_array_assign, parse_condition, parse_return_statement},
12+
};
13+
14+
pub(crate) fn parse_statement(
15+
pair: Pair<'_, Rule>,
16+
constants: &BTreeMap<String, usize>,
17+
trash_var_count: &mut usize,
18+
) -> Result<Line, ParseError> {
19+
let inner = pair.into_inner().next().unwrap();
20+
21+
match inner.as_rule() {
22+
Rule::single_assignment => parse_assignment(inner, constants),
23+
Rule::array_assign => parse_array_assign(inner, constants),
24+
Rule::if_statement => parse_if_statement(inner, constants, trash_var_count),
25+
Rule::for_statement => parse_for_statement(inner, constants, trash_var_count),
26+
Rule::return_statement => parse_return_statement(inner, constants),
27+
Rule::function_call => parse_function_call(&inner, constants, trash_var_count),
28+
Rule::assert_eq_statement => parse_assert_eq(inner, constants),
29+
Rule::assert_not_eq_statement => parse_assert_not_eq(inner, constants),
30+
Rule::break_statement => Ok(Line::Break),
31+
Rule::continue_statement => todo!("Continue statement not implemented yet"),
32+
_ => Err(ParseError::SemanticError("Unknown statement".to_string())),
33+
}
34+
}
35+
36+
pub(crate) fn parse_if_statement(
37+
pair: Pair<'_, Rule>,
38+
constants: &BTreeMap<String, usize>,
39+
trash_var_count: &mut usize,
40+
) -> Result<Line, ParseError> {
41+
let mut inner = pair.into_inner();
42+
let condition = parse_condition(inner.next().unwrap(), constants)?;
43+
44+
let mut then_branch = Vec::new();
45+
let mut else_branch = Vec::new();
46+
47+
for item in inner {
48+
match item.as_rule() {
49+
Rule::statement => then_branch.push(parse_statement(item, constants, trash_var_count)?),
50+
Rule::else_clause => {
51+
for else_item in item.into_inner() {
52+
if else_item.as_rule() == Rule::statement {
53+
else_branch.push(parse_statement(else_item, constants, trash_var_count)?);
54+
}
55+
}
56+
}
57+
_ => {}
58+
}
59+
}
60+
61+
Ok(Line::IfCondition {
62+
condition,
63+
then_branch,
64+
else_branch,
65+
})
66+
}

0 commit comments

Comments
 (0)