-
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.
Plus plenty of module changes
- Loading branch information
Showing
9 changed files
with
165 additions
and
97 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
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,76 @@ | ||
#[cfg(feature = "disassemble")] | ||
use crate::vm::block::disassembler::Disassembler; | ||
use crate::vm::block::opcodes::OpCode; | ||
use crate::vm::block::{Block, Constants}; | ||
|
||
impl Block { | ||
pub fn new(name: &str) -> Self { | ||
Block { | ||
name: String::from(name), | ||
constants: Constants::new(), | ||
instructions: Vec::new(), | ||
lines: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn write_op_code(&mut self, op_code: OpCode, line: usize) { | ||
self.lines.push(line); | ||
self.instructions.push(op_code as u8) | ||
} | ||
|
||
pub fn write_constant(&mut self, value: f64, line: usize) { | ||
let constant_index = self.constants.write_value(value); | ||
|
||
if constant_index <= 0xFF { | ||
self.write_op_code(OpCode::Constant, line); | ||
self.write_u8(constant_index as u8) | ||
} else if constant_index <= 0xFFFF { | ||
self.write_op_code(OpCode::Constant2, line); | ||
self.write_u16(constant_index as u16) | ||
} else { | ||
self.write_op_code(OpCode::Constant4, line); | ||
self.write_u32(constant_index) | ||
} | ||
} | ||
|
||
pub(super) fn write_u8(&mut self, value: u8) { | ||
self.instructions.push(value) | ||
} | ||
pub(super) fn write_u16(&mut self, value: u16) { | ||
self.instructions.push((value) as u8); | ||
self.instructions.push((value >> 8) as u8); | ||
} | ||
pub(super) fn write_u32(&mut self, value: u32) { | ||
self.instructions.push((value) as u8); | ||
self.instructions.push((value >> 8) as u8); | ||
self.instructions.push((value >> 16) as u8); | ||
self.instructions.push((value >> 24) as u8); | ||
} | ||
|
||
pub fn read_constant(&mut self, index: usize) -> f64 { | ||
self.constants.read_value(index) | ||
} | ||
|
||
pub fn read_u8(&self, offset: usize) -> u8 { | ||
self.instructions[offset] | ||
} | ||
|
||
pub fn read_u16(&self, offset: usize) -> u16 { | ||
let byte1 = self.instructions[offset] as u16; | ||
let byte2 = self.instructions[offset + 1] as u16; | ||
(byte2 << 8) | byte1 | ||
} | ||
|
||
pub fn read_u32(&self, offset: usize) -> u32 { | ||
let byte1 = self.instructions[offset] as u32; | ||
let byte2 = self.instructions[offset + 1] as u32; | ||
let byte3 = self.instructions[offset + 2] as u32; | ||
let byte4 = self.instructions[offset + 3] as u32; | ||
(byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1 | ||
} | ||
|
||
#[cfg(feature = "disassemble")] | ||
pub fn disassemble(&self) { | ||
self.disassemble_block(); | ||
} | ||
} |
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
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
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
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,10 @@ | ||
use enum_primitive_derive::Primitive; | ||
|
||
#[repr(u8)] | ||
#[derive(Debug, PartialEq, Primitive)] | ||
pub enum OpCode { | ||
Return = 0x00, | ||
Constant = 0x01, | ||
Constant2 = 0x02, | ||
Constant4 = 0x03, | ||
} |
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub(super) mod block; | ||
pub mod block; | ||
pub mod vm; |
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,50 @@ | ||
use crate::vm::block::{Block, OpCode}; | ||
use num_traits::FromPrimitive; | ||
|
||
pub struct VirtualMachine { | ||
block: Block, | ||
ip: usize, | ||
} | ||
|
||
pub enum Result { | ||
Ok, | ||
// CompileError, | ||
// RuntimeError, | ||
} | ||
|
||
impl VirtualMachine { | ||
pub fn new(block: Block) -> Self { | ||
VirtualMachine { block, ip: 0 } | ||
} | ||
pub fn interpret(&mut self) -> Result { | ||
return self.run(); | ||
} | ||
|
||
#[inline(always)] | ||
fn run(&mut self) -> Result { | ||
loop { | ||
match OpCode::from_u8(self.block.read_u8(self.ip)).unwrap() { | ||
OpCode::Return => return Result::Ok, | ||
OpCode::Constant => { | ||
let constant_index = self.block.read_u8(self.ip + 1) as usize; | ||
let constant = self.block.read_constant(constant_index); | ||
println!("{}", constant); | ||
self.ip += 1; | ||
} | ||
OpCode::Constant2 => { | ||
let constant_index = self.block.read_u16(self.ip + 1) as usize; | ||
let constant = self.block.read_constant(constant_index); | ||
println!("{}", constant); | ||
self.ip += 2; | ||
} | ||
OpCode::Constant4 => { | ||
let constant_index = self.block.read_u32(self.ip + 1) as usize; | ||
let constant = self.block.read_constant(constant_index); | ||
println!("{}", constant); | ||
self.ip += 4; | ||
} | ||
} | ||
self.ip += 1; | ||
} | ||
} | ||
} |