-
Notifications
You must be signed in to change notification settings - Fork 0
/
rust_pr7rs.rs
1108 lines (1034 loc) · 40.9 KB
/
rust_pr7rs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2021 Joshua J. Cogliati
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
extern crate core;
use core::fmt;
use std::io;
use std::io::Write;
use std::collections::HashMap;
use std::rc::Rc;
use std::env;
use std::fs;
enum Token{
IntegerToken(i64),
StringToken(String),
TokenList(Vec<Token>),
QuoteToken(Box<Token>),
Dot,
}
impl<'a> fmt::Debug for Token {
fn fmt(&self, f: &mut core::fmt::Formatter) -> fmt::Result
{
let mut result = Ok(());
match self {
&Token::IntegerToken(value) =>
result = result.and(write!(f, "{}", value)),
&Token::StringToken(ref value) =>
result = result.and(write!(f, "{}", value)),
&Token::TokenList(ref list) =>
result = result.and(write!(f, "{:?}", list)),
&Token::QuoteToken(ref value) =>
result = result.and(write!(f, "'{:?}", value)),
&Token::Dot =>
result = result.and(write!(f, " . "))
}
result
}
}
impl Token {
fn str(s: &str) -> Token
{
Token::StringToken(String::from(s))
}
}
impl Clone for Token {
fn clone(&self) -> Token {
match self {
&Token::IntegerToken(value) => Token::IntegerToken(value),
&Token::StringToken(ref value) => Token::StringToken(value.clone()),
&Token::TokenList(ref list) => Token::TokenList(list.clone()),
&Token::QuoteToken(ref value) => Token::QuoteToken(value.clone()),
&Token::Dot => Token::Dot
}
}
}
fn tokenize(chars: &str) -> Vec<&str> {
let mut tokens = vec![];
let mut buffer_start = 0;
let mut buffer_len: usize = 0;
let mut in_comment = false;
fn switch_token<'a>(ctokens:&mut Vec<&'a str>,cchars:&'a str,
buffer_start:usize,buffer_len:&mut usize){
if *buffer_len > 0
{ (*ctokens).push(&cchars[buffer_start..
buffer_start+*buffer_len]);
*buffer_len = 0;}
}
for (ind,l) in chars.char_indices() {
match l {
'\n' => {
switch_token(&mut tokens,chars,buffer_start,&mut buffer_len);
in_comment = false;
},
_ if in_comment => buffer_len = 0,
' ' | '\t' =>
switch_token(&mut tokens,chars,buffer_start,&mut buffer_len),
'(' =>
{switch_token(&mut tokens,chars,buffer_start,&mut buffer_len);
tokens.push("(")
},
')' =>
{switch_token(&mut tokens,chars,buffer_start,&mut buffer_len);
tokens.push(")")},
'\'' =>
tokens.push("\'"),
';' => in_comment = true,
_ => {if buffer_len == 0
{ buffer_start = ind }
buffer_len += 1}
}
}
switch_token(&mut tokens,chars, buffer_start,&mut buffer_len);
return tokens;
}
fn is_number(word: &str) -> bool
{
let mut chars = word.chars();
match chars.next() {
Some('0' ..= '9') => return true,
Some('+' | '-') => (),
_ => return false
}
match chars.next() {
Some('0' ..= '9') => return true,
_ => return false
}
}
fn atom(token: &str) -> Token {
if is_number(token) {
let int_result = token.parse::<i64>();
match int_result {
Ok(value) => return Token::IntegerToken(value),
_ => return Token::StringToken(format!("invalid_number_{}",token).clone()),
};
}
if token == "." {
return Token::Dot
}
Token::str(token)
}
enum ParseOption {
None,
Partial,
Some(Token),
}
fn read_from_tokens<'a>(tokens: &Vec<&'a str>, index: &mut usize) -> ParseOption {
if tokens.len() == 0 {
//println!("Unexpected end of program");
return ParseOption::None
};
//let mut index: usize = 0;
let first = tokens[*index];
//println!("first {}",first);
if first == "(" {
let mut token_list = vec![];
loop {
*index += 1;
if *index >= tokens.len() {
//No closing ), so probably need to read more input
return ParseOption::Partial
}
if tokens[*index] == ")" {break};
let result = read_from_tokens(tokens,index);
//println!("result {}",result);
match result {
ParseOption::Some(value) =>
token_list.push(value),
ParseOption::None => {println!("Failed above");
return ParseOption::None;},
ParseOption::Partial => {
return ParseOption::Partial;
}
}
}
//*index += 1; //Remove ")"
return ParseOption::Some(Token::TokenList(token_list));
} else if first == "'" {
*index += 1;
let result = read_from_tokens(tokens, index);
match result {
ParseOption::Some(value) => {
return ParseOption::Some(Token::QuoteToken(Box::new(value)));
}
ParseOption::None => {println!("Failed after quote");
return ParseOption::None;
}
ParseOption::Partial => {
return ParseOption::Partial;
}
}
} else if first == ")" {
println!("Syntax error unexpected )");
return ParseOption::None; }
else {
return ParseOption::Some(atom(first));
}
}
fn parse(program: &str) -> ParseOption {
let mut index = 0;
read_from_tokens(&tokenize(program), &mut index)
}
enum Value {
Integer(i64),
Boolean(bool),
Symbol(String),
Pair(Rc<Value>, Rc<Value>),
Function(fn(Vec<Rc<Value>>) -> Rc<Value>),
//RefFunction(fn(Vec<Value>) -> Value),
Undefined,
EmptyList,
Procedure(Procedure),
Partial(Rc<Token>, Rc<Env>), //Needs to be evaluated
Error(String)
}
impl<'a> fmt::Debug for Value {
fn fmt(&self, f: &mut core::fmt::Formatter) -> fmt::Result
{
let mut result = Ok(());
match self {
&Value::Integer(value) =>
result = result.and(write!(f, "{}", value)),
&Value::Boolean(true) =>
result = result.and(write!(f, "#t")),
&Value::Boolean(false) =>
result = result.and(write!(f, "#f")),
&Value::Symbol(ref value) =>
result = result.and(write!(f, "{}", value)),
&Value::Pair(ref car, ref cdr) =>
{
result = result.and(write!(f, "({:?}", car));
let mut cur_cdr = cdr;
let mut done = false;
while !done {
if let Value::Pair(ref cadr, ref cddr) = **cur_cdr {
result = result.and(write!(f, " {:?}", cadr));
cur_cdr = cddr
}
else if let Value::EmptyList = **cur_cdr {
result = result.and(write!(f, ")"));
done = true
} else {
result = result.and(write!(f, " . {:?})", cur_cdr));
done = true
}
}
},
&Value::Function(_) =>
result = result.and(write!(f, "<function>")),
&Value::Procedure(_) =>
result = result.and(write!(f, "<procedure>")),
/* &Value::RefFunction(_) =>
result = result.and(write!(f, "show not implemented for ref function")),*/
&Value::EmptyList => result = result.and(write!(f, "()")),
&Value::Undefined =>
result = result.and(write!(f, "Undefined")),
&Value::Partial(_, _) =>
result = result.and(write!(f, "<partial>")),
&Value::Error(ref value) =>
result = result.and(write!(f, "Error({})", value))
}
result
}
}
type REnv = Rc<Env>;
type PartialEnv = HashMap<String, Rc<Value>>;
struct Env {
inner: PartialEnv,
outer_env: Option<REnv>,
}
impl<'a> fmt::Debug for Env {
fn fmt(&self, f: &mut core::fmt::Formatter) -> fmt::Result
{
let mut result = Ok(());
result = result.and(write!(f, "inner: {:?} outer: {:?}", self.inner, self.outer_env));
result
}
}
impl Env {
fn new(hashmap: PartialEnv) -> Env {
Env {
inner: hashmap,
outer_env: None
}
}
fn new_sub(hashmap: PartialEnv, outer: Rc<Env>) -> Env {
Env {
inner: hashmap,
outer_env : Some(outer)
}
}
fn find(&self, key: &String) -> Option<Rc<Value>> {
let in_current = self.inner.get(key);
match in_current {
Some(_) => in_current.cloned(),
None => {
match self.outer_env {
Some(ref outer) => outer.find(key),
None => None
}
}
}
}
}
struct Procedure {
params: Vec<String>,
fixed_num: bool, //If true put all parameters list and put in one var
body: Rc<Token>,
declarations: Vec<Rc<Token>>,
outer_env: REnv
}
impl Procedure {
fn new(params: Vec<String>, body: Rc<Token>, outer_env: REnv, fixed_num: bool, declarations: Vec<Rc<Token>>) -> Procedure {
Procedure {
params: params,
fixed_num: fixed_num,
body: body,
declarations: declarations,
outer_env: outer_env
}
}
fn new_no_dec(params: Vec<String>, body: Rc<Token>, outer_env: REnv) -> Procedure {
Procedure {
params: params,
fixed_num: false,
body: body,
declarations: vec![],
outer_env: outer_env
}
}
fn new_with_dec(params: Vec<String>, body: Rc<Token>, declarations: Vec<Rc<Token>>, outer_env: REnv) -> Procedure {
Procedure {
params: params,
fixed_num: false,
body: body,
declarations: declarations,
outer_env: outer_env
}
}
fn new_single(param: String, body: Rc<Token>, outer_env: REnv) -> Procedure {
Procedure {
params: vec![param],
fixed_num: true,
body: body,
declarations: vec![],
outer_env: outer_env
}
}
fn new_single_with_dec(param: String, body: Rc<Token>, declarations: Vec<Rc<Token>>, outer_env: REnv) -> Procedure {
Procedure {
params: vec![param],
fixed_num: true,
body: body,
declarations: declarations,
outer_env: outer_env
}
}
fn call_partial(& self,list: Vec<Rc<Value>>) -> Rc<Value> {
if !self.fixed_num && self.params.len() != list.len() {
Rc::new(Value::Error(String::from("parameter length mismatch")))
} else
{
let mut new_env : PartialEnv = PartialEnv::new();
if self.fixed_num {
new_env.insert(self.params[0].clone(), Rc::clone(&make_list(list)));
} else {
for i in 0..self.params.len() {
new_env.insert(self.params[i].clone(),Rc::clone(&list[i]));
}
}
let env = Env::new_sub(new_env, Rc::clone(&self.outer_env));
if self.declarations.len() > 0 {
let mut env_rc = Rc::new(env);
for token in &self.declarations {
env_rc = eval_dec(&token, &env_rc);
}
Rc::new(Value::Partial(Rc::clone(&self.body), env_rc))
//eval_exp(&self.body, &env_rc)
} else {
//println!("calling with env {:?}\n",env);
//eval_exp(&self.body, &Rc::new(env))
Rc::new(Value::Partial(Rc::clone(&self.body), Rc::new(env)))
}
}
}
}
impl<'a> Clone for Procedure {
fn clone(&self) -> Procedure {
Procedure::new(self.params.clone(), self.body.clone(), self.outer_env.clone(), self.fixed_num, self.declarations.clone())
}
}
/*fn onearg(func: fn(Rc<Value>) -> Rc<Value>) -> Rc<dyn Fn(Vec<Rc<Value>>) -> Rc<Value>>
{
Rc::new(move |list: Vec<Rc<Value>>| if list.len() == 1 {
func(Rc::clone(&list[0]))
} else {
Rc::new(Value::Error(String::from("wrong number of arguments (expected 1)")))
})
}*/
fn onearg(list: Vec<Rc<Value>>, func: fn(Rc<Value>) -> Rc<Value>) -> Rc<Value>
{
if list.len() == 1 {
if let Value::Error(_) = *list[0] {
Rc::clone(&list[0])
} else {
func(Rc::clone(&list[0]))
}
} else {
Rc::new(Value::Error(String::from("wrong number of arguments (expected 1)")))
}
}
fn twoarg(list: Vec<Rc<Value>>, func: fn(Rc<Value>, Rc<Value>) -> Rc<Value>) -> Rc<Value>
{
if list.len() == 2 {
if let Value::Error(_) = *list[0] {
Rc::clone(&list[0])
} else if let Value::Error(_) = *list[1] {
Rc::clone(&list[1])
} else {
func(Rc::clone(&list[0]), Rc::clone(&list[1]))
}
} else {
Rc::new(Value::Error(String::from("wrong number of arguments (expected 1)")))
}
}
fn to_number_list(list: Vec<Rc<Value>>) -> Option<Vec<i64>> {
let mut ret_list: Vec<i64> = vec![];
for outer_value in list.iter() {
if let Value::Integer(value) = **outer_value {
ret_list.push(value as i64)
} else {
return None
}
}
Some(ret_list)
}
fn check_arith_to_value(result: Option<i64>) -> Value
{
match result {
Some(i) => Value::Integer(i),
None => Value::Boolean(false)
}
}
fn add(list: Vec<Rc<Value>>) -> Rc<Value> {
twoarg(list, |first, second| {
match [&*first, &*second] {
[Value::Integer(f), Value::Integer(s)] =>
Rc::new(check_arith_to_value(f.checked_add(*s))),
_ => Rc::new(Value::Error(String::from("can't add non numbers")))
}
})
}
fn mult(list: Vec<Rc<Value>>) -> Rc<Value> {
twoarg(list, |first, second| {
match [&*first, &*second] {
[Value::Integer(f), Value::Integer(s)] =>
Rc::new(check_arith_to_value(f.checked_mul(*s))),
_ => Rc::new(Value::Error(String::from("can't mult non numbers")))
}
})
}
fn sub(list: Vec<Rc<Value>>) -> Rc<Value> {
match to_number_list(list) {
None => Rc::new(Value::Error(String::from("can't subtract non numbers"))),
Some(int_list) => match int_list.as_slice() {
[] => Rc::new(Value::Error(String::from("no number for subtract"))),
[value] => Rc::new(check_arith_to_value(value.checked_neg())),
[f,s] => Rc::new(check_arith_to_value(f.checked_sub(*s))),
_ => Rc::new(Value::Error(String::from("unexpected subtract")))
}
}
}
fn num_equal(list: Vec<Rc<Value>>) -> Rc<Value> {
twoarg(list, |first, second| {
match [&*first, &*second] {
[Value::Integer(x),Value::Integer(y)] => Rc::new(Value::Boolean(x == y)),
_ => Rc::new(Value::Error(String::from("can't = on non numbers")))
}
})
}
fn num_increase(list: Vec<Rc<Value>>) -> Rc<Value> {
twoarg(list, |first, second| {
match [&*first, &*second] {
[Value::Integer(x),Value::Integer(y)] => Rc::new(Value::Boolean(x < y)),
_ => Rc::new(Value::Error(String::from("can't < on non numbers")))
}
})
}
fn num_decrease(list: Vec<Rc<Value>>) -> Rc<Value> {
twoarg(list, |first, second| {
match [&*first, &*second] {
[Value::Integer(x),Value::Integer(y)] => Rc::new(Value::Boolean(x > y)),
_ => Rc::new(Value::Error(String::from("can't > on non numbers")))
}
})
}
fn make_list<'a>(list: Vec<Rc<Value>>) -> Rc<Value> {
let mut cur_pair = Value::EmptyList;
for value in list.iter().rev() {
cur_pair = Value::Pair(Rc::clone(value), Rc::new(cur_pair))
}
Rc::new(cur_pair)
}
fn make_quote(token: &Token) -> Value {
match token {
&Token::IntegerToken(value) => Value::Integer(value),
&Token::StringToken(ref value) => Value::Symbol(value.clone()),
&Token::TokenList(ref list) => {
let mut list_iter = list.iter();
let mut cur_pair = Value::EmptyList;
if list.len() >= 3 {
if let Token::Dot = list[list.len()-2] {
if let Some(last) = list_iter.next_back() {
cur_pair = make_quote(last);
list_iter.next_back(); //Pop Dot off
}
}
}
for sub_token in list_iter.rev() {
cur_pair = Value::Pair(Rc::new(make_quote(sub_token)), Rc::new(cur_pair))
}
cur_pair
}
&Token::QuoteToken(ref value) => make_quote(value),
&Token::Dot => Value::Error(String::from("unexpected .")),
}
}
fn nullp(list: Vec<Rc<Value>>) -> Rc<Value>{
onearg(list, |value| {
if let Value::EmptyList = *value {
Rc::new(Value::Boolean(true))
} else {
Rc::new(Value::Boolean(false))
}
})
}
fn not_fn(list: Vec<Rc<Value>>) -> Rc<Value> {
onearg(list, |value| {
match *value {
Value::Boolean(false) => Rc::new(Value::Boolean(true)),
_ => Rc::new(Value::Boolean(false)),
}
})
}
fn car<'a>(list: Vec<Rc<Value>>) -> Rc<Value> {
onearg(list, |value| {
match *value {
Value::Pair(ref car, _) => Rc::clone(car),
_ => Rc::new(Value::Error(String::from("car has unexpected argument")))
}
})
}
fn cdr<'a>(list: Vec<Rc<Value>>) -> Rc<Value> {
onearg(list, |value| {
match *value {
Value::Pair(_, ref cdr) => Rc::clone(cdr),
_ => Rc::new(Value::Error(String::from("cdr has unexpected argument")))
}
})
}
fn cons<'a>(list: Vec<Rc<Value>>) -> Rc<Value> {
twoarg(list, |car, cdr| {
Rc::new(Value::Pair(Rc::clone(&car), Rc::clone(&cdr)))
})
}
fn eqvp(list: Vec<Rc<Value>>) -> Rc<Value> {
//Note, not completely r7rs compliant.
match list.as_slice() {
[first, second] => match [&**first, &**second] {
[Value::Symbol(ref x),Value::Symbol(ref y)] => Rc::new(Value::Boolean(x == y)),
[Value::Boolean(x),Value::Boolean(y)] => Rc::new(Value::Boolean(x == y)),
[Value::EmptyList, Value::EmptyList] => Rc::new(Value::Boolean(true)),
[Value::Integer(x), Value::Integer(y)] => Rc::new(Value::Boolean(x == y)),
[_, _] => Rc::new(Value::Boolean(false)),
}
_ => Rc::new(Value::Error(String::from("eqv? should have two arguments")))
}
}
fn numberp(list: Vec<Rc<Value>>) -> Rc<Value> {
match list.as_slice() {
[head] => match [&**head] {
[Value::Integer(_)] => Rc::new(Value::Boolean(true)),
[_] => Rc::new(Value::Boolean(false)),
}
_ => Rc::new(Value::Error(String::from("number? should have one arguments")))
}
}
fn pairp(list: Vec<Rc<Value>>) -> Rc<Value> {
onearg(list, |head| {
match &*head {
Value::Pair(_, _) => Rc::new(Value::Boolean(true)),
_ => Rc::new(Value::Boolean(false)),
}
})
}
fn booleanp(list: Vec<Rc<Value>>) -> Rc<Value> {
onearg(list, |head| {
match &*head {
Value::Boolean(_) => Rc::new(Value::Boolean(true)),
_ => Rc::new(Value::Boolean(false)),
}
})
}
fn procedurep(list: Vec<Rc<Value>>) -> Rc<Value> {
onearg(list, |head| {
match &*head {
Value::Procedure(_) => Rc::new(Value::Boolean(true)),
Value::Function(_) => Rc::new(Value::Boolean(true)),
_ => Rc::new(Value::Boolean(false)),
}
})
}
fn pair_to_list(pair: &Value) -> Option<Vec<Rc<Value>>> {
if let Value::Pair(ref car, ref cdr) = pair {
let mut cur_cdr = cdr;
let mut done = false;
let mut list: Vec<Rc<Value>> = vec![Rc::clone(car)];
while !done {
if let Value::Pair(ref cadr, ref cddr) = **cur_cdr {
list.push(Rc::clone(cadr));
cur_cdr = cddr
}
else if let Value::EmptyList = **cur_cdr {
done = true
} else {
//Not proper list so return None
return None
}
}
Some(list)
} else {
None
}
}
fn apply(list: Vec<Rc<Value>>) -> Rc<Value> {
twoarg(list, |first, second| {
match [&*first, &*second] {
[Value::Procedure(procedure), pair @ Value::Pair(_, _)] => {
if let Some(list) = pair_to_list(pair) {
procedure.call_partial(list)
} else {
Rc::new(Value::Error(String::from("apply requires function and proper list")))
}
},
[Value::Function(func), pair @ Value::Pair(_, _)] => {
if let Some(list) = pair_to_list(pair) {
func(list)
} else {
Rc::new(Value::Error(String::from("apply requires function and proper list")))
}
},
_ => Rc::new(Value::Error(String::from("apply requires function and list")))
}
})
}
fn symbolp(list: Vec<Rc<Value>>) -> Rc<Value> {
onearg(list, |head| {
match &*head {
Value::Symbol(_) => Rc::new(Value::Boolean(true)),
_ => Rc::new(Value::Boolean(false)),
}
})
}
fn if_fn(test: &Token, consequent: &Token, optional_alternate: Option<&Token>, env: &REnv)
-> Rc<Value>
{
let test_eval = eval_exp(test,env);
match *test_eval {
Value::Error(_) => test_eval,
Value::Boolean(false) =>
match optional_alternate {
Some(alternate) => Rc::new(Value::Partial(Rc::new(alternate.clone()), Rc::clone(env))), //eval_exp(alternate, env),
None => Rc::new(Value::Undefined)
},
_ =>
Rc::new(Value::Partial(Rc::new(consequent.clone()), Rc::clone(env)))
}
}
fn eval_both<'a, 'b, 'c>(token: &Token, env: &REnv) -> (REnv, Rc<Value>)
{
match token {
&Token::TokenList(ref list) => match list as &[Token] {
[Token::StringToken(ref string),Token::StringToken(ref _name), ref _raw_var_value] if string == "define" =>
(eval_dec(token, env), Rc::new(Value::Undefined)),
[Token::StringToken(ref string),ref raw_value] if string == "display" =>
{ print!("{:?}",eval_exp(raw_value, env));
(Rc::clone(env), Rc::new(Value::Undefined))},
[Token::StringToken(ref string)] if string == "newline" =>
{ println!(""); (Rc::clone(env), Rc::new(Value::Undefined))},
_ => (Rc::clone(env), eval_exp(token, env))
}
_ => (Rc::clone(env), eval_exp(token, env))
}
}
fn use_y_combiner(procedure: &Procedure, name: String) -> Rc<Value> {
//Needs to generate lambda like:
//Y1 (lambda (le) ((lambda (f) (f f)) (lambda (f) (le (lambda (x1) ((f f) x1))))))
//or
//Y2 (lambda (le) ((lambda (f) (f f)) (lambda (f) (le (lambda (x1 x2) ((f f) x1 x2))))))
//or
//(define YS (lambda (le) ((lambda (f) (f f)) (lambda (f) (le (lambda x (apply (f f) x)))))))
//and then make new procedure
//(Y1 Original procedure)
let size = procedure.params.len();
let args:Vec<Token> = vec![Token::str("#X0")];
let ff = Token::TokenList(
vec![Token::str("f"),
Token::str("f")
]);
let self_call = {
let mut extra = vec![Token::str("apply"),ff];
extra.extend(args.clone());
vec![Token::str("lambda"),
args[0].clone(),
Token::TokenList(extra)]
};
let new_tokens = Token::TokenList(
vec![Token::TokenList(
vec![Token::str("lambda"),
Token::TokenList(
vec![Token::str("f")]),
Token::TokenList(
vec![Token::str("f"),
Token::str("f")
])
]),
Token::TokenList(
vec![Token::str("lambda"),
Token::TokenList(
vec![Token::str("f")]),
Token::TokenList(
vec![Token::str("phi"),
Token::TokenList(self_call)
])
])
]);
let mut parameter_tokens = Vec::new();
for i in 0..size {
parameter_tokens.push(Token::StringToken(procedure.params[i].clone()));
}
// ((lambda (phi) new_tokens) (lambda (name) (lambda (parameters) body)))
//Now need to create new_tokens as a procedure
//TODO: Not sure if we should just pass a blank environment here.
let y_proc = Procedure::new_no_dec(vec![String::from("phi")], Rc::new(new_tokens), Rc::clone(&procedure.outer_env));
let new_lambda = {
let mut token_vec: Vec<Token> = if procedure.fixed_num {
vec![Token::str("lambda"),parameter_tokens[0].clone()]
} else {
vec![Token::str("lambda"),Token::TokenList(parameter_tokens)]
};
if procedure.declarations.len() > 0 {
for token in &procedure.declarations {
token_vec.push((**token).clone());
}
}
token_vec.push((*procedure.body).clone());
Token::TokenList(token_vec)
};
//Evaluate y combiner on original procedure, and return result
let orig_part = Procedure::new_no_dec(vec![name.clone()], Rc::new(new_lambda), Rc::clone(&procedure.outer_env)); //Rc::clone(&procedure.body)
//stuff into environment and eval
let mut new_env_part : PartialEnv = PartialEnv::new();
new_env_part.insert(String::from("Y"), Rc::new(Value::Procedure(y_proc)));
new_env_part.insert(name.clone() + &String::from("part"), Rc::new(Value::Procedure(orig_part)));
let new_env = Env::new_sub(new_env_part, Rc::clone(&procedure.outer_env));
let transformed = eval_exp(&Token::TokenList(vec![Token::str("Y"), Token::StringToken(name + &String::from("part"))]), &Rc::new(new_env));
transformed
}
fn eval_dec<'a, 'b, 'c>(token: &Token, env: &REnv) -> REnv
{
match token {
&Token::TokenList(ref list) => match list as &[Token] {
[Token::StringToken(ref string),Token::StringToken(ref name), ref raw_var_value] if string == "define" => {
let mut new_env: PartialEnv = PartialEnv::new();
let var_value = eval_exp(raw_var_value,env);
if let Value::Procedure(procedure) = &*var_value {
new_env.insert(name.clone(), use_y_combiner(procedure, name.to_string()));
} else {
new_env.insert(name.clone(), var_value);
}
Rc::new(Env::new_sub(new_env, Rc::clone(env)))
},
_ => {
println!("Internal error, unexpected in define");
Rc::clone(env)
}
}
_ => {
println!("Internal error, unexpected in define");
Rc::clone(env)
}
}
}
fn eval_exp<'a,'b, 'c>(token_orig: &Token, env_orig: &REnv)
-> Rc<Value>
{
let mut token = Rc::new(token_orig.clone());
let mut env = Rc::clone(env_orig);
loop {
//println!("eval_exp {:?}", token);
let eval = match &*token {
&Token::Dot => Rc::new(Value::Error(String::from("unexpected ."))),
&Token::IntegerToken(value) => Rc::new(Value::Integer(value)),
&Token::StringToken(ref value) => match env.find(&value) {
Some(entry) => entry,
None => {//println!("Can't find {:?} in {:?}",value, env);
Rc::new(Value::Error(String::from("token not found in env: ")+value))}
},
&Token::QuoteToken(ref value) => Rc::new(make_quote(value)),
&Token::TokenList(ref list) => match list as &[Token] {
[Token::StringToken(ref string), ref test, ref consequent, ref alternate] if string == "if" => {
if_fn(test, consequent, Some(alternate), &env)
},
[Token::StringToken(ref string), ref test, ref consequent] if string == "if" => {
if_fn(test, consequent, None, &env)
},
[Token::StringToken(ref string),..] if string == "if" => Rc::new(Value::Error(String::from("if missing a bit"))),
[Token::StringToken(ref string), Token::TokenList(ref test1), ..] if string == "cond" && test1.len() == 2 =>
{
let mut in_else = false;
if let Token::StringToken(ref cond_start) = test1[0] {
in_else = cond_start == "else";
}
if in_else {
eval_exp(&test1[1], &env)
} else if list.len() > 2 {
let mut rest:Vec<Token> = Vec::new();
rest.push(Token::str("cond"));
rest.extend_from_slice(&list[2..]);
if_fn(&test1[0], &test1[1], Some(&Token::TokenList(rest)), &env)
} else {
if_fn(&test1[0], &test1[1], None, &env)
}
},
[Token::StringToken(ref string)] if string == "and" =>
Rc::new(Value::Boolean(true)),
[Token::StringToken(ref string), ref test1] if string == "and" => eval_exp(test1, &env),
[Token::StringToken(ref string), ref test1, ..] if string == "and" =>
{
let mut rest:Vec<Token> = Vec::new();
rest.push(Token::str("and"));
rest.extend_from_slice(&list[2..]);
if_fn(&test1, &Token::TokenList(rest), Some(&Token::str("#f")), &env)
},
[Token::StringToken(ref string)] if string == "or" =>
Rc::new(Value::Boolean(false)),
[Token::StringToken(ref string), ref test1] if string == "or" =>
eval_exp(test1, &env),
[Token::StringToken(ref string), ref test1, rest @ ..] if string == "or" =>
{
let mut transformed:Vec<Token> = vec![Token::str("let")];
transformed.push(Token::TokenList(vec![
Token::TokenList(vec![Token::str("#x"), test1.clone()])]));
let mut rest_or:Vec<Token> = vec![Token::str("or")];
for rest_item in rest {
rest_or.push(rest_item.clone());
}
transformed.push(Token::TokenList(vec![
Token::str("if"),Token::str("#x"),Token::str("#x"),
Token::TokenList(rest_or)]));
eval_exp(&Token::TokenList(transformed), &env)
}
[Token::StringToken(ref string), Token::StringToken(ref param), ref body] if string == "lambda" => {
Rc::new(Value::Procedure(Procedure::new_single(String::from(param),Rc::new(body.clone()),env.clone())))
}
[Token::StringToken(ref string), Token::StringToken(ref param), middle @ .., ref body] if string == "lambda" => {
let mut middle_vec: Vec<Rc<Token>> = Vec::new();
for dec in middle {
middle_vec.push(Rc::new(dec.clone()));
}
Rc::new(Value::Procedure(Procedure::new_single_with_dec(String::from(param),Rc::new(body.clone()),middle_vec,env.clone())))
}
[Token::StringToken(ref string),Token::TokenList(ref parameters), ref body]
if string == "lambda" => {
let mut param_eval: Vec<String> = vec![];
for item in parameters.iter() {
match item as &Token {
&Token::StringToken(ref param) => param_eval.push(param.clone()),
_ => return Rc::new(Value::Error(String::from("Non string in lambda parameter list")))
}
}
Rc::new(Value::Procedure(Procedure::new_no_dec(param_eval,Rc::new(body.clone()),env.clone())))
},
[Token::StringToken(ref string),Token::TokenList(ref parameters), middle @ .., ref body]
if string == "lambda" => {
let mut middle_vec: Vec<Rc<Token>> = Vec::new();
for dec in middle {
middle_vec.push(Rc::new(dec.clone()));
}
let mut param_eval: Vec<String> = vec![];
for item in parameters.iter() {
match item as &Token {
&Token::StringToken(ref param) => param_eval.push(param.clone()),
_ => return Rc::new(Value::Error(String::from("Non string in lambda parameter list")))
}
}
Rc::new(Value::Procedure(Procedure::new_with_dec(param_eval,Rc::new(body.clone()), middle_vec, env.clone())))
},
[Token::StringToken(ref string),datum] if string == "quote" => {
Rc::new(make_quote(datum))
}
[Token::StringToken(ref string), Token::TokenList(var_inits), body @ ..] if string == "let" => {
let mut vars: Vec<Token> = Vec::new();
let mut inits: Vec<Token> = Vec::new();
for var_init in var_inits {
if let Token::TokenList(inner_list) = var_init {
vars.push(inner_list[0].clone());
inits.push(inner_list[1].clone());
} else {
return Rc::new(Value::Error(String::from("invalid let var init")))
}
}
let mut transformed: Vec<Token> = Vec::new();
let mut lambda_transform: Vec<Token> = Vec::new();
lambda_transform.push(Token::str("lambda"));
lambda_transform.push(Token::TokenList(vars));
for body_item in body {
lambda_transform.push(body_item.clone());
}
transformed.push(Token::TokenList(lambda_transform));
transformed.extend(inits);
eval_exp(&Token::TokenList(transformed), &env)
}
[] => Rc::new(Value::Undefined),
full_token =>
if let Some((head, tail)) = full_token.split_first() {
//[ref head, tail..] => {
//println!("Evaluating {:?} in &env {:?}\n",head,&env);
let head_eval = eval_exp(head,&env);
//println!("Evaluated {:?} in env {:?}\n",head_eval,&env);
//tail.iter().map(|t| eval_exp(t,&env).expect("Got none")).collect();
let mut tail_eval: Vec<Rc<Value>> = vec![];
for item in tail.iter() {
let ret = eval_exp(item,&env);
//println!("debug {:?}",item);
tail_eval.push(ret);
}
//let tail1_eval = eval_exp(tail1, &env).expect("Got none1");
match &*head_eval {