diff options
Diffstat (limited to 'src/expression.rs')
| -rw-r--r-- | src/expression.rs | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/src/expression.rs b/src/expression.rs index 5f2660b..4029f93 100644 --- a/src/expression.rs +++ b/src/expression.rs @@ -1,3 +1,4 @@ +use crate::complex::Complex; use crate::context::Context; use crate::function::FunctionArgument; use crate::string::{ContainsAndSkipBrackets, SplitMatchingBracket}; @@ -13,7 +14,7 @@ impl Expression { } } - pub fn evaluate(&self, context: &Context) -> Result<f64, String> { + pub fn evaluate(&self, context: &Context) -> Result<Complex, String> { let (repr, oprepr) = { if self.repr.starts_with('(') { let (oprepr, r) = self.repr.split_on_matching_bracket(); @@ -44,30 +45,24 @@ impl Expression { let first_expr = Expression::from_string(first); let rest_expr = Expression::from_string(rest); Ok(op.evaluate(first_expr.evaluate(context)?, rest_expr.evaluate(context)?)) - } else { - if let Ok(r) = repr.parse::<f64>() { - Ok(r) - } else { - if let Some((func, args)) = repr.split_once('(') { - let mut argv = Vec::new(); + } else if let Ok(r) = repr.parse::<Complex>() { + Ok(r) + } else if let Some((func, args)) = repr.split_once('(') { + let mut argv = Vec::new(); - for arg in args[..args.len() - 1].split(',') { - argv.push(Expression::from_string(arg).evaluate(context)?); - } + for arg in args[..args.len() - 1].split(',') { + argv.push(Expression::from_string(arg).evaluate(context)?); + } - if let Some(func) = context.function(func) { - Ok(func.eval(FunctionArgument::new(argv))) - } else { - Err(format!("function '{func}' not found")) - } - } else { - if let Some(res) = context.variable(&repr) { - Ok(*res) - } else { - Err(format!("variable '{repr}' not found")) - } - } + if let Some(func) = context.function(func) { + Ok(func.eval(FunctionArgument::new(argv))?) + } else { + Err(format!("function '{func}' not found")) } + } else if let Some(res) = context.variable(&repr) { + Ok(*res) + } else { + Err(format!("variable '{repr}' not found")) } } } |