Skip to content

Commit

Permalink
chore: cargo fix & fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakFarmer committed Oct 22, 2023
1 parent ed9150a commit 0cb647b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 45 deletions.
15 changes: 6 additions & 9 deletions evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -78,7 +78,7 @@ fn eval_expression(expression: &Expression, env: &Env) -> Result<Rc<Object>> {
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))?;
Expand Down Expand Up @@ -147,10 +147,7 @@ fn eval_if_expression(expression: &Expression, env: &Env) -> Result<Rc<Object>>
}
}

fn eval_index_expression(
left: Rc<Object>,
index: Rc<Object>,
) -> Result<Rc<Object>> {
fn eval_index_expression(left: Rc<Object>, index: Rc<Object>) -> Result<Rc<Object>> {
match (&*left, &*index) {
(Object::Array(elements), Object::Integer(index)) => {
let max = elements.len() - 1;
Expand Down Expand Up @@ -311,7 +308,7 @@ fn eval_literal(literal: &Literal, env: &Env) -> Result<Rc<Object>> {
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)));
Expand All @@ -324,4 +321,4 @@ fn eval_literal(literal: &Literal, env: &Env) -> Result<Rc<Object>> {

fn native_bool_to_bool_object(input: bool) -> Object {
Object::Boolean(input)
}
}
17 changes: 10 additions & 7 deletions evaluator/tests/evaluator_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -263,7 +264,9 @@ fn assert_string_literal_object(object: Rc<Object>, expected: &str) -> Result<()
// Dereference the Rc<Object> 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.")),
}
}
}
5 changes: 1 addition & 4 deletions interpreter/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down
2 changes: 1 addition & 1 deletion lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ impl<'a> Lexer<'a> {
self.read_char();
}
}
}
}
4 changes: 2 additions & 2 deletions lexer/tests/lexer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Error;

use token::TokenType;

use lexer::{*, token::Token};
use lexer::{token::Token, *};

#[test]
fn test_lexer() -> Result<(), Error> {
Expand Down Expand Up @@ -171,4 +171,4 @@ fn test_lexer() -> Result<(), Error> {
}

Ok(())
}
}
24 changes: 15 additions & 9 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Expression>;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
25 changes: 12 additions & 13 deletions parser/tests/parser_tests.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -568,4 +567,4 @@ fn assert_literal_expression(expression: &Expression, expected: &str) -> Result<
Ok(false)
}
}
}
}

0 comments on commit 0cb647b

Please sign in to comment.