Skip to content

Commit

Permalink
user defined functions complete
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser committed Jun 2, 2024
1 parent e5c8167 commit f3ecf04
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/backend/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use crate::{mk_bool, mk_native_fn, mk_null};
use super::native_fn::{native_println, native_time};

#[derive(Debug, Clone, PartialEq)]
pub struct Environment<'a> {
pub struct Environment {
values: HashMap<String, Val>,
is_mutable: HashSet<String>,
parent: Option<&'a Environment<'a>>,
parent: Option<Box<Environment>>,
}

impl<'a> Default for Environment<'a> {
impl Default for Environment {
fn default() -> Self {
Self::new()
}
Expand All @@ -31,7 +31,7 @@ fn setup_env(env: &mut Environment) {
env.declare_var("time", mk_native_fn!(native_time), false);
}

impl<'a> Environment<'a> {
impl Environment {
pub fn new() -> Self {
let mut env = Environment {
values: HashMap::new(),
Expand All @@ -42,20 +42,20 @@ impl<'a> Environment<'a> {
env
}

pub fn new_with_parent(parent: &'a Environment<'a>) -> Self {
pub fn new_with_parent(parent: Environment) -> Self {
Environment {
values: HashMap::new(),
is_mutable: HashSet::new(),
parent: Some(parent),
parent: Some(Box::new(parent)),
}
}

pub fn resolve(&'a self, varname: &str) -> Result<&'a Environment<'a>, String> {
pub fn resolve(&self, varname: &str) -> Result<&Environment, String> {
if self.values.contains_key(varname) {
return Ok(self);
}

if let Some(parent) = self.parent {
if let Some(parent) = &self.parent {
return parent.resolve(varname);
}

Expand Down
2 changes: 1 addition & 1 deletion src/backend/eval/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn evaluate_call_expr(args: Vec<Expr>, caller: Expr, env: &mut Environment)
match &function {
Val::NativeFunction(callable) => return (callable.call)(evaluated_args, env),
Val::Function(fn_value) => {
let mut scope = Environment::new_with_parent(env);
let mut scope = Environment::new_with_parent(fn_value.declaration_env.to_owned());
println!("{:#?}", scope);
for (i, _) in evaluated_args
.iter()
Expand Down
1 change: 1 addition & 0 deletions src/backend/eval/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub fn evaluate_declare_fn(
parameters,
body,
is_async,
declaration_env: env.to_owned(),
});

env.declare_var(&name, function, false)
Expand Down
1 change: 1 addition & 0 deletions src/backend/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub struct FunctionVal {
pub parameters: Vec<String>,
pub body: Vec<Stmt>,
pub is_async: bool,
pub declaration_env: Environment,
}

impl RuntimeVal for FunctionVal {
Expand Down

0 comments on commit f3ecf04

Please sign in to comment.