use std::fmt;
use super::Code;
impl<T: fmt::Debug> fmt::Debug for Code<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut result = String::new();
for i in 0..self.data.len() {
result.push_str(&format!("@{} = {:?}\n", i, self.data[i]));
}
let mut ip = 0;
let len = self.code.len();
loop {
if ip == len { break; }
for label in self.labels() {
if ip == label.0 {
result.push_str(&format!("\n.{}:\n", label.1));
break;
}
}
let op_code = self.code[ip];
let arity = self.code[ip + 1];
ip = ip + 2;
for symbol in self.symbols() {
if op_code == symbol.0 {
result.push_str(&format!("\t{}", symbol.1));
break;
}
}
for _i in 0..arity {
let const_idx = self.code[ip];
ip = ip + 1;
result.push_str(&format!(" @{}", const_idx));
}
result.push_str("\n");
}
write!(f, "{}", result)
}
}