Skip to content

Commit

Permalink
drop and forget native fns added
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser committed Jun 13, 2024
1 parent d461679 commit cedc829
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/backend/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::rc::Rc;
use crate::backend::values::{BoolVal, NativeFunctionVal, NullVal, Val};
use crate::{mk_bool, mk_native_fn, mk_null};

use super::native_fn::{native_println, native_time};
use super::native_fn::{native_drop, native_forget, native_println, native_time};

#[derive(Debug, Clone, PartialEq)]
pub struct Environment {
Expand All @@ -31,6 +31,8 @@ fn setup_env(env: &mut Environment) {
// Define a native built-in method
env.declare_var("println", mk_native_fn!(native_println), false);
env.declare_var("time", mk_native_fn!(native_time), false);
env.declare_var("forget", mk_native_fn!(native_forget), false);
env.declare_var("drop", mk_native_fn!(native_drop), false);
}

impl Environment {
Expand Down Expand Up @@ -98,6 +100,16 @@ impl Environment {
value
}

pub fn drop_var(&mut self, name: &str) -> Val {
let env = self.resolve(name).unwrap_or_else(|_| {
println!("Cannot resolve {} as it does not exist.", name);
process::exit(1);
});

let x = env.borrow_mut().values.remove(name).unwrap();
x
}

pub fn lookup_var(&self, name: &str) -> Val {
let env = self.resolve(name).unwrap_or_else(|_| {
println!("Cannot resolve {} as it does not exist.", name);
Expand Down
3 changes: 2 additions & 1 deletion src/backend/eval/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,13 @@ pub fn evaluate_object_expr(obj: Vec<Property>, env: &Rc<RefCell<Environment>>)

pub fn evaluate_call_expr(args: Vec<Expr>, caller: Expr, env: &Rc<RefCell<Environment>>) -> Val {
let evaluated_args: Vec<Val> = args
.clone()
.into_iter()
.map(|expr| evaluate_expr(expr, env))
.collect();
let function = evaluate_expr(caller, env);
match &function {
Val::NativeFunction(callable) => (callable.call)(evaluated_args, env),
Val::NativeFunction(callable) => (callable.call)(evaluated_args, env, args),
Val::Function(fn_value) => {
let scope = Rc::new(RefCell::new(Environment::new_with_parent(
fn_value.declaration_env.clone(),
Expand Down
30 changes: 27 additions & 3 deletions src/backend/native_fn.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
use std::{cell::RefCell, rc::Rc, time::SystemTime};

use crate::{mk_integer, mk_null};
use crate::{frontend::ast::Expr, mk_integer, mk_null};

use super::{environment::Environment, values::Val};
use crate::backend::values::{IntegerVal, NullVal};

pub fn native_println(args: Vec<Val>, _env: &Rc<RefCell<Environment>>) -> Val {
pub fn native_println(
args: Vec<Val>,
_env: &Rc<RefCell<Environment>>,
_raw_exprs: Vec<Expr>,
) -> Val {
for arg in args {
println!("{:?}", arg); // for now, just use debug print.
// TODO: Add actual std::fmt::Display impl for Val enums.
}
mk_null!()
}

pub fn native_time(_args: Vec<Val>, _env: &Rc<RefCell<Environment>>) -> Val {
pub fn native_time(_args: Vec<Val>, _env: &Rc<RefCell<Environment>>, _raw_exprs: Vec<Expr>) -> Val {
let time = match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => n.as_secs() as i64,
Err(e) => panic!("SystemTime before UNIX EPOCH! {}", e),
};
mk_integer!(time)
}

pub fn native_forget(
args: Vec<Val>,
_env: &Rc<RefCell<Environment>>,
_raw_exprs: Vec<Expr>,
) -> Val {
for arg in args {
std::mem::forget(arg)
}
mk_null!()
}

pub fn native_drop(_args: Vec<Val>, env: &Rc<RefCell<Environment>>, raw_exprs: Vec<Expr>) -> Val {
for raw_expr in raw_exprs {
if let Expr::Identifier(ident) = raw_expr {
env.borrow_mut().drop_var(&ident);
}
}
mk_null!()
}
4 changes: 2 additions & 2 deletions src/backend/values.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cell::RefCell, collections::HashMap, rc::Rc};

use crate::frontend::ast::Stmt;
use crate::frontend::ast::{Expr, Stmt};

use super::environment::Environment;

Expand Down Expand Up @@ -83,7 +83,7 @@ impl RuntimeVal for ObjectVal {
}
}

type NativeFunctionCallback = fn(Vec<Val>, &Rc<RefCell<Environment>>) -> Val;
type NativeFunctionCallback = fn(Vec<Val>, &Rc<RefCell<Environment>>, Vec<Expr>) -> Val;

#[derive(Debug, PartialEq, Clone)]
pub struct NativeFunctionVal {
Expand Down

0 comments on commit cedc829

Please sign in to comment.