Website Summary Files Commits Branches @commit @diff @stats
path: root / assembler / src / parser / instruction.rs
blob: f78c9a63e2ec76a0be66d170c168ff4b127ffece (plain) (blame)
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
use super::{ Instruction, Address };

impl Instruction {
    fn new_subleq(a: &Address, b: &Address, c: &Address) -> Instruction {
        Instruction {
            name: "subleq".to_string(),
            args: vec![a.clone(), b.clone(), c.clone()],
        }
    }

    pub(crate) fn collapse(&mut self, next: usize) -> Vec<Instruction> {
        self.name = self.name.to_lowercase();
        static Z: Address = Address::Memory(0);

        match &*self.name {
            "subleq" => vec![self.clone()],
            "jmp" => vec![
                Instruction::new_subleq(&Z, &Z, &self.args[0]),
            ],
            "add" => vec![
                Instruction::new_subleq(&self.args[0], &Z,            &Address::Instruction(next + 1)),
                Instruction::new_subleq(&Z,            &self.args[1], &Address::Instruction(next + 2)),
                Instruction::new_subleq(&Z,            &Z,            &Address::Instruction(next + 3)),
            ],
            _ => panic!("Unknown instruction {}!", self.name),
        }
    }
}