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
|
use super::{ Address, Program };
impl Program {
pub(crate) fn remove_definition_addresses(&mut self) {
for instr in &mut self.instructions {
for arg in &mut instr.args {
if let Address::Definitions(name) = arg {
*arg = self.definitions[name].clone();
}
}
}
self.definitions.clear();
}
pub(crate) fn analyze(&self) {
for instr in &self.instructions {
let result = instr.check_args();
if result.is_err() {
panic!("Instruction '{}' error: {:?}", instr.name, result.err().unwrap());
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::lexer::Token;
#[test]
fn remove_definition_addresses_removes_symbols() {
let mut prog = Program::new(vec![
Token::SymbolDef("hello".to_string()),
Token::Number(1000),
Token::SymbolDef("world".to_string()),
Token::Number(64),
Token::Operator("jsr".to_string()),
Token::Symbol("hello".to_string()),
Token::Operator("mov".to_string()),
Token::Address(1001),
Token::Symbol("world".to_string()),
Token::Operator("subleq".to_string()),
Token::Symbol("hello".to_string()),
Token::Address(1002),
Token::Symbol("world".to_string()),
], 10);
prog.remove_definition_addresses();
assert_eq!(prog.definitions.len(), 0);
for i in prog.instructions {
for arg in i.args {
assert!(! matches!(arg, Address::Definitions(_)));
}
}
}
}
|