From 0d0b43ed8819e66a0888eb6d1d1f47b171ae62e0 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 7 Feb 2022 19:29:52 +0300 Subject: fix(users/tazjin): rustfmt code with non-default settings 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 Autosubmit: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/treewalk/interpreter.rs | 106 ++++++-------------------- 1 file changed, 24 insertions(+), 82 deletions(-) (limited to 'users/tazjin/rlox/src/treewalk/interpreter.rs') diff --git a/users/tazjin/rlox/src/treewalk/interpreter.rs b/users/tazjin/rlox/src/treewalk/interpreter.rs index d9fe33661684..3285775bbec6 100644 --- a/users/tazjin/rlox/src/treewalk/interpreter.rs +++ b/users/tazjin/rlox/src/treewalk/interpreter.rs @@ -34,11 +34,7 @@ impl Callable { } } - fn call( - &self, - lox: &mut Interpreter, - args: Vec, - ) -> Result { + fn call(&self, lox: &mut Interpreter, args: Vec) -> Result { match self { Callable::Builtin(builtin) => builtin.call(args), @@ -50,10 +46,8 @@ impl Callable { fn_env.define(param, value)?; } - let result = lox.interpret_block_with_env( - Some(Rc::new(RwLock::new(fn_env))), - &func.body, - ); + let result = + lox.interpret_block_with_env(Some(Rc::new(RwLock::new(fn_env))), &func.body); match result { // extract returned values if applicable @@ -109,22 +103,13 @@ pub struct Environment { } impl Environment { - fn define( - &mut self, - name: &scanner::Token, - value: Value, - ) -> Result<(), Error> { + fn define(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { let ident = identifier_str(name)?; self.values.insert(ident.into(), value); Ok(()) } - fn get( - &self, - ident: &str, - line: usize, - depth: usize, - ) -> Result { + fn get(&self, ident: &str, line: usize, depth: usize) -> Result { if depth > 0 { match &self.enclosing { None => { @@ -137,9 +122,7 @@ impl Environment { }) } Some(parent) => { - let env = parent - .read() - .expect("fatal: environment lock poisoned"); + let env = parent.read().expect("fatal: environment lock poisoned"); return env.get(ident, line, depth - 1); } } @@ -154,11 +137,7 @@ impl Environment { }) } - fn assign( - &mut self, - name: &scanner::Token, - value: Value, - ) -> Result<(), Error> { + fn assign(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { let ident = identifier_str(name)?; match self.values.get_mut(ident) { @@ -242,22 +221,14 @@ impl Lox for Interpreter { impl Interpreter { // Environment modification helpers - fn define_var( - &mut self, - name: &scanner::Token, - value: Value, - ) -> Result<(), Error> { + fn define_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { self.env .write() .expect("environment lock is poisoned") .define(name, value) } - fn assign_var( - &mut self, - name: &scanner::Token, - value: Value, - ) -> Result<(), Error> { + fn assign_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { self.env .write() .expect("environment lock is poisoned") @@ -271,11 +242,10 @@ impl Interpreter { kind: ErrorKind::UndefinedVariable(ident.into()), })?; - self.env.read().expect("environment lock is poisoned").get( - ident, - var.name.line, - depth, - ) + self.env + .read() + .expect("environment lock is poisoned") + .get(ident, var.name.line, depth) } /// Interpret the block in the supplied environment. If no @@ -324,16 +294,10 @@ impl Interpreter { Value::Literal(Literal::String(output)) } Statement::Var(var) => return self.interpret_var(var), - Statement::Block(block) => { - return self.interpret_block_with_env(None, block) - } + Statement::Block(block) => return self.interpret_block_with_env(None, block), Statement::If(if_stmt) => return self.interpret_if(if_stmt), - Statement::While(while_stmt) => { - return self.interpret_while(while_stmt) - } - Statement::Function(func) => { - return self.interpret_function(func.clone()) - } + Statement::While(while_stmt) => return self.interpret_while(while_stmt), + Statement::Function(func) => return self.interpret_function(func.clone()), Statement::Return(ret) => { return Err(Error { line: 0, @@ -348,9 +312,7 @@ impl Interpreter { fn interpret_var(&mut self, var: &parser::Var) -> Result { let init = var.initialiser.as_ref().ok_or_else(|| Error { line: var.name.line, - kind: ErrorKind::InternalError( - "missing variable initialiser".into(), - ), + kind: ErrorKind::InternalError("missing variable initialiser".into()), })?; let value = self.eval(init)?; self.define_var(&var.name, value.clone())?; @@ -369,10 +331,7 @@ impl Interpreter { } } - fn interpret_while( - &mut self, - stmt: &parser::While, - ) -> Result { + fn interpret_while(&mut self, stmt: &parser::While) -> Result { let mut value = Value::Literal(Literal::Nil); while eval_truthy(&self.eval(&stmt.condition)?) { value = self.interpret_stmt(&stmt.body)?; @@ -381,10 +340,7 @@ impl Interpreter { Ok(value) } - fn interpret_function( - &mut self, - func: Rc, - ) -> Result { + fn interpret_function(&mut self, func: Rc) -> Result { let name = func.name.clone(); let value = Value::Callable(Callable::Function { func, @@ -414,9 +370,7 @@ impl Interpreter { (TokenKind::Minus, Value::Literal(Literal::Number(num))) => { Ok(Literal::Number(-num).into()) } - (TokenKind::Bang, right) => { - Ok(Literal::Boolean(!eval_truthy(&right)).into()) - } + (TokenKind::Bang, right) => Ok(Literal::Boolean(!eval_truthy(&right)).into()), (op, right) => Err(Error { line: expr.operator.line, @@ -478,10 +432,7 @@ impl Interpreter { Ok(value) } - fn eval_logical( - &mut self, - logical: &parser::Logical, - ) -> Result { + fn eval_logical(&mut self, logical: &parser::Logical) -> Result { let left = eval_truthy(&self.eval(&logical.left)?); let right = eval_truthy(&self.eval(&logical.right)?); @@ -490,10 +441,7 @@ impl Interpreter { TokenKind::Or => Ok(Literal::Boolean(left || right).into()), kind => Err(Error { line: logical.operator.line, - kind: ErrorKind::InternalError(format!( - "Invalid logical operator: {:?}", - kind - )), + kind: ErrorKind::InternalError(format!("Invalid logical operator: {:?}", kind)), }), } } @@ -504,10 +452,7 @@ impl Interpreter { Value::Literal(v) => { return Err(Error { line: call.paren.line, - kind: ErrorKind::RuntimeError(format!( - "not callable: {:?}", - v - )), + kind: ErrorKind::RuntimeError(format!("not callable: {:?}", v)), }) } }; @@ -546,10 +491,7 @@ fn eval_truthy(lit: &Value) -> bool { } } -fn set_enclosing_env( - this: &RwLock, - parent: Rc>, -) { +fn set_enclosing_env(this: &RwLock, parent: Rc>) { this.write() .expect("environment lock is poisoned") .enclosing = Some(parent); -- cgit 1.4.1