cpclib_asm/assembler/
control.rs

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
use cpclib_tokens::{Expr, ExprElement, FormattedExpr};

use super::{visit_assert, Env};
use crate::error::AssemblerError;
use crate::preamble::Z80Span;

/// This structure store the necessary information to replay the assembled stuff of previous passes when we do not reassemble
#[derive(Debug, Clone, PartialEq, Eq)]
struct ControlOutputByte {
    // The value to output
    value: u8
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ControlOutputBytes {
    // The value to output
    bytes: Vec<u8>
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ControlOrg {
    code: u16,
    output: u16
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ControlAssert {
    exp: Expr,
    txt: Option<Vec<FormattedExpr>>,
    span: Option<Z80Span>
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum ControlOutputCommand {
    Assert(ControlAssert),
    Byte(ControlOutputByte),
    Bytes(ControlOutputBytes),
    Org(ControlOrg)
}

impl From<ControlAssert> for ControlOutputCommand {
    fn from(c: ControlAssert) -> Self {
        Self::Assert(c)
    }
}

impl From<ControlOutputByte> for ControlOutputCommand {
    fn from(b: ControlOutputByte) -> Self {
        Self::Byte(b)
    }
}

impl From<ControlOutputBytes> for ControlOutputCommand {
    fn from(b: ControlOutputBytes) -> Self {
        Self::Bytes(b)
    }
}

impl From<ControlOrg> for ControlOutputCommand {
    fn from(value: ControlOrg) -> Self {
        Self::Org(value)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ControlOutputStore {
    pub(crate) remaining_passes: u8,
    pub(crate) commands: Vec<ControlOutputCommand>
}

impl ControlAssert {
    fn execute(&mut self, env: &mut Env) -> Result<(), AssemblerError> {
        visit_assert(&self.exp, self.txt.as_ref(), env, self.span.as_ref())?;
        Ok(())
    }
}

impl ControlOutputByte {
    fn execute(&mut self, env: &mut Env) -> Result<(), AssemblerError> {
        env.output_byte(self.value)?;
        Ok(())
    }
}

impl ControlOutputBytes {
    fn execute(&mut self, env: &mut Env) -> Result<(), AssemblerError> {
        env.output_bytes(&self.bytes)?;
        Ok(())
    }
}

impl ControlOrg {
    fn execute(&mut self, env: &mut Env) -> Result<(), AssemblerError> {
        env.visit_org_set_arguments(self.code, self.output)
    }
}

impl ControlOutputCommand {
    fn execute(&mut self, env: &mut Env) -> Result<(), AssemblerError> {
        match self {
            ControlOutputCommand::Assert(cmd) => cmd.execute(env),
            ControlOutputCommand::Byte(cmd) => cmd.execute(env),
            ControlOutputCommand::Bytes(cmd) => cmd.execute(env),
            ControlOutputCommand::Org(cmd) => cmd.execute(env),
            _ => unimplemented!()
        }
    }
}

impl ControlOutputStore {
    pub fn with_passes(passes: u8) -> Self {
        ControlOutputStore {
            remaining_passes: passes,
            commands: Default::default()
        }
    }

    pub fn store_byte(&mut self, value: u8) {
        match self.commands.last_mut() {
            Some(ControlOutputCommand::Byte(ControlOutputByte { value: previous })) => {
                let new_command = ControlOutputBytes {
                    bytes: vec![*previous, value]
                };
                self.commands.pop();
                self.commands.push(new_command.into());
            },
            Some(ControlOutputCommand::Bytes(ControlOutputBytes { bytes })) => {
                bytes.push(value);
            },
            _ => {
                self.commands.push(ControlOutputByte { value }.into());
            }
        }
    }

    pub fn store_org(&mut self, code: u16, output: u16) {
        self.commands.push(ControlOrg { code, output }.into())
    }

    pub fn store_assert(
        &mut self,
        exp: Expr,
        txt: Option<Vec<FormattedExpr>>,
        span: Option<Z80Span>
    ) {
        self.commands.push(ControlAssert { exp, txt, span }.into())
    }

    pub fn has_remaining_passes(&self) -> bool {
        self.remaining_passes > 0
    }

    pub fn execute(&mut self, env: &mut Env) -> Result<(), AssemblerError> {
        for cmd in &mut self.commands {
            cmd.execute(env)?;
        }
        Ok(())
    }

    pub fn new_pass(&mut self) {
        self.remaining_passes -= 1;
        self.commands.clear();
    }

    // pub fn extend<I: Iterator<Item = ControlOutputCommand> >(&mut self, iter: I) {
    // self.commands.extend(iter)
    // }
}