Add deconstruct operator
This commit is contained in:
parent
3b10aaf06f
commit
d92a6fbdb0
@ -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
|
||||
|
23
src/calc.rs
23
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<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
|
||||
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]))),
|
||||
|
@ -324,14 +324,14 @@ impl CalculatorEntry for Matrix {
|
||||
fn add(&self, arg: &Entry) -> CalculatorResult<Entry> {
|
||||
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<Entry> {
|
||||
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<Entry> {
|
||||
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<Entry> {
|
||||
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<Entry> {
|
||||
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<Entry> {
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ pub enum CalculatorOperation {
|
||||
ArithmeticOperation(ArithmeticOperation),
|
||||
BuildVector,
|
||||
BuildMatrix,
|
||||
Deconstruct,
|
||||
Undo,
|
||||
Redo,
|
||||
Drop,
|
||||
|
Loading…
Reference in New Issue
Block a user