logos calculator example
This commit is contained in:
parent
d974798fcb
commit
0c30f0022d
9 changed files with 414 additions and 22 deletions
56
src/parser.rs
Normal file
56
src/parser.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
use chumsky::{prelude::{just, recursive}, recursive, select, IterParser, Parser};
|
||||
|
||||
use crate::{ast::Expression, tokens::Token};
|
||||
|
||||
|
||||
#[allow(clippy::let_and_return)]
|
||||
/* ANCHOR: parser */
|
||||
pub fn parser<'src>(
|
||||
) -> impl Parser<'src, &'src [Token<'src>], Expression, chumsky::extra::Err<chumsky::error::Simple<'src, Token<'src>>>>
|
||||
{
|
||||
recursive(
|
||||
|p|
|
||||
{
|
||||
let atom = {
|
||||
let parenthesized = p
|
||||
.clone()
|
||||
.delimited_by(just(Token::ParenBegin), just(Token::ParenEnd));
|
||||
|
||||
let integer = select! {
|
||||
Token::Integer(n) => Expression::Integer(n),
|
||||
};
|
||||
|
||||
parenthesized.or(integer)
|
||||
};
|
||||
|
||||
let unary = just(Token::Substract)
|
||||
.repeated()
|
||||
.foldr(atom, |_op, rhs| Expression::Negate(Box::new(rhs)));
|
||||
|
||||
let binary_1 = unary.clone().foldl(
|
||||
just(Token::Multiply)
|
||||
.or(just(Token::Divide))
|
||||
.then(unary)
|
||||
.repeated(),
|
||||
|lhs, (op, rhs)| match op {
|
||||
Token::Multiply => Expression::Multiply(Box::new(lhs), Box::new(rhs)),
|
||||
Token::Divide => Expression::Divide(Box::new(lhs), Box::new(rhs)),
|
||||
_ => unreachable!(),
|
||||
},
|
||||
);
|
||||
|
||||
let binary_2 = binary_1.clone().foldl(
|
||||
just(Token::Add)
|
||||
.or(just(Token::Substract))
|
||||
.then(binary_1)
|
||||
.repeated(),
|
||||
|lhs, (op, rhs)| match op {
|
||||
Token::Add => Expression::Add(Box::new(lhs), Box::new(rhs)),
|
||||
Token::Substract => Expression::Substract(Box::new(lhs), Box::new(rhs)),
|
||||
_ => unreachable!(),
|
||||
},
|
||||
);
|
||||
|
||||
binary_2
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue