Added user function evaluation

pull/2/head
Mark 2023-08-04 21:53:12 -07:00
parent 4cb45432f9
commit c3323ccd29
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 46 additions and 2 deletions

View File

@ -5,15 +5,59 @@ use crate::parser::Operator;
use crate::parser::Expression; use crate::parser::Expression;
use crate::context::Context; use crate::context::Context;
use crate::errors::DaisyError; use crate::errors::DaisyError;
use super::evaluate;
pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Expression>, (LineLocation, DaisyError)> { pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Expression>, (LineLocation, DaisyError)> {
let Expression::Operator(op_loc, op, args) = g else {panic!()}; let Expression::Operator(op_loc, op, args) = g else {panic!()};
match op { match op {
Operator::Function(_) => unreachable!("Functions are handled seperately."), 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 => { Operator::Tuple => {
if args.len() != 2 { panic!() }; if args.len() != 2 { panic!() };