aboutsummaryrefslogtreecommitdiff
path: root/src/math/expression_function.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-01-19 19:06:09 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2024-01-19 19:06:09 +0100
commit52e4dfc10616accaa6e4dcdbfc4a706e94dbcdc9 (patch)
tree3f6c7d7886774f1c10b77e7782b071aca7d4d31a /src/math/expression_function.rs
parent22c656d0be6dd5356f8839fae716174a9fe474c7 (diff)
math: pregenerate eval tree
Diffstat (limited to 'src/math/expression_function.rs')
-rw-r--r--src/math/expression_function.rs10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/math/expression_function.rs b/src/math/expression_function.rs
index 20f4816..44f83d1 100644
--- a/src/math/expression_function.rs
+++ b/src/math/expression_function.rs
@@ -5,6 +5,7 @@ use crate::math::{
complex::Complex,
context::Context,
expression::Expression,
+ operation::Operation,
function::{Function, FunctionArgument, EmptyFunction}
};
@@ -16,7 +17,7 @@ pub struct ExpressionFunction {
}
impl ExpressionFunction {
- pub fn from_string(str: String) -> Self {
+ pub fn from_string(str: String, operations: &Vec<Operation>) -> Self {
let str = str.replace(' ', "");
let (lhs, expr) = str.split_once('=').unwrap();
let (name, a) = lhs.split_once('(').unwrap();
@@ -25,7 +26,7 @@ impl ExpressionFunction {
.map(|s| s.to_string())
.collect();
Self {
- expr: Expression::from_string(expr),
+ expr: Expression::from_string(expr, operations),
name: name.to_string(),
args,
}
@@ -41,10 +42,11 @@ impl ExpressionFunction {
}
impl Function for ExpressionFunction {
- fn eval(&self, args: FunctionArgument, ctx: &Context) -> Result<Complex, String> {
+ fn eval(&self, args: &FunctionArgument, ctx: &Context) -> Result<Complex, String> {
if args.len() == self.args.len() {
let mut nctx = ctx.clone();
- for (n, v) in zip(self.args.iter(), args.data().iter()) {
+ let vargs = args.eval_args(ctx)?;
+ for (n, v) in zip(self.args.iter(), vargs.iter()) {
nctx.set_variable(n, *v)
}
nctx.set_function(self.name(), Arc::new(EmptyFunction {}));