glulx_asm/
error.rs

1// SPDX-License-Identifier: Apache-2.0 WITH LLVM-Exception
2// Copyright 2024 Daniel Fox Franke.
3
4//! Definition and impls for [`AssemblerError`].
5
6use core::fmt::{Debug, Display};
7
8#[derive(Debug, Copy, Clone)]
9/// Errors that can occur during assembly.
10pub enum AssemblerError<L> {
11    /// Assembly would overflow Glulx's 4 GiB address space.
12    Overflow,
13    /// An operand referenced a label which was not defined.
14    UndefinedLabel(L),
15    /// A label was defined in multiple places.
16    DuplicateLabel(L),
17    /// A label was right-shifted beyond its alignment.
18    InsufficientAlignment {
19        /// The label that produced the error.
20        label: L,
21        /// The offset that was applied to the label.
22        offset: i32,
23        /// The attempted right-shift amount.
24        shift: u8,
25    },
26}
27
28impl<L> Display for AssemblerError<L>
29where
30    L: Display,
31{
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            AssemblerError::Overflow => write!(f, "address space overflow"),
35            AssemblerError::UndefinedLabel(l) => write!(f, "undefined label {l}"),
36            AssemblerError::DuplicateLabel(l) => write!(f, "duplicate label {l}"),
37            AssemblerError::InsufficientAlignment {
38                label,
39                offset,
40                shift,
41            } => write!(
42                f,
43                "label {label} + offset {offset} is insufficiently aligned to be shifted by {shift}"
44            ),
45        }
46    }
47}
48
49#[cfg(feature = "std")]
50impl<L> std::error::Error for AssemblerError<L> where L: Debug + Display {}