From 44174c985033e2ba5013b570c23df1b3b18fbb5d Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Fri, 7 Jan 2022 01:04:43 +0100 Subject: [PATCH] Allow matrices in prelude functions --- kalk/src/interpreter.rs | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/kalk/src/interpreter.rs b/kalk/src/interpreter.rs index 52563a8..a0fea36 100644 --- a/kalk/src/interpreter.rs +++ b/kalk/src/interpreter.rs @@ -352,15 +352,18 @@ pub(crate) fn eval_fn_call_expr( if result.is_nan() && expressions.len() == 1 { let x = eval_expr(context, &expressions[0], "")?; - // If a vector was given, call the function on every item - // in the vector. + // If a vector/matrix was given, call the function on every item + // in the vector/matrix. if let KalkValue::Vector(values) = x { let mut new_values = Vec::new(); let mut success = true; for value in values { - if let Some(result) = - prelude::call_unary_func(context, &identifier.full_name, value, "") - { + if let Some(result) = prelude::call_unary_func( + context, + &identifier.full_name, + value, + &context.angle_unit.clone(), + ) { new_values.push(result.0); } else { success = false; @@ -371,6 +374,31 @@ pub(crate) fn eval_fn_call_expr( if success { return Ok(KalkValue::Vector(new_values)); } + } else if let KalkValue::Matrix(rows) = x { + let mut new_rows = Vec::new(); + let mut success = true; + for row in rows { + let mut new_row = Vec::new(); + for value in row { + if let Some(result) = prelude::call_unary_func( + context, + &identifier.full_name, + value, + &context.angle_unit.clone(), + ) { + new_row.push(result.0); + } else { + success = false; + break; + } + } + + new_rows.push(new_row); + } + + if success { + return Ok(KalkValue::Matrix(new_rows)); + } } }