diff --git a/evaluator/src/lib.rs b/evaluator/src/lib.rs index 18ce1ec..373006c 100644 --- a/evaluator/src/lib.rs +++ b/evaluator/src/lib.rs @@ -3,8 +3,8 @@ use std::{cell::RefCell, rc::Rc}; use anyhow::{Error, Ok, Result}; use parser::ast::{ - BooleanLiteral, CallExpression, Expression, FunctionLiteral, IfExpression, Literal, Node, - Statement, ArrayLiteral, + ArrayLiteral, BooleanLiteral, CallExpression, Expression, FunctionLiteral, IfExpression, + Literal, Node, Statement, }; use object::{ @@ -78,7 +78,7 @@ fn eval_expression(expression: &Expression, env: &Env) -> Result> { let index = eval_expression(&index_expression.index, &Rc::clone(env))?; eval_index_expression(left, index) - }, + } Expression::Infix(infix_expression) => { let left = eval_expression(&infix_expression.left, &Rc::clone(env))?; let right = eval_expression(&infix_expression.right, &Rc::clone(env))?; @@ -147,10 +147,7 @@ fn eval_if_expression(expression: &Expression, env: &Env) -> Result> } } -fn eval_index_expression( - left: Rc, - index: Rc, -) -> Result> { +fn eval_index_expression(left: Rc, index: Rc) -> Result> { match (&*left, &*index) { (Object::Array(elements), Object::Integer(index)) => { let max = elements.len() - 1; @@ -311,7 +308,7 @@ fn eval_literal(literal: &Literal, env: &Env) -> Result> { Literal::Integer(integer) => Object::Integer(integer.value), Literal::Boolean(BooleanLiteral { value, .. }) => Object::Boolean(*value), Literal::String(string) => Object::String(string.value.clone()), - Literal::Array(ArrayLiteral { elements, ..}) => { + Literal::Array(ArrayLiteral { elements, .. }) => { let elements = eval_expressions(elements, env)?; return Ok(Rc::from(Object::Array(elements))); @@ -324,4 +321,4 @@ fn eval_literal(literal: &Literal, env: &Env) -> Result> { fn native_bool_to_bool_object(input: bool) -> Object { Object::Boolean(input) -} \ No newline at end of file +} diff --git a/evaluator/tests/evaluator_tests.rs b/evaluator/tests/evaluator_tests.rs index 1567121..161d840 100644 --- a/evaluator/tests/evaluator_tests.rs +++ b/evaluator/tests/evaluator_tests.rs @@ -5,9 +5,7 @@ use anyhow::Error; use evaluator::eval; use lexer::Lexer; use object::{environment::Environment, Object}; -use parser::{Parser, ast::Node}; - -use lexer::*; +use parser::{ast::Node, Parser}; #[test] fn test_eval_boolean_literals() -> Result<(), Error> { @@ -118,7 +116,7 @@ fn test_eval_arrays() -> Result<(), Error> { for (input, expected) in tests { let evaluated = assert_eval(input)?; - if let Object::Array(elements) = &* evaluated { + if let Object::Array(elements) = &*evaluated { assert_eq!(elements.len(), expected.len()); for (index, element) in elements.iter().enumerate() { @@ -183,7 +181,10 @@ fn test_eval_index_expressions() -> Result<(), Error> { ("$i = 0; [1][$i];", 1), ("[1, 2, 3][1 + 1];", 3), ("$myArray = [1, 2, 3]; $myArray[2];", 3), - ("$myArray = [1, 2, 3]; $myArray[0] + $myArray[1] + $myArray[2];", 6), + ( + "$myArray = [1, 2, 3]; $myArray[0] + $myArray[1] + $myArray[2];", + 6, + ), ]; for (input, expected) in tests { @@ -263,7 +264,9 @@ fn assert_string_literal_object(object: Rc, expected: &str) -> Result<() // Dereference the Rc to get an Object and then reference it again for pattern matching. match &*object { Object::String(string) if string == expected => Ok(()), - Object::String(_) => Err(anyhow::anyhow!("String value does not match expected value.")), + Object::String(_) => Err(anyhow::anyhow!( + "String value does not match expected value." + )), _ => Err(Error::msg("Object is not a String.")), } -} \ No newline at end of file +} diff --git a/interpreter/src/repl.rs b/interpreter/src/repl.rs index cb47647..0520ccd 100644 --- a/interpreter/src/repl.rs +++ b/interpreter/src/repl.rs @@ -17,10 +17,7 @@ pub fn init_repl() -> Result<(), Error> { info!("No previous history."); } - println!( - "php-rs interpreter v{}", - env!("CARGO_PKG_VERSION") - ); + println!("php-rs interpreter v{}", env!("CARGO_PKG_VERSION")); let env = Rc::new(RefCell::new(Environment::new())); diff --git a/lexer/src/lib.rs b/lexer/src/lib.rs index 8ef70bf..acdf591 100644 --- a/lexer/src/lib.rs +++ b/lexer/src/lib.rs @@ -179,4 +179,4 @@ impl<'a> Lexer<'a> { self.read_char(); } } -} \ No newline at end of file +} diff --git a/lexer/tests/lexer_tests.rs b/lexer/tests/lexer_tests.rs index 8f6aa3c..5e2340d 100644 --- a/lexer/tests/lexer_tests.rs +++ b/lexer/tests/lexer_tests.rs @@ -2,7 +2,7 @@ use anyhow::Error; use token::TokenType; -use lexer::{*, token::Token}; +use lexer::{token::Token, *}; #[test] fn test_lexer() -> Result<(), Error> { @@ -171,4 +171,4 @@ fn test_lexer() -> Result<(), Error> { } Ok(()) -} \ No newline at end of file +} diff --git a/parser/src/lib.rs b/parser/src/lib.rs index fcb8080..7996f4a 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -6,13 +6,13 @@ use log::info; pub mod ast; use ast::{ - Assignment, BlockStatement, BooleanLiteral, CallExpression, Expression, FunctionLiteral, - Identifier, IfExpression, InfixExpression, IntegerLiteral, Literal, PrefixExpression, Program, - ReturnStatement, Statement, StringLiteral, ArrayLiteral, IndexExpression, + ArrayLiteral, Assignment, BlockStatement, BooleanLiteral, CallExpression, Expression, + FunctionLiteral, Identifier, IfExpression, IndexExpression, InfixExpression, IntegerLiteral, + Literal, PrefixExpression, Program, ReturnStatement, Statement, StringLiteral, }; -use lexer::Lexer; use lexer::token::{Token, TokenType}; +use lexer::Lexer; type ParseResult = Result; @@ -117,16 +117,22 @@ impl<'a> Parser<'a> { parser.register_prefix(TokenType::Bang, |p| Parser::parse_prefix_expression(p)); parser.register_prefix(TokenType::Minus, |p| Parser::parse_prefix_expression(p)); - parser.register_prefix(TokenType::Dollar, |p| Parser::parse_variable_reference_expression(p)); - + parser.register_prefix(TokenType::Dollar, |p| { + Parser::parse_variable_reference_expression(p) + }); + parser.register_prefix(TokenType::True, |p| Parser::parse_boolean_literal(p)); parser.register_prefix(TokenType::False, |p| Parser::parse_boolean_literal(p)); parser.register_prefix(TokenType::Int, |p| Parser::parse_integer_literal(p)); parser.register_prefix(TokenType::String, |p| Parser::parse_string_literal(p)); parser.register_prefix(TokenType::LBracket, |p| Parser::parse_array_literal(p)); - parser.register_infix(TokenType::LParen, |p, left| Parser::parse_call_expression(p, left)); - parser.register_infix(TokenType::LBracket, |p, left| Parser::parse_index_expression(p, left)); + parser.register_infix(TokenType::LParen, |p, left| { + Parser::parse_call_expression(p, left) + }); + parser.register_infix(TokenType::LBracket, |p, left| { + Parser::parse_index_expression(p, left) + }); parser.register_infix(TokenType::Plus, |p, left| { Parser::parse_infix_expression(p, left) @@ -802,4 +808,4 @@ impl<'a> Parser<'a> { pub fn register_infix(&mut self, token_type: TokenType, function: InfixParseFn) { self.infix_parse_fns.insert(token_type, function); } -} \ No newline at end of file +} diff --git a/parser/tests/parser_tests.rs b/parser/tests/parser_tests.rs index a6b419e..838e83f 100644 --- a/parser/tests/parser_tests.rs +++ b/parser/tests/parser_tests.rs @@ -1,9 +1,12 @@ use lexer::Lexer; -use parser::{*, ast::{Expression, Literal, BlockStatement}}; +use parser::{ + ast::{BlockStatement, Expression, Literal}, + *, +}; use anyhow::{Error, Result}; -use parser::ast::{Statement, ArrayLiteral}; +use parser::ast::{ArrayLiteral, Statement}; #[test] fn test_assignment_statements() -> Result<(), Error> { @@ -64,9 +67,7 @@ fn test_boolean_expression() -> Result<(), Error> { } } Statement::Assign(variable_assignment) => { - if let Expression::Literal(Literal::Boolean(boolean)) = - &variable_assignment.value - { + if let Expression::Literal(Literal::Boolean(boolean)) = &variable_assignment.value { assert_eq!(expected_values[i], boolean.value); } else { assert!(false, "Expected Boolean expression"); @@ -135,9 +136,7 @@ fn test_if_expression() -> Result<(), Error> { assert_eq!(1, if_expression.consequence.statements.len()); - if let Statement::Return(return_statement) = - &if_expression.consequence.statements[0] - { + if let Statement::Return(return_statement) = &if_expression.consequence.statements[0] { assert_identifier(&return_statement.return_value, "$x")?; } else { assert!(false, "Expected ReturnStatement"); @@ -173,9 +172,7 @@ fn test_if_else_expression() -> Result<(), Error> { assert_eq!(1, if_expression.consequence.statements.len()); - if let Statement::Return(return_statement) = - &if_expression.consequence.statements[0] - { + if let Statement::Return(return_statement) = &if_expression.consequence.statements[0] { assert_identifier(&return_statement.return_value, "$x")?; } else { assert!(false, "Expected ReturnStatement"); @@ -307,7 +304,9 @@ fn test_array_literal_expression() -> Result<(), Error> { parser.check_errors()?; if let Statement::Expr(expression) = &program.statements[0] { - if let Expression::Literal(Literal::Array(ArrayLiteral { token: _, elements})) = &expression { + if let Expression::Literal(Literal::Array(ArrayLiteral { token: _, elements })) = + &expression + { assert_eq!(3, elements.len()); assert_integer_literal(&elements[0], 1)?; @@ -568,4 +567,4 @@ fn assert_literal_expression(expression: &Expression, expected: &str) -> Result< Ok(false) } } -} \ No newline at end of file +}