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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/// All the stuff to parse z80 code.
pub mod parser;

/// Definition of the tokens.
pub mod tokens;

/// Production of the bytecodes from the tokens.
pub mod assembler;

/// Utility functions to manually create tokens.
pub mod builder;

#[derive(Debug, Fail)]
pub enum AssemblerError {
    #[fail(display = "Assembling bug: {}", msg)]
    BugInAssembler { msg: String },
    #[fail(display = "Parser bug: {}", error)]
    BugInParser { error: String },

    // TODO add more information
    #[fail(display = "Syntax error: {}", error)]
    SyntaxError { error: String },

    // TODO add more information
    #[fail(display = "Assembling error: {}", msg)]
    AssemblingError { msg: String },

    // TODO remove this case and dispatch it everywhere else
    #[fail(display = "To be sorted error: {}", msg)]
    GenericError { msg: String },

    #[fail(display = "Assertion failed -- {}: {}", test, msg)]
    AssertionFailed { test: String, msg: String },

    #[fail(display = "Symbol `{}`already present on the symbol table", symbol)]
    SymbolAlreadyExists { symbol: String },

    #[fail(display = "Unknown symbol: {}. Closest one is: {:?}", symbol, closest)]
    UnknownSymbol {
        symbol: String,
        closest: Option<String>,
    },

    #[fail(display = "IO error: {}", msg)]
    IOError { msg: String },

    #[fail(display = "Current assembling address is unknown.")]
    UnknownAssemblingAddress,

    #[fail(display = "Unable to resolve expression {}.", expression)]
    ExpressionUnresolvable {
        expression: crate::assembler::tokens::expression::Expr,
    },
}

impl From<String> for AssemblerError {
    fn from(msg: String) -> AssemblerError {
        AssemblerError::GenericError { msg }
    }
}

impl From<&String> for AssemblerError {
    fn from(msg: &String) -> AssemblerError {
        AssemblerError::GenericError {
            msg: msg.to_string(),
        }
    }
}

/// Configuration of the assembler. By default the assembler is case sensitive and has no symbol
#[derive(Clone)]
pub struct AssemblingOptions {
    /// Set to true to consider that the assembler pay attention to the case of the labels
    case_sensitive: bool,
    /// Contains some symbols that could be used during assembling
    symbols: assembler::SymbolsTable,
}

impl Default for AssemblingOptions {
    fn default() -> AssemblingOptions {
        AssemblingOptions {
            case_sensitive: true,
            symbols: Default::default()
        }
    }
}

impl AssemblingOptions {
    pub fn new_case_sensitive() -> Self {
        Self::default()
    }

    pub fn new_case_insensitive() -> Self {
        let mut options = Self::new_case_sensitive();
        options.case_sensitive = false;
        options
    }

    /// Creation an option object with the given symbol table
    pub fn new_with_table(symbols: &assembler::SymbolsTable) -> Self {
        let mut options = Self::default();
        options.set_symbols(symbols);
        options
    }

    /// Specify if the assembler must be case sensitive or not
    pub fn set_case_sensitive(&mut self, val: bool) -> &mut Self {
        self.case_sensitive = val;
        self
    }

    /// Specify a symbol table to copy
    pub fn set_symbols(&mut self, val: &assembler::SymbolsTable) -> &mut Self {
        self.symbols = val.clone();
        self
    }

    pub fn symbols(&self) -> &assembler::SymbolsTable {
        &self.symbols
    }

    pub fn case_sensitive(&self) -> bool {
        self.case_sensitive
    }


}

/// Assemble a piece of code and returns the associated list of bytes
pub fn assemble(code: &str) -> Result<Vec<u8>, AssemblerError> {
    assemble_and_table(code).map(|(b, _)| b)
}

#[deprecated(note = "use assemble_with_options instead.")]
pub fn assemble_and_table(
    code: &str,
) -> Result<(Vec<u8>, assembler::SymbolsTable), AssemblerError> {
    let tokens = parser::parse_str(code.into())?;
    let options = AssemblingOptions::default();
    let env = assembler::visit_tokens_all_passes_with_options(&tokens, &options)?;

    Ok((env.produced_bytes(), env.symbols().as_ref().clone()))
}

#[deprecated(note = "use assemble_with_options instead.")]
pub fn assemble_with_table(
    code: &str,
    table: &assembler::SymbolsTable,
) -> Result<(Vec<u8>, assembler::SymbolsTable), AssemblerError> {
    let tokens = parser::parse_str(code.into())?;
    let options = AssemblingOptions::new_with_table(table);
    let env = assembler::visit_tokens_all_passes_with_options(&tokens, &options)?;

    Ok((env.produced_bytes(), env.symbols().as_ref().clone()))
}

pub fn assemble_with_options(
    code: &str,
    options: &AssemblingOptions,
) -> Result<(Vec<u8>, assembler::SymbolsTable), AssemblerError> {
    let tokens = parser::parse_str(code.into())?;
    let env = assembler::visit_tokens_all_passes_with_options(&tokens, &options)?;

    Ok((env.produced_bytes(), env.symbols().as_ref().clone()))
}

#[cfg(test)]
mod test_super {
    use super::*;

    #[test]
    fn simple_test_assemble() {
        let code = "
		org 0
		db 1, 2
		db 3, 4
		";

        let bytes = assemble(code).unwrap_or_else(|e| panic!("Unable to assemble {}: {}", code, e));
        assert_eq!(bytes.len(), 4);
        assert_eq!(bytes, vec![1, 2, 3, 4]);
    }

    #[test]
    fn located_test_assemble() {
        let code = "
		org 0x100
		db 1, 2
		db 3, 4
		";

        let bytes = assemble(code).unwrap_or_else(|e| panic!("Unable to assemble {}: {}", code, e));
        assert_eq!(bytes, vec![1, 2, 3, 4]);
    }

    #[test]
    fn case_verification() {
        let code = "
		ld hl, TruC
Truc
		";

        let options = AssemblingOptions::new_case_sensitive();
        println!("{:?}", assemble_with_options(code, &options));
        assert!(assemble_with_options(code, &options).is_err());

        let options = AssemblingOptions::new_case_insensitive();
        println!("{:?}", assemble_with_options(code, &options));
        assert!(assemble_with_options(code, &options).is_ok());
    }
}