Skip to content

Commit 37312c8

Browse files
committed
Merge branch 'refactor/give_invoke_rc_values'
2 parents 21ac5ce + 88da3b7 commit 37312c8

File tree

4 files changed

+31
-42
lines changed

4 files changed

+31
-42
lines changed

src/ifn.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::value::Value;
1515
use dyn_clone::DynClone;
1616

1717
use std::fmt::Debug;
18+
use std::rc::Rc;
1819

1920
//
2021
// Based on: clojure.lang.IFn
@@ -36,6 +37,6 @@ use std::fmt::Debug;
3637
//
3738

3839
pub trait IFn: Debug + DynClone {
39-
fn invoke(&self, args: Vec<&Value>) -> Value;
40+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value;
4041
}
4142
dyn_clone::clone_trait_object!(IFn);

src/lambda.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl ToValue for Fn {
1818
}
1919
}
2020
impl IFn for Fn {
21-
fn invoke(&self, args: Vec<&Value>) -> Value {
21+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
2222
let local_environment = Rc::new(Environment::new_local_environment(Rc::clone(
2323
&self.enclosing_environment,
2424
)));

src/rust_core.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl ToValue for StrFn {
3030
}
3131
}
3232
impl IFn for StrFn {
33-
fn invoke(&self, args: Vec<&Value>) -> Value {
33+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
3434
Value::String(
3535
args.into_iter()
3636
.map(|arg| arg.to_string())
@@ -48,7 +48,7 @@ impl ToValue for StringPrintFn {
4848
}
4949
}
5050
impl IFn for StringPrintFn {
51-
fn invoke(&self, args: Vec<&Value>) -> Value {
51+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
5252
Value::String(
5353
args.into_iter()
5454
.map(|arg| arg.to_string())
@@ -66,9 +66,9 @@ impl ToValue for AddFn {
6666
}
6767
}
6868
impl IFn for AddFn {
69-
fn invoke(&self, args: Vec<&Value>) -> Value {
69+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
7070
args.into_iter().fold(0_i32.to_value(), |a, b| match a {
71-
Value::I32(a_) => match b {
71+
Value::I32(a_) => match *b {
7272
Value::I32(b_) => Value::I32(a_ + b_),
7373
_ => Value::Condition(format!(
7474
"Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}",
@@ -100,7 +100,7 @@ impl ToValue for EvalFn {
100100
}
101101
}
102102
impl IFn for EvalFn {
103-
fn invoke(&self, args: Vec<&Value>) -> Value {
103+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
104104
// @TODO generalize arity exceptions, and other exceptions
105105
if args.len() != 1 {
106106
return Value::Condition(format!(
@@ -122,12 +122,12 @@ impl ToValue for DoFn {
122122
}
123123
}
124124
impl IFn for DoFn {
125-
fn invoke(&self, args: Vec<&Value>) -> Value {
125+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
126126
// @TODO generalize arity exceptions, and other exceptions
127127
if args.is_empty() {
128128
return Value::Nil;
129129
}
130-
(*args.last().unwrap()).clone()
130+
(**args.last().unwrap()).clone()
131131
}
132132
}
133133

@@ -145,7 +145,7 @@ impl ToValue for DoMacro {
145145
}
146146
}
147147
impl IFn for DoMacro {
148-
fn invoke(&self, args: Vec<&Value>) -> Value {
148+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
149149
// @TODO generalize arity exceptions, and other exceptions
150150
if args.is_empty() {
151151
return vec![Symbol::intern("do").to_rc_value(), Rc::new(Value::Nil)]
@@ -173,7 +173,7 @@ impl ToValue for NthFn {
173173
}
174174
}
175175
impl IFn for NthFn {
176-
fn invoke(&self, args: Vec<&Value>) -> Value {
176+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
177177
// @TODO generalize arity exceptions, and other exceptions
178178
if args.len() != 2 {
179179
return Value::Condition(format!(
@@ -183,13 +183,13 @@ impl IFn for NthFn {
183183
}
184184
// @TODO change iteration to work with Value references, or even change invoke to work on Rc<..>
185185
// as we do everything else; surely we don't want to clone just to read from a collection
186-
if let Some(Value::I32(ind)) = args.get(1) {
187-
if *ind < 0 {
186+
if let Value::I32(ind) = **args.get(1).unwrap() {
187+
if ind < 0 {
188188
return Value::Condition(format!("Index cannot be negative; Index ({})", ind));
189189
}
190-
let ind = *ind as usize;
190+
let ind = ind as usize;
191191

192-
match args.get(0).unwrap() {
192+
match &**args.get(0).unwrap() {
193193
Value::PersistentList(Cons(head, tail, count)) => {
194194
let count = *count as usize;
195195
if ind >= count {
@@ -240,9 +240,9 @@ impl ToValue for ConcatFn {
240240
}
241241
}
242242
impl IFn for ConcatFn {
243-
fn invoke(&self, args: Vec<&Value>) -> Value {
244-
let concatted_vec = args.iter().fold(vec![], |mut sum, coll| {
245-
let coll_vec = match coll {
243+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
244+
let concatted_vec = args.iter().fold(Vec::new(), |mut sum, coll| {
245+
let coll_vec = match &**coll {
246246
Value::PersistentList(plist) => {
247247
Rc::new(plist.clone()).iter().collect::<Vec<Rc<Value>>>()
248248
}
@@ -269,7 +269,7 @@ impl ToValue for PrintStringFn {
269269
}
270270
}
271271
impl IFn for PrintStringFn {
272-
fn invoke(&self, args: Vec<&Value>) -> Value {
272+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
273273
if args.len() != 1 {
274274
return Value::Condition(format!(
275275
"Wrong number of arguments given to function (Given: {}, Expected: {})",
@@ -291,7 +291,7 @@ impl ToValue for AssocFn {
291291
}
292292
}
293293
impl IFn for AssocFn {
294-
fn invoke(&self, args: Vec<&Value>) -> Value {
294+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
295295
// We don't want even args, because assoc works like
296296
// (assoc {} :a 1) ;; 3 args
297297
// (assoc {} :a 1 :b 2) ;; 5 args
@@ -303,12 +303,12 @@ impl IFn for AssocFn {
303303
));
304304
}
305305

306-
if let Value::PersistentListMap(pmap) = args.get(0).unwrap() {
306+
if let Value::PersistentListMap(pmap) = &*(args.get(0).unwrap().clone()) {
307307
let mut retval = pmap.clone();
308308
for (key_value, val_value) in args.into_iter().skip(1).tuples() {
309309
let key = key_value.to_rc_value();
310310
let val = val_value.to_rc_value();
311-
println!("key: {:?}, val: {:?}",key,val);
311+
println!("key: {:?}, val: {:?}", key, val);
312312
retval = pmap.assoc(key, val);
313313
}
314314
return Value::PersistentListMap(retval);

src/value.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,12 @@ impl Value {
293293
match self {
294294
Value::IFn(ifn) => {
295295
// Eval arguments
296-
let evaled_arg_values = PersistentList::iter(args)
297-
.map(|rc_arg| rc_arg.eval(Rc::clone(environment)))
298-
.collect::<Vec<Value>>();
299-
// Collect references for invoke
300-
let evaled_args_refs = evaled_arg_values
301-
.iter()
302-
.map(|arg| arg)
303-
.collect::<Vec<&Value>>();
296+
let evaled_arg_refs = PersistentList::iter(args)
297+
.map(|rc_arg| rc_arg.eval_to_rc(Rc::clone(environment)))
298+
.collect::<Vec<Rc<Value>>>();
299+
304300
// Invoke fn on arguments
305-
Some(Rc::new(ifn.invoke(evaled_args_refs)))
301+
Some(Rc::new(ifn.invoke(evaled_arg_refs)))
306302
}
307303
LexicalEvalFn => {
308304
if args.len() != 1 {
@@ -313,8 +309,8 @@ impl Value {
313309
}
314310
// This should only be one value
315311
let evaled_arg_values = PersistentList::iter(args)
316-
.map(|rc_arg| rc_arg.eval(Rc::clone(environment)))
317-
.collect::<Vec<Value>>();
312+
.map(|rc_arg| rc_arg.eval_to_rc(Rc::clone(environment)))
313+
.collect::<Vec<Rc<Value>>>();
318314

319315
let evaled_arg = evaled_arg_values.get(0).unwrap();
320316

@@ -329,15 +325,7 @@ impl Value {
329325
// that's never interested me all that much
330326
//
331327
Value::Macro(ifn) => {
332-
// Copy the args of the form into a new vector; these new values
333-
// will be used by the macro to create the new expanded form
334-
// (@TODO just reference the original args of the list if you can,
335-
// since they're not mutated)
336-
let arg_values = PersistentList::iter(args)
337-
.map(|rc_arg| (*rc_arg).clone())
338-
.collect::<Vec<Value>>();
339-
340-
let arg_refs = arg_values.iter().map(|arg| arg).collect::<Vec<&Value>>();
328+
let arg_refs = PersistentList::iter(args).collect::<Vec<Rc<Value>>>();
341329

342330
let macroexpansion = Rc::new(ifn.invoke(arg_refs));
343331

0 commit comments

Comments
 (0)