about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-10-20T13·57+0200
committertazjin <mail@tazj.in>2021-10-22T09·42+0000
commitd57e43e1615ae28cfdc74c1c65cbe7863d782018 (patch)
tree3ff9e6843475aec1573355cf73e6bea209de26da /users/tazjin/rlox/src/bytecode
parent4ea7fc392a562fac2158973191ad626baca16644 (diff)
refactor(tazjin/rlox): Return index after adding operations r/2985
Change-Id: I100eb9b55ace37e5c7c878d3c224b567ee8d1e36
Reviewed-on: https://cl.tvl.fyi/c/depot/+/3738
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'users/tazjin/rlox/src/bytecode')
-rw-r--r--users/tazjin/rlox/src/bytecode/chunk.rs6
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs24
-rw-r--r--users/tazjin/rlox/src/bytecode/opcode.rs3
3 files changed, 18 insertions, 15 deletions
diff --git a/users/tazjin/rlox/src/bytecode/chunk.rs b/users/tazjin/rlox/src/bytecode/chunk.rs
index b9a3994fe1c1..fc5cd34fdf4f 100644
--- a/users/tazjin/rlox/src/bytecode/chunk.rs
+++ b/users/tazjin/rlox/src/bytecode/chunk.rs
@@ -1,6 +1,6 @@
 use std::ops::Index;
 
-use super::opcode::{ConstantIdx, OpCode};
+use super::opcode::{CodeIdx, ConstantIdx, OpCode};
 use super::value;
 
 // In the book, this type is a hand-rolled dynamic array
@@ -25,11 +25,11 @@ struct Span {
 }
 
 impl Chunk {
-    pub fn add_op(&mut self, data: OpCode, line: usize) -> usize {
+    pub fn add_op(&mut self, data: OpCode, line: usize) -> CodeIdx {
         let idx = self.code.len();
         self.code.push(data);
         self.add_line(line);
-        idx
+        CodeIdx(idx)
     }
 
     pub fn add_constant(&mut self, data: value::Value) -> usize {
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs
index c2f02aff9d28..392fc9b72d6e 100644
--- a/users/tazjin/rlox/src/bytecode/compiler.rs
+++ b/users/tazjin/rlox/src/bytecode/compiler.rs
@@ -1,7 +1,7 @@
 use super::chunk::Chunk;
 use super::errors::{Error, ErrorKind, LoxResult};
 use super::interner::{InternedStr, Interner};
-use super::opcode::{ConstantIdx, OpCode, StackIdx};
+use super::opcode::{CodeIdx, ConstantIdx, OpCode, StackIdx};
 use super::value::Value;
 use crate::scanner::{self, Token, TokenKind};
 
@@ -350,7 +350,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
             TokenKind::Bang => self.emit_op(OpCode::OpNot),
             TokenKind::Minus => self.emit_op(OpCode::OpNegate),
             _ => unreachable!("only called for unary operator tokens"),
-        }
+        };
 
         Ok(())
     }
@@ -372,7 +372,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
 
             TokenKind::BangEqual => {
                 self.emit_op(OpCode::OpEqual);
-                self.emit_op(OpCode::OpNot);
+                self.emit_op(OpCode::OpNot)
             }
 
             TokenKind::EqualEqual => self.emit_op(OpCode::OpEqual),
@@ -380,17 +380,17 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
 
             TokenKind::GreaterEqual => {
                 self.emit_op(OpCode::OpLess);
-                self.emit_op(OpCode::OpNot);
+                self.emit_op(OpCode::OpNot)
             }
 
             TokenKind::Less => self.emit_op(OpCode::OpLess),
             TokenKind::LessEqual => {
                 self.emit_op(OpCode::OpGreater);
-                self.emit_op(OpCode::OpNot);
+                self.emit_op(OpCode::OpNot)
             }
 
             _ => unreachable!("only called for binary operator tokens"),
-        }
+        };
 
         Ok(())
     }
@@ -401,7 +401,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
             TokenKind::True => self.emit_op(OpCode::OpTrue),
             TokenKind::False => self.emit_op(OpCode::OpFalse),
             _ => unreachable!("only called for literal value tokens"),
-        }
+        };
 
         Ok(())
     }
@@ -432,16 +432,16 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
             match local_idx {
                 Some(idx) => self.emit_op(OpCode::OpSetLocal(idx)),
                 None => {
-                    self.emit_op(OpCode::OpSetGlobal(ident.unwrap()));
+                    self.emit_op(OpCode::OpSetGlobal(ident.unwrap()))
                 }
-            }
+            };
         } else {
             match local_idx {
                 Some(idx) => self.emit_op(OpCode::OpGetLocal(idx)),
                 None => {
                     self.emit_op(OpCode::OpGetGlobal(ident.unwrap()))
                 }
-            }
+            };
         }
 
         Ok(())
@@ -579,9 +579,9 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         Ok(())
     }
 
-    fn emit_op(&mut self, op: OpCode) {
+    fn emit_op(&mut self, op: OpCode) -> CodeIdx {
         let line = self.previous().line;
-        self.current_chunk().add_op(op, line);
+        self.current_chunk().add_op(op, line)
     }
 
     fn emit_constant(&mut self, val: Value, with_op: bool) -> ConstantIdx {
diff --git a/users/tazjin/rlox/src/bytecode/opcode.rs b/users/tazjin/rlox/src/bytecode/opcode.rs
index d2a264242496..13e2f23939ed 100644
--- a/users/tazjin/rlox/src/bytecode/opcode.rs
+++ b/users/tazjin/rlox/src/bytecode/opcode.rs
@@ -4,6 +4,9 @@ pub struct ConstantIdx(pub usize);
 #[derive(Clone, Copy, Debug)]
 pub struct StackIdx(pub usize);
 
+#[derive(Clone, Copy, Debug)]
+pub struct CodeIdx(pub usize);
+
 #[derive(Debug)]
 pub enum OpCode {
     /// Push a constant onto the stack.