c64_assembler/validator/
mod.rs

1//! Validate the consistency of an application.
2//!
3//! ```
4//! use c64_assembler_macro::application;
5//! use c64_assembler::validator::Validator;
6//!
7//! let application = application!(
8//!     name="Set black border"
9//!     include_vic2_defines
10//!     module!(
11//!         name="main"
12//!         instructions!(
13//!         include_basic_header
14//!         main_entry_point:
15//!             "Load black color into accumulator"
16//!             lda #$00
17//!             sta VIC2_BORDER_COLOR
18//!             rts
19//!         )
20//!     )
21//! ).unwrap();
22//! assert!(application.validate().is_ok());
23//! ```
24use address_names_exists::validate_address_names_exists;
25use address_names_unique::validate_address_names_unique;
26
27use crate::Application;
28
29mod address_names_exists;
30mod address_names_unique;
31mod relative_addressing;
32
33pub trait Validator {
34    fn validate(&self) -> AssemblerResult<()>;
35}
36
37pub type AssemblerResult<T> = Result<T, Error>;
38
39#[derive(Debug)]
40pub enum Error {
41    /// An address is reference by name, but the name isn't known.
42    AddressNameUnknown(String),
43    /// An address with the same name has been defined multiple times.
44    AddressNameNotUnique(String),
45    /// Assembler did take a branch that it could not recover from.
46    InternalCompilerError,
47}
48
49impl Validator for Application {
50    fn validate(&self) -> AssemblerResult<()> {
51        validate_address_names_exists(self)?;
52        validate_address_names_unique(self)?;
53        Ok(())
54    }
55}