Skip to content

Commit

Permalink
Errors use owned data
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewilson2002 committed Aug 12, 2019
1 parent c6f0337 commit 31e05e8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
18 changes: 9 additions & 9 deletions src/computer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ pub trait Num {
/// | UnrecognizedIdentifier | When an identifier could not be resolved: it was not found in the Computer's variables. |
/// | UnrecognizedFunctionIdentifier | When the identifier could not be found in the Computer's functions. |
#[derive(Debug, Clone, PartialEq)]
pub enum ComputeError<'a> {
pub enum ComputeError {
InvalidFactorial,
VariableIsConstant(&'a str),
UnrecognizedIdentifier(&'a str),
UnrecognizedFunctionIdentifier(&'a str),
VariableIsConstant(String),
UnrecognizedIdentifier(String),
UnrecognizedFunctionIdentifier(String),
}
use self::ComputeError::*;

Expand Down Expand Up @@ -116,12 +116,12 @@ impl<'fun, T: Num + Clone + PartialOrd + Neg<Output = T> + Add<Output = T> + Sub
}
}

fn compute_expr<'a>(&mut self, expr: &Expr<'a, T>) -> Result<T, ComputeError<'a>> { // TODO: a lot of .to_owned() happens here to compare &'a str to Strings: there must be a more efficient way
fn compute_expr<'a>(&mut self, expr: &Expr<'a, T>) -> Result<T, ComputeError> { // TODO: a lot of .to_owned() happens here to compare &'a str to Strings: there must be a more efficient way
match expr {
Expr::Constant(num) => Ok(num.clone()),
Expr::Identifier(id) => match self.variables.get(id.to_owned()) {
Some(value) => Ok(value.0.clone()),
None => Err(UnrecognizedIdentifier(id)),
None => Err(UnrecognizedIdentifier(id.to_string())),
},
Expr::Neg(expr) => Ok(-self.compute_expr(expr)?),
Expr::BinOp(op, lexpr, rexpr) => {
Expand All @@ -141,13 +141,13 @@ impl<'fun, T: Num + Clone + PartialOrd + Neg<Output = T> + Add<Output = T> + Sub
let value = self.compute_expr(&expr)?;
match self.functions.get(id.to_owned()) {
Some(func) => Ok(func(value)),
None => Err(UnrecognizedFunctionIdentifier(id)),
None => Err(UnrecognizedFunctionIdentifier(id.to_string())),
}
}
Expr::Assignment(id, expr) => {
let value = self.compute_expr(&expr)?;
if self.variables.contains_key(id.to_owned()) && self.variables.get(id.to_owned()).unwrap().1 == true {
return Err(VariableIsConstant(id));
return Err(VariableIsConstant(id.to_string()));
}
self.variables.insert((*id).to_owned(), (value.clone(), false));
Ok(value)
Expand Down Expand Up @@ -180,7 +180,7 @@ impl<'fun, T: Num + Clone + PartialOrd + Neg<Output = T> + Add<Output = T> + Sub
/// // Using this function to create the result from the `Expr`.
/// let result = computer.compute(&ast).unwrap();
/// ```
pub fn compute<'a>(&mut self, expr: &Expr<'a, T>) -> Result<T, ComputeError<'a>> {
pub fn compute<'a>(&mut self, expr: &Expr<'a, T>) -> Result<T, ComputeError> {
let val = self.compute_expr(expr);
match &val {
Ok(n) => {
Expand Down
6 changes: 3 additions & 3 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub enum Token<'a, T> {
/// | InvalidCharacter | If the input contains any characters not recognized by the lexer to be numbers or characters, ex: 'ƒ' |
/// | InvalidNumber | A number entered invalidly: '2.34.2' or '..3' |
#[derive(Debug, Clone, PartialEq)]
pub enum LexerError<'a> {
pub enum LexerError {
InvalidCharacter(char),
InvalidNumber(&'a str),
InvalidNumber(String),
}

/// Turn a string into a vector of tokens. This function generally takes the most time,
Expand Down Expand Up @@ -81,7 +81,7 @@ where

match (&src[start_idx..=end_idx]).parse::<T>() {
Ok(num) => tokens.push(Token::Number(num)),
_ => return Err(LexerError::InvalidNumber(&src[start_idx..=end_idx])),
_ => return Err(LexerError::InvalidNumber(src[start_idx..=end_idx].to_owned())),
}
} else if c.is_alphabetic() {
let start_idx = i;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ impl Num for f64 {
/// [parser::ParserError](parser/enum.ParserError.html), and [lexer::LexerError](lexer/enum.LexerError.html). Produced when using `eval` helper functions.
#[derive(Debug, Clone)]
pub enum EvalError<'a, T: Clone + std::fmt::Debug> {
ComputeError(computer::ComputeError<'a>),
ComputeError(computer::ComputeError),
ParserError(parser::ParserError<'a, T>),
LexerError(lexer::LexerError<'a>),
LexerError(lexer::LexerError),
}

/// Turn an expression inside a string into a number.
Expand Down

0 comments on commit 31e05e8

Please sign in to comment.