rust_forth_compiler/
error.rs

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