Skip to content

Commit

Permalink
Rename math to binary_operator
Browse files Browse the repository at this point in the history
  • Loading branch information
sam701 committed Jul 20, 2024
1 parent f024475 commit 001d3a1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use super::*;
use nom_locate::position;

#[derive(Debug, PartialEq)]
pub struct MathOperation<'a> {
pub struct BinaryOperatorExpr<'a> {
pub expr1: ExprWithLocation<'a>,
pub expr2: ExprWithLocation<'a>,
pub op: MathOp,
pub op: BinaryOperator,
}

#[derive(Debug, Eq, PartialEq)]
pub enum MathOp {
pub enum BinaryOperator {
Add,
Sub,
Mul,
Expand All @@ -35,9 +35,9 @@ pub fn expr_sum(input: Span) -> IResult<Span, ExprWithLocation> {
(
rem,
if x.fragment() == &"+" {
MathOp::Add
BinaryOperator::Add
} else {
MathOp::Sub
BinaryOperator::Sub
},
)
},
Expand All @@ -47,7 +47,7 @@ pub fn expr_sum(input: Span) -> IResult<Span, ExprWithLocation> {
cut(expr_sum),
)),
)),
|(a, x)| map_math_op(a, x),
|(a, x)| map_binary_operator(a, x),
)(input)
}

Expand All @@ -65,9 +65,9 @@ pub fn expr_prod(input: Span) -> IResult<Span, ExprWithLocation> {
(
rem,
if x.fragment() == &"*" {
MathOp::Mul
BinaryOperator::Mul
} else {
MathOp::Div
BinaryOperator::Div
},
)
},
Expand All @@ -77,17 +77,18 @@ pub fn expr_prod(input: Span) -> IResult<Span, ExprWithLocation> {
cut(expr_prod),
)),
)),
|(a, x)| map_math_op(a, x),
|(a, x)| map_binary_operator(a, x),
)(input)
}

fn map_math_op<'a>(
fn map_binary_operator<'a>(
expr1: ExprWithLocation<'a>,
x: Option<((Span<'a>, MathOp), ExprWithLocation<'a>)>,
x: Option<((Span<'a>, BinaryOperator), ExprWithLocation<'a>)>,
) -> ExprWithLocation<'a> {
match x {
Some(((position, op), expr2)) => {
Expr::Math(Box::new(MathOperation { expr1, expr2, op })).with_location(position)
Expr::BinaryOperator(Box::new(BinaryOperatorExpr { expr1, expr2, op }))
.with_location(position)
}
None => expr1,
}
Expand Down
2 changes: 1 addition & 1 deletion syconf-lib/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum Expr<'a> {
Block(BlockExpr<'a>),
Identifier(&'a str),
FuncDefinition(Rc<FuncDefinition<'a>>),
Math(Box<MathOperation<'a>>),
BinaryOperator(Box<BinaryOperatorExpr<'a>>),
Comparison(Box<Comparison<'a>>),
Conditional(Box<Conditional<'a>>),
Logical(Box<Logical<'a>>),
Expand Down
4 changes: 2 additions & 2 deletions syconf-lib/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ use nom::combinator::{all_consuming, cut, map, peek};
use nom::sequence::{delimited, pair, preceded, tuple};
use nom::IResult;

pub use binary_operators::*;
pub use block::{Assignment, BlockExpr};
pub use comparison::*;
pub use conditional::*;
pub use expr::*;
pub use func::*;
pub use logical::*;
pub use math::*;
pub use spaces::*;
pub use suffix_operators::*;
pub use value::*;

use crate::parser::block::block_body;
use std::sync::Arc;

mod binary_operators;
mod block;
mod comparison;
mod conditional;
mod expr;
mod func;
mod leaf;
mod logical;
mod math;
mod spaces;
mod suffix_operators;
#[cfg(test)]
Expand Down
12 changes: 6 additions & 6 deletions syconf-lib/src/resolver/operators/math.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::parser::number::Number;
use crate::parser::MathOp;
use crate::parser::BinaryOperator;
use crate::resolver::value::FunctionSig;
use crate::resolver::{Error, Value};
use std::ops::{Add, Div, Mul, Sub};

pub fn math(op: &MathOp) -> &'static FunctionSig {
pub fn math(op: &BinaryOperator) -> &'static FunctionSig {
match op {
MathOp::Add => &op_add,
MathOp::Sub => &op_sub,
MathOp::Mul => &op_mul,
MathOp::Div => &op_div,
BinaryOperator::Add => &op_add,
BinaryOperator::Sub => &op_sub,
BinaryOperator::Mul => &op_mul,
BinaryOperator::Div => &op_div,
}
}

Expand Down
4 changes: 2 additions & 2 deletions syconf-lib/src/resolver/tree_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl NodeTreeBuilder {
Expr::Block(block) => return self.block(ctx, block),
Expr::Identifier(id) => self.identifier(ctx, id, &expr.location)?,
Expr::FuncDefinition(fd) => self.func_definition(ctx, fd)?,
Expr::Math(op) => self.math_op(ctx, op)?,
Expr::BinaryOperator(op) => self.math_op(ctx, op)?,
Expr::Comparison(cmp) => self.comparison(ctx, cmp)?,
Expr::Conditional(cond) => self.conditional(ctx, cond)?,
Expr::Logical(logical) => self.logical(ctx, logical)?,
Expand Down Expand Up @@ -103,7 +103,7 @@ impl NodeTreeBuilder {
})
}

fn math_op(&self, ctx: &Context, op: &MathOperation) -> Result<NodeContent, Error> {
fn math_op(&self, ctx: &Context, op: &BinaryOperatorExpr) -> Result<NodeContent, Error> {
let args = vec![
self.build_tree(ctx, &op.expr1)?,
self.build_tree(ctx, &op.expr2)?,
Expand Down

0 comments on commit 001d3a1

Please sign in to comment.