about summary refs log tree commit diff
path: root/users/tazjin
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-02-27T20·15+0200
committertazjin <mail@tazj.in>2021-02-27T20·19+0000
commit758730d25db055ff28f7b1f76d59b69d85047795 (patch)
treeedf8ccb4c4e5bda4c39b4e1c62fecd13f3382e90 /users/tazjin
parent56f6a6e9f20236cb97d2e6bc2a7819762ecb0fe7 (diff)
feat(tazjin/rlox): Print compiled bytecode with disassemble feature r/2243
Change-Id: I42293b334248b5228dd90f13b9a400ccdca20a84
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2562
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin')
-rw-r--r--users/tazjin/rlox/src/bytecode/chunk.rs7
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler/mod.rs4
2 files changed, 11 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/bytecode/chunk.rs b/users/tazjin/rlox/src/bytecode/chunk.rs
index 649aafcfd82c..b2a7c7b6a2c1 100644
--- a/users/tazjin/rlox/src/bytecode/chunk.rs
+++ b/users/tazjin/rlox/src/bytecode/chunk.rs
@@ -84,3 +84,10 @@ pub fn disassemble_instruction(chunk: &Chunk, offset: usize) {
         op => println!("{:?}", op),
     }
 }
+
+#[cfg(feature = "disassemble")]
+pub fn disassemble_chunk(chunk: &Chunk) {
+    for (idx, _) in chunk.code.iter().enumerate() {
+        disassemble_instruction(chunk, idx);
+    }
+}
diff --git a/users/tazjin/rlox/src/bytecode/compiler/mod.rs b/users/tazjin/rlox/src/bytecode/compiler/mod.rs
index 58a5c1b98a02..547c1ab18fd7 100644
--- a/users/tazjin/rlox/src/bytecode/compiler/mod.rs
+++ b/users/tazjin/rlox/src/bytecode/compiler/mod.rs
@@ -219,6 +219,10 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
 
     fn end_compiler(&mut self) -> LoxResult<()> {
         self.emit_op(OpCode::OpReturn);
+
+        #[cfg(feature = "disassemble")]
+        chunk::disassemble_chunk(&self.chunk);
+
         Ok(())
     }