From 95ba627a9c5e4e6e1d79f7aaff1854eb831511b4 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 17 Jan 2024 23:54:52 +0100 Subject: hand context on to Function trait --- src/function.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/function.rs') diff --git a/src/function.rs b/src/function.rs index 0da4574..126477c 100644 --- a/src/function.rs +++ b/src/function.rs @@ -1,4 +1,4 @@ -use crate::complex::Complex; +use crate::{complex::Complex, context::Context}; pub struct FunctionArgument { args : Vec, @@ -23,16 +23,16 @@ impl FunctionArgument { } pub trait Function { - fn eval(&self, args: FunctionArgument) -> Result; + fn eval(&self, args: FunctionArgument, ctx: &Context) -> Result; } #[macro_export] macro_rules! functions { {$($x:expr => $y:expr), *} => { { - let mut h : HashMap> = HashMap::new(); + let mut h : HashMap> = HashMap::new(); $( - h.insert($x.to_string(), Box::new($y)); + h.insert($x.to_string(), Arc::new($y)); )* h } @@ -45,7 +45,7 @@ impl Function for T where T: Fn(Complex) -> Complex, { - fn eval(&self, args: FunctionArgument) -> Result { + fn eval(&self, args: FunctionArgument, _ctx: &Context) -> Result { if args.len() == 1 { Ok(self(args.get(0))) } else { @@ -53,3 +53,12 @@ where } } } + + +pub struct EmptyFunction {} + +impl Function for EmptyFunction { + fn eval(&self, _args: FunctionArgument, _ctx: &Context) -> Result { + Err("function not implemented in this scope".to_string()) + } +} -- cgit v1.2.3-70-g09d2