Created
June 26, 2025 09:47
-
-
Save dmateos/abc5da4d982c7005d15683789570e96f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use std::collections::HashMap; | |
#[derive(Debug)] | |
pub enum Instruction { | |
ADD, | |
MULT, | |
PRINT, | |
END, | |
PUSH(i64), | |
LOAD(String), | |
STORE(String, i64), | |
} | |
pub struct VirtualMachine { | |
stack: Vec<i64>, | |
instructions: Vec<Instruction>, | |
symbol_table: HashMap<String, i64>, | |
instruction_pointer: usize, | |
} | |
impl VirtualMachine { | |
pub fn new() -> VirtualMachine { | |
VirtualMachine { | |
stack: Vec::new(), | |
instructions: Vec::new(), | |
symbol_table: HashMap::new(), | |
instruction_pointer: 0, | |
} | |
} | |
fn advance(&mut self) { | |
self.instruction_pointer += 1; | |
} | |
pub fn run(&mut self) { | |
loop { | |
match &self.instructions[self.instruction_pointer] { | |
Instruction::ADD => { | |
let var1 = self.stack.pop().unwrap(); | |
let var2 = self.stack.pop().unwrap(); | |
//println!("adding {} and {}", var1, var2); | |
self.stack.push(var1 + var2); | |
} | |
Instruction::MULT => { | |
let var1 = self.stack.pop().unwrap(); | |
let var2 = self.stack.pop().unwrap(); | |
//println!("multing {} and {}", var1, var2); | |
self.stack.push(var1 * var2); | |
} | |
Instruction::PRINT => { | |
let var = self.stack.pop().unwrap_or(0); | |
println!("{}", var); | |
} | |
Instruction::END => { | |
//println!("stopping "); | |
break; | |
} | |
Instruction::PUSH(n) => { | |
//println!("pushing {}", n); | |
self.stack.push(*n); | |
} | |
Instruction::LOAD(s) => { | |
self.stack.push(*self.symbol_table.get(s).unwrap()); | |
} | |
Instruction::STORE(s, v) => { | |
self.symbol_table.insert(s.to_string(), *v); | |
} | |
} | |
self.advance(); | |
} | |
} | |
pub fn push(&mut self, instruction: Instruction) { | |
self.instructions.push(instruction); | |
} | |
pub fn reset(&mut self) { | |
self.instructions.clear(); | |
self.instruction_pointer = 0; | |
} | |
#[allow(dead_code)] | |
pub fn print_instructions(&self) { | |
for (i, instruction) in self.instructions.iter().enumerate() { | |
println!("{}: {:?}", i, instruction); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment