Skip to content

Commit 2e7b811

Browse files
committed
parser: mv expression parser
1 parent e35d3e2 commit 2e7b811

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::collections::BTreeMap;
2+
3+
use pest::iterators::Pair;
4+
5+
use super::{ParseError, Rule, parse_binary_expr, parse_primary};
6+
use crate::{intermediate_bytecode::HighLevelOperation, lang::Expression};
7+
8+
pub(crate) fn parse_expression(
9+
pair: Pair<'_, Rule>,
10+
constants: &BTreeMap<String, usize>,
11+
) -> Result<Expression, ParseError> {
12+
match pair.as_rule() {
13+
Rule::expression => parse_expression(pair.into_inner().next().unwrap(), constants),
14+
Rule::add_expr => parse_binary_expr(pair, constants, HighLevelOperation::Add),
15+
Rule::sub_expr => parse_binary_expr(pair, constants, HighLevelOperation::Sub),
16+
Rule::mul_expr => parse_binary_expr(pair, constants, HighLevelOperation::Mul),
17+
Rule::div_expr => parse_binary_expr(pair, constants, HighLevelOperation::Div),
18+
Rule::exp_expr => parse_binary_expr(pair, constants, HighLevelOperation::Exp),
19+
Rule::primary => parse_primary(pair, constants),
20+
_ => Err(ParseError::SemanticError("Invalid expression".to_string())),
21+
}
22+
}

crates/leanVm/src/parser/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub mod statement;
2323
pub(crate) use statement::*;
2424
pub mod condition;
2525
pub(crate) use condition::*;
26+
pub mod expression;
27+
pub(crate) use expression::*;
2628

2729
#[derive(Parser, Debug)]
2830
#[grammar = "grammar.pest"]
@@ -140,22 +142,6 @@ fn parse_return_statement(
140142
Ok(Line::FunctionRet { return_data })
141143
}
142144

143-
fn parse_expression(
144-
pair: Pair<'_, Rule>,
145-
constants: &BTreeMap<String, usize>,
146-
) -> Result<Expression, ParseError> {
147-
match pair.as_rule() {
148-
Rule::expression => parse_expression(pair.into_inner().next().unwrap(), constants),
149-
Rule::add_expr => parse_binary_expr(pair, constants, HighLevelOperation::Add),
150-
Rule::sub_expr => parse_binary_expr(pair, constants, HighLevelOperation::Sub),
151-
Rule::mul_expr => parse_binary_expr(pair, constants, HighLevelOperation::Mul),
152-
Rule::div_expr => parse_binary_expr(pair, constants, HighLevelOperation::Div),
153-
Rule::exp_expr => parse_binary_expr(pair, constants, HighLevelOperation::Exp),
154-
Rule::primary => parse_primary(pair, constants),
155-
_ => Err(ParseError::SemanticError("Invalid expression".to_string())),
156-
}
157-
}
158-
159145
fn parse_array_access(
160146
pair: Pair<'_, Rule>,
161147
constants: &BTreeMap<String, usize>,

0 commit comments

Comments
 (0)