Skip to content

Commit

Permalink
Refactor object type references
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed Oct 23, 2023
1 parent 84af0f6 commit e607342
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 43 deletions.
12 changes: 6 additions & 6 deletions src/evaluator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::object::null::Null;
use crate::object::r#macro::quote::Quote;
use crate::object::return_value::ReturnValue;
use crate::object::string::StringObj;
use crate::object::ObjectType::{ArrayObj, HashObj, IntegerObj};
use crate::object::{Object, ObjectInterface, ObjectType};
use crate::object::ObjectType;
use crate::object::{Object, ObjectInterface};
use crate::{FALSE, NULL, TRUE};
use log::trace;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -191,7 +191,7 @@ fn eval_block_statement(block: &BlockStatement, env: &mut Environment) -> anyhow
trace!("[eval_block_statement] result is ({:?})", result);
match result.clone() {
Object::ReturnValue(value) => {
if value.object_type() == ObjectType::ReturnObj {
if value.object_type() == ObjectType::Return {
return Ok(value.into());
}
}
Expand Down Expand Up @@ -288,7 +288,7 @@ fn eval_bang_operator_expression(right: Object) -> anyhow::Result<Object> {
fn eval_minus_prefix_operator_expression(right: Object) -> anyhow::Result<Object> {
match right {
Object::Integer(value) => Ok(Integer::new(-value.value()).into()),
value if value.object_type() != IntegerObj => Ok(Null.into()),
value if value.object_type() != ObjectType::Integer => Ok(Null.into()),
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -317,9 +317,9 @@ fn eval_index_expression(left: Object, index: Object) -> anyhow::Result<Object>
left,
index
);
if left.object_type() == ArrayObj && index.object_type() == IntegerObj {
if left.object_type() == ObjectType::Array && index.object_type() == ObjectType::Integer {
eval_array_index_expression(left, index)
} else if left.object_type() == HashObj {
} else if left.object_type() == ObjectType::Hash {
eval_hash_index_expression(left, index)
} else {
Err(Error::IndexOperatorNotSupported(left.object_type().to_string()).into())
Expand Down
2 changes: 1 addition & 1 deletion src/object/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl NodeInterface for Array {

impl ObjectInterface for Array {
fn object_type(&self) -> ObjectType {
ObjectType::ArrayObj
ObjectType::Array
}

fn inspect(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/object/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Boolean {

impl ObjectInterface for Boolean {
fn object_type(&self) -> ObjectType {
ObjectType::BooleanObj
ObjectType::Boolean
}

fn inspect(&self) -> String {
Expand Down
14 changes: 7 additions & 7 deletions src/object/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::ast::NodeInterface;
use crate::error::Error;
use crate::object::array::Array;
use crate::object::integer::Integer;
use crate::object::ObjectType::ArrayObj;
use crate::object::{Object, ObjectInterface, ObjectType};
use crate::object::ObjectType;
use crate::object::{Object, ObjectInterface};
use crate::NULL;
use std::fmt::{Display, Formatter};

Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn array_first_element(args: Vec<Object>) -> anyhow::Result<Object> {
.into());
}

if args[0].object_type() != ArrayObj {
if args[0].object_type() != ObjectType::Array {
return Err(Error::ArgumentFirstMustArray {
got: args[0].object_type().to_string(),
}
Expand All @@ -85,7 +85,7 @@ pub fn array_last_element(args: Vec<Object>) -> anyhow::Result<Object> {
.into());
}

if args[0].object_type() != ArrayObj {
if args[0].object_type() != ObjectType::Array {
return Err(Error::ArgumentFirstMustArray {
got: args[0].object_type().to_string(),
}
Expand All @@ -110,7 +110,7 @@ pub fn array_rest_element(args: Vec<Object>) -> anyhow::Result<Object> {
.into());
}

if args[0].object_type() != ArrayObj {
if args[0].object_type() != ObjectType::Array {
return Err(Error::ArgumentFirstMustArray {
got: args[0].object_type().to_string(),
}
Expand All @@ -136,7 +136,7 @@ pub fn array_push_element(args: Vec<Object>) -> anyhow::Result<Object> {
.into());
}

if args[0].object_type() != ArrayObj {
if args[0].object_type() != ObjectType::Array {
return Err(Error::ArgumentFirstMustArray {
got: args[0].object_type().to_string(),
}
Expand All @@ -162,7 +162,7 @@ pub fn puts(args: Vec<Object>) -> anyhow::Result<Object> {

impl ObjectInterface for Builtin {
fn object_type(&self) -> ObjectType {
ObjectType::ArrayObj
ObjectType::Array
}

fn inspect(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/object/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Display for Function {

impl ObjectInterface for Function {
fn object_type(&self) -> ObjectType {
ObjectType::FunctionObj
ObjectType::Function
}

fn inspect(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/object/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Display for Hash {

impl ObjectInterface for Hash {
fn object_type(&self) -> ObjectType {
ObjectType::HashObj
ObjectType::Hash
}

fn inspect(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/object/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Integer {

impl ObjectInterface for Integer {
fn object_type(&self) -> ObjectType {
ObjectType::IntegerObj
ObjectType::Integer
}

fn inspect(&self) -> String {
Expand Down
4 changes: 2 additions & 2 deletions src/object/macro/quote.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ast::Node;
use crate::ast::NodeInterface;
use crate::error::Error;
use crate::object::ObjectType::QueueObj;
use crate::object::ObjectType::Queue;
use crate::object::{Object, ObjectInterface, ObjectType};
use std::fmt::{Display, Formatter};

Expand Down Expand Up @@ -38,7 +38,7 @@ impl NodeInterface for Quote {

impl ObjectInterface for Quote {
fn object_type(&self) -> ObjectType {
QueueObj
Queue
}

fn inspect(&self) -> String {
Expand Down
40 changes: 20 additions & 20 deletions src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@ pub mod string;

#[derive(Debug, PartialEq, Eq)]
pub enum ObjectType {
IntegerObj,
BooleanObj,
NullObj,
ReturnObj,
FunctionObj,
StringObj,
BuiltinObj,
ArrayObj,
HashObj,
QueueObj,
Integer,
Boolean,
Null,
Return,
Function,
String,
Builtin,
Array,
Hash,
Queue,
}

impl Display for ObjectType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::IntegerObj => write!(f, "INTEGER"),
Self::BooleanObj => write!(f, "BOOLEAN"),
Self::NullObj => write!(f, "NULL"),
Self::ReturnObj => write!(f, "RETURN"),
Self::FunctionObj => write!(f, "FUNCTION"),
Self::StringObj => write!(f, "STRING"),
Self::BuiltinObj => write!(f, "BUILTIN"),
Self::ArrayObj => write!(f, "ARRAY"),
Self::HashObj => write!(f, "HASH"),
Self::QueueObj => write!(f, "QUOTE"),
Self::Integer => write!(f, "INTEGER"),
Self::Boolean => write!(f, "BOOLEAN"),
Self::Null => write!(f, "NULL"),
Self::Return => write!(f, "RETURN"),
Self::Function => write!(f, "FUNCTION"),
Self::String => write!(f, "STRING"),
Self::Builtin => write!(f, "BUILTIN"),
Self::Array => write!(f, "ARRAY"),
Self::Hash => write!(f, "HASH"),
Self::Queue => write!(f, "QUOTE"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/object/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl NodeInterface for Null {

impl ObjectInterface for Null {
fn object_type(&self) -> ObjectType {
ObjectType::NullObj
ObjectType::Null
}

fn inspect(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/object/return_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl NodeInterface for ReturnValue {

impl ObjectInterface for ReturnValue {
fn object_type(&self) -> ObjectType {
ObjectType::ReturnObj
ObjectType::Return
}

fn inspect(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/object/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Display for StringObj {

impl ObjectInterface for StringObj {
fn object_type(&self) -> ObjectType {
ObjectType::StringObj
ObjectType::String
}

fn inspect(&self) -> String {
Expand Down

0 comments on commit e607342

Please sign in to comment.