feat: completed parser
This commit is contained in:
parent
f05002fc50
commit
3cc81812e6
@ -2,6 +2,6 @@ pub mod lexer;
|
|||||||
pub mod errors;
|
pub mod errors;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
|
||||||
pub fn test() {
|
pub fn calculate(expression: &str) -> Result<f64, parser::errors::ParserErrors> {
|
||||||
println!("{}", "hi there");
|
Ok(parser::parse(lexer::Lexer::new(expression))?)
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
use s5_cb_calculator::lexer::Lexer;
|
use s5_cb_calculator::lexer::Lexer;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut lexer = Lexer::new("15+(30^2-5)*2/4".chars().collect());
|
println!("Hi there! Welcome to OverComplicatedCalculator v1.0!");
|
||||||
while let Ok(Some(token)) = lexer.next() {
|
println!("To have OCC calculate something, just type it in!");
|
||||||
println!("Token: {:?}", token)
|
|
||||||
}
|
|
||||||
println!("Hello, world!");
|
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use crate::lexer::tokens;
|
|||||||
use crate::parser::errors::ParserErrors;
|
use crate::parser::errors::ParserErrors;
|
||||||
use crate::parser::errors::ParserErrors::UnexpectedTokenError;
|
use crate::parser::errors::ParserErrors::UnexpectedTokenError;
|
||||||
|
|
||||||
mod errors;
|
pub mod errors;
|
||||||
|
|
||||||
type Result<T> = result::Result<T, errors::ParserErrors>;
|
type Result<T> = result::Result<T, errors::ParserErrors>;
|
||||||
|
|
||||||
@ -47,6 +47,7 @@ pub fn parse(mut lexer: lexer::Lexer) -> Result<f64> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn s_proc(tbox: &mut TokenBox) -> Result<f64> {
|
pub fn s_proc(tbox: &mut TokenBox) -> Result<f64> {
|
||||||
|
println!("Proc S");
|
||||||
let mut result = a_proc(tbox)?;
|
let mut result = a_proc(tbox)?;
|
||||||
match tbox.read_token()? {
|
match tbox.read_token()? {
|
||||||
None => Ok(result),
|
None => Ok(result),
|
||||||
@ -55,35 +56,48 @@ pub fn s_proc(tbox: &mut TokenBox) -> Result<f64> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn a_proc(tbox: &mut TokenBox) -> Result<f64> {
|
fn a_proc(tbox: &mut TokenBox) -> Result<f64> {
|
||||||
|
println!("Proc A");
|
||||||
let mut result = m_proc(tbox)?;
|
let mut result = m_proc(tbox)?;
|
||||||
match &tbox.read_token()? {
|
match &tbox.read_token()? {
|
||||||
Some(Token::OP(_, OpType::ADD)) => Ok(result + a_proc(tbox)?),
|
Some(Token::OP(_, OpType::ADD)) => Ok(result + a_proc(tbox)?),
|
||||||
Some(Token::OP(_, OpType::SUB)) => Ok(result - a_proc(tbox)?),
|
Some(Token::OP(_, OpType::SUB)) => Ok(result - a_proc(tbox)?),
|
||||||
None => Ok(result),
|
None => Ok(result),
|
||||||
Some(token) => Err(ParserErrors::UnexpectedTokenError(token.clone(), String::from("A")))
|
Some(token) => {
|
||||||
|
tbox.regress();
|
||||||
|
Ok(result)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn m_proc(tbox: &mut TokenBox) -> Result<f64> {
|
fn m_proc(tbox: &mut TokenBox) -> Result<f64> {
|
||||||
|
println!("Proc M");
|
||||||
let mut result = g_proc(tbox)?;
|
let mut result = g_proc(tbox)?;
|
||||||
match &tbox.read_token()? {
|
match &tbox.read_token()? {
|
||||||
Some(Token::OP(_, OpType::MUL)) => Ok(result * m_proc(tbox)?),
|
Some(Token::OP(_, OpType::MUL)) => Ok(result * m_proc(tbox)?),
|
||||||
Some(Token::OP(_, OpType::DIV)) => Ok(result / m_proc(tbox)?),
|
Some(Token::OP(_, OpType::DIV)) => Ok(result / m_proc(tbox)?),
|
||||||
None => Ok(result),
|
None => Ok(result),
|
||||||
Some(token) => Err(UnexpectedTokenError(token.clone(), String::from("M"))),
|
Some(token) => {
|
||||||
|
tbox.regress();
|
||||||
|
Ok(result)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn g_proc(tbox: &mut TokenBox) -> Result<f64> {
|
fn g_proc(tbox: &mut TokenBox) -> Result<f64> {
|
||||||
|
println!("Proc G");
|
||||||
let mut result = p_proc(tbox)?;
|
let mut result = p_proc(tbox)?;
|
||||||
match tbox.read_token()? {
|
match tbox.read_token()? {
|
||||||
Some(Token::OP(_, OpType::POW)) => Ok(result.powf(g_proc(tbox)?)),
|
Some(Token::OP(_, OpType::POW)) => Ok(result.powf(g_proc(tbox)?)),
|
||||||
None => Ok(result),
|
None => Ok(result),
|
||||||
Some(token) => Err(UnexpectedTokenError(token.clone(), String::from("G"))),
|
Some(token) => {
|
||||||
|
tbox.regress();
|
||||||
|
Ok(result)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn p_proc(tbox: &mut TokenBox) -> Result<f64> {
|
fn p_proc(tbox: &mut TokenBox) -> Result<f64> {
|
||||||
|
println!("Proc P");
|
||||||
match expect_token(tbox.read_token()?)? {
|
match expect_token(tbox.read_token()?)? {
|
||||||
Token::OBR(_) => {
|
Token::OBR(_) => {
|
||||||
let result = a_proc(tbox)?;
|
let result = a_proc(tbox)?;
|
||||||
@ -103,9 +117,53 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_formula() -> Result<()> {
|
fn simple_formula() -> Result<()> {
|
||||||
let res = parse(lexer::Lexer::new("15+3"))?;
|
let res = parse(lexer::Lexer::new("15+3-5*2"))?;
|
||||||
println!("res: {}", res);
|
println!("res: {}", res);
|
||||||
assert_eq!(res, 18.0);
|
assert_eq!(res, 8.0);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn skript_formula_1() -> Result<()> {
|
||||||
|
let res = parse(lexer::Lexer::new("2+5/3*2"))?;
|
||||||
|
assert_eq!(res, 5.3333333);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn skript_formula_2() -> Result<()> {
|
||||||
|
let res = parse(lexer::Lexer::new("(2+5)/3"))?;
|
||||||
|
assert_eq!(res, 2.3333333333333335);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn skript_formula_3() -> Result<()> {
|
||||||
|
let res = parse(lexer::Lexer::new("((10-213)*25)+27"))?;
|
||||||
|
assert_eq!(res, -5048.0);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn skript_formula_4() -> Result<()> {
|
||||||
|
let res = parse(lexer::Lexer::new("(7-3)*4^3"))?;
|
||||||
|
assert_eq!(res, 256.0);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn skript_error_1() {
|
||||||
|
assert!(matches!(
|
||||||
|
parse(lexer::Lexer::new("((1+1)*1-2")),
|
||||||
|
Err(ParserErrors::MissingTokenError)
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn skript_error_2() {
|
||||||
|
assert!(matches!(
|
||||||
|
parse(lexer::Lexer::new("a+1*3")),
|
||||||
|
Err(ParserErrors::LexerError(lexer::errors::LexerErrors::InvalidTokenError { token: _, pos: _ }))
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user