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
use Value;
use Operation;
use operators;
use operators::Operator;

#[derive(Serialize, Debug, Clone)]
pub struct InternalState {
  pub register: Option<Value>,
	pub input_tape: Vec<Value>,
	pub output_tape: Vec<Value>,
	pub memory: Vec<Option<Value>>,
	pub instruction_counter: usize,
	_executed_instructions: u32
}


macro_rules! apply_operation {
	($self: ident, $operator:expr) => ({
		let op = $operator;
    match op.apply_to($self) {
			Ok(()) => {
				if !op.changes_instruction_counter() {
					$self.instruction_counter += 1;
				}
				$self.increase_executed_instructions();
				Ok(())
			},
			Err(reason) => {
				Err(reason)
			}
		}
	})
}

impl InternalState {
	pub fn new(register: Option<Value>, counter: usize) -> InternalState {
		InternalState {
			register,
			input_tape: vec!(),
			output_tape: vec!(),
			memory: vec!(),
			instruction_counter: counter,
			_executed_instructions: 0
		}
	}

	pub fn with_input_tape(mut self, new_input_tape: Vec<Value>) -> Self {
		self.input_tape = new_input_tape;
		self
	}

	pub fn with_memory(mut self, new_memory: Vec<Option<Value>>) -> Self {
		self.memory = new_memory;
		self
	}

    pub fn executed_instructions(&self) -> u32 {
		self._executed_instructions
	}

	pub fn increase_executed_instructions(&mut self) {
		self._executed_instructions += 1;
	}

	pub fn apply(&mut self, op: Operation) -> Result<(), String> {
		match op {
			Operation::Add{cell: _cell} => {
				apply_operation!(self, operators::add::AddOp{cell: _cell})
			},
			Operation::Sub{cell: _cell} => {
				apply_operation!(self, operators::sub::SubOp{cell: _cell})
			},
			Operation::Inbox => {
				apply_operation!(self, operators::inbox::InboxOp{})
			},
			Operation::Outbox => {
				apply_operation!(self, operators::outbox::OutboxOp{})
			},
			Operation::CopyFrom{cell: _cell} => {
				apply_operation!(self, operators::copyfrom::CopyFromOp{cell: _cell})
			},
			Operation::CopyTo{cell: _cell} => {
				apply_operation!(self, operators::copyto::CopyToOp{cell: _cell})
			},
			Operation::Label => {
				apply_operation!(self, operators::jump::LabelOp)
			},
			Operation::Jump{next_operation: _next_op} => {
				apply_operation!(self, operators::jump::JumpOp{next_operation: _next_op})
			},
			Operation::JumpEqualsZero{next_operation: _next_op} => {
				apply_operation!(self, operators::jump::JumpEqualsZeroOp{next_operation: _next_op})
			}
			Operation::JumpNegative{next_operation: _next_op} => {
				apply_operation!(self, operators::jump::JumpNegativeOp{next_operation: _next_op})
			}
			Operation::BumpPlus{cell: _cell} => {
				apply_operation!(self, operators::bump::BumpPlusOp{cell: _cell})
			}
			Operation::BumpMinus{cell: _cell} => {
				apply_operation!(self, operators::bump::BumpMinusOp{cell: _cell})
			}
		}
	}
}

#[cfg(test)]
mod test {
	use state::InternalState;
	use Value;
	use Operation;

	#[test]
	fn executed_counter_at_start() {
		let state = InternalState::new(None, 0);

		assert_eq!(state.executed_instructions(), 0);
	}

	#[test]
	fn executed_counter_is_increased() {
		let mut state = InternalState::new(None, 0)
			.with_input_tape(vec!(Value::Number{value: 1}));

		let _ = state.apply(Operation::Inbox);

		assert_eq!(state.executed_instructions(), 1);
	}
}