bbc-basic: Slight tweak to heap size.
[jackhill/mal.git] / rust / types.rs
1 use std::rc::Rc;
2 use std::cell::RefCell;
3 //use std::collections::HashMap;
4 use fnv::FnvHashMap;
5 use itertools::Itertools;
6
7 use types::MalErr::{ErrString,ErrMalVal};
8 use types::MalVal::{Nil,Bool,Int,Str,Sym,List,Vector,Hash,Func,MalFunc,Atom};
9 use env::{Env,env_bind};
10
11 #[derive(Debug, Clone)]
12 pub enum MalVal {
13 Nil,
14 Bool(bool),
15 Int(i64),
16 //Float(f64),
17 Str(String),
18 Sym(String),
19 List(Rc<Vec<MalVal>>, Rc<MalVal>),
20 Vector(Rc<Vec<MalVal>>, Rc<MalVal>),
21 Hash(Rc<FnvHashMap<String, MalVal>>, Rc<MalVal>),
22 Func(fn(MalArgs) -> MalRet, Rc<MalVal>),
23 MalFunc {
24 eval: fn(ast: MalVal, env: Env) -> MalRet,
25 ast: Rc<MalVal>,
26 env: Env,
27 params: Rc<MalVal>,
28 is_macro: bool,
29 meta: Rc<MalVal>,
30 },
31 Atom(Rc<RefCell<MalVal>>),
32 }
33
34 #[derive(Debug)]
35 pub enum MalErr {
36 ErrString(String),
37 ErrMalVal(MalVal),
38 }
39
40 pub type MalArgs = Vec<MalVal>;
41 pub type MalRet = Result<MalVal,MalErr>;
42
43 // type utility macros
44
45 macro_rules! list {
46 ($seq:expr) => {{
47 List(Rc::new($seq),Rc::new(Nil))
48 }};
49 [$($args:expr),*] => {{
50 let v: Vec<MalVal> = vec![$($args),*];
51 List(Rc::new(v),Rc::new(Nil))
52 }}
53 }
54
55 macro_rules! vector {
56 ($seq:expr) => {{
57 Vector(Rc::new($seq),Rc::new(Nil))
58 }};
59 [$($args:expr),*] => {{
60 let v: Vec<MalVal> = vec![$($args),*];
61 Vector(Rc::new(v),Rc::new(Nil))
62 }}
63 }
64
65 // type utility functions
66
67 pub fn error(s: &str) -> MalRet {
68 Err(ErrString(s.to_string()))
69 }
70
71 pub fn format_error(e: MalErr) -> String {
72 match e {
73 ErrString(s) => s.clone(),
74 ErrMalVal(mv) => mv.pr_str(true),
75 }
76 }
77
78 pub fn atom(mv: &MalVal) -> MalVal {
79 Atom(Rc::new(RefCell::new(mv.clone())))
80 }
81
82 impl MalVal {
83 pub fn keyword(&self) -> MalRet {
84 match self {
85 Str(s) if s.starts_with("\u{29e}") => Ok(Str(s.to_string())),
86 Str(s) => Ok(Str(format!("\u{29e}{}", s))),
87 _ => error("invalid type for keyword"),
88 }
89 }
90
91 pub fn empty_q(&self) -> MalRet {
92 match self {
93 List(l,_) | Vector(l,_) => Ok(Bool(l.len() == 0)),
94 Nil => Ok(Bool(true)),
95 _ => error("invalid type for empty?"),
96 }
97 }
98
99 pub fn count(&self) -> MalRet {
100 match self {
101 List(l,_) | Vector(l,_) => Ok(Int(l.len() as i64)),
102 Nil => Ok(Int(0)),
103 _ => error("invalid type for count"),
104 }
105 }
106
107 pub fn apply(&self, args: MalArgs) -> MalRet {
108 match *self {
109 Func(f,_) => f(args),
110 MalFunc{eval, ref ast, ref env, ref params, ..} => {
111 let a = &**ast;
112 let p = &**params;
113 let fn_env = env_bind(Some(env.clone()), p.clone(), args)?;
114 Ok(eval(a.clone(), fn_env)?)
115 }
116 _ => error("attempt to call non-function"),
117 }
118 }
119
120 pub fn keyword_q(&self) -> bool {
121 match self {
122 Str(s) if s.starts_with("\u{29e}") => true,
123 _ => false,
124 }
125 }
126
127 pub fn deref(&self) -> MalRet {
128 match self {
129 Atom(a) => Ok(a.borrow().clone()),
130 _ => error("attempt to deref a non-Atom"),
131 }
132 }
133
134 pub fn reset_bang(&self, new: &MalVal) -> MalRet {
135 match self {
136 Atom(a) => {
137 *a.borrow_mut() = new.clone();
138 Ok(new.clone())
139 },
140 _ => error("attempt to reset! a non-Atom"),
141 }
142 }
143
144 pub fn swap_bang(&self, args: &MalArgs) -> MalRet {
145 match self {
146 Atom(a) => {
147 let f = &args[0];
148 let mut fargs = args[1..].to_vec();
149 fargs.insert(0, a.borrow().clone());
150 *a.borrow_mut() = f.apply(fargs)?;
151 Ok(a.borrow().clone())
152 },
153 _ => error("attempt to swap! a non-Atom"),
154 }
155 }
156
157 pub fn get_meta(&self) -> MalRet {
158 match self {
159 List(_,meta) | Vector(_,meta) | Hash(_,meta) => Ok((&**meta).clone()),
160 Func(_,meta) => Ok((&**meta).clone()),
161 MalFunc{meta,..} => Ok((&**meta).clone()),
162 _ => error("meta not supported by type"),
163 }
164 }
165
166 pub fn with_meta(&mut self, new_meta: &MalVal) -> MalRet {
167 match self {
168 List(_, ref mut meta) |
169 Vector(_, ref mut meta) |
170 Hash(_, ref mut meta) |
171 Func(_,ref mut meta) |
172 MalFunc{ref mut meta, ..} => {
173 *meta = Rc::new((&*new_meta).clone());
174 },
175 _ => return error("with-meta not supported by type"),
176 };
177 Ok(self.clone())
178 }
179 }
180
181 impl PartialEq for MalVal {
182 fn eq(&self, other: &MalVal) -> bool {
183 match (self, other) {
184 (Nil,Nil) => true,
185 (Bool(ref a),Bool(ref b)) => a == b,
186 (Int(ref a),Int(ref b)) => a == b,
187 (Str(ref a),Str(ref b)) => a == b,
188 (Sym(ref a),Sym(ref b)) => a == b,
189 (List(ref a,_),List(ref b,_)) |
190 (Vector(ref a,_),Vector(ref b,_)) |
191 (List(ref a,_),Vector(ref b,_)) |
192 (Vector(ref a,_),List(ref b,_)) => a == b,
193 (Hash(ref a,_),Hash(ref b,_)) => a == b,
194 (MalFunc{..}, MalFunc{..}) => false,
195 _ => false,
196 }
197 }
198 }
199
200 pub fn func(f: fn(MalArgs) -> MalRet) -> MalVal {
201 Func(f, Rc::new(Nil))
202 }
203
204 pub fn _assoc(mut hm: FnvHashMap<String,MalVal>, kvs: MalArgs) -> MalRet {
205 if kvs.len() % 2 != 0 {
206 return error("odd number of elements")
207 }
208 for (k, v) in kvs.iter().tuples() {
209 match k {
210 Str(s) => { hm.insert(s.to_string(), v.clone()); },
211 _ => { return error("key is not string") },
212 }
213 }
214 Ok(Hash(Rc::new(hm),Rc::new(Nil)))
215 }
216
217 pub fn _dissoc(mut hm: FnvHashMap<String,MalVal>, ks: MalArgs) -> MalRet {
218 for k in ks.iter() {
219 match k {
220 Str(ref s) => { hm.remove(s); },
221 _ => { return error("key is not string") },
222 }
223 }
224 Ok(Hash(Rc::new(hm),Rc::new(Nil)))
225 }
226
227 pub fn hash_map(kvs: MalArgs) -> MalRet {
228 let hm: FnvHashMap<String,MalVal> = FnvHashMap::default();
229 _assoc(hm, kvs)
230 }
231
232 // vim: ts=2:sw=2:expandtab