Skip to content

Commit

Permalink
Refactoring evaluator and test files; fix function argument cloning.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed Oct 10, 2023
1 parent 16ef70f commit 0a7bb81
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/evaluator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn apply_function(fn_obj: Object, args: Vec<Object>) -> anyhow::Result<Object> {
let mut extend_env = extend_function_env(fn_value.clone(), args);
trace!("[apply_function] extend_env is {:?}", extend_env);

let evaluated = eval(fn_value.body.into(), &mut extend_env)?;
let evaluated = eval(fn_value.body().clone().into(), &mut extend_env)?;
trace!("[apply_function] call function result is {}", evaluated);

Ok(evaluated)
Expand All @@ -149,7 +149,7 @@ fn eval_hash_literal(node: HashLiteral, env: &mut Environment) -> anyhow::Result
}

fn extend_function_env(fn_obj: Function, args: Vec<Object>) -> Environment {
let mut env = Environment::new_enclosed_environment(fn_obj.env.clone());
let mut env = Environment::new_enclosed_environment(fn_obj.env().clone());
for (param_idx, param) in fn_obj.parameters().iter().enumerate() {
env.store(param.value.clone(), args[param_idx].clone()); // TODO need imporve
}
Expand Down
4 changes: 2 additions & 2 deletions src/evaluator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,8 @@ fn test_function_object() -> anyhow::Result<()> {

let expected_body = "(x + 2);";

if format!("{}", value.body) != expected_body {
eprintln!("body is not {}. got = {}", expected_body, value.body);
if format!("{}", value.body()) != expected_body {
eprintln!("body is not {}. got = {}", expected_body, value.body());
}

Ok(())
Expand Down
11 changes: 7 additions & 4 deletions src/object/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@ use std::fmt::{Display, Formatter};

const BUILD_FUNC: &str = "builtin function";

type BuildBoxFuncType = Box<fn(Vec<Object>) -> anyhow::Result<Object>>;
type BuildFuncType = fn(Vec<Object>) -> anyhow::Result<Object>;

#[derive(Debug, Clone, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct Builtin {
built_in_function: Box<fn(Vec<Object>) -> anyhow::Result<Object>>,
built_in_function: BuildBoxFuncType,
}

impl Builtin {
pub fn new(func: fn(Vec<Object>) -> anyhow::Result<Object>) -> Self {
pub fn new(func: BuildFuncType) -> Self {
Self {
built_in_function: Box::new(func),
}
}

pub fn value(&self) -> &Box<fn(Vec<Object>) -> anyhow::Result<Object>> {
pub fn value(&self) -> &BuildBoxFuncType {
&self.built_in_function
}
}

impl Display for Builtin {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "built_in_function")
write!(f, "{}", BUILD_FUNC)
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/object/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use string_join::Join;
#[derive(Debug, Clone, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct Function {
parameters: Vec<Identifier>,
pub body: BlockStatement,
pub env: Environment,
body: BlockStatement,
env: Environment,
}

impl Function {
Expand All @@ -25,6 +25,14 @@ impl Function {
pub fn parameters(&self) -> &Vec<Identifier> {
&self.parameters
}

pub fn body(&self) -> &BlockStatement {
&self.body
}

pub fn env(&self) -> &Environment {
&self.env
}
}

impl Display for Function {
Expand Down

0 comments on commit 0a7bb81

Please sign in to comment.