mirror of
https://github.com/PaddiM8/kalker.git
synced 2024-11-07 16:34:21 +01:00
Added prod function and proper sum symbol
This commit is contained in:
parent
349aeb7f0b
commit
e07c5e5339
@ -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;
|
||||
|
@ -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),
|
||||
};
|
||||
|
||||
|
@ -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 == "∫"
|
||||
|
@ -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,
|
||||
});
|
||||
|
@ -33,7 +33,7 @@
|
||||
"π",
|
||||
",",
|
||||
"%",
|
||||
"Σ",
|
||||
"∑",
|
||||
"⌊",
|
||||
"⌈",
|
||||
"∫",
|
||||
@ -272,7 +272,11 @@
|
||||
break;
|
||||
}
|
||||
case "sum": {
|
||||
newSubstring = "Σ";
|
||||
newSubstring = "∑";
|
||||
break;
|
||||
}
|
||||
case "prod": {
|
||||
newSubstring = "∏";
|
||||
break;
|
||||
}
|
||||
case "integrate": {
|
||||
|
Loading…
Reference in New Issue
Block a user