2024-10-12 07:06:44 +00:00
|
|
|
use std::mem::replace;
|
|
|
|
|
2024-09-12 04:39:31 +00:00
|
|
|
use crate::{
|
2024-10-12 08:50:30 +00:00
|
|
|
parse, value::Primitive, AnnotatedError, Chunk, ChunkError, DustError, Identifier, Instruction,
|
|
|
|
Operation, Span, Value, ValueError,
|
2024-09-12 04:39:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn run(source: &str) -> Result<Option<Value>, DustError> {
|
|
|
|
let chunk = parse(source)?;
|
|
|
|
let mut vm = Vm::new(chunk);
|
|
|
|
|
|
|
|
vm.run()
|
|
|
|
.map_err(|error| DustError::Runtime { error, source })
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub struct Vm {
|
|
|
|
ip: usize,
|
2024-10-12 07:06:44 +00:00
|
|
|
chunk: Chunk,
|
|
|
|
stack: Vec<Register>,
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Vm {
|
|
|
|
const STACK_LIMIT: usize = u16::MAX as usize;
|
|
|
|
|
|
|
|
pub fn new(chunk: Chunk) -> Self {
|
|
|
|
Self {
|
|
|
|
ip: 0,
|
2024-10-12 07:06:44 +00:00
|
|
|
chunk,
|
|
|
|
stack: Vec::new(),
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self) -> Result<Option<Value>, VmError> {
|
2024-09-24 03:38:49 +00:00
|
|
|
// DRY helper to get constant or register values for binary operations
|
2024-09-23 23:16:15 +00:00
|
|
|
fn get_arguments(
|
2024-09-19 03:02:28 +00:00
|
|
|
vm: &mut Vm,
|
|
|
|
instruction: Instruction,
|
|
|
|
position: Span,
|
2024-09-23 23:16:15 +00:00
|
|
|
) -> Result<(&Value, &Value), VmError> {
|
2024-09-24 20:49:17 +00:00
|
|
|
let left = if instruction.b_is_constant() {
|
|
|
|
vm.chunk.get_constant(instruction.b(), position)?
|
2024-09-18 01:10:44 +00:00
|
|
|
} else {
|
2024-09-24 20:49:17 +00:00
|
|
|
vm.get(instruction.b(), position)?
|
2024-09-18 01:10:44 +00:00
|
|
|
};
|
2024-09-24 20:49:17 +00:00
|
|
|
let right = if instruction.c_is_constant() {
|
|
|
|
vm.chunk.get_constant(instruction.c(), position)?
|
2024-09-18 01:10:44 +00:00
|
|
|
} else {
|
2024-09-24 20:49:17 +00:00
|
|
|
vm.get(instruction.c(), position)?
|
2024-09-18 01:10:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok((left, right))
|
2024-09-19 03:02:28 +00:00
|
|
|
}
|
2024-09-18 01:10:44 +00:00
|
|
|
|
2024-09-12 04:39:31 +00:00
|
|
|
while let Ok((instruction, position)) = self.read(Span(0, 0)).copied() {
|
2024-10-12 07:06:44 +00:00
|
|
|
log::info!(
|
|
|
|
"{} | {} | {} | {}",
|
|
|
|
self.ip - 1,
|
|
|
|
position,
|
|
|
|
instruction.operation(),
|
|
|
|
instruction
|
|
|
|
.disassembly_info(Some(&self.chunk))
|
|
|
|
.0
|
|
|
|
.unwrap_or_default()
|
|
|
|
);
|
2024-09-12 04:39:31 +00:00
|
|
|
|
2024-09-15 05:24:04 +00:00
|
|
|
match instruction.operation() {
|
2024-09-13 01:14:15 +00:00
|
|
|
Operation::Move => {
|
2024-10-12 07:06:44 +00:00
|
|
|
let to_register = instruction.a();
|
|
|
|
let from_register = instruction.b();
|
|
|
|
let value = self.take(from_register, to_register, position)?;
|
2024-09-13 01:14:15 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(to_register, value, position)?;
|
2024-09-13 01:14:15 +00:00
|
|
|
}
|
2024-09-18 03:06:58 +00:00
|
|
|
Operation::Close => {
|
2024-10-12 07:06:44 +00:00
|
|
|
let from_register = instruction.b();
|
|
|
|
let to_register = instruction.c();
|
|
|
|
|
|
|
|
if self.stack.len() < to_register as usize {
|
|
|
|
return Err(VmError::StackUnderflow { position });
|
|
|
|
}
|
2024-09-18 03:06:58 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
for register_index in from_register..to_register {
|
|
|
|
self.stack[register_index as usize] = Register::Empty;
|
2024-09-18 03:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-18 17:42:32 +00:00
|
|
|
Operation::LoadBoolean => {
|
2024-09-24 20:49:17 +00:00
|
|
|
let to_register = instruction.a();
|
|
|
|
let boolean = instruction.b_as_boolean();
|
|
|
|
let skip = instruction.c_as_boolean();
|
2024-09-18 17:42:32 +00:00
|
|
|
let value = Value::boolean(boolean);
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(to_register, value, position)?;
|
2024-09-18 17:42:32 +00:00
|
|
|
|
2024-09-23 08:01:36 +00:00
|
|
|
if skip {
|
2024-09-18 17:42:32 +00:00
|
|
|
self.ip += 1;
|
|
|
|
}
|
|
|
|
}
|
2024-09-12 04:39:31 +00:00
|
|
|
Operation::LoadConstant => {
|
2024-09-24 20:49:17 +00:00
|
|
|
let to_register = instruction.a();
|
|
|
|
let from_constant = instruction.b();
|
2024-09-25 13:55:10 +00:00
|
|
|
let jump = instruction.c_as_boolean();
|
2024-09-12 04:39:31 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set_constant(to_register, from_constant, position)?;
|
2024-09-25 13:55:10 +00:00
|
|
|
|
|
|
|
if jump {
|
|
|
|
self.ip += 1;
|
|
|
|
}
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
2024-09-17 23:35:33 +00:00
|
|
|
Operation::LoadList => {
|
2024-09-24 20:49:17 +00:00
|
|
|
let to_register = instruction.a();
|
|
|
|
let first_register = instruction.b();
|
2024-09-25 02:58:14 +00:00
|
|
|
let last_register = instruction.c();
|
2024-10-12 10:16:12 +00:00
|
|
|
let item_type = self.get(first_register, position)?.r#type();
|
|
|
|
let value = Value::list(first_register, last_register, item_type);
|
2024-09-17 23:35:33 +00:00
|
|
|
|
2024-10-12 08:50:30 +00:00
|
|
|
self.set(to_register, value, position)?;
|
2024-09-17 23:35:33 +00:00
|
|
|
}
|
2024-09-18 15:27:41 +00:00
|
|
|
Operation::DefineLocal => {
|
2024-09-24 20:49:17 +00:00
|
|
|
let from_register = instruction.a();
|
|
|
|
let to_local = instruction.b();
|
2024-09-12 09:08:55 +00:00
|
|
|
|
2024-09-13 06:28:18 +00:00
|
|
|
self.chunk.define_local(to_local, from_register, position)?;
|
2024-09-12 09:08:55 +00:00
|
|
|
}
|
2024-09-12 17:03:24 +00:00
|
|
|
Operation::GetLocal => {
|
2024-10-12 07:06:44 +00:00
|
|
|
let to_register = instruction.a();
|
2024-09-24 20:49:17 +00:00
|
|
|
let local_index = instruction.b();
|
2024-09-25 02:58:14 +00:00
|
|
|
let local = self.chunk.get_local(local_index, position)?;
|
2024-10-12 14:55:34 +00:00
|
|
|
let value = self.take(local.register_index, to_register, position)?;
|
2024-09-12 18:16:26 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(to_register, value, position)?;
|
2024-09-12 09:08:55 +00:00
|
|
|
}
|
2024-09-15 01:05:03 +00:00
|
|
|
Operation::SetLocal => {
|
2024-10-12 07:06:44 +00:00
|
|
|
let register = instruction.a();
|
2024-09-24 20:49:17 +00:00
|
|
|
let local_index = instruction.b();
|
2024-09-25 02:58:14 +00:00
|
|
|
let local = self.chunk.get_local(local_index, position)?;
|
2024-10-05 04:11:03 +00:00
|
|
|
|
|
|
|
if !local.is_mutable {
|
|
|
|
return Err(VmError::CannotMutateImmutableLocal {
|
2024-09-25 02:58:14 +00:00
|
|
|
identifier: local.identifier.clone(),
|
|
|
|
position,
|
|
|
|
});
|
2024-10-05 04:11:03 +00:00
|
|
|
}
|
2024-09-25 02:58:14 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.chunk.define_local(local_index, register, position)?;
|
2024-09-15 01:05:03 +00:00
|
|
|
}
|
2024-09-12 04:39:31 +00:00
|
|
|
Operation::Add => {
|
2024-09-23 23:16:15 +00:00
|
|
|
let (left, right) = get_arguments(self, instruction, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
let sum = left
|
2024-09-23 12:57:49 +00:00
|
|
|
.add(right)
|
2024-09-12 04:39:31 +00:00
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(instruction.a(), sum, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
Operation::Subtract => {
|
2024-09-23 23:16:15 +00:00
|
|
|
let (left, right) = get_arguments(self, instruction, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
let difference = left
|
2024-09-23 12:57:49 +00:00
|
|
|
.subtract(right)
|
2024-09-12 04:39:31 +00:00
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(instruction.a(), difference, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
Operation::Multiply => {
|
2024-09-23 23:16:15 +00:00
|
|
|
let (left, right) = get_arguments(self, instruction, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
let product = left
|
2024-09-23 12:57:49 +00:00
|
|
|
.multiply(right)
|
2024-09-12 04:39:31 +00:00
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(instruction.a(), product, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
Operation::Divide => {
|
2024-09-23 23:16:15 +00:00
|
|
|
let (left, right) = get_arguments(self, instruction, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
let quotient = left
|
2024-09-23 12:57:49 +00:00
|
|
|
.divide(right)
|
2024-09-12 04:39:31 +00:00
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(instruction.a(), quotient, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
2024-09-17 21:51:39 +00:00
|
|
|
Operation::Modulo => {
|
2024-09-23 23:16:15 +00:00
|
|
|
let (left, right) = get_arguments(self, instruction, position)?;
|
2024-09-17 21:51:39 +00:00
|
|
|
let remainder = left
|
2024-09-23 12:57:49 +00:00
|
|
|
.modulo(right)
|
2024-09-17 21:51:39 +00:00
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(instruction.a(), remainder, position)?;
|
2024-09-17 21:51:39 +00:00
|
|
|
}
|
2024-09-19 15:41:18 +00:00
|
|
|
Operation::Test => {
|
2024-09-24 20:49:17 +00:00
|
|
|
let register = instruction.a();
|
|
|
|
let test_value = instruction.c_as_boolean();
|
2024-09-23 23:16:15 +00:00
|
|
|
let value = self.get(register, position)?;
|
2024-10-12 08:50:30 +00:00
|
|
|
let boolean = if let Value::Primitive(Primitive::Boolean(boolean)) = value {
|
|
|
|
*boolean
|
|
|
|
} else {
|
|
|
|
return Err(VmError::ExpectedBoolean {
|
|
|
|
found: value.clone(),
|
|
|
|
position,
|
|
|
|
});
|
|
|
|
};
|
2024-09-19 15:41:18 +00:00
|
|
|
|
2024-09-24 20:49:17 +00:00
|
|
|
if boolean != test_value {
|
2024-09-19 15:41:18 +00:00
|
|
|
self.ip += 1;
|
|
|
|
}
|
2024-09-17 21:51:39 +00:00
|
|
|
}
|
2024-09-19 15:41:18 +00:00
|
|
|
Operation::TestSet => {
|
2024-09-24 20:49:17 +00:00
|
|
|
let to_register = instruction.a();
|
|
|
|
let argument = instruction.b();
|
|
|
|
let test_value = instruction.c_as_boolean();
|
2024-10-12 07:06:44 +00:00
|
|
|
let borrowed_value = self.get(argument, position)?;
|
|
|
|
let boolean =
|
2024-10-12 08:50:30 +00:00
|
|
|
if let Value::Primitive(Primitive::Boolean(boolean)) = borrowed_value {
|
|
|
|
*boolean
|
|
|
|
} else {
|
|
|
|
return Err(VmError::ExpectedBoolean {
|
2024-10-12 07:06:44 +00:00
|
|
|
found: borrowed_value.clone(),
|
|
|
|
position,
|
2024-10-12 08:50:30 +00:00
|
|
|
});
|
|
|
|
};
|
2024-09-19 15:41:18 +00:00
|
|
|
|
|
|
|
if boolean == test_value {
|
2024-10-12 07:06:44 +00:00
|
|
|
let value = self.take(argument, to_register, position)?;
|
|
|
|
|
|
|
|
self.set(to_register, value, position)?;
|
2024-09-19 15:41:18 +00:00
|
|
|
} else {
|
|
|
|
self.ip += 1;
|
|
|
|
}
|
2024-09-17 21:51:39 +00:00
|
|
|
}
|
2024-09-18 20:43:34 +00:00
|
|
|
Operation::Equal => {
|
2024-10-12 07:06:44 +00:00
|
|
|
debug_assert_eq!(
|
|
|
|
self.chunk.get_instruction(self.ip, position)?.0.operation(),
|
|
|
|
Operation::Jump
|
|
|
|
);
|
2024-09-24 14:16:19 +00:00
|
|
|
|
2024-09-23 23:16:15 +00:00
|
|
|
let (left, right) = get_arguments(self, instruction, position)?;
|
2024-10-12 08:50:30 +00:00
|
|
|
let equal_result = left
|
2024-09-23 12:57:49 +00:00
|
|
|
.equal(right)
|
2024-10-12 08:50:30 +00:00
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
let boolean =
|
|
|
|
if let Value::Primitive(Primitive::Boolean(boolean)) = equal_result {
|
|
|
|
boolean
|
|
|
|
} else {
|
|
|
|
return Err(VmError::ExpectedBoolean {
|
|
|
|
found: equal_result.clone(),
|
|
|
|
position,
|
|
|
|
});
|
|
|
|
};
|
2024-09-24 20:49:17 +00:00
|
|
|
let compare_to = instruction.a_as_boolean();
|
2024-09-18 20:43:34 +00:00
|
|
|
|
2024-09-24 14:16:19 +00:00
|
|
|
if boolean == compare_to {
|
|
|
|
self.ip += 1;
|
|
|
|
} else {
|
2024-10-12 07:06:44 +00:00
|
|
|
let (jump, _) = *self.chunk.get_instruction(self.ip, position)?;
|
2024-09-24 20:49:17 +00:00
|
|
|
let jump_distance = jump.a();
|
2024-10-06 01:30:48 +00:00
|
|
|
let is_positive = jump.b_as_boolean();
|
2024-09-24 14:16:19 +00:00
|
|
|
let new_ip = if is_positive {
|
|
|
|
self.ip + jump_distance as usize
|
|
|
|
} else {
|
|
|
|
self.ip - jump_distance as usize
|
|
|
|
};
|
|
|
|
|
|
|
|
self.ip = new_ip;
|
2024-09-18 20:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Operation::Less => {
|
2024-10-12 07:06:44 +00:00
|
|
|
debug_assert_eq!(
|
|
|
|
self.chunk.get_instruction(self.ip, position)?.0.operation(),
|
|
|
|
Operation::Jump
|
|
|
|
);
|
2024-09-24 14:16:19 +00:00
|
|
|
|
2024-09-23 23:16:15 +00:00
|
|
|
let (left, right) = get_arguments(self, instruction, position)?;
|
2024-10-12 08:50:30 +00:00
|
|
|
let less_result = left
|
2024-09-24 15:40:12 +00:00
|
|
|
.less_than(right)
|
2024-10-12 08:50:30 +00:00
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
let boolean = if let Value::Primitive(Primitive::Boolean(boolean)) = less_result
|
|
|
|
{
|
|
|
|
boolean
|
|
|
|
} else {
|
|
|
|
return Err(VmError::ExpectedBoolean {
|
|
|
|
found: less_result.clone(),
|
2024-09-24 14:16:19 +00:00
|
|
|
position,
|
2024-10-12 08:50:30 +00:00
|
|
|
});
|
|
|
|
};
|
2024-09-24 20:49:17 +00:00
|
|
|
let compare_to = instruction.a_as_boolean();
|
2024-09-18 20:43:34 +00:00
|
|
|
|
2024-09-24 14:16:19 +00:00
|
|
|
if boolean == compare_to {
|
|
|
|
self.ip += 1;
|
|
|
|
} else {
|
2024-10-12 07:06:44 +00:00
|
|
|
let jump = self.chunk.get_instruction(self.ip, position)?.0;
|
2024-09-24 20:49:17 +00:00
|
|
|
let jump_distance = jump.a();
|
2024-10-06 01:30:48 +00:00
|
|
|
let is_positive = jump.b_as_boolean();
|
2024-09-24 14:16:19 +00:00
|
|
|
let new_ip = if is_positive {
|
|
|
|
self.ip + jump_distance as usize
|
|
|
|
} else {
|
|
|
|
self.ip - jump_distance as usize
|
|
|
|
};
|
|
|
|
|
|
|
|
self.ip = new_ip;
|
2024-09-18 20:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Operation::LessEqual => {
|
2024-10-12 07:06:44 +00:00
|
|
|
debug_assert_eq!(
|
|
|
|
self.chunk.get_instruction(self.ip, position)?.0.operation(),
|
|
|
|
Operation::Jump
|
|
|
|
);
|
2024-09-24 15:40:12 +00:00
|
|
|
|
2024-09-23 23:16:15 +00:00
|
|
|
let (left, right) = get_arguments(self, instruction, position)?;
|
2024-10-12 08:50:30 +00:00
|
|
|
let less_or_equal_result = left
|
2024-09-24 15:40:12 +00:00
|
|
|
.less_than_or_equal(right)
|
2024-10-12 08:50:30 +00:00
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
let boolean = if let Value::Primitive(Primitive::Boolean(boolean)) =
|
|
|
|
less_or_equal_result
|
|
|
|
{
|
|
|
|
boolean
|
|
|
|
} else {
|
|
|
|
return Err(VmError::ExpectedBoolean {
|
|
|
|
found: less_or_equal_result.clone(),
|
2024-09-24 14:16:19 +00:00
|
|
|
position,
|
2024-10-12 08:50:30 +00:00
|
|
|
});
|
|
|
|
};
|
2024-09-24 20:49:17 +00:00
|
|
|
let compare_to = instruction.a_as_boolean();
|
2024-09-18 20:43:34 +00:00
|
|
|
|
2024-09-24 14:16:19 +00:00
|
|
|
if boolean == compare_to {
|
|
|
|
self.ip += 1;
|
|
|
|
} else {
|
2024-10-12 07:06:44 +00:00
|
|
|
let jump = self.chunk.get_instruction(self.ip, position)?.0;
|
2024-09-24 20:49:17 +00:00
|
|
|
let jump_distance = jump.a();
|
2024-10-06 01:30:48 +00:00
|
|
|
let is_positive = jump.b_as_boolean();
|
2024-09-24 14:16:19 +00:00
|
|
|
let new_ip = if is_positive {
|
|
|
|
self.ip + jump_distance as usize
|
|
|
|
} else {
|
|
|
|
self.ip - jump_distance as usize
|
|
|
|
};
|
|
|
|
|
|
|
|
self.ip = new_ip;
|
2024-09-18 20:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-12 04:39:31 +00:00
|
|
|
Operation::Negate => {
|
2024-09-24 20:49:17 +00:00
|
|
|
let value = if instruction.b_is_constant() {
|
|
|
|
self.chunk.get_constant(instruction.b(), position)?
|
2024-09-18 01:10:44 +00:00
|
|
|
} else {
|
2024-09-24 20:49:17 +00:00
|
|
|
self.get(instruction.b(), position)?
|
2024-09-18 01:10:44 +00:00
|
|
|
};
|
2024-09-12 04:39:31 +00:00
|
|
|
let negated = value
|
|
|
|
.negate()
|
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(instruction.a(), negated, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
2024-09-17 21:51:39 +00:00
|
|
|
Operation::Not => {
|
2024-09-24 20:49:17 +00:00
|
|
|
let value = if instruction.b_is_constant() {
|
|
|
|
self.chunk.get_constant(instruction.b(), position)?
|
2024-09-18 01:10:44 +00:00
|
|
|
} else {
|
2024-09-24 20:49:17 +00:00
|
|
|
self.get(instruction.b(), position)?
|
2024-09-18 01:10:44 +00:00
|
|
|
};
|
2024-09-18 11:58:31 +00:00
|
|
|
let not = value
|
2024-09-17 21:51:39 +00:00
|
|
|
.not()
|
|
|
|
.map_err(|error| VmError::Value { error, position })?;
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.set(instruction.a(), not, position)?;
|
2024-09-17 21:51:39 +00:00
|
|
|
}
|
2024-09-18 20:43:34 +00:00
|
|
|
Operation::Jump => {
|
2024-09-24 20:49:17 +00:00
|
|
|
let offset = instruction.b();
|
|
|
|
let is_positive = instruction.c_as_boolean();
|
2024-09-18 20:43:34 +00:00
|
|
|
let new_ip = if is_positive {
|
|
|
|
self.ip + offset as usize
|
|
|
|
} else {
|
2024-10-05 06:07:26 +00:00
|
|
|
self.ip - (offset + 1) as usize
|
2024-09-18 20:43:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.ip = new_ip;
|
|
|
|
}
|
2024-10-12 14:55:34 +00:00
|
|
|
Operation::Call => {
|
|
|
|
let function_index = instruction.a();
|
|
|
|
let argument_count = instruction.b();
|
|
|
|
let function = if let Value::Primitive(Primitive::Function(function)) =
|
|
|
|
self.get(function_index, position)?.clone()
|
|
|
|
{
|
|
|
|
function
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
};
|
2024-10-13 00:19:21 +00:00
|
|
|
let mut function_vm = Vm::new(function.take_chunk());
|
2024-10-12 14:55:34 +00:00
|
|
|
let first_argument_index = function_index + 1;
|
|
|
|
let last_argument_index = first_argument_index + argument_count;
|
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
for argument_index in first_argument_index..last_argument_index {
|
2024-10-12 14:55:34 +00:00
|
|
|
let argument = self.empty(argument_index, position)?;
|
|
|
|
|
|
|
|
function_vm.stack.push(Register::Value(argument));
|
|
|
|
}
|
|
|
|
|
|
|
|
let return_value = function_vm.run()?;
|
|
|
|
|
|
|
|
if let Some(value) = return_value {
|
|
|
|
self.set(function_index, value, position)?;
|
2024-10-13 00:19:21 +00:00
|
|
|
} else {
|
|
|
|
self.empty(function_index, position)?;
|
2024-10-12 14:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-12 04:39:31 +00:00
|
|
|
Operation::Return => {
|
2024-10-12 07:06:44 +00:00
|
|
|
let should_return_value = instruction.b_as_boolean();
|
|
|
|
|
|
|
|
return if should_return_value {
|
2024-10-13 00:19:21 +00:00
|
|
|
let top_of_stack = (self.stack.len() - 1) as u8;
|
2024-10-12 14:55:34 +00:00
|
|
|
let value = self.empty(top_of_stack, position)?;
|
2024-10-12 07:06:44 +00:00
|
|
|
|
2024-10-05 02:33:47 +00:00
|
|
|
Ok(Some(value))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
};
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(None)
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
fn set(&mut self, index: u8, value: Value, position: Span) -> Result<(), VmError> {
|
|
|
|
let length = self.stack.len();
|
|
|
|
let index = index as usize;
|
2024-09-15 01:05:03 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
if length == Self::STACK_LIMIT {
|
|
|
|
return Err(VmError::StackOverflow { position });
|
|
|
|
}
|
2024-09-12 04:39:31 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
if index == length {
|
|
|
|
log::trace!("Push register {index} with value {value}");
|
2024-09-12 13:11:49 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.stack.push(Register::Value(value));
|
|
|
|
|
|
|
|
return Ok(());
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
if index < length {
|
|
|
|
log::trace!("Replace register {index} with value {value}");
|
2024-09-19 15:41:18 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
self.stack[index] = Register::Value(value);
|
2024-09-19 15:41:18 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
return Ok(());
|
2024-09-19 15:41:18 +00:00
|
|
|
}
|
2024-10-12 07:06:44 +00:00
|
|
|
|
|
|
|
Err(VmError::SkippedRegister {
|
|
|
|
index,
|
|
|
|
length: self.stack.len(),
|
|
|
|
position,
|
|
|
|
})
|
2024-09-19 15:41:18 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
fn set_constant(
|
|
|
|
&mut self,
|
|
|
|
index: u8,
|
|
|
|
constant_index: u8,
|
|
|
|
position: Span,
|
|
|
|
) -> Result<(), VmError> {
|
|
|
|
let length = self.stack.len();
|
2024-09-23 12:57:49 +00:00
|
|
|
let index = index as usize;
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
if length == Self::STACK_LIMIT {
|
|
|
|
return Err(VmError::StackOverflow { position });
|
|
|
|
}
|
|
|
|
|
|
|
|
if index == length {
|
|
|
|
log::trace!("Push register {index} with constant {constant_index}");
|
|
|
|
|
|
|
|
self.stack.push(Register::Constant(constant_index));
|
2024-09-23 12:57:49 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
return Ok(());
|
2024-09-23 12:57:49 +00:00
|
|
|
}
|
2024-10-12 07:06:44 +00:00
|
|
|
|
|
|
|
if index < length {
|
|
|
|
log::trace!("Replace register {index} with constant {constant_index}");
|
|
|
|
|
|
|
|
self.stack[index] = Register::Constant(constant_index);
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(VmError::SkippedRegister {
|
|
|
|
index,
|
|
|
|
length: self.stack.len(),
|
|
|
|
position,
|
|
|
|
})
|
2024-09-23 12:57:49 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 08:50:30 +00:00
|
|
|
pub fn get(&self, index: u8, position: Span) -> Result<&Value, VmError> {
|
2024-09-15 01:05:03 +00:00
|
|
|
let index = index as usize;
|
2024-10-12 07:06:44 +00:00
|
|
|
let register = self
|
|
|
|
.stack
|
|
|
|
.get(index)
|
|
|
|
.ok_or_else(|| VmError::RegisterIndexOutOfBounds { position })?;
|
2024-09-15 01:05:03 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
match register {
|
|
|
|
Register::Value(value) => Ok(value),
|
|
|
|
Register::Pointer(register_index) => {
|
|
|
|
let value = self.get(*register_index, position)?;
|
2024-09-14 19:06:13 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
Register::Constant(constant_index) => {
|
|
|
|
let value = self.chunk.get_constant(*constant_index, position)?;
|
2024-09-14 19:06:13 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
Register::Empty => Err(VmError::EmptyRegister { index, position }),
|
2024-09-13 01:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
fn take(&mut self, index: u8, point_to: u8, position: Span) -> Result<Value, VmError> {
|
2024-09-18 15:27:41 +00:00
|
|
|
let index = index as usize;
|
2024-10-12 07:06:44 +00:00
|
|
|
let register = replace(&mut self.stack[index], Register::Pointer(point_to));
|
2024-09-18 15:27:41 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
match register {
|
|
|
|
Register::Value(value) => Ok(value),
|
|
|
|
Register::Pointer(register_index) => {
|
|
|
|
let value = self.take(register_index, point_to, position)?;
|
2024-09-18 15:27:41 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
Register::Constant(constant_index) => {
|
|
|
|
let value = self.chunk.take_constant(constant_index, position)?;
|
2024-09-18 15:27:41 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
Register::Empty => Err(VmError::EmptyRegister { index, position }),
|
2024-09-18 15:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 14:55:34 +00:00
|
|
|
fn empty(&mut self, index: u8, position: Span) -> Result<Value, VmError> {
|
|
|
|
let index = index as usize;
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
if index >= self.stack.len() {
|
|
|
|
return Err(VmError::RegisterIndexOutOfBounds { position });
|
2024-09-18 11:58:31 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
let register = replace(&mut self.stack[index], Register::Empty);
|
|
|
|
|
|
|
|
match register {
|
|
|
|
Register::Value(value) => Ok(value),
|
|
|
|
Register::Pointer(register_index) => {
|
2024-10-12 14:55:34 +00:00
|
|
|
let value = self.empty(register_index, position)?;
|
2024-10-12 07:06:44 +00:00
|
|
|
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
Register::Constant(constant_index) => {
|
|
|
|
let value = self.chunk.take_constant(constant_index, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
Register::Empty => Err(VmError::EmptyRegister { index, position }),
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read(&mut self, position: Span) -> Result<&(Instruction, Span), VmError> {
|
2024-09-12 13:11:49 +00:00
|
|
|
let current = self.chunk.get_instruction(self.ip, position)?;
|
2024-09-12 04:39:31 +00:00
|
|
|
|
|
|
|
self.ip += 1;
|
|
|
|
|
|
|
|
Ok(current)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
enum Register {
|
|
|
|
Empty,
|
|
|
|
Value(Value),
|
|
|
|
Pointer(u8),
|
|
|
|
Constant(u8),
|
|
|
|
}
|
|
|
|
|
2024-09-12 04:39:31 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum VmError {
|
2024-10-05 04:11:03 +00:00
|
|
|
CannotMutateImmutableLocal {
|
|
|
|
identifier: Identifier,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-12 04:39:31 +00:00
|
|
|
EmptyRegister {
|
|
|
|
index: usize,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-19 15:41:18 +00:00
|
|
|
ExpectedBoolean {
|
|
|
|
found: Value,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-12 04:39:31 +00:00
|
|
|
RegisterIndexOutOfBounds {
|
|
|
|
position: Span,
|
|
|
|
},
|
|
|
|
InvalidInstruction {
|
|
|
|
instruction: Instruction,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-10-12 07:06:44 +00:00
|
|
|
SkippedRegister {
|
|
|
|
index: usize,
|
|
|
|
length: usize,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-12 04:39:31 +00:00
|
|
|
StackOverflow {
|
|
|
|
position: Span,
|
|
|
|
},
|
|
|
|
StackUnderflow {
|
|
|
|
position: Span,
|
|
|
|
},
|
|
|
|
UndefinedVariable {
|
|
|
|
identifier: Identifier,
|
|
|
|
position: Span,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Wrappers for foreign errors
|
|
|
|
Chunk(ChunkError),
|
|
|
|
Value {
|
|
|
|
error: ValueError,
|
|
|
|
position: Span,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ChunkError> for VmError {
|
2024-09-18 15:27:41 +00:00
|
|
|
fn from(error: ChunkError) -> Self {
|
|
|
|
Self::Chunk(error)
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AnnotatedError for VmError {
|
|
|
|
fn title() -> &'static str {
|
|
|
|
"Runtime Error"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
match self {
|
2024-10-05 04:11:03 +00:00
|
|
|
Self::CannotMutateImmutableLocal { .. } => "Cannot mutate immutable variable",
|
2024-09-12 04:39:31 +00:00
|
|
|
Self::EmptyRegister { .. } => "Empty register",
|
2024-09-19 15:41:18 +00:00
|
|
|
Self::ExpectedBoolean { .. } => "Expected boolean",
|
2024-09-12 04:39:31 +00:00
|
|
|
Self::RegisterIndexOutOfBounds { .. } => "Register index out of bounds",
|
|
|
|
Self::InvalidInstruction { .. } => "Invalid instruction",
|
2024-10-12 07:06:44 +00:00
|
|
|
Self::SkippedRegister { .. } => "Skipped register",
|
2024-09-12 04:39:31 +00:00
|
|
|
Self::StackOverflow { .. } => "Stack overflow",
|
|
|
|
Self::StackUnderflow { .. } => "Stack underflow",
|
|
|
|
Self::UndefinedVariable { .. } => "Undefined variable",
|
2024-09-18 11:58:31 +00:00
|
|
|
Self::Chunk(error) => error.description(),
|
2024-10-12 07:06:44 +00:00
|
|
|
Self::Value { .. } => "Value error",
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn details(&self) -> Option<String> {
|
|
|
|
match self {
|
|
|
|
Self::EmptyRegister { index, .. } => Some(format!("Register {index} is empty")),
|
|
|
|
Self::UndefinedVariable { identifier, .. } => {
|
|
|
|
Some(format!("{identifier} is not in scope"))
|
|
|
|
}
|
|
|
|
Self::Chunk(error) => error.details(),
|
|
|
|
Self::Value { error, .. } => Some(error.to_string()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn position(&self) -> Span {
|
|
|
|
match self {
|
2024-10-05 04:11:03 +00:00
|
|
|
Self::CannotMutateImmutableLocal { position, .. } => *position,
|
2024-09-12 04:39:31 +00:00
|
|
|
Self::EmptyRegister { position, .. } => *position,
|
2024-09-19 15:41:18 +00:00
|
|
|
Self::ExpectedBoolean { position, .. } => *position,
|
2024-09-12 04:39:31 +00:00
|
|
|
Self::RegisterIndexOutOfBounds { position } => *position,
|
|
|
|
Self::InvalidInstruction { position, .. } => *position,
|
2024-10-12 07:06:44 +00:00
|
|
|
Self::SkippedRegister { position, .. } => *position,
|
2024-09-12 04:39:31 +00:00
|
|
|
Self::StackUnderflow { position } => *position,
|
|
|
|
Self::StackOverflow { position } => *position,
|
|
|
|
Self::UndefinedVariable { position, .. } => *position,
|
|
|
|
Self::Chunk(error) => error.position(),
|
|
|
|
Self::Value { position, .. } => *position,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|