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
use crate::parser::{
    instruction::Instruction,
    operand::{CellOperand, CellValue, ExpandError, Operand},
    ram_code::RamCode,
    Parser, ParserError,
};
use thiserror::Error;

#[derive(Debug, PartialEq, Eq)]
pub struct RamMachine {
    code: RamCode,
    tape: Vec<Option<CellValue>>,
    pointer: usize,
    input: Vec<CellValue>,
    input_pointer: usize,
    output: Vec<CellValue>,
}

#[derive(Error, Debug, PartialEq, Eq)]
#[error("Buffer could not be accessed, because its value was never set.")]
pub struct BufferError;

#[derive(Error, Debug, PartialEq, Eq)]
pub enum RamMachineError {
    #[error(transparent)]
    ExpandError(#[from] ExpandError),
    #[error(transparent)]
    BufferError(#[from] BufferError),
    #[error(transparent)]
    InputAccessError(#[from] InputAccessError),
    #[error(transparent)]
    JumpError(#[from] JumpError),
    #[error("Addition of `{0}` to `{1}` failed.")]
    AdditionFailed(CellValue, CellValue),
    #[error("Subtraction of `{0}` from `{1}` failed.")]
    SubtractionFailed(CellValue, CellValue),
    #[error("Multiplication by `{0}` of `{1}~ failed.")]
    MultiplicationFailed(CellValue, CellValue),
    #[error("Division by `{0}` of `{1}` failed.")]
    DivisionFailed(CellValue, CellValue),
}

#[derive(Error, Debug, PartialEq, Eq)]
pub enum InputAccessError {
    #[error("Input at index `{0}` not found.")]
    NotExistentInput(usize),
}

#[derive(Error, Debug, PartialEq, Eq)]
pub enum JumpError {
    #[error("Label `{0}` could not be found in Ram code.")]
    LabelNotFound(String),
}

#[derive(PartialEq, Eq)]
pub enum RunState {
    Running,
    Halted,
}

impl RamMachine {
    pub fn new(code: RamCode, input: Vec<CellValue>) -> Self {
        RamMachine {
            code,
            tape: vec![None],
            pointer: 0,
            input_pointer: 0,
            input,
            output: Vec::new(),
        }
    }

    pub fn from_str(str: &str, input: Vec<CellValue>) -> Result<Self, ParserError> {
        Ok(RamMachine::new(Parser::new().parse(str)?, input))
    }

    pub fn run(mut self) -> Result<Vec<CellValue>, RamMachineError> {
        loop {
            let instruction = self.code.instructions[self.pointer].clone();
            if self.execute(&instruction)? == RunState::Halted {
                break;
            }
        }
        Ok(self.output)
    }

    // pub fn run_line(&mut self) -> Result<Vec<CellValue>, RamMachineError> {
    //     todo!()
    // }

    fn jump_to(&mut self, label: &str) -> Result<RunState, JumpError> {
        match self
            .code
            .jump_table
            .get(label)
            .ok_or_else(|| JumpError::LabelNotFound(label.to_owned()))
        {
            Ok(v) => {
                if *v < self.code.instructions.len() {
                    self.pointer = *v;
                    return Ok(RunState::Running);
                }
                Ok(RunState::Halted)
            }
            Err(e) => Err(e),
        }
    }

    fn get_input(&mut self) -> Result<&CellValue, InputAccessError> {
        let input = self
            .input
            .get(self.input_pointer)
            .ok_or(InputAccessError::NotExistentInput(self.input_pointer));
        self.input_pointer += 1;
        input
    }

    fn advance_pointer(&mut self) -> RunState {
        self.pointer += 1;
        if self.pointer < self.code.instructions.len() {
            return RunState::Running;
        }
        RunState::Halted
    }

    fn get(&self, operand: &Operand) -> Result<CellValue, ExpandError> {
        Ok(*operand.expand(&self.tape)?)
    }

    fn set(&mut self, cell_operand: &CellOperand, value: CellValue) -> Result<(), ExpandError> {
        let index = cell_operand.expand(&self.tape)?;
        if self.tape.len() < index + 1 {
            self.tape.resize(index + 1, None);
        }
        *self.tape.get_mut(index).expect("Tape was just resized") = Some(value);
        Ok(())
    }

    fn buffer(&self) -> Result<&CellValue, BufferError> {
        self.tape
            .first()
            .and_then(|val| val.as_ref())
            .ok_or(BufferError)
    }

    fn buffer_mut(&mut self) -> &mut Option<CellValue> {
        self.tape
            .get_mut(0)
            .expect("Tape was initialized with length 1")
    }

    fn execute(&mut self, instruction: &Instruction) -> Result<RunState, RamMachineError> {
        use Instruction::*;
        match instruction {
            Load(o) => {
                *self.buffer_mut() = Some(self.get(o)?);
                Ok(self.advance_pointer())
            }
            Store(o) => {
                self.set(o, *self.buffer()?)?;
                Ok(self.advance_pointer())
            }
            Add(o) => {
                *self.buffer_mut() = Some(self.buffer()?.checked_add(self.get(o)?).ok_or(
                    RamMachineError::AdditionFailed(
                        self.get(o).expect("Checked before"),
                        *self.buffer().expect("Checked before"),
                    ),
                )?);
                Ok(self.advance_pointer())
            }
            Sub(o) => {
                *self.buffer_mut() = Some(self.buffer()?.checked_sub(self.get(o)?).ok_or(
                    RamMachineError::SubtractionFailed(
                        self.get(o).expect("Checked before"),
                        *self.buffer().expect("Checked before"),
                    ),
                )?);
                Ok(self.advance_pointer())
            }
            Mult(o) => {
                *self.buffer_mut() = Some(self.buffer()?.checked_mul(self.get(o)?).ok_or(
                    RamMachineError::MultiplicationFailed(
                        self.get(o).expect("Checked before"),
                        *self.buffer().expect("Checked before"),
                    ),
                )?);
                Ok(self.advance_pointer())
            }
            Div(o) => {
                *self.buffer_mut() = Some(self.buffer()?.checked_div(self.get(o)?).ok_or(
                    RamMachineError::DivisionFailed(
                        self.get(o).expect("Checked before"),
                        *self.buffer().expect("Checked before"),
                    ),
                )?);
                Ok(self.advance_pointer())
            }
            Read(o) => {
                let input = *self.get_input()?;
                self.set(o, input)?;
                Ok(self.advance_pointer())
            }
            Write(o) => {
                self.output.push(self.get(o)?);
                Ok(self.advance_pointer())
            }
            Jump(s) => Ok(self.jump_to(s)?),
            Jgtz(s) => {
                if *self.buffer()? > 0 {
                    self.jump_to(s)?;
                    return Ok(RunState::Running);
                }
                Ok(self.advance_pointer())
            }
            Jzero(s) => {
                if *self.buffer()? == 0 {
                    self.jump_to(s)?;
                    Ok(RunState::Running)
                } else {
                    Ok(self.advance_pointer())
                }
            }
            Halt => Ok(RunState::Halted),
        }
    }
}