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
53
54
55
56
57
58
59
extern crate rust_simple_stack_processor;

use rust_simple_stack_processor::StackMachineError;

/// This Enum lists the errors that the Forth Interpreter might return
#[derive(Debug)]
pub enum ForthError {
    UnknownError,
    UnknownToken(String),
    NumberStackUnderflow,
    LoopStackUnderflow,
    ScratchStackUnderflow,
    InvalidCellOperation,
    InvalidSyntax(String),
    MissingSemicolonAfterColon,
    MissingCommandAfterColon,
    SemicolonBeforeColon,
    UnhandledTrap,
    RanOutOfGas,
    InternalNumericOverflow,
}

/// Convert StackMachineError to a ForthError so our Interpreter functions can
/// return a single Error type.
impl From<StackMachineError> for ForthError {
    fn from(err: StackMachineError) -> ForthError {
        match err {
            StackMachineError::NumberStackUnderflow => ForthError::NumberStackUnderflow,
            StackMachineError::LoopStackUnderflow => ForthError::LoopStackUnderflow,
            StackMachineError::ScratchStackUnderflow => ForthError::ScratchStackUnderflow,
            StackMachineError::InvalidCellOperation => ForthError::InvalidCellOperation,
            StackMachineError::UnkownError => ForthError::UnknownError,
            StackMachineError::UnhandledTrap => ForthError::UnhandledTrap,
            StackMachineError::RanOutOfGas => ForthError::RanOutOfGas,
            StackMachineError::NumericOverflow => ForthError::InternalNumericOverflow,
        }
    }
}

/// Helper to convert ForthError codes to numeric codes for exit()
impl From<ForthError> for i32 {
    fn from(err: ForthError) -> Self {
        match err {
            ForthError::UnknownError => 2,
            ForthError::UnknownToken(_) => 3,
            ForthError::NumberStackUnderflow => 4,
            ForthError::LoopStackUnderflow => 5,
            ForthError::ScratchStackUnderflow => 13,
            ForthError::InvalidCellOperation => 14,
            ForthError::InvalidSyntax(_) => 6,
            ForthError::MissingSemicolonAfterColon => 7,
            ForthError::MissingCommandAfterColon => 8,
            ForthError::SemicolonBeforeColon => 9,
            ForthError::UnhandledTrap => 10,
            ForthError::RanOutOfGas => 11,
            ForthError::InternalNumericOverflow => 12,
        }
    }
}