Added UnsupportedExpression errors.

This commit is contained in:
PaddiM8 2020-06-17 21:28:54 +02:00
parent 643509ce4a
commit f917b744b5
3 changed files with 16 additions and 1 deletions

View File

@ -108,6 +108,14 @@ fn invert_binary(
// If the left expression contains the unit, invert the right one instead, // If the left expression contains the unit, invert the right one instead,
// since the unit should not be moved. // since the unit should not be moved.
if contains_the_unit(symbol_table, left) { if contains_the_unit(symbol_table, left) {
// But if the right expression *also* contains the unit,
// throw an error, since it can't handle this yet.
if contains_the_unit(symbol_table, right) {
return Err(CalcError::UnsupportedExpression(String::from(
"Can't invert expressions with several instances of an unknown variable (yet).",
)));
}
return Ok(invert( return Ok(invert(
Expr::Binary(Box::new(target_expr), op_inv, Box::new(right.clone())), Expr::Binary(Box::new(target_expr), op_inv, Box::new(right.clone())),
symbol_table, symbol_table,
@ -148,7 +156,9 @@ fn invert_unit(
_identifier: &str, _identifier: &str,
_expr: &Expr, _expr: &Expr,
) -> Result<(Expr, Expr), CalcError> { ) -> Result<(Expr, Expr), CalcError> {
unimplemented!() Err(CalcError::UnsupportedExpression(String::from(
"Cannot invert expressions containing other units (yet).",
)))
} }
fn invert_var( fn invert_var(
@ -253,6 +263,9 @@ fn multiply_into(expr: &Expr, base_expr: &Expr) -> Result<Expr, CalcError> {
TokenKind::Star, TokenKind::Star,
Box::new(base_expr.clone()), Box::new(base_expr.clone()),
)), )),
Expr::Group(_) => Err(CalcError::UnsupportedExpression(String::from(
"Cannot invert parenthesis multiplied with parenthesis (yet).",
))),
_ => unimplemented!(), _ => unimplemented!(),
} }
} }

View File

@ -70,6 +70,7 @@ pub enum CalcError {
UnexpectedToken(TokenKind), UnexpectedToken(TokenKind),
UndefinedFn(String), UndefinedFn(String),
UndefinedVar(String), UndefinedVar(String),
UnsupportedExpression(String),
Unknown, Unknown,
} }

View File

@ -96,6 +96,7 @@ fn print_calc_err(err: CalcError) {
UnexpectedToken(kind) => format!("Unexpected token: '{:?}'.", kind), UnexpectedToken(kind) => format!("Unexpected token: '{:?}'.", kind),
UndefinedFn(name) => format!("Undefined function: '{}'.", name), UndefinedFn(name) => format!("Undefined function: '{}'.", name),
UndefinedVar(name) => format!("Undefined variable: '{}'.", name), UndefinedVar(name) => format!("Undefined variable: '{}'.", name),
UnsupportedExpression(msg) => format!("{}", msg),
Unknown => format!("Unknown error."), Unknown => format!("Unknown error."),
}); });
} }