added autosum
This commit is contained in:
15
src/lib.rs
15
src/lib.rs
@@ -62,6 +62,7 @@ enum Words {
|
|||||||
Over,
|
Over,
|
||||||
ClearStack,
|
ClearStack,
|
||||||
Emit,
|
Emit,
|
||||||
|
Sum,
|
||||||
DefineStart,
|
DefineStart,
|
||||||
DefineEnd,
|
DefineEnd,
|
||||||
Definition(Definition),
|
Definition(Definition),
|
||||||
@@ -301,6 +302,16 @@ impl RPN {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sum(&mut self) -> Result {
|
||||||
|
let mut accumulator = self.pop()?;
|
||||||
|
|
||||||
|
while let Ok(x) = self.pop() {
|
||||||
|
accumulator += x;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.push(accumulator)
|
||||||
|
}
|
||||||
|
|
||||||
fn define(&mut self, name: &str, definition: Definition) {
|
fn define(&mut self, name: &str, definition: Definition) {
|
||||||
let name = name.to_lowercase();
|
let name = name.to_lowercase();
|
||||||
self.custom.insert(name, definition);
|
self.custom.insert(name, definition);
|
||||||
@@ -348,6 +359,7 @@ impl RPN {
|
|||||||
Words::Atan => self.atan(),
|
Words::Atan => self.atan(),
|
||||||
Words::Tanh => self.tanh(),
|
Words::Tanh => self.tanh(),
|
||||||
Words::Emit => self.emit(),
|
Words::Emit => self.emit(),
|
||||||
|
Words::Sum => self.sum(),
|
||||||
Words::Epsilon => self.push(f64::EPSILON),
|
Words::Epsilon => self.push(f64::EPSILON),
|
||||||
Words::Euler => self.push(std::f64::consts::E),
|
Words::Euler => self.push(std::f64::consts::E),
|
||||||
Words::Pi => self.push(std::f64::consts::PI),
|
Words::Pi => self.push(std::f64::consts::PI),
|
||||||
@@ -411,6 +423,7 @@ impl RPN {
|
|||||||
"tau" => Ok(Words::Tau),
|
"tau" => Ok(Words::Tau),
|
||||||
"help" | "?" => Ok(Words::Help),
|
"help" | "?" => Ok(Words::Help),
|
||||||
"emit" | "." => Ok(Words::Emit),
|
"emit" | "." => Ok(Words::Emit),
|
||||||
|
"sum" => Ok(Words::Sum),
|
||||||
"clear" | "c" => Ok(Words::ClearStack),
|
"clear" | "c" => Ok(Words::ClearStack),
|
||||||
"quit" | "q" => Ok(Words::Quit),
|
"quit" | "q" => Ok(Words::Quit),
|
||||||
unk => {
|
unk => {
|
||||||
@@ -537,6 +550,8 @@ impl RPN {
|
|||||||
|
|
||||||
emit or . print the last number pushed to the stack, removing it from the stack
|
emit or . print the last number pushed to the stack, removing it from the stack
|
||||||
|
|
||||||
|
sum sum the entire stack, pushing the result back onto the stack
|
||||||
|
|
||||||
|
|
||||||
Additionally, this implements a subset of FORTH for a basic level of programability.
|
Additionally, this implements a subset of FORTH for a basic level of programability.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user