use crate::{
ast::{self, BinaryOp},
diagnostic::{DiagnosticCode, EvaluationFrame, SourceId},
value::{ElementValue, RuntimeValue},
};
#[derive(Debug)]
pub struct PrimitiveArgumentError {
pub name: &'static str,
pub range: ast::ByteRange,
pub expected: String,
pub value: RuntimeValue,
}
#[derive(Debug)]
pub struct PrimitiveArgumentsError {
pub range: ast::ByteRange,
pub function: &'static str,
pub requirement: String,
pub help: Option<String>,
pub arguments: Vec<PrimitiveArgumentError>,
}
#[derive(Debug)]
pub struct PrimitiveValueError {
pub range: ast::ByteRange,
pub function: &'static str,
pub requirement: String,
pub argument: &'static str,
pub found_range: ast::ByteRange,
pub value: RuntimeValue,
pub constraint: String,
pub help: Option<String>,
}
#[derive(Debug)]
pub struct ShapeMismatchError {
pub range: ast::ByteRange,
pub action: &'static str,
pub first: ElementValue,
pub second: ElementValue,
}
#[derive(Debug, Clone, Copy)]
pub enum NonAdditiveSubject {
Pool(u32),
Sequence(usize),
Operand,
}
#[derive(Debug)]
pub struct NonAdditiveSumError {
pub range: ast::ByteRange,
pub action: &'static str,
pub subject: NonAdditiveSubject,
pub witness: Option<ElementValue>,
pub field: Option<usize>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArityMismatch {
pub words: String,
pub available: Vec<usize>,
pub found: usize,
pub comma_insertion: Option<usize>,
}
#[derive(Debug)]
pub enum RuntimeError {
InFunction {
range: ast::ByteRange,
source: Box<RuntimeError>,
body_source: SourceId,
frame: Box<EvaluationFrame>,
},
Semantic {
code: DiagnosticCode,
range: ast::ByteRange,
message: String,
},
LabelsOnNonTupleOutput {
range: ast::ByteRange,
value_range: ast::ByteRange,
value: RuntimeValue,
},
OutputLabelCountMismatch {
range: ast::ByteRange,
expected: usize,
found: usize,
},
OutputNotAtTopLevel {
range: ast::ByteRange,
},
SetNotAtTopLevel {
range: ast::ByteRange,
},
ReturnOutsideFunction {
range: ast::ByteRange,
},
LoopOverNonSequence {
range: ast::ByteRange,
value: RuntimeValue,
},
UndefinedReference {
range: ast::ByteRange,
name: String,
},
UndefinedFunction {
range: ast::ByteRange,
name: String,
arity_mismatch: Option<ArityMismatch>,
},
InvalidCondition {
range: ast::ByteRange,
value: RuntimeValue,
},
RangeHasNonSequenceEndpoints {
range: ast::ByteRange,
value: RuntimeValue,
},
InvalidArgumentToOperator {
operator_range: ast::ByteRange,
op: BinaryOp,
expected: &'static str,
found_range: ast::ByteRange,
value: RuntimeValue,
},
NegativeArgumentToFunction {
range: ast::ByteRange,
name: String,
found_range: ast::ByteRange,
value: i32,
},
InvalidPrimitiveArguments(Box<PrimitiveArgumentsError>),
InvalidPrimitiveValue(Box<PrimitiveValueError>),
ShapeMismatch(Box<ShapeMismatchError>),
NonAdditiveSum(Box<NonAdditiveSumError>),
InvalidRepeatExpression {
range: ast::ByteRange,
value: RuntimeValue,
},
MathError {
range: ast::ByteRange,
message: String,
},
}
impl RuntimeError {
pub fn range(&self) -> ast::ByteRange {
match self {
RuntimeError::InFunction { range, .. } => *range,
RuntimeError::Semantic { range, .. } => *range,
RuntimeError::LabelsOnNonTupleOutput { range, .. } => *range,
RuntimeError::OutputLabelCountMismatch { range, .. } => *range,
RuntimeError::OutputNotAtTopLevel { range } => *range,
RuntimeError::SetNotAtTopLevel { range } => *range,
RuntimeError::ReturnOutsideFunction { range } => *range,
RuntimeError::LoopOverNonSequence { range, .. } => *range,
RuntimeError::UndefinedReference { range, .. } => *range,
RuntimeError::UndefinedFunction { range, .. } => *range,
RuntimeError::InvalidCondition { range, .. } => *range,
RuntimeError::RangeHasNonSequenceEndpoints { range, .. } => *range,
RuntimeError::InvalidArgumentToOperator { operator_range, .. } => *operator_range,
RuntimeError::NegativeArgumentToFunction { range, .. } => *range,
RuntimeError::InvalidPrimitiveArguments(error) => error.range,
RuntimeError::InvalidPrimitiveValue(error) => error.range,
RuntimeError::ShapeMismatch(error) => error.range,
RuntimeError::NonAdditiveSum(error) => error.range,
RuntimeError::InvalidRepeatExpression { range, .. } => *range,
RuntimeError::MathError { range, .. } => *range,
}
}
}