Alpha Chen 2 years ago
parent 93a074bda9
commit e9b4e8cbfd

@ -1,3 +1,7 @@
use vec::Vec;
use crate::vec;
#[repr(u8)] #[repr(u8)]
pub enum OpCode { pub enum OpCode {
Constant, Constant,
@ -9,6 +13,7 @@ type Value = f32;
pub struct Chunk { pub struct Chunk {
code: Vec<u8>, code: Vec<u8>,
constants: Vec<Value>, constants: Vec<Value>,
lines: Vec<usize>,
} }
impl Chunk { impl Chunk {
@ -16,12 +21,13 @@ impl Chunk {
Chunk { Chunk {
code: Vec::new(), code: Vec::new(),
constants: Vec::new(), constants: Vec::new(),
lines: Vec::new(),
} }
} }
// https://doc.rust-lang.org/nomicon/vec/vec-push-pop.html pub fn write(&mut self, byte: u8, line: usize) {
pub fn write(&mut self, byte: u8) {
self.code.push(byte); self.code.push(byte);
self.lines.push(line);
} }
pub fn add_constant(&mut self, value: Value) -> u8 { pub fn add_constant(&mut self, value: Value) -> u8 {
@ -44,6 +50,12 @@ impl Chunk {
fn disassemble_instruction(&self, offset: usize) -> usize { fn disassemble_instruction(&self, offset: usize) -> usize {
print!("{:04} ", offset); print!("{:04} ", offset);
if offset > 0 && self.lines[offset] == self.lines[offset - 1] {
print!(" | ");
} else {
print!("{:>4} ", self.lines[offset]);
}
match self.code[offset] { match self.code[offset] {
0 => self.constant_instruction("OP_CONSTANT", offset), 0 => self.constant_instruction("OP_CONSTANT", offset),
1 => self.simple_instruction("OP_RETURN", offset), 1 => self.simple_instruction("OP_RETURN", offset),
@ -59,7 +71,7 @@ impl Chunk {
fn constant_instruction(&self, name: &str, offset: usize) -> usize { fn constant_instruction(&self, name: &str, offset: usize) -> usize {
let constant_index = self.code[offset+1]; let constant_index = self.code[offset+1];
let value = self.constants[constant_index as usize]; let value = self.constants[constant_index as usize];
println!("{:<16} {:04} '{}'", name, constant_index, value); println!("{:<16} {:>4} '{}'", name, constant_index, value);
offset + 2 offset + 2
} }
} }

@ -5,11 +5,12 @@ pub mod vec;
fn main() { fn main() {
let mut chunk = Chunk::default(); let mut chunk = Chunk::default();
chunk.write(OpCode::Return as u8);
let constant = chunk.add_constant(1.2); let constant = chunk.add_constant(1.2);
chunk.write(OpCode::Constant as u8); chunk.write(OpCode::Constant as u8, 123);
chunk.write(constant); chunk.write(constant, 123);
chunk.write(OpCode::Return as u8, 123);
chunk.disassemble("test chunk"); chunk.disassemble("test chunk");
} }

Loading…
Cancel
Save