Files
crafting-interpreters/rlox/src/vm.rs

37 lines
634 B
Rust
Raw Normal View History

2023-03-29 20:03:16 +02:00
use std::fmt;
2023-03-29 20:07:31 +02:00
#[repr(u8)]
#[derive(Debug)]
pub enum Op {
Return
}
2023-03-29 20:03:16 +02:00
pub struct Chunk {
code: Vec<Op>,
name: String
}
impl Chunk {
pub fn new(name: String) -> Chunk {
Chunk {
code: Vec::new(),
name
}
}
pub fn add(&mut self, op: Op) {
self.code.push(op)
}
}
impl fmt::Debug for Chunk {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
writeln!(f, "-*-*- {} -*-*-", self.name)?;
for (idx, op) in self.code.iter().enumerate() {
write!(f, "{:04} {:?}", idx, op)?;
}
return Ok(());
}
}