From c3323ccd299f386a64ae7adab54a4601e5e7ef63 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 4 Aug 2023 21:53:12 -0700 Subject: [PATCH] Added user function evaluation --- src/evaluate/operator.rs | 48 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/evaluate/operator.rs b/src/evaluate/operator.rs index 2ee61f5..c64efde 100644 --- a/src/evaluate/operator.rs +++ b/src/evaluate/operator.rs @@ -5,15 +5,59 @@ use crate::parser::Operator; use crate::parser::Expression; use crate::context::Context; use crate::errors::DaisyError; +use super::evaluate; -pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result, (LineLocation, DaisyError)> { +pub fn eval_operator(g: &Expression, context: &mut Context) -> Result, (LineLocation, DaisyError)> { let Expression::Operator(op_loc, op, args) = g else {panic!()}; match op { Operator::Function(_) => unreachable!("Functions are handled seperately."), - Operator::UserFunction(_) => unimplemented!(), + + Operator::UserFunction(s) => { + let (sh_vars, exp) = context.get_function(s).unwrap(); + + if args.len() != 1 {panic!()}; + let a = &args[0]; + + if sh_vars.len() == 1 { + if let Expression::Tuple(l, v) = a { + return Err(( + *l + *op_loc, + DaisyError::BadArguments(s.clone(), 1, v.len()) + )) + }; + + context.add_shadow(sh_vars[0].clone(), Some(a.clone())); + } else { + let Expression::Tuple(l, v) = a else { + return Err(( + a.get_linelocation() + *op_loc, + DaisyError::BadArguments(s.clone(), sh_vars.len(), 1) + )); + }; + + if sh_vars.len() != v.len() { + return Err(( + *l + *op_loc, + DaisyError::BadArguments(s.clone(), sh_vars.len(), v.len()) + )); + } + + let mut i = 0; + while i < sh_vars.len() { + context.add_shadow(sh_vars[i].clone(), Some(v[i].clone())); + i += 1; + } + } + + + let r = evaluate(&exp, context)?; + context.clear_shadow(); + + return Ok(Some(r)); + }, Operator::Tuple => { if args.len() != 2 { panic!() };