Skip to content

Commit

Permalink
functions fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser committed Jun 14, 2024
1 parent 67b004d commit 3b531ea
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/backend/eval/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ pub fn evaluate_call_expr(
.map(|expr| evaluate_expr(expr, env, root_env))
.collect();
let scope = Rc::new(RefCell::new(Environment::new_with_parent(root_env.clone())));
scope
.borrow_mut()
.declare_var(&fn_value.name, Val::Function(fn_value.clone()), false);
for (varname, arg) in fn_value.parameters.iter().zip(evaluated_args.iter()) {
scope.borrow_mut().declare_var(varname, arg.clone(), false);
}
Expand Down
4 changes: 2 additions & 2 deletions src/backend/eval/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ pub fn evaluate_declare_fn(
is_async: bool,
env: &Rc<RefCell<Environment>>,
) -> Val {
let function: Val = Val::Function(FunctionVal {
let function: Val = Val::Function(Rc::new(FunctionVal {
name: name.clone(),
parameters,
body,
is_async,
});
}));

env.borrow_mut().declare_var(&name, function, false)
}
Expand Down
7 changes: 5 additions & 2 deletions src/backend/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub enum Val {
Bool(BoolVal),
Object(ObjectVal),
NativeFunction(NativeFunctionVal),
Function(FunctionVal),
Function(Rc<FunctionVal>),
Special(SpecialVal),
}

Expand Down Expand Up @@ -196,7 +196,10 @@ impl std::fmt::Display for Val {
Val::Bool(BoolVal { value }) => write!(f, "{}", value),
Val::Object(ObjectVal { properties }) => write!(f, "{:?}", properties),
Val::NativeFunction(NativeFunctionVal { call }) => write!(f, "fn:{:?}", call),
Val::Function(FunctionVal { name, .. }) => write!(f, "fn:{}", name),
Val::Function(ref fn_val) => {
let function_val = fn_val.as_ref();
write!(f, "fn:{}", function_val.name)
}
Val::Special(SpecialVal { keyword, .. }) => write!(f, "{:?}", keyword),
}
}
Expand Down

0 comments on commit 3b531ea

Please sign in to comment.