Add deconstruct operator

This commit is contained in:
Austen Adler 2021-06-02 22:24:28 -04:00
parent 3b10aaf06f
commit d92a6fbdb0
4 changed files with 33 additions and 8 deletions

View File

@ -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 # TODO: Update this to v0.5.0 when it finally comes out -- for now, use latest git master
# version = "0.4.0" # version = "0.4.0"
git = "https://github.com/rust-cli/confy/" git = "https://github.com/rust-cli/confy/"
# TOML will not serialize because of ordering issues
features = ["yaml_conf"] features = ["yaml_conf"]
default-features = false default-features = false

View File

@ -279,6 +279,7 @@ impl Calculator {
// Temporary // Temporary
'V' => self.op(CalculatorOperation::BuildVector), 'V' => self.op(CalculatorOperation::BuildVector),
'M' => self.op(CalculatorOperation::BuildMatrix), 'M' => self.op(CalculatorOperation::BuildMatrix),
'_' => self.op(CalculatorOperation::Deconstruct),
// Special // Special
'\\' => self.op(CalculatorOperation::Drop), '\\' => self.op(CalculatorOperation::Drop),
' ' => self.op(CalculatorOperation::Dup), ' ' => self.op(CalculatorOperation::Dup),
@ -572,6 +573,27 @@ impl Calculator {
push: OpArgs::Unary(new_entry), push: OpArgs::Unary(new_entry),
}) })
} }
pub fn deconstruct(&mut self) -> CalculatorResult<CalculatorStateChange> {
let entry = self.peek(0)?;
let entries: Vec<Entry> = 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 /// Performs a calculator operation such as undo, redo, operator, or dup
pub fn op(&mut self, op: CalculatorOperation) -> CalculatorResult<()> { pub fn op(&mut self, op: CalculatorOperation) -> CalculatorResult<()> {
@ -647,6 +669,7 @@ impl Calculator {
} }
CalculatorOperation::BuildVector => self.build_vector(), CalculatorOperation::BuildVector => self.build_vector(),
CalculatorOperation::BuildMatrix => self.build_matrix(), CalculatorOperation::BuildMatrix => self.build_matrix(),
CalculatorOperation::Deconstruct => self.deconstruct(),
CalculatorOperation::Dup => self.unary_op(|a| Ok(OpArgs::Binary([a.clone(), a]))), CalculatorOperation::Dup => self.unary_op(|a| Ok(OpArgs::Binary([a.clone(), a]))),
CalculatorOperation::Drop => self.unary_op(|_| Ok(OpArgs::None)), CalculatorOperation::Drop => self.unary_op(|_| Ok(OpArgs::None)),
CalculatorOperation::Swap => self.binary_op(|[a, b]| Ok(OpArgs::Binary([b, a]))), CalculatorOperation::Swap => self.binary_op(|[a, b]| Ok(OpArgs::Binary([b, a]))),

View File

@ -324,14 +324,14 @@ impl CalculatorEntry for Matrix {
fn add(&self, arg: &Entry) -> CalculatorResult<Entry> { fn add(&self, arg: &Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::add), 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), Entry::Number(number) => self.iterated_binary_num(number, Vector::add),
} }
} }
fn sub(&self, arg: &Entry) -> CalculatorResult<Entry> { fn sub(&self, arg: &Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::sub), 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), Entry::Number(number) => self.iterated_binary_num(number, Vector::sub),
} }
} }
@ -339,35 +339,35 @@ impl CalculatorEntry for Matrix {
// TODO: Correct implementation // TODO: Correct implementation
match arg { match arg {
Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::mul), 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), Entry::Number(number) => self.iterated_binary_num(number, Vector::mul),
} }
} }
fn div(&self, arg: &Entry) -> CalculatorResult<Entry> { fn div(&self, arg: &Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::div), 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), Entry::Number(number) => self.iterated_binary_num(number, Vector::div),
} }
} }
fn int_divide(&self, arg: &Entry) -> CalculatorResult<Entry> { fn int_divide(&self, arg: &Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::int_divide), 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), Entry::Number(number) => self.iterated_binary_num(number, Vector::int_divide),
} }
} }
fn modulo(&self, arg: &Entry) -> CalculatorResult<Entry> { fn modulo(&self, arg: &Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Matrix(m2) => self.iterated_binary_mat(m2, Vector::modulo), 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), Entry::Number(number) => self.iterated_binary_num(number, Vector::modulo),
} }
} }
fn pow(&self, arg: &Entry) -> CalculatorResult<Entry> { fn pow(&self, arg: &Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Matrix(m2) => Err(CalculatorError::TypeMismatch), Entry::Matrix(_m2) => Err(CalculatorError::TypeMismatch),
Entry::Vector(vector) => Err(CalculatorError::TypeMismatch), Entry::Vector(_vector) => Err(CalculatorError::TypeMismatch),
Entry::Number(number) => self.iterated_binary_num(number, Vector::pow), Entry::Number(number) => self.iterated_binary_num(number, Vector::pow),
} }
} }

View File

@ -29,6 +29,7 @@ pub enum CalculatorOperation {
ArithmeticOperation(ArithmeticOperation), ArithmeticOperation(ArithmeticOperation),
BuildVector, BuildVector,
BuildMatrix, BuildMatrix,
Deconstruct,
Undo, Undo,
Redo, Redo,
Drop, Drop,