rust_forth_compiler/
error.rs

1extern crate rust_simple_stack_processor;
2
3use rust_simple_stack_processor::StackMachineError;
4
5/// This Enum lists the errors that the Forth Interpreter might return
6#[derive(Debug)]
7pub enum ForthError {
8    UnknownError,
9    UnknownToken(String),
10    NumberStackUnderflow,
11    LoopStackUnderflow,
12    ScratchStackUnderflow,
13    InvalidCellOperation,
14    InvalidSyntax(String),
15    MissingSemicolonAfterColon,
16    MissingCommandAfterColon,
17    SemicolonBeforeColon,
18    UnhandledTrap,
19    RanOutOfGas,
20    InternalNumericOverflow,
21}
22
23/// Convert StackMachineError to a ForthError so our Interpreter functions can
24/// return a single Error type.
25impl From<StackMachineError> for ForthError {
26    fn from(err: StackMachineError) -> ForthError {
27        match err {
28            StackMachineError::NumberStackUnderflow => ForthError::NumberStackUnderflow,
29            StackMachineError::LoopStackUnderflow => ForthError::LoopStackUnderflow,
30            StackMachineError::ScratchStackUnderflow => ForthError::ScratchStackUnderflow,
31            StackMachineError::InvalidCellOperation => ForthError::InvalidCellOperation,
32            StackMachineError::UnkownError => ForthError::UnknownError,
33            StackMachineError::UnhandledTrap => ForthError::UnhandledTrap,
34            StackMachineError::RanOutOfGas => ForthError::RanOutOfGas,
35            StackMachineError::NumericOverflow => ForthError::InternalNumericOverflow,
36        }
37    }
38}
39
40/// Helper to convert ForthError codes to numeric codes for exit()
41impl From<ForthError> for i32 {
42    fn from(err: ForthError) -> Self {
43        match err {
44            ForthError::UnknownError => 2,
45            ForthError::UnknownToken(_) => 3,
46            ForthError::NumberStackUnderflow => 4,
47            ForthError::LoopStackUnderflow => 5,
48            ForthError::ScratchStackUnderflow => 13,
49            ForthError::InvalidCellOperation => 14,
50            ForthError::InvalidSyntax(_) => 6,
51            ForthError::MissingSemicolonAfterColon => 7,
52            ForthError::MissingCommandAfterColon => 8,
53            ForthError::SemicolonBeforeColon => 9,
54            ForthError::UnhandledTrap => 10,
55            ForthError::RanOutOfGas => 11,
56            ForthError::InternalNumericOverflow => 12,
57        }
58    }
59}