diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-10-09 15:41:30 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-12-12 07:46:17 +0100 |
| commit | 16114b2ca358dbbef09cb5a8d3a843a0e11a3b88 (patch) | |
| tree | 49103ed4c0e906128df23c20656d88e4c4d1645e /lib/xixanta/src/errors.rs | |
| parent | abd9a7679e5da78a51ed5eb488ae081a2bcb2b6c (diff) | |
| download | tools.nes-16114b2ca358dbbef09cb5a8d3a843a0e11a3b88.tar.gz tools.nes-16114b2ca358dbbef09cb5a8d3a843a0e11a3b88.zip | |
Adapt the assembler to the changes on the parser
As of 184c39579227 ("Re-work the parser from scratch") the parser has a
proper AST, and the assembler has to traverse it accordingly.
Moreover, the assembler has been stripped from a lot of unneeded memory
allocations and it's overall more keen on using mere references when
possible.
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'lib/xixanta/src/errors.rs')
| -rw-r--r-- | lib/xixanta/src/errors.rs | 98 |
1 files changed, 93 insertions, 5 deletions
diff --git a/lib/xixanta/src/errors.rs b/lib/xixanta/src/errors.rs index 6c99f57..d5444df 100644 --- a/lib/xixanta/src/errors.rs +++ b/lib/xixanta/src/errors.rs @@ -1,23 +1,35 @@ use std::fmt; -// TODO: global error -// TODO: more errors, the `parse` thing is a hack! +#[derive(Debug, Clone, PartialEq)] +pub enum Error { + Parse(ParseError), + Context(ContextError), + Eval(EvalError), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::Parse(parse_error) => write!(f, "{}", parse_error), + Error::Context(context_error) => write!(f, "{}", context_error), + Error::Eval(eval_error) => write!(f, "{}", eval_error), + } + } +} + #[derive(Debug, Clone, PartialEq)] pub struct ParseError { pub line: usize, pub message: String, - pub parse: bool, } impl std::error::Error for ParseError {} impl From<std::io::Error> for ParseError { fn from(err: std::io::Error) -> Self { - // TODO ParseError { line: 0, message: err.to_string(), - parse: true, } } } @@ -27,3 +39,79 @@ impl fmt::Display for ParseError { write!(f, "parser (line {}): {}.", self.line + 1, self.message) } } + +#[derive(Debug, Clone, PartialEq)] +pub enum ContextErrorReason { + Redefinition, + UnknownVariable, + BadScope, + Other, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct ContextError { + pub line: usize, + pub reason: ContextErrorReason, + pub message: String, +} + +impl std::error::Error for ContextError {} + +// TODO: needed? +impl From<std::io::Error> for ContextError { + fn from(err: std::io::Error) -> Self { + ContextError { + line: 0, + reason: ContextErrorReason::Other, + message: err.to_string(), + } + } +} + +impl fmt::Display for ContextError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "Context error (line {}): {}.", + self.line + 1, + self.message + ) + } +} + +#[derive(Debug, Clone, PartialEq)] +pub struct EvalError { + pub line: usize, + pub message: String, +} + +impl std::error::Error for EvalError {} + +impl From<std::io::Error> for EvalError { + fn from(err: std::io::Error) -> Self { + EvalError { + line: 0, + message: err.to_string(), + } + } +} + +impl From<ContextError> for EvalError { + fn from(err: ContextError) -> Self { + EvalError { + line: err.line, + message: err.message, + } + } +} + +impl fmt::Display for EvalError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "Evaluation error (line {}): {}.", + self.line + 1, + self.message + ) + } +} |
