main
Alpha Chen 2 years ago
parent 5348c99b00
commit 3138932c13
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -1,7 +1,10 @@
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use thiserror::Error; use thiserror::Error;
use crate::chunk::{Chunk, DisassembledInstruction, OpCode}; use crate::{
chunk::{Chunk, DisassembledInstruction, OpCode},
value::Value,
};
use tracing::debug; use tracing::debug;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -15,11 +18,18 @@ pub enum InterpretError {
pub struct VM { pub struct VM {
chunk: Chunk, chunk: Chunk,
ip: usize, ip: usize,
stack: [Value; 256],
stack_top: usize,
} }
impl VM { impl VM {
pub fn new(chunk: Chunk) -> Self { 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<()> { pub fn interpret(&mut self) -> Result<()> {
@ -37,4 +47,14 @@ impl VM {
self.ip += 1; 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]
}
} }

Loading…
Cancel
Save