Croissant Runtime
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crsn/crsn/src/asm/error.rs

59 lines
1.7 KiB

4 years ago
use std::borrow::Cow;
use std::error::Error;
4 years ago
4 years ago
use thiserror::Error;
use sexp::SourcePosition;
use crate::asm::data::{Register};
use crate::asm::data::literal::Label;
use crate::asm::instr::Cond;
4 years ago
use std::io;
4 years ago
/// csn_asm unified error type
4 years ago
#[derive(Error, Debug)]
4 years ago
pub enum CrsnError {
#[error("S-expression parsing error: {0}")]
Sexp(#[from] Box<sexp::Error>),
#[error("Parse error: {0} at {1}")]
Parse(Cow<'static, str>, SourcePosition),
#[error("Parse error: {0} at {1}")]
ParseOther(Box<dyn Error + Send + Sync>, SourcePosition),
#[error("Assembler error: {0} at {1}")]
Asm(AsmError, SourcePosition),
4 years ago
#[error("IO error: {0}")]
IOError(#[from] io::Error),
}
impl CrsnError {
/// Get error pos
pub fn pos(&self) -> Option<&SourcePosition> {
match self {
CrsnError::Parse(_, p) => Some(p),
CrsnError::ParseOther(_, p) => Some(p),
CrsnError::Asm(_, p) => Some(p),
CrsnError::Sexp(se) => Some(&se.pos),
CrsnError::IOError(_) =>None,
}
}
}
4 years ago
/// Error from the assembler stage (after parsing S-expressions and basic validation)
4 years ago
#[derive(Error, Debug)]
4 years ago
pub enum AsmError {
#[error("Unknown instruction")]
UnknownInstruction,
#[error("Value provided as output argument")]
ValueAsOutput,
#[error("Discard (_) provided as input argument")]
DiscardAsValue,
4 years ago
#[error("Conditional branch already defined for \"{0}\"")]
ConditionalAlreadyUsed(Cond),
#[error("Label \"{0}\" not defined")]
LabelNotDefined(Label),
#[error("Label \"{0}\" already defined in this scope")]
LabelDuplicate(Label),
#[error("Bad register type: {0}")]
BadRegisterType(Register),
4 years ago
}