Skip to content

Commit

Permalink
🚧 WIP
Browse files Browse the repository at this point in the history
semver: chore
  • Loading branch information
Lucas de Jong committed Oct 29, 2024
1 parent 8af81f8 commit 9a2d40b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod lexer;
pub mod parser;

fn main() {
let input = "2 == 12;\n";
let input = "2 == 2 % 12;\n";

let mut parser = Parser::new(input);
let symbol = match parser.parse() {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ pub enum BinaryOperator {
Multiply,
Divide,
Modulo,
Equal,
NotEqual,
Equality,
Inequality,
LessThan,
LessThanOrEqual,
GreaterThan,
Expand Down
109 changes: 23 additions & 86 deletions src/parser/expression/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,184 +5,121 @@ use crate::parser::{
};
use miette::Result;

pub fn addition<'de>(
pub fn parse_binary_expression<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
operator: BinaryOperator,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::Add,
operator,
left: Box::new(lhs),
right: Box::new(rhs),
})
}

pub fn multiplication<'de>(
pub fn addition<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;
parse_binary_expression(parser, lhs, bp, BinaryOperator::Add)
}

Ok(Expression::Binary {
operator: BinaryOperator::Multiply,
left: Box::new(lhs),
right: Box::new(rhs),
})
pub fn multiplication<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
parse_binary_expression(parser, lhs, bp, BinaryOperator::Multiply)
}

pub fn subtraction<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::Subtract,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::Subtract)
}

pub fn division<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::Divide,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::Divide)
}

pub fn modulo<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::Modulo,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::Modulo)
}

pub fn equal<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::Equal,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::Equality)
}

pub fn not_equal<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::NotEqual,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::Inequality)
}

pub fn less_than<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::LessThan,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::LessThan)
}

pub fn less_than_or_equal<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::LessThanOrEqual,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::LessThanOrEqual)
}

pub fn greater_than<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::GreaterThan,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::GreaterThan)
}

pub fn greater_than_or_equal<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::GreaterThanOrEqual,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::GreaterThanOrEqual)
}

pub fn and<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::And,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::And)
}

pub fn or<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = crate::parser::expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::Or,
left: Box::new(lhs),
right: Box::new(rhs),
})
parse_binary_expression(parser, lhs, bp, BinaryOperator::Or)
}

0 comments on commit 9a2d40b

Please sign in to comment.