1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::assembler::tokens::expression::*;
use crate::assembler::tokens::registers::*;
use std::fmt;

#[derive(Debug, PartialEq, Eq, Clone)]
/// Encode the way mnemonics access to data
pub enum DataAccess {
    /// We are using an indexed register associated to its index
    IndexRegister16WithIndex(IndexRegister16, Expr),
    IndexRegister16(IndexRegister16),
    IndexRegister8(IndexRegister8),
    /// Represents a standard 16 bits register
    Register16(Register16),
    /// Represents a standard 8 bits register
    Register8(Register8),
    /// Represents a memory access indexed by a register
    MemoryRegister16(Register16),
    /// Represents any expression
    Expression(Expr),
    /// Represents an address
    Memory(Expr),
    /// Represnts the test of bit flag
    FlagTest(FlagTest),
    /// Special register I
    SpecialRegisterI,
    /// Special register R
    SpecialRegisterR,
}

impl From<Expr> for DataAccess {
    fn from(exp: Expr) -> DataAccess {
        DataAccess::Expression(exp)
    }
}

impl From<&str> for DataAccess {
    fn from(txt: &str) -> DataAccess {
        DataAccess::Expression(txt.into())
    }
}

impl From<Register8> for DataAccess {
    fn from(reg: Register8) -> DataAccess {
        DataAccess::Register8(reg)
    }
}

impl From<Register16> for DataAccess {
    fn from(reg: Register16) -> DataAccess {
        DataAccess::Register16(reg)
    }
}

impl From<IndexRegister8> for DataAccess {
    fn from(reg: IndexRegister8) -> DataAccess {
        DataAccess::IndexRegister8(reg)
    }
}

impl From<IndexRegister16> for DataAccess {
    fn from(reg: IndexRegister16) -> DataAccess {
        DataAccess::IndexRegister16(reg)
    }
}

impl From<FlagTest> for DataAccess {
    fn from(test: FlagTest) -> DataAccess {
        DataAccess::FlagTest(test)
    }
}

impl fmt::Display for DataAccess {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            &DataAccess::IndexRegister16WithIndex(ref reg, ref delta) => {
                write!(f, "({} {})", reg, delta)
            }
            &DataAccess::IndexRegister16(ref reg) => write!(f, "{}", reg),
            &DataAccess::Register16(ref reg) => write!(f, "{}", reg),
            &DataAccess::IndexRegister8(ref reg) => write!(f, "{}", reg),
            &DataAccess::Register8(ref reg) => write!(f, "{}", reg),
            &DataAccess::MemoryRegister16(ref reg) => write!(f, "({})", reg),
            &DataAccess::Expression(ref exp) => write!(f, "{}", exp),
            &DataAccess::Memory(ref exp) => write!(f, "({})", exp),
            &DataAccess::FlagTest(ref test) => write!(f, "{}", test),
            &DataAccess::SpecialRegisterI => write!(f, "I"),
            &DataAccess::SpecialRegisterR => write!(f, "R"),
        }
    }
}

impl DataAccess {
    pub fn expr(&self) -> Option<&Expr> {
        match self {
            &DataAccess::Expression(ref expr) => Some(expr),
            _ => None,
        }
    }

    pub fn is_register_a(&self) -> bool {
        match self {
            DataAccess::Register8(Register8::A) => true,
            _ => false,
        }
    }

    pub fn is_register_i(&self) -> bool {
        match self {
            DataAccess::SpecialRegisterI => true,
            _ => false,
        }
    }

    pub fn is_register_r(&self) -> bool {
        match self {
            DataAccess::SpecialRegisterR => true,
            _ => false,
        }
    }

    pub fn is_register8(&self) -> bool {
        match self {
            &DataAccess::Register8(_) => true,
            _ => false,
        }
    }

    pub fn is_register16(&self) -> bool {
        match self {
            &DataAccess::Register16(_) => true,
            _ => false,
        }
    }

    pub fn is_indexregister16(&self) -> bool {
        match self {
            &DataAccess::IndexRegister16(_) => true,
            _ => false,
        }
    }

    pub fn is_memory(&self) -> bool {
        match self {
            &DataAccess::Memory(_) => true,
            _ => false,
        }
    }

    pub fn is_address_in_register16(&self) -> bool {
        match self {
            &DataAccess::MemoryRegister16(_) => true,
            _ => false,
        }
    }

    pub fn get_register16(&self) -> Option<Register16> {
        match self {
            &DataAccess::Register16(ref reg) => Some(reg.clone()),
            &DataAccess::MemoryRegister16(ref reg) => Some(reg.clone()),
            _ => None,
        }
    }

    pub fn get_indexregister16(&self) -> Option<IndexRegister16> {
        match self {
            &DataAccess::IndexRegister16(ref reg) => Some(reg.clone()),
            _ => None,
        }
    }

    pub fn get_register8(&self) -> Option<Register8> {
        match self {
            &DataAccess::Register8(ref reg) => Some(reg.clone()),
            _ => None,
        }
    }
}