From d92a6fbdb08d2f164e7c7898d3cc9acbf9e5127f Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Wed, 2 Jun 2021 22:24:28 -0400 Subject: [PATCH] Add deconstruct operator --- Cargo.toml | 1 + src/calc.rs | 23 +++++++++++++++++++++++ src/calc/entries.rs | 16 ++++++++-------- src/calc/operations.rs | 1 + 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d096133..a696739 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,5 +22,6 @@ toml = "0.4.2" # TODO: Update this to v0.5.0 when it finally comes out -- for now, use latest git master # version = "0.4.0" git = "https://github.com/rust-cli/confy/" +# TOML will not serialize because of ordering issues features = ["yaml_conf"] default-features = false diff --git a/src/calc.rs b/src/calc.rs index 4386c96..49847da 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -279,6 +279,7 @@ impl Calculator { // Temporary 'V' => self.op(CalculatorOperation::BuildVector), 'M' => self.op(CalculatorOperation::BuildMatrix), + '_' => self.op(CalculatorOperation::Deconstruct), // Special '\\' => self.op(CalculatorOperation::Drop), ' ' => self.op(CalculatorOperation::Dup), @@ -572,6 +573,27 @@ impl Calculator { push: OpArgs::Unary(new_entry), }) } + pub fn deconstruct(&mut self) -> CalculatorResult { + let entry = self.peek(0)?; + let entries: Vec = match &entry { + Entry::Matrix(matrix) => Ok(matrix + .vectors + .iter() + .map(|v| Entry::Vector(v.clone())) + .collect()), + Entry::Vector(vector) => Ok(vector + .values + .iter() + .map(|n| Entry::Number(n.clone())) + .collect()), + Entry::Number(_number) => Err(CalculatorError::TypeMismatch), + }?; + + Ok(CalculatorStateChange { + pop: OpArgs::Unary(entry), + push: OpArgs::Variable(entries), + }) + } /// Performs a calculator operation such as undo, redo, operator, or dup pub fn op(&mut self, op: CalculatorOperation) -> CalculatorResult<()> { @@ -647,6 +669,7 @@ impl Calculator { } CalculatorOperation::BuildVector => self.build_vector(), CalculatorOperation::BuildMatrix => self.build_matrix(), + CalculatorOperation::Deconstruct => self.deconstruct(), CalculatorOperation::Dup => self.unary_op(|a| Ok(OpArgs::Binary([a.clone(), a]))), CalculatorOperation::Drop => self.unary_op(|_| Ok(OpArgs::None)), CalculatorOperation::Swap => self.binary_op(|[a, b]| Ok(OpArgs::Binary([b, a]))), diff --git a/src/calc/entries.rs b/src/calc/entries.rs index f0e2ea4..88209cc 100644 --- a/src/calc/entries.rs +++ b/src/calc/entries.rs @@ -324,14 +324,14 @@ impl CalculatorEntry for Matrix { fn add(&self, arg: &Entry) -> CalculatorResult { match arg { Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::add), - Entry::Vector(vector) => Err(CalculatorError::TypeMismatch), + Entry::Vector(_vector) => Err(CalculatorError::TypeMismatch), Entry::Number(number) => self.iterated_binary_num(number, Vector::add), } } fn sub(&self, arg: &Entry) -> CalculatorResult { match arg { Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::sub), - Entry::Vector(vector) => Err(CalculatorError::TypeMismatch), + Entry::Vector(_vector) => Err(CalculatorError::TypeMismatch), Entry::Number(number) => self.iterated_binary_num(number, Vector::sub), } } @@ -339,35 +339,35 @@ impl CalculatorEntry for Matrix { // TODO: Correct implementation match arg { Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::mul), - Entry::Vector(vector) => Err(CalculatorError::TypeMismatch), + Entry::Vector(_vector) => Err(CalculatorError::TypeMismatch), Entry::Number(number) => self.iterated_binary_num(number, Vector::mul), } } fn div(&self, arg: &Entry) -> CalculatorResult { match arg { Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::div), - Entry::Vector(vector) => Err(CalculatorError::TypeMismatch), + Entry::Vector(_vector) => Err(CalculatorError::TypeMismatch), Entry::Number(number) => self.iterated_binary_num(number, Vector::div), } } fn int_divide(&self, arg: &Entry) -> CalculatorResult { match arg { Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::int_divide), - Entry::Vector(vector) => Err(CalculatorError::TypeMismatch), + Entry::Vector(_vector) => Err(CalculatorError::TypeMismatch), Entry::Number(number) => self.iterated_binary_num(number, Vector::int_divide), } } fn modulo(&self, arg: &Entry) -> CalculatorResult { match arg { Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::modulo), - Entry::Vector(vector) => Err(CalculatorError::TypeMismatch), + Entry::Vector(_vector) => Err(CalculatorError::TypeMismatch), Entry::Number(number) => self.iterated_binary_num(number, Vector::modulo), } } fn pow(&self, arg: &Entry) -> CalculatorResult { match arg { - Entry::Matrix(m2) => Err(CalculatorError::TypeMismatch), - Entry::Vector(vector) => Err(CalculatorError::TypeMismatch), + Entry::Matrix(_m2) => Err(CalculatorError::TypeMismatch), + Entry::Vector(_vector) => Err(CalculatorError::TypeMismatch), Entry::Number(number) => self.iterated_binary_num(number, Vector::pow), } } diff --git a/src/calc/operations.rs b/src/calc/operations.rs index c480f9a..d108ce4 100644 --- a/src/calc/operations.rs +++ b/src/calc/operations.rs @@ -29,6 +29,7 @@ pub enum CalculatorOperation { ArithmeticOperation(ArithmeticOperation), BuildVector, BuildMatrix, + Deconstruct, Undo, Redo, Drop,