From 3138932c13613f98ca8e903d4313ba99df8b6e5a Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sun, 16 Oct 2022 20:53:49 -0700 Subject: [PATCH] 15.2.1 --- rust/src/vm.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/rust/src/vm.rs b/rust/src/vm.rs index cc2eb68..db7c172 100644 --- a/rust/src/vm.rs +++ b/rust/src/vm.rs @@ -1,7 +1,10 @@ use color_eyre::eyre::Result; use thiserror::Error; -use crate::chunk::{Chunk, DisassembledInstruction, OpCode}; +use crate::{ + chunk::{Chunk, DisassembledInstruction, OpCode}, + value::Value, +}; use tracing::debug; #[derive(Error, Debug)] @@ -15,11 +18,18 @@ pub enum InterpretError { pub struct VM { chunk: Chunk, ip: usize, + stack: [Value; 256], + stack_top: usize, } impl VM { pub fn new(chunk: Chunk) -> Self { - Self { chunk, ip: 0 } + Self { + chunk, + ip: 0, + stack: [0.0; 256], + stack_top: 0, + } } pub fn interpret(&mut self) -> Result<()> { @@ -37,4 +47,14 @@ impl VM { self.ip += 1; } } + + fn push(&mut self, value: Value) { + self.stack[self.stack_top] = value; + self.stack_top += 1; + } + + fn pop(&mut self) -> Value { + self.stack_top -= 1; + self.stack[self.stack_top] + } }