1
use super::stack_machine::StackMachineError;
2

            
3
//type StackType = i32;
4

            
5
/// This Enum lists the errors that the Forth Interpreter might return
6
3
#[derive(Debug, PartialEq)]
7
pub enum ForthError {
8
    UnknownError,
9
    UnknownToken(String),
10
    DROPOfEmptyStack,
11
    InvalidSyntax(String),
12
    MissingSemicolonAfterColon,
13
    //  Io(std::io::Error),
14
    UnhandledTrap,
15
    RanOutOfGas,
16
    ConstantAlreadyExists(String),
17
    ConstantDoesNotExist(String),
18
}
19

            
20
/*
21
/// Convert io::Errors to a ForthError so our Interpreter functions can
22
/// return a single Error type.
23
impl From<std::io::Error> for ForthError {
24
    fn from(err: std::io::Error) -> ForthError {
25
        ForthError::Io(err)
26
    }
27
}
28
*/
29

            
30
/// Convert StackMachineError to a ForthError so our Interpreter functions can
31
/// return a single Error type.
32

            
33
impl From<StackMachineError> for ForthError {
34
3
    fn from(err: StackMachineError) -> ForthError {
35
3
        match err {
36
1
            StackMachineError::NumberStackUnderflow => ForthError::DROPOfEmptyStack,
37
            StackMachineError::UnkownError => ForthError::UnknownError,
38
            StackMachineError::UnhandledTrap => ForthError::UnhandledTrap,
39
2
            StackMachineError::RanOutOfGas => ForthError::RanOutOfGas,
40
        }
41
3
    }
42
}
43

            
44
/*
45
/// Helper to convert ForthError codes to numeric codes for exit()
46
impl From<ForthError> for StackType {
47
    fn from(err: ForthError) -> Self {
48
        match err {
49
            ForthError::UnknownError => 2,
50
            ForthError::UnknownToken(_) => 3,
51
            ForthError::DROPOfEmptyStack => 4,
52
            ForthError::InvalidSyntax(_) => 5,
53
            ForthError::MissingSemicolonAfterColon => 6,
54
            ForthError::Io(_) => 7,
55
            ForthError::UnhandledTrap => 8,
56
            ForthError::RanOutOfGas => 9,
57
            ForthError::ConstantAlreadyExists(_) => 10,
58
            ForthError::ConstantDoesNotExist(_) => 11,
59
        }
60
    }
61
}
62
*/