From 209328c5fa7d57805cb362e3a792232cb6e39ad0 Mon Sep 17 00:00:00 2001 From: Cody Date: Wed, 12 Apr 2023 15:21:11 -0500 Subject: Hahaha --- crates/sloth_vm/src/lib.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'crates/sloth_vm/src/lib.rs') diff --git a/crates/sloth_vm/src/lib.rs b/crates/sloth_vm/src/lib.rs index be134d8..121ea6a 100644 --- a/crates/sloth_vm/src/lib.rs +++ b/crates/sloth_vm/src/lib.rs @@ -11,6 +11,8 @@ pub mod native; pub mod value; pub mod vm; +use std::ops::{Index, IndexMut}; + use value::{Object, ObjectType}; use crate::value::Primitive; @@ -25,8 +27,8 @@ const STACK_SIZE: usize = 1024; #[derive(Debug)] pub struct Stack { - top: usize, stack: [Primitive; STACK_SIZE], + top: usize, } impl Default for Stack { @@ -68,6 +70,25 @@ impl Stack { pub fn peek(&self) -> Primitive { self.stack[self.top - 1] } + + #[inline(always)] + pub fn peek_nth(&self, nth: usize) -> Primitive { + self.stack[self.top - 1 - nth] + } +} + +impl Index for Stack { + type Output = Primitive; + + fn index(&self, index: usize) -> &Self::Output { + &self.stack[index] + } +} + +impl IndexMut for Stack { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + &mut self.stack[index] + } } pub struct ObjectMap { -- cgit v1.2.3