Fix build

This commit is contained in:
Book-reader 2025-09-05 16:11:11 +12:00
parent 0725850ad0
commit fe539a637e

View file

@ -20,13 +20,19 @@ pub fn calculate(query: &str) -> Option<Calculation> {
#[derive(Debug)] #[derive(Debug)]
enum Token { enum Token {
Op(Op), Op(Op),
Atom(f64), Atom(Atom),
/* Number(f64), /* Number(f64),
Func(Func),*/ Func(Func),*/
} }
#[derive(Debug)] #[derive(Debug)]
enum Op { 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, Add,
Subtract, Subtract,
Multiply, Multiply,
@ -34,12 +40,12 @@ enum Op {
Exponent, Exponent,
LParen, LParen,
RParen, RParen,
Func(Func)
} }
#[derive(Debug)] #[derive(Debug)]
enum Atom { enum Atom {
Number(f64), Number(f64), // TODO: use the unlimited precision floats library instead
Const(Const),
} }
#[derive(Debug)] #[derive(Debug)]
@ -58,6 +64,12 @@ enum Func {
SquareRoot, SquareRoot,
} }
#[derive(Debug)]
enum Const {
Pi,
E,
}
#[derive(Debug)] #[derive(Debug)]
enum LexErr { enum LexErr {
Eof, 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))) // 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<'_> { 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<Token, LexErr> { fn next(&mut self) -> Result<Token, LexErr> {
match self.data.chars().nth(self.idx) { match self.data.chars().nth(self.idx) {
@ -86,22 +98,22 @@ impl Lexer<'_> {
// TODO: make more efficient // TODO: make more efficient
self.data_ptr = &self.data[self.idx..]; self.data_ptr = &self.data[self.idx..];
match val { match val {
'+' => Ok(Token::Add), '+' => Ok(Token::Op(Op::BinOp(BinOp::Add))),
'-' => Ok(Token::Subtract), '-' => Ok(Token::Op(Op::BinOp(BinOp::Subtract))),
'×' | '*' => Ok(Token::Multiply), '×' | '*' => Ok(Token::Op(Op::BinOp(BinOp::Multiply))),
'÷' | '/' => Ok(Token::Divide), '÷' | '/' => Ok(Token::Op(Op::BinOp(BinOp::Divide))),
'^' => Ok(Token::Exponent), '^' => Ok(Token::Op(Op::BinOp(BinOp::Exponent))),
'(' => Ok(Token::LParen), '(' => Ok(Token::Op(Op::BinOp(BinOp::LParen))),
')' => Ok(Token::RParen), ')' => Ok(Token::Op(Op::BinOp(BinOp::RParen))),
_ if val.is_whitespace() => self.next(), _ 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) => { _ if val.is_digit(10) => {
let start = self.idx - 1; 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;} 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() { 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), Err(e) => Err(LexErr::Invalid),
} }
}, },
@ -149,8 +161,8 @@ impl Parser<'_> {
} }
fn parse_expr(&mut self, min_bp: f64) -> Result<Expr, ParseErr> { fn parse_expr(&mut self, min_bp: f64) -> Result<Expr, ParseErr> {
while let Ok(val) = self.lexer.next() {debug!("token: {:?}", )} while let Ok(val) = self.lex.next() {debug!("token: {:?}", val)}
match self.lex.next().ok()? { match self.lex.next().err() {
_ => return Err(ParseErr::Invalid), _ => return Err(ParseErr::Invalid),
} }