about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'users/tazjin/rlox/src/bytecode')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs39
1 files changed, 21 insertions, 18 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs
index 8e70067e3182..f2fdeb0e9feb 100644
--- a/users/tazjin/rlox/src/bytecode/compiler.rs
+++ b/users/tazjin/rlox/src/bytecode/compiler.rs
@@ -153,6 +153,15 @@ fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
     }
 }
 
+macro_rules! consume {
+    ( $self:ident, $expected:pat, $err:expr ) => {
+        match $self.current().kind {
+            $expected => $self.advance(),
+            _ => $self.error_at($self.current().line, $err),
+        }
+    };
+}
+
 impl<T: Iterator<Item = Token>> Compiler<T> {
     fn compile(&mut self) -> LoxResult<()> {
         self.advance();
@@ -193,9 +202,10 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
 
     fn print_statement(&mut self) -> LoxResult<()> {
         self.expression()?;
-        self.consume(
-            &TokenKind::Semicolon,
-            ErrorKind::ExpectedToken("Expected ';' after value"),
+        consume!(
+            self,
+            TokenKind::Semicolon,
+            ErrorKind::ExpectedToken("Expected ';' after value")
         );
         self.emit_op(OpCode::OpPrint);
         Ok(())
@@ -203,9 +213,10 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
 
     fn expression_statement(&mut self) -> LoxResult<()> {
         self.expression()?;
-        self.consume(
-            &TokenKind::Semicolon,
-            ErrorKind::ExpectedToken("Expected ';' after expression"),
+        consume!(
+            self,
+            TokenKind::Semicolon,
+            ErrorKind::ExpectedToken("Expected ';' after expression")
         );
         self.emit_op(OpCode::OpPop);
         Ok(())
@@ -222,9 +233,10 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
 
     fn grouping(&mut self) -> LoxResult<()> {
         self.expression()?;
-        self.consume(
-            &TokenKind::RightParen,
-            ErrorKind::ExpectedToken("Expected ')' after expression"),
+        consume!(
+            self,
+            TokenKind::RightParen,
+            ErrorKind::ExpectedToken("Expected ')' after expression")
         );
         Ok(())
     }
@@ -334,15 +346,6 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         Ok(())
     }
 
-    fn consume(&mut self, expected: &TokenKind, err: ErrorKind) {
-        if self.current().kind == *expected {
-            self.advance();
-            return;
-        }
-
-        self.error_at(self.current().line, err);
-    }
-
     fn current_chunk(&mut self) -> &mut Chunk {
         &mut self.chunk
     }