From 1f6b59bdae24b27e3f3dfcc4cd7a35d3fc91fa83 Mon Sep 17 00:00:00 2001 From: bakk Date: Tue, 1 Jun 2021 15:52:41 +0200 Subject: [PATCH] Added prod function and proper sum symbol --- kalk/src/interpreter.rs | 27 ++++++++++++++++++++++----- kalk/src/lexer.rs | 1 + kalk/src/prelude/mod.rs | 3 +++ kalk_cli/src/repl.rs | 2 ++ kalk_web/src/KalkCalculator.svelte | 8 ++++++-- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/kalk/src/interpreter.rs b/kalk/src/interpreter.rs index 4701555..d57f77f 100644 --- a/kalk/src/interpreter.rs +++ b/kalk/src/interpreter.rs @@ -310,25 +310,42 @@ pub(crate) fn eval_fn_call_expr( // Special functions match identifier.full_name.as_ref() { - "sum" | "Σ" => { + "sum" | "Σ" | "∑" | "prod" | "∏" => { // Make sure exactly 3 arguments were supplied. if expressions.len() != 3 { return Err(CalcError::IncorrectAmountOfArguments( 3, - "sum".into(), + "sum/prod".into(), expressions.len(), )); } let start = eval_expr(context, &expressions[0], "")?.to_f64() as i128; let end = eval_expr(context, &expressions[1], "")?.to_f64() as i128; - let mut sum = KalkNum::default(); + let sum_else_prod = match identifier.full_name.as_ref() { + "sum" => true, + "Σ" => true, + "∑" => true, + "prod" => false, + "∏" => false, + _ => unreachable!(), + }; + let mut sum = if sum_else_prod { + KalkNum::default() + } else { + KalkNum::from(1f64) + }; for n in start..=end { context.sum_n_value = Some(n); let eval = eval_expr(context, &expressions[2], "")?; - sum.value += eval.value; - sum.imaginary_value += eval.imaginary_value; + if sum_else_prod { + sum.value += eval.value; + sum.imaginary_value += eval.imaginary_value; + } else { + sum.value *= eval.value; + sum.imaginary_value *= eval.imaginary_value; + } } context.sum_n_value = None; diff --git a/kalk/src/lexer.rs b/kalk/src/lexer.rs index f9aa5cb..54af437 100644 --- a/kalk/src/lexer.rs +++ b/kalk/src/lexer.rs @@ -143,6 +143,7 @@ impl<'a> Lexer<'a> { 'τ' => build(TokenKind::Identifier, "τ", span), 'ϕ' => build(TokenKind::Identifier, "ϕ", span), 'Γ' => build(TokenKind::Identifier, "Γ", span), + '∏' => build(TokenKind::Identifier, "Γ", span), _ => build(TokenKind::Unknown, "", span), }; diff --git a/kalk/src/prelude/mod.rs b/kalk/src/prelude/mod.rs index 0e2bee2..db2b75c 100644 --- a/kalk/src/prelude/mod.rs +++ b/kalk/src/prelude/mod.rs @@ -161,6 +161,9 @@ impl BinaryFuncInfo { pub fn is_prelude_func(identifier: &str) -> bool { identifier == "sum" || identifier == "Σ" + || identifier == "∑" + || identifier == "prod" + || identifier == "∏" || identifier == "integrate" || identifier == "integral" || identifier == "∫" diff --git a/kalk_cli/src/repl.rs b/kalk_cli/src/repl.rs index b9caf5d..3a736c8 100644 --- a/kalk_cli/src/repl.rs +++ b/kalk_cli/src/repl.rs @@ -115,6 +115,7 @@ lazy_static! { m.insert("floor", "⌊⌋"); m.insert("gamma", "Γ"); m.insert("sum", "Σ()"); + m.insert("prod", "∏()"); m.insert("integrate", "∫()"); m.insert("integral", "∫()"); m.insert("phi", "ϕ"); @@ -152,6 +153,7 @@ impl Completer for RLHelper { line.insert_str(line.pos(), elected); line.move_forward(match elected { "Σ()" => 2, + "∏()" => 2, "∫()" => 2, _ => 1, }); diff --git a/kalk_web/src/KalkCalculator.svelte b/kalk_web/src/KalkCalculator.svelte index 5286b28..38d9cfb 100644 --- a/kalk_web/src/KalkCalculator.svelte +++ b/kalk_web/src/KalkCalculator.svelte @@ -33,7 +33,7 @@ "π", ",", "%", - "Σ", + "∑", "⌊", "⌈", "∫", @@ -272,7 +272,11 @@ break; } case "sum": { - newSubstring = "Σ"; + newSubstring = "∑"; + break; + } + case "prod": { + newSubstring = "∏"; break; } case "integrate": {