1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Provides abstract error handling for the Elemental interpreter.

use colored::*;

/// Enumerates the types of errors available to the Elemental interpreter.
pub enum Error {
    CouldNotFlushOutput,
    CouldNotReadStdin,
    ImproperDimensions,
    InvalidOperands,
    InvalidOperator,
    InvalidValue,
    CouldNotParseNumeric,
    UnexpectedEof,
    CouldNotFindFunction,
    WrongNumberOfArgs,
    RequiresUnitMatrix,
    SquareMatrixRequired,
    ExpectedIdentifier,
    ExpectedCloseParen,
    DividedByZero,
    UndeclaredVariable (String),
    CouldNotReadFile (String),
}

pub use Error::*;


/// Throws errors.
pub fn throw(error: Error) {
    let message: String = match error {
        CouldNotFlushOutput => "could not flush stdout".to_string(),
        CouldNotReadStdin => "could not read stdin".to_string(),
        ImproperDimensions => "improper dimensions".to_string(),
        InvalidOperands => "invalid binary operands".to_string(),
        InvalidOperator => "invalid operator".to_string(),
        InvalidValue => "at least one value in this matrix is not a numeric literal".to_string(),
        CouldNotParseNumeric => "could not parse numeric input".to_string(),
        UnexpectedEof => "unexpected token or end of token stream".to_string(),
        CouldNotFindFunction => "could not find function in standard library".to_string(),
        WrongNumberOfArgs => "wrong number of arguments passed to function".to_string(),
        RequiresUnitMatrix => "function requires a unit (1x1) matrix".to_string(),
        SquareMatrixRequired => "function requires a square matrix".to_string(),
        ExpectedIdentifier => "expected identifier".to_string(),
        ExpectedCloseParen => "expected closing parenthesis".to_string(),
        DividedByZero => "attempted to divide by zero".to_string(),
        UndeclaredVariable (s) => format!("found undeclared variable {}", s),
        CouldNotReadFile (s) => format!("could not read file {}", s),
    };

    println!("{}: {}", "error".bold().red(), message);
}