2024-01-22 10:45:23 +00:00
|
|
|
use std::{
|
|
|
|
cmp::Ordering,
|
|
|
|
fmt::{self, Display, Formatter},
|
|
|
|
};
|
|
|
|
|
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
|
|
use crate::instructions::instruction;
|
2024-01-20 13:04:58 +00:00
|
|
|
|
2024-01-21 01:21:37 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2024-01-20 13:04:58 +00:00
|
|
|
pub enum SyntaxErr {
|
2024-01-21 01:21:37 +00:00
|
|
|
/// false for '(' true for ')'
|
|
|
|
UnmatchedParen(bool),
|
|
|
|
UnexpectedChar,
|
|
|
|
OutsideOp(String),
|
2024-01-23 01:19:24 +00:00
|
|
|
InvalidRegister,
|
2024-01-20 13:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for SyntaxErr {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
2024-01-21 01:21:37 +00:00
|
|
|
SyntaxErr::UnmatchedParen(_) => write!(f, "unmatched parenthesis"),
|
|
|
|
SyntaxErr::UnexpectedChar => write!(f, "unexpected character"),
|
2024-01-26 01:41:50 +00:00
|
|
|
SyntaxErr::OutsideOp(kind) => write!(f, "'{kind}' before opcode"),
|
2024-01-23 01:19:24 +00:00
|
|
|
SyntaxErr::InvalidRegister => write!(f, "invalid register"),
|
2024-01-21 12:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SyntaxErr {
|
|
|
|
pub fn note(&self) -> String {
|
|
|
|
match self {
|
2024-01-22 10:45:23 +00:00
|
|
|
SyntaxErr::UnmatchedParen(false) => "add `)` after the register name".to_string(),
|
|
|
|
SyntaxErr::UnmatchedParen(true) => "add `(` before the register name".to_string(),
|
2024-01-21 12:36:38 +00:00
|
|
|
SyntaxErr::UnexpectedChar => "ensure the input is well-formed".to_string(),
|
2024-01-22 10:45:23 +00:00
|
|
|
SyntaxErr::OutsideOp(_) => format!("only add arguments after the opcode"),
|
2024-01-23 01:19:24 +00:00
|
|
|
SyntaxErr::InvalidRegister => {
|
2024-01-22 00:12:59 +00:00
|
|
|
"registers are either xN (N < 32 with no leading 0) or the standard aliases"
|
|
|
|
.to_string()
|
|
|
|
}
|
2024-01-20 13:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 01:21:37 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2024-01-20 13:04:58 +00:00
|
|
|
pub enum RuntimeErr {
|
2024-01-22 10:45:23 +00:00
|
|
|
InvalidOp,
|
|
|
|
/// op, actual, expected
|
2024-01-20 13:04:58 +00:00
|
|
|
InvalidOpArity(String, usize, usize),
|
2024-01-22 10:45:23 +00:00
|
|
|
/// actual, expected
|
2024-01-22 00:12:59 +00:00
|
|
|
InvalidType(String, String),
|
2024-01-26 01:41:50 +00:00
|
|
|
LabelNotFound,
|
2024-01-20 13:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for RuntimeErr {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
2024-01-22 22:17:20 +00:00
|
|
|
RuntimeErr::InvalidOp => write!(f, "invalid opcode"),
|
2024-01-26 01:41:50 +00:00
|
|
|
RuntimeErr::InvalidOpArity(_, actual, expected) => {
|
|
|
|
write!(f, "expected {} args, got {}", expected, actual)
|
2024-01-22 10:45:23 +00:00
|
|
|
}
|
2024-01-22 00:12:59 +00:00
|
|
|
RuntimeErr::InvalidType(actual, expected) => {
|
2024-01-26 01:41:50 +00:00
|
|
|
write!(f, "expected '{}', got '{}'", expected, actual)
|
2024-01-22 10:45:23 +00:00
|
|
|
}
|
2024-01-26 01:41:50 +00:00
|
|
|
RuntimeErr::LabelNotFound => write!(f, "label not found"),
|
2024-01-22 10:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RuntimeErr {
|
|
|
|
pub fn note(&self) -> String {
|
|
|
|
match self {
|
|
|
|
RuntimeErr::InvalidOp => "check the ref sheet for the avaliable opcodes".to_string(),
|
|
|
|
RuntimeErr::InvalidOpArity(op, actual, expected) => {
|
2024-01-22 22:17:20 +00:00
|
|
|
let args = instruction(op).unwrap().1;
|
2024-01-22 10:45:23 +00:00
|
|
|
match actual.cmp(expected) {
|
|
|
|
Ordering::Equal => unreachable!(),
|
|
|
|
Ordering::Greater if actual - expected == 1 => {
|
|
|
|
"remove the extra argument".to_string()
|
|
|
|
}
|
|
|
|
Ordering::Greater => "remove the extra arguments".to_string(),
|
|
|
|
Ordering::Less if expected - actual == 1 => {
|
2024-01-26 01:41:50 +00:00
|
|
|
format!("add the extra '{}' argument", args.last().unwrap().kind())
|
2024-01-22 10:45:23 +00:00
|
|
|
}
|
|
|
|
Ordering::Less => format!(
|
|
|
|
"add the extra `{}` arguments",
|
|
|
|
args.get((actual - 1)..)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|arg| arg.kind())
|
2024-01-26 01:41:50 +00:00
|
|
|
.join("', '")
|
2024-01-22 10:45:23 +00:00
|
|
|
),
|
|
|
|
}
|
2024-01-22 00:12:59 +00:00
|
|
|
}
|
2024-01-22 10:45:23 +00:00
|
|
|
RuntimeErr::InvalidType(_, _) => "ensure the operation is valid".to_string(),
|
2024-01-26 01:41:50 +00:00
|
|
|
RuntimeErr::LabelNotFound => "ensure the label is spelled correctly".to_string(),
|
2024-01-20 13:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|