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 d2629bf commit 8af81f8
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ pub enum TokenKind {
Equality,
/// An inequality sign; `!=`.
Inequality,
/// An and sign; `&&`.
And,
/// An or sign; `||`.
Or,

/// An at sign; `@`.
At,
Expand Down Expand Up @@ -211,6 +215,8 @@ impl Display for TokenKind {
TokenKind::Question => write!(f, "`?`"),
TokenKind::Pipe => write!(f, "`|`"),
TokenKind::Caret => write!(f, "`^`"),
TokenKind::And => todo!("`&&`"),
TokenKind::Or => todo!("`||`"),
}
}
}
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 = "1 / ( 1 * 1 )\n";
let input = "2 == 12;\n";

let mut parser = Parser::new(input);
let symbol = match parser.parse() {
Expand Down
19 changes: 19 additions & 0 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub enum Expression<'de> {
left: Box<Expression<'de>>,
right: Box<Expression<'de>>,
},
Unary {
operator: UnaryOperator,
operand: Box<Expression<'de>>,
},
Group(Box<Expression<'de>>),
}

Expand All @@ -37,4 +41,19 @@ pub enum BinaryOperator {
Subtract,
Multiply,
Divide,
Modulo,
Equal,
NotEqual,
LessThan,
LessThanOrEqual,
GreaterThan,
GreaterThanOrEqual,
And,
Or,
}

#[derive(Debug)]
pub enum UnaryOperator {
Negate,
Negative,
}
126 changes: 126 additions & 0 deletions src/parser/expression/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,129 @@ pub fn division<'de>(
right: Box::new(rhs),
})
}

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),
})
}

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),
})
}

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),
})
}

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),
})
}

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),
})
}

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),
})
}

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),
})
}

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),
})
}

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),
})
}
1 change: 1 addition & 0 deletions src/parser/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{

pub mod binary;
pub mod primitive;
pub mod unary;

pub fn parse<'de>(
parser: &mut Parser<'de>,
Expand Down
33 changes: 33 additions & 0 deletions src/parser/expression/unary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::{
lexer::TokenKind,
parser::{
ast::{Expression, UnaryOperator},
lookup::BindingPower,
Parser,
},
};
use miette::Result;

pub fn negate<'de>(parser: &mut Parser<'de>) -> Result<Expression<'de>> {
parser
.lexer
.expect(TokenKind::Not, "expected a negate operator")?;
let expression = crate::parser::expression::parse(parser, BindingPower::None)?;

Ok(Expression::Unary {
operator: UnaryOperator::Negate,
operand: Box::new(expression),
})
}

pub fn negative<'de>(parser: &mut Parser<'de>) -> Result<Expression<'de>> {
parser
.lexer
.expect(TokenKind::Minus, "expected a negative operator")?;
let expression = crate::parser::expression::parse(parser, BindingPower::None)?;

Ok(Expression::Unary {
operator: UnaryOperator::Negative,
operand: Box::new(expression),
})
}
47 changes: 47 additions & 0 deletions src/parser/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ impl<'de> Default for Lookup<'de> {
.add_expression_handler(TokenKind::Decimal, expression::primitive::decimal)
.add_expression_handler(TokenKind::ParenOpen, group)
.add_expression_handler(TokenKind::If, if_)
.add_expression_handler(TokenKind::Not, expression::unary::negate)
.add_expression_handler(TokenKind::Minus, expression::unary::negative)
.add_left_expression_handler(
TokenKind::Plus,
BindingPower::Additive,
Expand All @@ -133,6 +135,51 @@ impl<'de> Default for Lookup<'de> {
BindingPower::Multiplicative,
expression::binary::division,
)
.add_left_expression_handler(
TokenKind::Equality,
BindingPower::Assignment,
expression::binary::equal,
)
.add_left_expression_handler(
TokenKind::Inequality,
BindingPower::Assignment,
expression::binary::not_equal,
)
.add_left_expression_handler(
TokenKind::LessThan,
BindingPower::Relational,
expression::binary::less_than,
)
.add_left_expression_handler(
TokenKind::LessThanOrEqual,
BindingPower::Relational,
expression::binary::less_than_or_equal,
)
.add_left_expression_handler(
TokenKind::GreaterThan,
BindingPower::Relational,
expression::binary::greater_than,
)
.add_left_expression_handler(
TokenKind::GreaterThanOrEqual,
BindingPower::Relational,
expression::binary::greater_than_or_equal,
)
.add_left_expression_handler(
TokenKind::Percent,
BindingPower::Multiplicative,
expression::binary::modulo,
)
.add_left_expression_handler(
TokenKind::And,
BindingPower::Relational,
expression::binary::and,
)
.add_left_expression_handler(
TokenKind::Or,
BindingPower::Relational,
expression::binary::or,
)
}
}

Expand Down

0 comments on commit 8af81f8

Please sign in to comment.