Skip to content

Commit 7ee5df2

Browse files
committed
Add ns macro to change namespace from within clojurers
1 parent 9cf5d65 commit 7ee5df2

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/rust_core.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub use self::_divide_::*;
2727

2828
pub(crate) mod _multiply_;
2929
pub use self::_multiply_::*;
30+
31+
pub(crate) mod ns;
32+
pub use self::ns::*;
33+
3034
//
3135
// This module will hold core function and macro primitives that aren't special cases
3236
// (like the quote macro, or let), and can't be implemented in clojure itself
@@ -113,7 +117,7 @@ impl EvalFn {
113117
EvalFn {
114118
enclosing_environment,
115119
}
116-
}
120+
}
117121
}
118122
impl ToValue for EvalFn {
119123
fn to_value(&self) -> Value {

src/rust_core/ns.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::ifn::IFn;
2+
use crate::value::{ToValue, Value};
3+
use crate::type_tag::TypeTag;
4+
use crate::environment::Environment;
5+
use std::rc::Rc;
6+
7+
use crate::error_message;
8+
9+
#[derive(Debug, Clone)]
10+
pub struct NsMacro {
11+
enclosing_environment: Rc<Environment>
12+
}
13+
impl NsMacro {
14+
pub fn new(enclosing_environment: Rc<Environment>) -> NsMacro {
15+
NsMacro {
16+
enclosing_environment,
17+
}
18+
}
19+
}
20+
impl ToValue for NsMacro {
21+
fn to_value(&self) -> Value {
22+
Value::Macro(Rc::new(self.clone()))
23+
}
24+
}
25+
impl IFn for NsMacro {
26+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
27+
if args.len() != 1 {
28+
return error_message::wrong_arg_count(1,args.len());
29+
}
30+
31+
let namespace = args.get(0).unwrap();
32+
match &**namespace {
33+
Value::Symbol(sym) => {
34+
self.enclosing_environment.change_namespace(sym.clone());
35+
Value::Nil
36+
},
37+
_ => error_message::type_mismatch(TypeTag::Symbol,&**namespace)
38+
}
39+
40+
}
41+
}

0 commit comments

Comments
 (0)