-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b047618
commit 8284942
Showing
5 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ pub mod object; | |
|
||
mod bytecode; | ||
mod compiler; | ||
mod vm; | ||
|
||
#[cfg(test)] | ||
mod test; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ pub enum Object { | |
Float(f64), | ||
Boolean(bool), | ||
String(Rc<String>), | ||
Null, | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod compiler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::{bytecode::Bytecode, object::Object}; | ||
|
||
const STACK_SIZE: usize = 4096; | ||
|
||
#[derive(Debug)] | ||
pub struct VirtualMachine { | ||
stack: Vec<Object>, | ||
// StackPointer which points to the next value. | ||
// Top of the stack is stack[sp-1] | ||
sp: usize, | ||
} | ||
|
||
impl VirtualMachine { | ||
pub fn new() -> Self { | ||
Self { | ||
stack: vec![Object::Null; STACK_SIZE], | ||
sp: 0, | ||
} | ||
} | ||
|
||
fn push(&mut self, obj: Object) { | ||
if self.sp >= self.stack.len() { | ||
panic!("stack overflow"); | ||
} | ||
|
||
self.stack[self.sp] = obj; | ||
self.sp += 1; | ||
} | ||
|
||
fn pop(&mut self) -> Object { | ||
self.sp -= 1; | ||
self.stack[self.sp].clone() | ||
} | ||
|
||
pub fn run(&mut self, bytecode: &Bytecode) { | ||
for inst in &bytecode.instructions { | ||
match inst { | ||
crate::bytecode::Instruction::Constant(idx) => { | ||
self.push(bytecode.constants[*idx].clone()) | ||
} | ||
crate::bytecode::Instruction::Pop => { | ||
self.pop(); | ||
} | ||
} | ||
} | ||
} | ||
} |