Skip to content

Commit

Permalink
Object and test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Yag000 committed Jul 16, 2023
1 parent 8178135 commit f2b0d7f
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 97 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ edition = "2021"
lexer = { path = "../lexer" }
parser = { path = "../parser" }
interpreter = { path = "../interpreter" }
object = { path = "../object" }

# External crates
byteorder = "1.4.3"
num-derive = "0.4.0"
num-traits = "0.2.15"

11 changes: 6 additions & 5 deletions crates/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::code::{Instructions, Opcode};
use crate::symbol_table::SymbolTable;
use interpreter::object::Object;
use lexer::token::Token;
use num_traits::FromPrimitive;
use object::object::Object;
use parser::ast::Program;
use parser::ast::{BlockStatement, Conditional, Expression, InfixOperator, Primitive, Statement};

Expand Down Expand Up @@ -312,10 +312,11 @@ pub mod tests {

use std::rc::Rc;

use crate::{
code::Opcode,
test_utils::{check_constants, check_instructions, parse},
};
use object::test_utils::check_constants;
use parser::parser::parse;

use crate::code::Opcode;
use crate::test_utils::check_instructions;

use super::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod code;
pub mod compiler;
pub mod symbol_table;
pub mod test_utils;
mod test_utils;
28 changes: 1 addition & 27 deletions crates/compiler/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
use crate::code::Instructions;
use interpreter::object::Object;
use lexer::lexer::Lexer;
use parser::{ast::Program, parser::Parser};
use std::rc::Rc;

pub fn parse(input: &str) -> Program {
let lexer = Lexer::new(input);
let mut parser = Parser::new(lexer);
parser.parse_program()
}

#[allow(dead_code)]
pub fn check_instructions(instructions: &Instructions, expected: &Instructions) {
assert_eq!(
instructions.data.len(),
Expand All @@ -21,20 +12,3 @@ pub fn check_instructions(instructions: &Instructions, expected: &Instructions)
"wrong instructions. want={expected:?}, got={instructions:?}"
);
}

pub fn check_constants(constants: &Vec<Object>, expected: &Vec<Rc<Object>>) {
assert_eq!(
constants.len(),
expected.len(),
"wrong number of constants. got={:?}, want={:?}",
constants.len(),
expected.len()
);

for (expected_constant, constant) in expected.iter().zip(constants.iter()) {
assert_eq!(
**expected_constant, *constant,
"constant not equal. got={constant:?}, want={expected_constant:?}"
);
}
}
3 changes: 2 additions & 1 deletion crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

lexer = { path = "../lexer" }
parser = { path = "../parser" }

object = { path = "../object" }
12 changes: 4 additions & 8 deletions crates/interpreter/src/evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use crate::{
use lexer::token::Token;
use object::{
enviroment::Environment,
object::{BuiltinFunction, Function, Object},
object::{Object, FALSE, NULL, TRUE, Function, BuiltinFunction},
};
use lexer::token::Token;
use parser::ast::{
BlockStatement, Conditional, Expression, HashMapLiteral, Identifier, IndexExpression,
Primitive, Program, Statement,
};
use std::{cell::RefCell, collections::HashMap, rc::Rc};

const TRUE: Object = Object::BOOLEAN(true);
const FALSE: Object = Object::BOOLEAN(false);
pub const NULL: Object = Object::NULL;

pub struct Evaluator {
env: Rc<RefCell<Environment>>,
}
Expand Down Expand Up @@ -102,7 +98,7 @@ impl Evaluator {
Expression::FunctionLiteral(x) => {
let parameters = x.parameters;
let body = x.body;
Object::FUNCTION(Function {
Object::FUNCTION(Function{
parameters,
body,
environment: Rc::clone(&self.env),
Expand Down
2 changes: 0 additions & 2 deletions crates/interpreter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
mod enviroment;
pub mod evaluator;
pub mod object;
12 changes: 12 additions & 0 deletions crates/object/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "object"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

parser = { path = "../parser" }


Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::object::Object;
use std::{cell::RefCell, collections::HashMap, rc::Rc};

use crate::object::Object;

#[derive(Debug, PartialEq, Clone)]
pub struct Environment {
store: HashMap<String, Object>,
Expand Down
3 changes: 3 additions & 0 deletions crates/object/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod object;
pub mod enviroment;
pub mod test_utils;
24 changes: 8 additions & 16 deletions crates/interpreter/src/object.rs → crates/object/src/object.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use super::enviroment::Environment;
use crate::evaluator::NULL;
use parser::ast::{BlockStatement, Identifier};
use std::{cell::RefCell, cmp::Ordering, collections::HashMap, fmt::Display, hash::Hash, rc::Rc};

use parser::ast::{BlockStatement, Identifier};

use crate::enviroment::Environment;

pub const TRUE: Object = Object::BOOLEAN(true);
pub const FALSE: Object = Object::BOOLEAN(false);
pub const NULL: Object = Object::NULL;

#[derive(Debug, PartialEq, Clone)]
pub enum Object {
INTEGER(i64),
Expand Down Expand Up @@ -123,19 +128,6 @@ impl Display for BuiltinFunction {

#[allow(clippy::needless_pass_by_value)] // false positive
impl BuiltinFunction {
pub fn get_builtins() -> Environment {
let mut env = Environment::new();
env.set(String::from("len"), Object::BUILTIN(BuiltinFunction::LEN));
env.set(
String::from("first"),
Object::BUILTIN(BuiltinFunction::FIRST),
);
env.set(String::from("last"), Object::BUILTIN(BuiltinFunction::LAST));
env.set(String::from("rest"), Object::BUILTIN(BuiltinFunction::REST));
env.set(String::from("push"), Object::BUILTIN(BuiltinFunction::PUSH));
env
}

pub fn get_builtin(name: &str) -> Option<Object> {
match name {
"len" => Some(Object::BUILTIN(BuiltinFunction::LEN)),
Expand Down
20 changes: 20 additions & 0 deletions crates/object/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::rc::Rc;

use crate::object::Object;

pub fn check_constants(constants: &Vec<Object>, expected: &Vec<Rc<Object>>) {
assert_eq!(
constants.len(),
expected.len(),
"wrong number of constants. got={:?}, want={:?}",
constants.len(),
expected.len()
);

for (expected_constant, constant) in expected.iter().zip(constants.iter()) {
assert_eq!(
**expected_constant, *constant,
"constant not equal. got={constant:?}, want={expected_constant:?}"
);
}
}
6 changes: 6 additions & 0 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ impl Parser {
}
}

pub fn parse(input: &str) -> Program {
let lexer = Lexer::new(input);
let mut parser = Parser::new(lexer);
parser.parse_program()
}

#[cfg(test)]
mod tests {
use lexer::token::Token;
Expand Down
3 changes: 1 addition & 2 deletions crates/repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ edition = "2021"
[dependencies]
lexer = { path = "../lexer" }
parser = { path = "../parser" }
object = { path = "../object" }
interpreter = { path = "../interpreter" }
compiler = { path = "../compiler" }
vm = { path = "../vm" }

# External crates
clap = "4.3.11"
clap_derive = "4.3.2"


4 changes: 2 additions & 2 deletions crates/repl/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use clap_derive::{Parser, ValueEnum};
use compiler::compiler::{Bytecode, Compiler};
use compiler::symbol_table::SymbolTable;
use interpreter::evaluator::Evaluator;
use interpreter::object::Object;
use lexer::lexer::Lexer;
use lexer::token::Token;
use object::object::{Object, NULL};
use parser::parser::{Parser, ParserErrors};
use std::io::{self, Write};
use std::rc::Rc;
use std::{error::Error, fs};
use vm::vm::{GLOBALS_SIZE, NULL, VM};
use vm::vm::{GLOBALS_SIZE, VM};

use crate::errors::{CompilerError, LexerErrors, RuntimeError};

Expand Down
2 changes: 2 additions & 0 deletions crates/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ edition = "2021"
[dependencies]
# Internal crates

parser = { path = "../parser" }
compiler = { path = "../compiler" }
interpreter = { path = "../interpreter" }
object = { path = "../object" }

# External crates
num-traits = "0.2.15"
Loading

0 comments on commit f2b0d7f

Please sign in to comment.