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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//! This module contains the virtual machine which executes Strontium bytecode. The VM uses a set of typed 
//! registers to do number arithmetic, a memory vector provides the storage space for anything else.

pub mod instruction;
pub mod opcode;
pub mod register;

use self::opcode::Opcode;
use self::register::{RegisterValue, Registers};
use self::instruction::*;

use crate::types::StrontiumError;

use std::convert::TryInto;
use std::collections::HashMap;
use std::rc::Rc;
// use byteorder::{LittleEndian, ReadBytesExt};

#[derive(Debug, Clone, PartialEq)]
pub enum Value {
	Bytes(Vec<u8>),
	Int(i64),
	UInt(u64),
	Float(f64),
	String(String),
}


pub struct StackFrame {
    /// The instruction pointer to return to after the function returns
    pub return_address: usize,
    /// The index of the first argument in the `registers` array
    pub arg_start: usize,
    /// The number of arguments passed to the function
    pub arg_count: usize,
}

pub struct Strontium {
	/// Holds general-purpose registers for storing different types of values
	pub registers: Registers,
	/// Points to the next index in the buffer of the bytecode register.
	pub ip:        usize,
	/// Contains references for function arguments and return values
	pub call_stack: Vec<u8>,
	should_continue: bool,
	executors: HashMap<Opcode, Rc<dyn Executor>>,
}

impl Strontium {
	/// Create a new instance of the virtual machine
	pub fn new() -> Self {
		let mut executors: HashMap<Opcode, Rc<dyn Executor>> = HashMap::new();

        executors.insert(Opcode::HALT, Rc::new(HaltExecutor));
        executors.insert(Opcode::LOAD, Rc::new(LoadExecutor));
        executors.insert(Opcode::CALCULATE, Rc::new(CalculateExecutor));
        executors.insert(Opcode::INTERRUPT, Rc::new(InterruptExecutor));

		Self {
			registers:  Registers::new(),
			ip:      	0,
			call_stack: vec![],
			should_continue: true,
			executors,
		}
	}

	/// Append machine code to the array in the bytecode register.
    pub fn push_bytecode(&mut self, bytes: Vec<u8>) {
		let mut bytecode = self.bc();
        bytecode.extend(bytes);
        self.registers.set("bc", RegisterValue::Array(
			bytecode
				.iter()
				.map(|v| RegisterValue::UInt8(*v))
				.collect()
		));
    }

	/// Execute a single instruction.
    pub fn execute(&mut self, instruction: Instruction) -> Result<bool, StrontiumError> {
		let opcode: Opcode = instruction.clone().into();
		let executor = self.executors.get(&opcode).cloned();

		self.should_continue = match executor {
			Some(executor) => executor.execute(
				self,
				instruction,
			)?,
			None => return Err(StrontiumError::IllegalOpcode(self.peek())),
		};

		Ok(self.should_continue)
	}

	/// Execute instructions until a `HALT` instruction is encountered.
/*
	pub fn execute_until_halt(&mut self) -> Result<bool, StrontiumError> {
        self.should_continue = true;

        while self.should_continue && !self.eof() {
            self.execute()?;
        }

        Ok(true)
    }
*/

	fn ip(&self) -> usize {
		match self.registers.get("ip").unwrap() {
			RegisterValue::UInt64(ip) => *ip as usize,
			_ => unreachable!(),
		}
	}

	fn bc(&self) -> Vec<u8> {
		let bc = match self.registers.get("bc").unwrap() {
			RegisterValue::Array(bytes) => bytes.clone(),
			_ => unreachable!(),
		};

		bc.iter().map(|v| match v {
            RegisterValue::UInt8(b) => *b,
            _ => unreachable!(),
        }).collect()
	}

	fn _set_register(&mut self, name: &str, value: RegisterValue) {
		self.registers.set(name, value);
	}

	fn _get_register(&self, name: &str) -> Option<&RegisterValue> {
		self.registers.get(name)
	}

	fn consume_bytes(&mut self, size: usize) -> Result<Vec<u8>, StrontiumError> {
		let ip = self.ip();
		let bytecode = self.bc();

		if ip + size > bytecode.len() {
			Err(StrontiumError::UnexpectedEof)
		} else {
			let bytes = bytecode[ip .. ip + size].to_vec();
			self.advance_by(size)?;
			Ok(bytes)
		}
	}

	/// Consume an unsigned 64-bit integer from the bytecode register.
	///
	/// This performs a lookahead on the bytecode register to read the next eight bytes
	/// and converts the bytes into a 64-bit integer value. The byte encoding within
	/// Strontium bytecode is always Little Endian.
    pub fn consume_u64(&mut self) -> Result<u64, StrontiumError> {
		let bytes = self.consume_bytes(8)?;
		Ok(u64::from_le_bytes(bytes.try_into().unwrap()))
	}

	pub fn consume_u32(&mut self) -> Result<u32, StrontiumError> {
		let bytes = self.consume_bytes(4)?;
		Ok(u32::from_le_bytes(bytes.try_into().unwrap()))
	}

	pub fn consume_u16(&mut self) -> Result<u16, StrontiumError> {
		let bytes = self.consume_bytes(2)?;
		Ok(u16::from_le_bytes(bytes.try_into().unwrap()))
	}

	pub fn consume_u8(&mut self) -> Result<u8, StrontiumError> {
		let bytes = self.consume_bytes(1)?;
		Ok(bytes[0])
	}

	pub fn consume_i64(&mut self) -> Result<i64, StrontiumError> {
		let bytes = self.consume_bytes(8)?;
		Ok(i64::from_le_bytes(bytes.try_into().unwrap()))
	}

	pub fn consume_i32(&mut self) -> Result<i32, StrontiumError> {
		let bytes = self.consume_bytes(4)?;
		Ok(i32::from_le_bytes(bytes.try_into().unwrap()))
	}

	pub fn consume_i16(&mut self) -> Result<i16, StrontiumError> {
		let bytes = self.consume_bytes(2)?;
		Ok(i16::from_le_bytes(bytes.try_into().unwrap()))
	}

	pub fn consume_i8(&mut self) -> Result<i8, StrontiumError> {
		let bytes = self.consume_bytes(1)?;
		Ok(bytes[0] as i8)
	}

	pub fn consume_f64(&mut self) -> Result<f64, StrontiumError> {
		let bytes = self.consume_bytes(8)?;
		Ok(f64::from_le_bytes(bytes.try_into().unwrap()))
	}

	pub fn consume_f32(&mut self) -> Result<f32, StrontiumError> {
		let bytes = self.consume_bytes(4)?;
		Ok(f32::from_le_bytes(bytes.try_into().unwrap()))
	}

	pub fn consume_bool(&mut self) -> Result<bool, StrontiumError> {
		let bytes = self.consume_bytes(1)?;
		Ok(bytes[0] == 1)
	}

	pub fn consume_byte(&mut self) -> Result<u8, StrontiumError> {
		let bytes = self.consume_bytes(1)?;
		Ok(bytes[0])
	}

	pub fn consume_string(&mut self) -> Result<String, StrontiumError> {
		println!("Consume String");
		// First, consume the length of the string (assuming it's stored as a 32-bit unsigned integer)
		let length = self.consume_u32()? as usize;
		println!("Length: {}", length);

		// Now, consume the actual string bytes
		let bytes = self.consume_bytes(length)?;

		// Convert the bytes to a UTF-8 string
		match String::from_utf8(bytes) {
			Ok(string) => Ok(string),
			Err(_) => Err(StrontiumError::InvalidUtf8String),
		}
	}

	fn peek(&self) -> u8 {
		let bytecode = self.bc();
		bytecode[self.ip()]
	}

	fn advance_by(&mut self, n: usize) -> Result<(), StrontiumError> {
		let ip = self.ip().clone();
		
		if ip + n < self.bc().len() {
			self.registers.set(
				"ip",
				RegisterValue::UInt64((ip + n) as u64),
			);
			Ok(())
		} else {
			Err(StrontiumError::UnexpectedEof)
		}
	}

	/// Returns true when the instruction pointer is at the end of the memory array.
	fn _eof(&mut self) -> bool {
		let ip = self.ip().clone();
		ip > self.bc().len()
	}
}

mod test {

}