aboutsummaryrefslogtreecommitdiff
path: root/crates/sloth_vm/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/sloth_vm/src')
-rw-r--r--crates/sloth_vm/src/lib.rs5
-rw-r--r--crates/sloth_vm/src/value.rs10
-rw-r--r--crates/sloth_vm/src/vm.rs17
3 files changed, 22 insertions, 10 deletions
diff --git a/crates/sloth_vm/src/lib.rs b/crates/sloth_vm/src/lib.rs
index 295827d..9cf552b 100644
--- a/crates/sloth_vm/src/lib.rs
+++ b/crates/sloth_vm/src/lib.rs
@@ -19,9 +19,10 @@ use value::{Object, ObjectType};
use crate::value::Primitive;
pub use crate::vm::VM;
+#[derive(Default)]
pub struct Chunk {
- constants: Vec<Primitive>,
- code: Vec<u8>,
+ pub constants: Vec<Primitive>,
+ pub code: Vec<u8>,
}
const STACK_SIZE: usize = 1024;
diff --git a/crates/sloth_vm/src/value.rs b/crates/sloth_vm/src/value.rs
index f149c0e..4450b5a 100644
--- a/crates/sloth_vm/src/value.rs
+++ b/crates/sloth_vm/src/value.rs
@@ -35,14 +35,14 @@ pub enum ObjectType {
}
pub struct Function {
- pub(crate) name: Option<String>,
- pub(crate) chunk: Chunk,
- pub(crate) arity: u8,
- pub(crate) returns_value: bool,
+ pub name: Option<String>,
+ pub chunk: Chunk,
+ pub arity: u8,
+ pub returns_value: bool,
}
impl Function {
- pub(crate) fn root(chunk: Chunk) -> Self {
+ pub fn root(chunk: Chunk) -> Self {
Self {
name: None,
chunk,
diff --git a/crates/sloth_vm/src/vm.rs b/crates/sloth_vm/src/vm.rs
index 2d58c3d..3600719 100644
--- a/crates/sloth_vm/src/vm.rs
+++ b/crates/sloth_vm/src/vm.rs
@@ -3,7 +3,7 @@ use std::mem::MaybeUninit;
use sloth_bytecode::Opcode;
use crate::value::{Function, Object, ObjectType, Primitive};
-use crate::{native, ObjectMap, Stack};
+use crate::{native, vm, ObjectMap, Stack};
#[derive(Clone, Copy)]
pub struct CallFrame {
@@ -62,10 +62,11 @@ impl CallStack {
}
}
+// TODO: Fix visibility
pub struct VM {
- stack: Stack,
+ pub stack: Stack,
call_stack: CallStack,
- objects: ObjectMap,
+ pub objects: ObjectMap,
}
impl Default for VM {
@@ -128,6 +129,16 @@ impl VM {
self.stack[self.call_stack.peek().stack_offset + idx] = value;
}
+ Opcode::Box => {
+ // FIXME: TODO: MEGA CURSED
+ let pos = self.read_u16() as usize;
+ let value = self.stack.pop();
+
+ let object = vm::Object::new(ObjectType::Box(value));
+
+ self.objects.heap[pos] = object;
+ self.stack.push(Object(pos as u32));
+ }
Opcode::Add => {
let value = match self.stack.pop2() {