Skip to content

Commit f8c2c93

Browse files
committed
parser: reorg with utils
1 parent 2e7b811 commit f8c2c93

File tree

4 files changed

+258
-255
lines changed

4 files changed

+258
-255
lines changed

crates/leanVm/src/parser/function.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use std::collections::BTreeMap;
22

33
use pest::iterators::Pair;
44

5-
use super::{ParseError, Rule, parse_parameter, parse_statement, parse_var_list};
5+
use super::{
6+
ParseError, Rule, parse_parameter, parse_statement, parse_tuple_expression, parse_var_list,
7+
};
68
use crate::{
79
lang::{Function, Line, SimpleExpr},
8-
parser::{PRECOMPILES, parse_tuple_expression},
10+
parser::PRECOMPILES,
911
};
1012

1113
pub(crate) fn parse_function(

crates/leanVm/src/parser/mod.rs

Lines changed: 1 addition & 249 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
use std::collections::BTreeMap;
2-
3-
use p3_field::{PrimeCharacteristicRing, PrimeField};
4-
use pest::iterators::Pair;
51
use pest_derive::Parser;
62

7-
use crate::{
8-
bytecode::precompiles::PRECOMPILES,
9-
constant::F,
10-
intermediate_bytecode::HighLevelOperation,
11-
lang::{Boolean, ConstExpression, ConstantValue, Expression, Line, Program, SimpleExpr},
12-
};
3+
use crate::{bytecode::precompiles::PRECOMPILES, lang::Program};
134

145
pub mod error;
156
pub use error::*;
@@ -30,245 +21,6 @@ pub(crate) use expression::*;
3021
#[grammar = "grammar.pest"]
3122
pub struct LangParser;
3223

33-
fn parse_constant_declaration(
34-
pair: Pair<'_, Rule>,
35-
constants: &BTreeMap<String, usize>,
36-
) -> Result<(String, usize), ParseError> {
37-
let mut inner = pair.into_inner();
38-
let name = inner.next().unwrap().as_str().to_string();
39-
let value = parse_expression(inner.next().unwrap(), constants)?;
40-
let value = value
41-
.eval_with(
42-
&|simple_expr| match simple_expr {
43-
SimpleExpr::Constant(cst) => cst.naive_eval(),
44-
SimpleExpr::Var(var) => constants.get(var).map(|f| F::from_usize(*f)),
45-
SimpleExpr::ConstMallocAccess { .. } => unreachable!(),
46-
},
47-
&|_, _| None,
48-
)
49-
.unwrap_or_else(|| panic!("Failed to evaluate constant: {name}"))
50-
.as_canonical_biguint()
51-
.try_into()
52-
.unwrap();
53-
Ok((name, value))
54-
}
55-
56-
fn parse_parameter(pair: Pair<'_, Rule>) -> Result<(String, bool), ParseError> {
57-
let mut inner = pair.into_inner();
58-
let first = inner.next().unwrap();
59-
60-
if first.as_rule() == Rule::const_keyword {
61-
// If the first token is "const", the next one should be the identifier
62-
let identifier = inner.next().ok_or_else(|| {
63-
ParseError::SemanticError("Expected identifier after 'const'".to_string())
64-
})?;
65-
return Ok((identifier.as_str().to_string(), true));
66-
}
67-
68-
Ok((first.as_str().to_string(), false))
69-
}
70-
71-
fn parse_assignment(
72-
pair: Pair<'_, Rule>,
73-
constants: &BTreeMap<String, usize>,
74-
) -> Result<Line, ParseError> {
75-
let mut inner = pair.into_inner();
76-
let var = inner.next().unwrap().as_str().to_string();
77-
let expr = inner.next().unwrap();
78-
let value = parse_expression(expr, constants)?;
79-
80-
Ok(Line::Assignment { var, value })
81-
}
82-
83-
fn parse_array_assign(
84-
pair: Pair<'_, Rule>,
85-
constants: &BTreeMap<String, usize>,
86-
) -> Result<Line, ParseError> {
87-
let mut inner = pair.into_inner();
88-
let array = inner.next().unwrap().as_str().to_string();
89-
let index = parse_expression(inner.next().unwrap(), constants)?;
90-
let value = parse_expression(inner.next().unwrap(), constants)?;
91-
Ok(Line::ArrayAssign {
92-
array,
93-
index,
94-
value,
95-
})
96-
}
97-
98-
fn parse_for_statement(
99-
pair: Pair<'_, Rule>,
100-
constants: &BTreeMap<String, usize>,
101-
trash_var_count: &mut usize,
102-
) -> Result<Line, ParseError> {
103-
let mut inner = pair.into_inner();
104-
let iterator = inner.next().unwrap().as_str().to_string();
105-
let start = parse_expression(inner.next().unwrap(), constants)?;
106-
let end = parse_expression(inner.next().unwrap(), constants)?;
107-
108-
let mut unroll = false;
109-
let mut body = Vec::new();
110-
111-
for item in inner {
112-
match item.as_rule() {
113-
Rule::unroll_clause => {
114-
unroll = true;
115-
}
116-
Rule::statement => {
117-
body.push(parse_statement(item, constants, trash_var_count)?);
118-
}
119-
_ => {}
120-
}
121-
}
122-
123-
Ok(Line::ForLoop {
124-
iterator,
125-
start,
126-
end,
127-
body,
128-
unroll,
129-
})
130-
}
131-
132-
fn parse_return_statement(
133-
pair: Pair<'_, Rule>,
134-
constants: &BTreeMap<String, usize>,
135-
) -> Result<Line, ParseError> {
136-
let mut return_data = Vec::new();
137-
for item in pair.into_inner() {
138-
if item.as_rule() == Rule::tuple_expression {
139-
return_data = parse_tuple_expression(item, constants)?;
140-
}
141-
}
142-
Ok(Line::FunctionRet { return_data })
143-
}
144-
145-
fn parse_array_access(
146-
pair: Pair<'_, Rule>,
147-
constants: &BTreeMap<String, usize>,
148-
) -> Result<Expression, ParseError> {
149-
let mut inner = pair.into_inner();
150-
let array = inner.next().unwrap().as_str().to_string();
151-
let index = parse_expression(inner.next().unwrap(), constants)?;
152-
Ok(Expression::ArrayAccess {
153-
array,
154-
index: Box::new(index),
155-
})
156-
}
157-
158-
fn parse_binary_expr(
159-
pair: Pair<'_, Rule>,
160-
constants: &BTreeMap<String, usize>,
161-
operation: HighLevelOperation,
162-
) -> Result<Expression, ParseError> {
163-
let mut inner = pair.into_inner();
164-
let mut expr = parse_expression(inner.next().unwrap(), constants)?;
165-
166-
for right in inner {
167-
let right_expr = parse_expression(right, constants)?;
168-
expr = Expression::Binary {
169-
left: Box::new(expr),
170-
operation,
171-
right: Box::new(right_expr),
172-
};
173-
}
174-
175-
Ok(expr)
176-
}
177-
178-
fn parse_primary(
179-
pair: Pair<'_, Rule>,
180-
constants: &BTreeMap<String, usize>,
181-
) -> Result<Expression, ParseError> {
182-
let inner = pair.into_inner().next().unwrap();
183-
match inner.as_rule() {
184-
Rule::expression => parse_expression(inner, constants),
185-
Rule::var_or_constant => Ok(Expression::Value(parse_var_or_constant(inner, constants)?)),
186-
Rule::array_access_expr => parse_array_access(inner, constants),
187-
_ => Err(ParseError::SemanticError(
188-
"Invalid primary expression".to_string(),
189-
)),
190-
}
191-
}
192-
193-
fn parse_tuple_expression(
194-
pair: Pair<'_, Rule>,
195-
constants: &BTreeMap<String, usize>,
196-
) -> Result<Vec<Expression>, ParseError> {
197-
pair.into_inner()
198-
.map(|item| parse_expression(item, constants))
199-
.collect()
200-
}
201-
202-
fn parse_assert_eq(
203-
pair: Pair<'_, Rule>,
204-
constants: &BTreeMap<String, usize>,
205-
) -> Result<Line, ParseError> {
206-
let mut inner = pair.into_inner();
207-
let left = parse_expression(inner.next().unwrap(), constants)?;
208-
let right = parse_expression(inner.next().unwrap(), constants)?;
209-
Ok(Line::Assert(Boolean::Equal { left, right }))
210-
}
211-
212-
fn parse_assert_not_eq(
213-
pair: Pair<'_, Rule>,
214-
constants: &BTreeMap<String, usize>,
215-
) -> Result<Line, ParseError> {
216-
let mut inner = pair.into_inner();
217-
let left = parse_expression(inner.next().unwrap(), constants)?;
218-
let right = parse_expression(inner.next().unwrap(), constants)?;
219-
Ok(Line::Assert(Boolean::Different { left, right }))
220-
}
221-
222-
fn parse_var_or_constant(
223-
pair: Pair<'_, Rule>,
224-
constants: &BTreeMap<String, usize>,
225-
) -> Result<SimpleExpr, ParseError> {
226-
let text = pair.as_str();
227-
228-
match pair.as_rule() {
229-
Rule::var_or_constant => {
230-
parse_var_or_constant(pair.into_inner().next().unwrap(), constants)
231-
}
232-
Rule::identifier | Rule::constant_value => match text {
233-
"public_input_start" => Ok(SimpleExpr::Constant(ConstExpression::Value(
234-
ConstantValue::PublicInputStart,
235-
))),
236-
"pointer_to_zero_vector" => Ok(SimpleExpr::Constant(ConstExpression::Value(
237-
ConstantValue::PointerToZeroVector,
238-
))),
239-
_ => constants.get(text).map_or_else(
240-
|| {
241-
text.parse::<usize>().map_or_else(
242-
|_| Ok(SimpleExpr::Var(text.to_string())),
243-
|value| {
244-
Ok(SimpleExpr::Constant(ConstExpression::Value(
245-
ConstantValue::Scalar(value),
246-
)))
247-
},
248-
)
249-
},
250-
|value| {
251-
Ok(SimpleExpr::Constant(ConstExpression::Value(
252-
ConstantValue::Scalar(*value),
253-
)))
254-
},
255-
),
256-
},
257-
_ => Err(ParseError::SemanticError(
258-
"Expected identifier or constant".to_string(),
259-
)),
260-
}
261-
}
262-
263-
fn parse_var_list(
264-
pair: Pair<'_, Rule>,
265-
constants: &BTreeMap<String, usize>,
266-
) -> Result<Vec<SimpleExpr>, ParseError> {
267-
pair.into_inner()
268-
.map(|item| parse_var_or_constant(item, constants))
269-
.collect()
270-
}
271-
27224
#[cfg(test)]
27325
mod tests {
27426
use super::*;

crates/leanVm/src/parser/program.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ use std::collections::BTreeMap;
22

33
use pest::Parser;
44

5-
use super::{
6-
LangParser, ParseError, Rule, parse_constant_declaration, parse_function, remove_comments,
7-
};
8-
use crate::parser::Program;
5+
use super::{LangParser, ParseError, Rule, parse_function, remove_comments};
6+
use crate::parser::{Program, parse_constant_declaration};
97

108
pub fn parse_program(input: &str) -> Result<Program, ParseError> {
119
let input = remove_comments(input);

0 commit comments

Comments
 (0)