Skip to content

Commit

Permalink
rename OpCode to Instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
oysandvik94 committed Aug 31, 2024
1 parent 8e1219b commit 1a2b80c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
18 changes: 11 additions & 7 deletions crates/interpreter/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bytecode::OpCode;
use bytecode::Instruction;

use crate::{
eval::objects::PrimitiveObject,
Expand All @@ -13,12 +13,12 @@ pub mod internal_error;

#[derive(Default)]
pub struct Compiler {
pub instructions: Vec<OpCode>,
pub instructions: Vec<Instruction>,
pub constants: Vec<PrimitiveObject>,
}

pub struct ByteCode {
pub instructions: Vec<OpCode>,
pub instructions: Vec<Instruction>,
pub constants: Vec<PrimitiveObject>,
}

Expand All @@ -45,7 +45,7 @@ impl Compiler {
let integer_object = PrimitiveObject::Integer(*integer);
let constant_index = self.add_constant(integer_object);

self.add_instruction(OpCode::OpConstant(constant_index as u16));
self.add_instruction(Instruction::OpConstant(constant_index as u16));
}
Expression::BooleanLiteral(_) => todo!(),
Expression::Array(_) => todo!(),
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Compiler {
match operator {
Operator::Bang => todo!(),
Operator::Minus => todo!(),
Operator::Plus => self.add_instruction(OpCode::Add),
Operator::Plus => self.add_instruction(Instruction::Add),
Operator::Multiply => todo!(),
Operator::Equals => todo!(),
Operator::NotEquals => todo!(),
Expand All @@ -96,7 +96,7 @@ impl Compiler {
}
}

fn add_instruction(&mut self, instruction: OpCode) -> usize {
fn add_instruction(&mut self, instruction: Instruction) -> usize {
let new_instruction_position = self.instructions.len();
self.instructions.push(instruction);
new_instruction_position
Expand All @@ -115,7 +115,11 @@ mod tests {
let test_cases: Vec<CompilerTestCase> = vec![CompilerTestCase {
input: String::from("1 + 2"),
expected_constants: vec![PrimitiveObject::Integer(1), PrimitiveObject::Integer(2)],
expected_instructions: vec![OpCode::OpConstant(0), OpCode::OpConstant(1), OpCode::Add],
expected_instructions: vec![
Instruction::OpConstant(0),
Instruction::OpConstant(1),
Instruction::Add,
],
}];

test_util::run_compiler_tests(test_cases);
Expand Down
10 changes: 5 additions & 5 deletions crates/interpreter/src/compiler/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use std::fmt::Display;

#[repr(u8)]
#[derive(Clone, Debug, PartialEq)]
pub enum OpCode {
pub enum Instruction {
OpConstant(u16),
Add,
}

impl Display for OpCode {
impl Display for Instruction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OpCode::OpConstant(num) => write!(f, "OpConstant({num})"),
OpCode::Add => write!(f, "Add"),
Instruction::OpConstant(num) => write!(f, "OpConstant({num})"),
Instruction::Add => write!(f, "Add"),
}
}
}
Expand All @@ -22,6 +22,6 @@ mod tests {

#[test]
fn assert_operation_size() {
assert_eq!(size_of::<OpCode>(), 4);
assert_eq!(size_of::<Instruction>(), 4);
}
}
6 changes: 3 additions & 3 deletions crates/interpreter/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tracing::{event, Level};
use tracing_subscriber::FmtSubscriber;

use crate::{
compiler::{bytecode::OpCode, Compiler},
compiler::{bytecode::Instruction, Compiler},
eval::{
self,
objects::{Environment, Object, PrimitiveObject},
Expand Down Expand Up @@ -190,7 +190,7 @@ pub fn setup_logger() {
pub struct CompilerTestCase {
pub input: String,
pub expected_constants: Vec<PrimitiveObject>,
pub expected_instructions: Vec<OpCode>,
pub expected_instructions: Vec<Instruction>,
}

pub fn run_compiler_tests(test_cases: Vec<CompilerTestCase>) {
Expand All @@ -208,7 +208,7 @@ pub fn run_compiler_tests(test_cases: Vec<CompilerTestCase>) {
}
}

fn test_instructions(expected_instructions: Vec<OpCode>, instructions: Vec<OpCode>) {
fn test_instructions(expected_instructions: Vec<Instruction>, instructions: Vec<Instruction>) {
assert_eq!(
expected_instructions.len(),
instructions.len(),
Expand Down
10 changes: 5 additions & 5 deletions crates/interpreter/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod runtime_error;

use crate::{
compiler::{bytecode::OpCode, internal_error::InternalError, ByteCode},
compiler::{bytecode::Instruction, internal_error::InternalError, ByteCode},
eval::objects::{Object, PrimitiveObject},
};

Expand All @@ -12,7 +12,7 @@ const STACK_SIZE: usize = 2048;

pub struct VirtualMachine {
constants: Vec<PrimitiveObject>,
instructions: Vec<OpCode>,
instructions: Vec<Instruction>,
stack: Vec<Object>,
stack_pointer: usize,
}
Expand All @@ -30,15 +30,15 @@ impl VirtualMachine {
pub fn run(&mut self) -> Result<Object> {
let mut instruction_pointer = 0;
while instruction_pointer < self.instructions.len() {
let operation: &OpCode = &self.instructions[instruction_pointer];
let operation: &Instruction = &self.instructions[instruction_pointer];
instruction_pointer += 1;

match operation {
OpCode::OpConstant(constant_index) => {
Instruction::OpConstant(constant_index) => {
let constant = self.constants[*constant_index as usize].clone();
self.push(Object::Primitive(constant))?;
}
OpCode::Add => {
Instruction::Add => {
let right = self.pop()?;
let left = self.pop()?;

Expand Down

0 comments on commit 1a2b80c

Please sign in to comment.