about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-02-07T16·29+0300
committerclbot <clbot@tvl.fyi>2022-02-07T16·58+0000
commit0d0b43ed8819e66a0888eb6d1d1f47b171ae62e0 (patch)
tree305c04d3fe26c92ed7037f0b0f41f38444ce83ea /users/tazjin/rlox/src/bytecode
parent8b8c98380e85b2057a3c35ce3d76879fab4266b0 (diff)
fix(users/tazjin): rustfmt code with non-default settings r/3776
rustfmt only sometimes detects path-based nested config
files (probably some kind of race?), so my users folder uses a
separate formatting check for rustfmt to avoid flaky CI. Enough flakes
around already ...

Change-Id: Ifd862f9974f071b3a256643dd8e56c019116156a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5242
Reviewed-by: tazjin <tazjin@tvl.su>
Autosubmit: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/bytecode')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs89
-rw-r--r--users/tazjin/rlox/src/bytecode/mod.rs5
-rw-r--r--users/tazjin/rlox/src/bytecode/vm.rs33
3 files changed, 42 insertions, 85 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs
index 3e8a80653f91..89584f19d720 100644
--- a/users/tazjin/rlox/src/bytecode/compiler.rs
+++ b/users/tazjin/rlox/src/bytecode/compiler.rs
@@ -63,9 +63,11 @@ enum Precedence {
     Equality,   // == !=
     Comparison, // < > <= >=
     Term,       // + -
-    Factor,     // * /
-    Unary,      // ! -
-    Call,       // . ()
+    Factor,     //
+    // 
+    // * /
+    Unary, // ! -
+    Call,  // . ()
     Primary,
 }
 
@@ -78,11 +80,7 @@ struct ParseRule<T: Iterator<Item = Token>> {
 }
 
 impl<T: Iterator<Item = Token>> ParseRule<T> {
-    fn new(
-        prefix: Option<ParseFn<T>>,
-        infix: Option<ParseFn<T>>,
-        precedence: Precedence,
-    ) -> Self {
+    fn new(prefix: Option<ParseFn<T>>, infix: Option<ParseFn<T>>, precedence: Precedence) -> Self {
         ParseRule {
             prefix,
             infix,
@@ -105,18 +103,16 @@ impl Precedence {
             Precedence::Factor => Precedence::Unary,
             Precedence::Unary => Precedence::Call,
             Precedence::Call => Precedence::Primary,
-            Precedence::Primary => panic!(
-                "invalid parser state: no higher precedence than Primary"
-            ),
+            Precedence::Primary => {
+                panic!("invalid parser state: no higher precedence than Primary")
+            }
         }
     }
 }
 
 fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
     match token {
-        TokenKind::LeftParen => {
-            ParseRule::new(Some(Compiler::grouping), None, Precedence::None)
-        }
+        TokenKind::LeftParen => ParseRule::new(Some(Compiler::grouping), None, Precedence::None),
 
         TokenKind::Minus => ParseRule::new(
             Some(Compiler::unary),
@@ -124,57 +120,33 @@ fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
             Precedence::Term,
         ),
 
-        TokenKind::Plus => {
-            ParseRule::new(None, Some(Compiler::binary), Precedence::Term)
-        }
+        TokenKind::Plus => ParseRule::new(None, Some(Compiler::binary), Precedence::Term),
 
-        TokenKind::Slash => {
-            ParseRule::new(None, Some(Compiler::binary), Precedence::Factor)
-        }
+        TokenKind::Slash => ParseRule::new(None, Some(Compiler::binary), Precedence::Factor),
 
-        TokenKind::Star => {
-            ParseRule::new(None, Some(Compiler::binary), Precedence::Factor)
-        }
+        TokenKind::Star => ParseRule::new(None, Some(Compiler::binary), Precedence::Factor),
 
-        TokenKind::Number(_) => {
-            ParseRule::new(Some(Compiler::number), None, Precedence::None)
-        }
+        TokenKind::Number(_) => ParseRule::new(Some(Compiler::number), None, Precedence::None),
 
-        TokenKind::True => {
-            ParseRule::new(Some(Compiler::literal), None, Precedence::None)
-        }
+        TokenKind::True => ParseRule::new(Some(Compiler::literal), None, Precedence::None),
 
-        TokenKind::False => {
-            ParseRule::new(Some(Compiler::literal), None, Precedence::None)
-        }
+        TokenKind::False => ParseRule::new(Some(Compiler::literal), None, Precedence::None),
 
-        TokenKind::Nil => {
-            ParseRule::new(Some(Compiler::literal), None, Precedence::None)
-        }
+        TokenKind::Nil => ParseRule::new(Some(Compiler::literal), None, Precedence::None),
 
-        TokenKind::Bang => {
-            ParseRule::new(Some(Compiler::unary), None, Precedence::None)
-        }
+        TokenKind::Bang => ParseRule::new(Some(Compiler::unary), None, Precedence::None),
 
-        TokenKind::BangEqual => {
-            ParseRule::new(None, Some(Compiler::binary), Precedence::Equality)
-        }
+        TokenKind::BangEqual => ParseRule::new(None, Some(Compiler::binary), Precedence::Equality),
 
-        TokenKind::EqualEqual => {
-            ParseRule::new(None, Some(Compiler::binary), Precedence::Equality)
-        }
+        TokenKind::EqualEqual => ParseRule::new(None, Some(Compiler::binary), Precedence::Equality),
 
-        TokenKind::Greater => {
-            ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
-        }
+        TokenKind::Greater => ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison),
 
         TokenKind::GreaterEqual => {
             ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
         }
 
-        TokenKind::Less => {
-            ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
-        }
+        TokenKind::Less => ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison),
 
         TokenKind::LessEqual => {
             ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
@@ -184,9 +156,7 @@ fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
             ParseRule::new(Some(Compiler::variable), None, Precedence::None)
         }
 
-        TokenKind::String(_) => {
-            ParseRule::new(Some(Compiler::string), None, Precedence::None)
-        }
+        TokenKind::String(_) => ParseRule::new(Some(Compiler::string), None, Precedence::None),
 
         _ => ParseRule::new(None, None, Precedence::None),
     }
@@ -236,9 +206,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
 
     fn define_variable(&mut self, var: Option<ConstantIdx>) -> LoxResult<()> {
         if self.locals.scope_depth == 0 {
-            self.emit_op(OpCode::OpDefineGlobal(
-                var.expect("should be global"),
-            ));
+            self.emit_op(OpCode::OpDefineGlobal(var.expect("should be global")));
         } else {
             self.locals
                 .locals
@@ -305,9 +273,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
     }
 
     fn block(&mut self) -> LoxResult<()> {
-        while !self.check(&TokenKind::RightBrace)
-            && !self.check(&TokenKind::Eof)
-        {
+        while !self.check(&TokenKind::RightBrace) && !self.check(&TokenKind::Eof) {
             self.declaration()?;
         }
 
@@ -712,9 +678,8 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
 
 pub fn compile(code: &str) -> Result<(Interner, Chunk), Vec<Error>> {
     let chars = code.chars().collect::<Vec<char>>();
-    let tokens = scanner::scan(&chars).map_err(|errors| {
-        errors.into_iter().map(Into::into).collect::<Vec<Error>>()
-    })?;
+    let tokens = scanner::scan(&chars)
+        .map_err(|errors| errors.into_iter().map(Into::into).collect::<Vec<Error>>())?;
 
     let mut compiler = Compiler {
         tokens: tokens.into_iter().peekable(),
diff --git a/users/tazjin/rlox/src/bytecode/mod.rs b/users/tazjin/rlox/src/bytecode/mod.rs
index c6f3a737aef8..117f17824ac6 100644
--- a/users/tazjin/rlox/src/bytecode/mod.rs
+++ b/users/tazjin/rlox/src/bytecode/mod.rs
@@ -23,10 +23,7 @@ impl crate::Lox for Interpreter {
         Interpreter {}
     }
 
-    fn interpret(
-        &mut self,
-        code: String,
-    ) -> Result<Self::Value, Vec<Self::Error>> {
+    fn interpret(&mut self, code: String) -> Result<Self::Value, Vec<Self::Error>> {
         let (strings, chunk) = compiler::compile(&code)?;
         vm::interpret(strings, chunk).map_err(|e| vec![e])
     }
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs
index d287ec7cb8c5..30ffebc79cf7 100644
--- a/users/tazjin/rlox/src/bytecode/vm.rs
+++ b/users/tazjin/rlox/src/bytecode/vm.rs
@@ -118,12 +118,7 @@ impl VM {
 
                 OpCode::OpNegate => {
                     let v = self.pop();
-                    with_type!(
-                        self,
-                        v,
-                        Value::Number(num),
-                        self.push(Value::Number(-num))
-                    );
+                    with_type!(self, v, Value::Number(num), self.push(Value::Number(-num)));
                 }
 
                 OpCode::OpSubtract => binary_op!(self, Number, -),
@@ -141,15 +136,18 @@ impl VM {
                             self.push(Value::String(new_s.into()));
                         }
 
-                        (Value::Number(n_a), Value::Number(n_b)) =>
-                            self.push(Value::Number(n_a + n_b)),
+                        (Value::Number(n_a), Value::Number(n_b)) => {
+                            self.push(Value::Number(n_a + n_b))
+                        }
 
-                        _ => return Err(Error {
-                            line: self.chunk.get_line(self.ip - 1),
-                            kind: ErrorKind::TypeError(
-                                "'+' operator only works on strings and numbers".into()
-                            ),
-                        })
+                        _ => {
+                            return Err(Error {
+                                line: self.chunk.get_line(self.ip - 1),
+                                kind: ErrorKind::TypeError(
+                                    "'+' operator only works on strings and numbers".into(),
+                                ),
+                            })
+                        }
                     }
                 }
 
@@ -205,8 +203,7 @@ impl VM {
                         self.stack.len() > local_idx.0,
                         "stack is not currently large enough for local"
                     );
-                    self.stack[local_idx.0] =
-                        self.stack.last().unwrap().clone();
+                    self.stack[local_idx.0] = self.stack.last().unwrap().clone();
                 }
 
                 OpCode::OpJumpPlaceholder(_) => {
@@ -255,9 +252,7 @@ impl VM {
     fn print_value(&self, val: Value) -> String {
         match val {
             Value::String(LoxString::Heap(s)) => s,
-            Value::String(LoxString::Interned(id)) => {
-                self.strings.lookup(id).into()
-            }
+            Value::String(LoxString::Interned(id)) => self.strings.lookup(id).into(),
             _ => format!("{:?}", val),
         }
     }