add eval command

This commit is contained in:
2024-10-12 01:36:39 -04:00
parent df83422811
commit 806fd95ab8
4 changed files with 176 additions and 0 deletions

23
src/commands/eval/mod.rs Normal file
View File

@@ -0,0 +1,23 @@
use crate::common::{Context, Error};
mod tokenizer;
mod parse;
fn evaluate(expr: &str) -> Result<f64, Error> {
let tokens = tokenizer::Token::tokenize(expr)?;
let mut tokens = tokens.iter();
let tree = parse::ParseTree::new(&mut tokens)?;
Ok(tree.evaluate())
}
/// Evaluates an expression (uses Polish Notation)
#[poise::command(slash_command, prefix_command)]
pub async fn eval(ctx: Context<'_>,
#[rest]
expr: String) -> Result<(), Error>
{
ctx.reply(format!("{}", evaluate(&expr)?)).await?;
Ok(())
}