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

33 lines
593 B
Rust
Raw Normal View History

2023-03-29 20:03:16 +02:00
use crate::Op;
use std::fmt;
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(());
}
}