sicompiler 1.0.1

A basic compiler for SiCoMe programs
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::collections::HashMap;
use std::{fs, io};

use crate::models::{instruction::Instruction, variable::Variable, init::Init, program::Program};
use crate::errors::error::SicompilerError;

/// The `Tokenizer` struct is responsible for tokenizing input source code,
/// removing comments and empty lines, and providing a sequence of valid code lines.
pub struct Tokenizer {
    input: String,
    rep: String
}

impl Tokenizer {
    /// Remove one-line comments and empty lines from the input source code.
    /// 
    /// ## Arguments
    /// - `content` - The file content
    /// 
    /// ## Returns
    /// A String with the new content without comments and empty lines.
    /// 
    fn remove_oneline_comments(content: &str) -> String {
        content.lines()
            .map(|line: &str| {
                let trimmed_line: &str = line.trim();
                if trimmed_line.is_empty() {
                    String::from(line)
                } else if let Some(index) = trimmed_line.find('#') {
                    String::from(&trimmed_line[..index])
                } else {
                    String::from(trimmed_line)
                }
            })
            .collect::<Vec<String>>()
            .join("\n")
    }

    /// Remove multi-line comments and empty lines from the input source code.
    /// 
    /// ## Arguments
    /// - `content` - The file content without one-line comments
    /// 
    /// ## Returns
    /// A String with the new content without comments and empty lines.
    /// 
    fn remove_multiline_comments(content: &str) -> String {
        let mut result: String = String::new();
        let mut in_comment: bool = false;

        for line in content.lines() {
            let trimmed_line: &str = line.trim();

            if trimmed_line.starts_with("***") {
                in_comment = true;
                continue;
            }

            if trimmed_line.ends_with("***") {
                in_comment = false;
                continue;
            }

            if !in_comment {
                result.push_str(line);
                result.push('\n');
            }
        }

        result
    }

    /// Tokenizes instruction 
    /// 
    /// ## Arguments
    /// - `section` - The instruction section of the file
    /// 
    /// ## Returns 
    /// A vector of `Instruction` instances.
    /// 
    fn tokenize_instructions(section: &str) -> Vec<Instruction> {
        section
            .lines()
            .filter(|token| !token.is_empty())
            .map(|token| {
                let parts: Vec<&str> = token.split_whitespace().collect();
                Instruction::new(parts[0], parts[1..].to_vec())
            })
            .collect()
    }

    /// Tokenizes varibles 
    /// 
    /// ## Arguments
    /// - `section` - The varibles section of the file
    /// 
    /// ## Returns 
    /// A vector of `Varibles` instances or an Error.
    /// 
    fn tokenize_variables(section: &str) -> Result<Vec<Variable>, SicompilerError> {
        let mut variables: Vec<Variable> = Vec::new();
        
        for token in section.lines() {
            if token.is_empty() { continue }
    
            let parts: Vec<&str> = token.split_whitespace().collect();
            
            if parts.len() != 2 { 
                return Err(
                    SicompilerError::TokenizationError(format!("Invalid variable format, the correct way is <DIR NAME>"))
                );
            }

            variables.push(Variable::new(parts[0], parts[1]));
        }

        Ok(variables)
    }

    /// Tokenizes init section 
    /// 
    /// ## Arguments
    /// - `section` - The init section of the file
    /// 
    /// ## Returns 
    /// A `Init` instances or an Error.
    /// 
    fn tokenize_init(section: &str) -> Result<Init, SicompilerError> {
        if section.is_empty() { 
            return Err(SicompilerError::TokenizationError(format!("There is no any Init section.")));
        }

        let valid_section: Vec<&str> = section.split_whitespace().collect();

        if valid_section.is_empty() { 
            return Err(SicompilerError::TokenizationError(format!("There is no any Init address.")));
        }

        if valid_section.len() > 1 {
            return Err(SicompilerError::TokenizationError(format!("There is more than one Init address.")));
        }

        let dir: &str = valid_section[0];
        Ok(Init::new(dir))
    }
    
    /// Creates a new `Tokenizer` instance with the specified input file name.
    ///
    /// ## Arguments
    ///
    /// - `input` - The name of the input file to be tokenized.
    /// 
    pub fn new(input: &str, rep: &str) -> Tokenizer { 
        Tokenizer { input: input.to_string(), rep: rep.to_string() }
    }

    /// Tokenizes the content of a repertoire file, creating a mapping of mnemonics to instructions.
    ///
    /// # Arguments
    ///
    /// - `repertoire_input` - A string representing the path to the repertoire file.
    ///
    /// # Returns
    ///
    /// - `Result<HashMap<String, Instruction>, Error>` - Result containing a mapping of mnemonics to instructions
    ///   if successful, or an `Error` if any issues occur during tokenization or file reading.
    ///
    pub fn tokenize_repertoire(&self) -> Result<HashMap<String, Instruction>, SicompilerError> {
        let mut repertorie: HashMap<String, Instruction> = HashMap::new();
        
        let content: String = fs::read_to_string(&self.rep)?;

        if !content.contains("$") {
            return Err(SicompilerError::TokenizationError(
                format!("Invalid repertoire structure, the file must contain a microprogram section.")
            ));
        }

        let content: Vec<&str> = content.split('$').collect();

        let mut instructions_part: &str = content[2];

        if instructions_part.starts_with("\n") {
            instructions_part = &instructions_part[1..];
        }

        if instructions_part.lines().count() > 32 {
            return Err(SicompilerError::TokenizationError(
                format!("Invalid number of instructions, the max is 32 but get {}", instructions_part.lines().count())
            ));
        }

        for token in instructions_part.lines() {
            if token.is_empty() { continue }
            
            let parts: Vec<&str> = token.split_whitespace().collect();

            let mnemonic: String = parts[0].to_string();
            let flag: bool = parts[1] == "true";

            let mut instruction: Instruction = Instruction::new(&mnemonic, vec![]);

            if flag {
                instruction.set_flag(true);
                
                //* An instruction only have 1 argument  
                instruction.set_params(vec!["0x123"]);
            }

            repertorie.insert(mnemonic, instruction);
        }
        
        Ok(repertorie)
    }
    
    /// Tokenizes the content of the input file and returns a `Result` containing a `Program` or an `Error`.
    /// 
    /// ## Arguments
    /// 
    /// - `&self` - Reference to the `Tokenizer` instance.
    /// 
    /// ## Returns
    /// 
    /// - `Result<Program, Error>` - Result containing a `Program` instance if successful, or an `Error` if any issues occur.
    /// 
    /// ## Errors
    /// 
    /// Returns an `Error` if:
    /// 
    /// - The file is empty.
    /// - The number of sections in the file is not equal to 3.
    /// - No init dir is found.
    /// 
    pub fn tokenize(&self) -> Result<Program, SicompilerError> {
        let mut content: String = fs::read_to_string(&self.input)
            .map_err(|err: io::Error| 
                SicompilerError::Io(io::Error::new(err.kind(), format!("Can't open {}", self.input)))
            )?;
        
        if content.is_empty() { 
            return Err(SicompilerError::TokenizationError(format!("The file is empty")));
        }

        content = Tokenizer::remove_oneline_comments(&content);
        content = Tokenizer::remove_multiline_comments(&content);

        let sections: Vec<&str> = content.split('@').collect();

        if sections.len() != 3 {
            return Err(SicompilerError::TokenizationError(
                format!("Invalid number of sections, must be 3 but get {}", sections.len())
            ))
        }
        
        let mut variables: Vec<Variable> = Vec::new();
        if let Some(variable_section) = sections.get(0) {
            variables = Tokenizer::tokenize_variables(variable_section)?;
        }
        
        let mut init: Init = Init::new("");
        if let Some(init_section) = sections.get(1) {
            init = Tokenizer::tokenize_init(init_section)?;
        }
        
        let mut instructions: Vec<Instruction> = Vec::new();
        if let Some(instruction_section) = sections.get(2) {
            instructions = Tokenizer::tokenize_instructions(instruction_section);
        }

        Ok(Program::new(variables, init, instructions))
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::models::{
        instruction::Instruction, 
        variable::Variable, 
        init::Init, 
        program::Program
    };

    use crate::errors::error::SicompilerError;

    use super::*;

    #[test]
    fn test_remove_one_line_comment() {
        let content: &str = "This is a line without comments
This is a line with a comment #This is a one-line comment
Another line without comments";

        let result: String = Tokenizer::remove_oneline_comments(content);
        assert_eq!(result, "This is a line without comments\nThis is a line with a comment \nAnother line without comments");
    }

    #[test]
    fn test_remove_multi_line_comment() {
        let content: &str = "This is a line without comments.
*** This 
is a multiline 
comment ***
Another line without comments.";
    
        let result: String = Tokenizer::remove_multiline_comments(content);
        assert_eq!(result, "This is a line without comments.\nAnother line without comments.\n");
    }

    #[test]
    fn test_tokenize_instructions() {
        let section: &str = "HALT\nADD 1";
        let instructions: Vec<Instruction> = Tokenizer::tokenize_instructions(section);

        assert_eq!(instructions.len(), 2);
        assert_eq!(instructions[0].mnemonic(), "HALT");
        assert_eq!(instructions[0].params().len(), 0);
        assert_eq!(instructions[1].mnemonic(), "ADD");
        assert_eq!(instructions[1].params().len(), 1);
    }

    #[test]
    fn test_tokenize_variables() {
        let section: &str = "1 0003\n3 0000";
        let result: Result<Vec<Variable>, SicompilerError> = Tokenizer::tokenize_variables(section);

        assert!(result.is_ok());
        assert_eq!(result.as_ref().unwrap().len(), 2);
        assert_eq!(result.as_ref().unwrap()[0].dir(), "1");
        assert_eq!(result.as_ref().unwrap()[0].name(), "0003");
        assert_eq!(result.as_ref().unwrap()[1].dir(), "3");
        assert_eq!(result.as_ref().unwrap()[1].name(), "0000");

        let section: &str = "1=0003\n3 = 0000\n";
        let result: Result<Vec<Variable>, SicompilerError> = Tokenizer::tokenize_variables(section);

        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "Tokenization error: Invalid variable format, the correct way is <DIR NAME>");
    }

    #[test]
    fn test_tokenize_init() {
        let section: &str = "1";
        let init: Result<Init, SicompilerError> = Tokenizer::tokenize_init(section);

        assert!(init.is_ok());
        assert_eq!(init.unwrap().dir(), "1");

        let section: &str = "";
        let result: Result<Init, SicompilerError> = Tokenizer::tokenize_init(section);

        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "Tokenization error: There is no any Init section.");

        let section: &str = " ";
        let result: Result<Init, SicompilerError> = Tokenizer::tokenize_init(section);
    
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "Tokenization error: There is no any Init address.");
        
        let section: &str = "2 3 5";
        let result: Result<Init, SicompilerError> = Tokenizer::tokenize_init(section);
    
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "Tokenization error: There is more than one Init address.");
    
    }

    #[test]
    fn test_tokenize_repertoire() {
        let tokenizer: Tokenizer = Tokenizer::new("tests-files/test-input.txt", "tests-files/test-repertoire.rep");
        let result: Result<HashMap<String, Instruction>, SicompilerError>=  tokenizer.tokenize_repertoire();

        assert!(result.is_ok());
        assert_eq!(result.as_ref().unwrap().len(), 2);
        assert_eq!(result.as_ref().unwrap().get("HALT").unwrap().mnemonic(), "HALT");
        assert_eq!(result.as_ref().unwrap().get("HALT").unwrap().params().len(), 0);
        assert_eq!(result.as_ref().unwrap().get("ADD").unwrap().mnemonic(), "ADD");
        assert_eq!(result.as_ref().unwrap().get("ADD").unwrap().params().len(), 1);
        
        let tokenizer: Tokenizer = Tokenizer::new("tests-files/test-input.txt", "tests-files/fails-files/invalid-repertoire.rep");
        let result: Result<HashMap<String, Instruction>, SicompilerError> = tokenizer.tokenize_repertoire();
    
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "Tokenization error: Invalid repertoire structure, the file must contain a microprogram section.");
        
        let tokenizer: Tokenizer = Tokenizer::new("tests-files/test-input.txt", "tests-files/fails-files/more-instructions-rep.rep");
        let result: Result<HashMap<String, Instruction>, SicompilerError> = tokenizer.tokenize_repertoire();

        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "Tokenization error: Invalid number of instructions, the max is 32 but get 35");
    }

    #[test]
    fn test_tokenize() {
        let tokenizer: Tokenizer = Tokenizer::new("tests-files/test-input.txt", "tests-files/test-repertoire.rep");
        let result: Result<Program, SicompilerError> = tokenizer.tokenize();

        assert!(result.is_ok());
        assert_eq!(result.as_ref().unwrap().variables().len(), 3);
        assert_eq!(result.as_ref().unwrap().init().dir(), "6");
        assert_eq!(result.as_ref().unwrap().instructions().len(), 2);

        let tokenizer: Tokenizer = Tokenizer::new("tests-files/fails-files/no-exits-file.txt", "tests-files/test-repertoire.rep");
        let result: Result<Program, SicompilerError> = tokenizer.tokenize();
    
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "I/O error: Can't open tests-files/fails-files/no-exits-file.txt");
        
        let tokenizer: Tokenizer = Tokenizer::new("tests-files/fails-files/empty-file.txt", "tests-files/test-repertoire.rep");
        let result: Result<Program, SicompilerError> = tokenizer.tokenize();
    
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "Tokenization error: The file is empty");
        
        let tokenizer: Tokenizer = Tokenizer::new("tests-files/fails-files/invalid-file.txt", "tests-files/test-repertoire.rep");
        let result: Result<Program, SicompilerError> = tokenizer.tokenize();
        
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().to_string(), "Tokenization error: Invalid number of sections, must be 3 but get 1");
    
    }
}