From fe539a637e7853d161a0ba4d28c0c87743bd0d29 Mon Sep 17 00:00:00 2001 From: Book-reader Date: Fri, 5 Sep 2025 16:11:11 +1200 Subject: [PATCH] Fix build --- asklyphe-frontend/src/math.rs | 42 ++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/asklyphe-frontend/src/math.rs b/asklyphe-frontend/src/math.rs index 101a7c7..84a2022 100644 --- a/asklyphe-frontend/src/math.rs +++ b/asklyphe-frontend/src/math.rs @@ -20,13 +20,19 @@ pub fn calculate(query: &str) -> Option { #[derive(Debug)] enum Token { Op(Op), - Atom(f64), + Atom(Atom), /* Number(f64), Func(Func),*/ } #[derive(Debug)] enum Op { + BinOp(BinOp), + Func(Func), // A function is an Op that takes whatever the next thing is and binds it, either the next number or whatever is in parens +} + +#[derive(Debug)] +enum BinOp { Add, Subtract, Multiply, @@ -34,12 +40,12 @@ enum Op { Exponent, LParen, RParen, - Func(Func) } #[derive(Debug)] enum Atom { - Number(f64), + Number(f64), // TODO: use the unlimited precision floats library instead + Const(Const), } #[derive(Debug)] @@ -58,6 +64,12 @@ enum Func { SquareRoot, } +#[derive(Debug)] +enum Const { + Pi, + E, +} + #[derive(Debug)] enum LexErr { Eof, @@ -75,7 +87,7 @@ struct Lexer<'a> { // TODO: refactor with iterator that returns Option(Token) where one token option is Eof (or a enum of Token(Token) and Eof, or just Option(Option(Token))) impl Lexer<'_> { - fn new(data: &str) -> Lexer { Lexer {data, data_ptr: data, idx: 0} } + fn new(data: &str) -> Lexer<'_> { Lexer {data, data_ptr: data, idx: 0} } fn next(&mut self) -> Result { match self.data.chars().nth(self.idx) { @@ -86,22 +98,22 @@ impl Lexer<'_> { // TODO: make more efficient self.data_ptr = &self.data[self.idx..]; match val { - '+' => Ok(Token::Add), - '-' => Ok(Token::Subtract), - '×' | '*' => Ok(Token::Multiply), - '÷' | '/' => Ok(Token::Divide), - '^' => Ok(Token::Exponent), - '(' => Ok(Token::LParen), - ')' => Ok(Token::RParen), + '+' => Ok(Token::Op(Op::BinOp(BinOp::Add))), + '-' => Ok(Token::Op(Op::BinOp(BinOp::Subtract))), + '×' | '*' => Ok(Token::Op(Op::BinOp(BinOp::Multiply))), + '÷' | '/' => Ok(Token::Op(Op::BinOp(BinOp::Divide))), + '^' => Ok(Token::Op(Op::BinOp(BinOp::Exponent))), + '(' => Ok(Token::Op(Op::BinOp(BinOp::LParen))), + ')' => Ok(Token::Op(Op::BinOp(BinOp::RParen))), _ if val.is_whitespace() => self.next(), - // TODO: parse - as part of number so I can do '1 + -1' and similar + // TODO: maybe parse '-' as part of number so I can do '1 + -1' and similar _ if val.is_digit(10) => { let start = self.idx - 1; self.data_ptr.chars().take_while(|c| c.is_digit(10)).for_each(|_| self.idx += 1);//.next().unwrap_or(' ').is_digit(10) {self.idx += 1;} match self.data[start..self.idx].parse() { - Ok(val) => Ok(Token::Number(val)), + Ok(val) => Ok(Token::Atom(Atom::Number(val))), Err(e) => Err(LexErr::Invalid), } }, @@ -149,8 +161,8 @@ impl Parser<'_> { } fn parse_expr(&mut self, min_bp: f64) -> Result { - while let Ok(val) = self.lexer.next() {debug!("token: {:?}", )} - match self.lex.next().ok()? { + while let Ok(val) = self.lex.next() {debug!("token: {:?}", val)} + match self.lex.next().err() { _ => return Err(ParseErr::Invalid), }