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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use failure::Fail;

use smpl::ModuleId;
use smpl::error::Error as StaticError;
use smpl::byte_gen::{ Instruction, InstructionPointerType };

#[derive(Debug, Clone)]
pub enum VmError {
    StaticError(StaticError),
    BuiltinCollision(ModuleFnPair),
    NotABuiltin(ModuleFnPair),
    NotAFn(ModuleFnPair),
    NotAModule(String),
}

impl<T> From<T> for VmError where T: Into<StaticError> {
    fn from(e: T) -> VmError {
        VmError::StaticError(e.into())
    }
}

#[derive(Debug, Clone)]
pub struct ModuleFnPair {
    pub module: String,
    pub function: String,
}

#[derive(Fail, Debug, Clone)]
pub enum InternalError {
    #[fail(display = "Invalid number of arguments. Found {}. {}", _0, _1)]
    InvalidArgCount(usize, ExpectedArgCount),
    #[fail(
        display = "Unexpected argument type at {}. Found {}. Expected {}",
        index, found, expected
    )]
    InvalidArgType {
        index: usize,
        found: String,
        expected: String,
    },

    #[fail(display = "Invalid instruction pointer {}. Max: {}", ip, max)]
    InstructionPointerOutOfBounds {
        ip: InstructionPointerType,
        max: usize
    },

    #[fail(display = "Integer {} out of the range [{}, {}", v, min_inclusive, max_inclusive)]
    IntegerOutOfRange {
        v: i64,
        min_inclusive: i64,
        max_inclusive: i64,
    },

    #[fail(display = "InvalidInstruction: {}", _0)]
    InvalidInstruction(IIReason),

    #[fail(display = "Runtime instruction error: {}", _0)]
    RuntimeInstructionError(RuntimeInstructionError),
}

#[derive(Fail, Debug, Clone)]
pub enum RuntimeInstructionError {
    // TODO: canonical string representation of instructions
    #[fail(display = "Expected int in: {:?}", _0)]
    ExpectedInt(Instruction),

    #[fail(display = "Expected float in: {:?}", _0)]
    ExpectedFloat(Instruction),

    #[fail(display = "Expected bool in: {:?}", _0)]
    ExpectedBool(Instruction),

    #[fail(display = "Expected function in: {:?}", _0)]
    ExpectedFunction(Instruction),

    #[fail(display = "No return found for instruction at {}", _0)]
    NoReturnValue(InstructionPointerType),

    #[fail(display = "Attempting to add {} to current IP({}) results in underflow", addition, current)]
    IPUnderflow {
        current: InstructionPointerType,
        addition: i64,
    },

    #[fail(display = "Attempting to add {} to current IP({}) results in overflow", addition, current)]
    IPOverflow {
        current: InstructionPointerType,
        addition: i64,
    },
}

#[derive(Fail, Debug, Clone)]
pub enum IIReason {
    // TODO: canonical string representation of instructions
    #[fail(display = "Expected int in: {:?}", _0)]
    ExpectedInt(Instruction),

    #[fail(display = "Expected float in: {:?}", _0)]
    ExpectedFloat(Instruction),

    #[fail(display = "Expected bool in: {:?}", _0)]
    ExpectedBool(Instruction),
}

#[derive(Fail, Debug, Clone)]
pub enum ExpectedArgCount {
    #[fail(display = "Expected {}..={}", _0, _1)]
    Range(usize, usize),
    #[fail(display = "Expected >= {}", _0)]
    Min(usize),
    #[fail(display = "Expected <= {}", _0)]
    Max(usize),
    #[fail(display = "Expected == {}", _0)]
    Exact(usize),
}

// Checks for no arguments
#[macro_export]
macro_rules! no_args {
    ($args: expr) => {{
        use crate::err::*;
        if $args.len() != 0 {
            Err(InternalError::InvalidArgCount(
                $args.len(),
                ExpectedArgCount::Exact(0),
            ))
        } else {
            Ok(())
        }
    }};
}

#[macro_export]
macro_rules! exact_args {
    ($exact: expr, $args: expr) => {{
        use crate::err::*;
        if $args.len() != $exact {
            Err(InternalError::InvalidArgCount(
                $args.len(),
                ExpectedArgCount::Exact($exact),
            ))
        } else {
            Ok($args)
        }
    }};
}

// Checks for 1+ args
#[macro_export]
macro_rules! min_args {
    ($min: expr, $args: expr) => {{
        use crate::err::*;
        if $args.len() < $min {
            Err(InternalError::InvalidArgCount(
                $args.len(),
                ExpectedArgCount::Min($min),
            ))
        } else {
            Ok($args)
        }
    }};
}

// Checks for <= max args
#[macro_export]
macro_rules! max_args {
    ($max: expr, $args: expr) => {{
        use crate::err::*;
        if $args.len() > $max {
            Err(InternalError::InvalidArgCount(
                $args.len(),
                ExpectedArgCount::Max($max),
            ))
        } else {
            Ok($args)
        }
    }};
}

// Checks for [min, max] args
#[macro_export]
macro_rules! arg_range_inclusive {
    // Assume min_inclusive is greater than 1
    ($min_inclusive: expr, $max_inclusive: expr, $args: expr) => {{
        use crate::err::*;
        if $min_inclusive <= $args.len() && $max_inclusive >= $args.len() {
            Ok($args)
        } else {
            Err(InternalError::InvalidArgCount(
                $args.len(),
                ExpectedArgCount::Range($min_inclusive, $max_inclusive),
            ))
        }
    }};
}