-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
1170 lines (1111 loc) · 39.7 KB
/
interpreter.c
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
#include <stdio.h>
#include <string.h>
#include "value.h"
#include "talloc.h"
#include "linkedlist.h"
/* Attempts to look up the symbol associated with the given Value* in the given
* frame and its parents. If the symbol is not found, returns NULL, otherwise
* returns the associated value (without calling eval on it). */
Value *lookup_symbol(Value *expr, Frame *frame) {
Value curr_frame, *value, *pair;
Frame *current = frame;
if (expr->type != SYMBOL_TYPE) {
fprintf(stderr, "Evaluation error: called lookup_symbol on value of type %d\n", expr->type);
texit(4);
}
while (current != NULL) {
value = current->bindings;
while (value->type == CONS_TYPE) {
pair = car(value);
if (strcmp(car(pair)->s, expr->s) == 0) {
return cdr(pair);
}
value = cdr(value);
}
current = current->parent;
}
// if while loops end, never found matching symbol in frames
return NULL;
}
void error_display_tree(char *name, Value *args) {
Value tmp_cons, tmp_symbol;
tmp_symbol.type = SYMBOL_TYPE;
tmp_symbol.s = name;
tmp_cons.type = CONS_TYPE;
tmp_cons.c.car = &tmp_symbol;
tmp_cons.c.cdr = args;
display_to_fd(&tmp_cons, stderr);
return;
}
Value *eval(Value *expr, Frame *frame);
////////////////////////////////////////
////////// BUILT-IN FUNCTIONS //////////
////////////////////////////////////////
Value *eval_begin(Value *args, Frame *frame) {
Value *current = args, *result = NULL;
while (current->type == CONS_TYPE) {
result = eval(car(current), frame);
current = cdr(current);
}
if (current->type != NULL_TYPE) {
fprintf(stderr, "Evaluation error: built-in function `begin`: bad form in arguments: ");
error_display_tree("begin", args);
texit(4);
}
if (result == NULL) {
result = makeVoid(); // sequence of zero expressions
}
return result;
}
Value *eval_not(Value *args, Frame *frame) {
Value *cond, *result;
int argc = length(args);
if (argc != 1) {
fprintf(stderr, "Evaluation error: built-in function `not`: expected 1 argument, received %d\n", argc);
texit(4);
}
cond = eval(car(args), frame);
if (cond->type != BOOL_TYPE) {
fprintf(stderr, "Evaluation error: built-in function `not`: expected type %d (BOOL_TYPE) as first argument, but received %d\n", BOOL_TYPE, cond->type);
texit(4);
}
result = makeBool(!(cond->i));
return result;
}
Value *eval_if(Value *args, Frame *frame) {
Value *cond, *result;
int argc = length(args);
if (argc < 2 || argc > 3) {
fprintf(stderr, "Evaluation error: built-in function `if`: expected 2 or 3 arguments, received %d\n", argc);
texit(4);
}
cond = eval(car(args), frame);
if (cond->type != BOOL_TYPE) {
fprintf(stderr, "Evaluation error: built-in function `if`: expected type %d (BOOL_TYPE) as first argument, but received %d\n", BOOL_TYPE, cond->type);
texit(4);
}
if (cond->i) {
result = eval(car(cdr(args)), frame);
} else if (argc == 2) {
result = makeVoid();
} else {
result = eval(car(cdr(cdr(args))), frame);
}
return result;
}
Value *eval_cond(Value *args, Frame *frame) {
Value *current = args, *cur_clause, *test;
if (length(args) == 0)
goto COND_ERROR_BAD_FORM;
while (current->type == CONS_TYPE) {
cur_clause = car(current);
if (cur_clause->type != CONS_TYPE)
goto COND_ERROR_BAD_FORM;
test = car(cur_clause);
if (test->type == SYMBOL_TYPE && strcmp(test->s, "else") == 0)
return eval_begin(cdr(cur_clause), frame);
test = eval(test, frame);
if (test->type != BOOL_TYPE)
goto COND_ERROR_BAD_FORM;
if (test->i == 1)
return eval_begin(cdr(cur_clause), frame);
current = cdr(current);
}
if (current->type != NULL_TYPE)
goto COND_ERROR_BAD_FORM;
return makeVoid();
COND_ERROR_BAD_FORM:
fprintf(stderr, "Evaluation error: built-in function `cond`: bad form in arguments: ");
error_display_tree("cond", args);
texit(4);
return NULL; // will never return
}
Value *eval_when(Value *args, Frame *frame) {
Value *cond, *result = NULL;
cond = eval(car(args), frame);
if (cond->type != BOOL_TYPE) {
fprintf(stderr, "Evaluation error: built-in function `when`: expected type %d (BOOL_TYPE) as first argument, but received %d\n", BOOL_TYPE, cond->type);
texit(4);
}
if (cond->i) {
result = eval_begin(cdr(args), frame);
} else {
result = makeVoid();
}
return result;
}
Value *eval_unless(Value *args, Frame *frame) {
Value *cond, *current, *result = NULL;
cond = eval(car(args), frame);
if (cond->type != BOOL_TYPE) {
fprintf(stderr, "Evaluation error: built-in function `unless`: expected type %d (BOOL_TYPE) as first argument, but received %d\n", BOOL_TYPE, cond->type);
texit(4);
}
if (!(cond->i)) {
result = eval_begin(cdr(args), frame);
} else {
result = makeVoid();
}
return result;
}
/* Sets the evaluated binding in the given frame */
void letrec_eval_bindings_helper(Value *list, Frame *frame, int evaluate, int star) {
Value *current, *cur_pair, *cur_bind, *binding;
Frame *cur_frame;
current = list;
while (current->type == CONS_TYPE) {
cur_pair = car(current);
cur_frame = frame;
while (cur_frame != NULL) {
binding = frame->bindings;
while (binding->type == CONS_TYPE) {
cur_bind = car(binding);
if (strcmp(car(cur_bind)->s, car(cur_pair)->s) == 0) {
cur_bind->c.cdr = evaluate ? eval(car(cdr(cur_pair)), frame) : cdr(cur_pair);
goto FOUND_BINDING;
}
binding = cdr(binding);
}
cur_frame = star ? cur_frame->parent : NULL;
}
// Should be imposible to get here, since matching binding should always be found
fprintf(stderr, "Evaluation error: built-in function `%s`: temporary binding for evaluated variable no longer found in frame: %s", star ? "letrec*" : "letrec", car(cur_pair)->s);
texit(4);
FOUND_BINDING:
current = cdr(current);
}
}
/* Evaluate the bindings which had previously been set to UNSPECIFIED_TYPE by
* going through the original list of (var expr) pairs, evaluating expr, and
* setting var to the result in the current frame. If star, then the setting
* happens immediately after evaluation; else, the setting happens after all
* expressions have been evaulated. */
void letrec_eval_bindings(Value *pairs, Frame *frame, int star) {
Value *current, *cur_pair, *evaluated, *eval_list;
if (pairs->type == NULL_TYPE)
return;
if (star) {
letrec_eval_bindings_helper(pairs, frame, 1, star);
return;
}
eval_list = makeNull();
current = pairs;
while (current->type == CONS_TYPE) {
cur_pair = car(current);
evaluated = cons(car(cur_pair), eval(car(cdr(cur_pair)), frame));
if (cdr(evaluated)->type == UNSPECIFIED_TYPE) {
fprintf(stderr, "Evaluation error: built-in function `%s`: unbound variable ", star ? "letrec*" : "letrec");
display_to_fd(car(evaluated), stderr);
texit(4);
}
eval_list = cons(evaluated, eval_list);
current = cdr(current);
}
letrec_eval_bindings_helper(eval_list, frame, 0, star);
}
Value *let_helper(Value *args, Frame *frame, int star, int rec) {
Value *current, *current_pair, *result, *binding;
Frame *new_frame, *eval_frame;
char *name_possibilities[4] = {"let", "letrec", "let*", "letrec*"};
char *name = name_possibilities[(!star << 1) | (!rec)];
if (length(args) < 2)
goto LET_ERROR_BAD_FORM;
new_frame = talloc(sizeof(Frame));
new_frame->bindings = makeNull();
new_frame->parent = frame;
current = car(args); // list of (symbol value) pairs
while (current->type == CONS_TYPE) {
current_pair = car(current);
if ((current_pair->type != CONS_TYPE)
|| (length(current_pair) != 2)
|| (car(current_pair)->type != SYMBOL_TYPE))
goto LET_ERROR_BAD_FORM;
binding = new_frame->bindings;
while (binding->type == CONS_TYPE) {
if (strcmp(car(car(binding))->s, car(current_pair)->s) == 0) {
fprintf(stderr, "Evaluation error: built-in function `%s`: duplicate bound variable %s in form ", name, car(current_pair)->s);
goto LET_ERROR_DISPLAY_TREE;
}
binding = cdr(binding);
}
new_frame->bindings = cons(
cons(
car(current_pair),
rec ? makeUnspecified() : eval(car(cdr(current_pair)), frame)),
new_frame->bindings);
if (star) {
frame = new_frame;
new_frame = talloc(sizeof(Frame));
new_frame->bindings = makeNull();
new_frame->parent = frame;
}
current = cdr(current);
}
if (current->type != NULL_TYPE)
goto LET_ERROR_BAD_FORM;
if (rec)
letrec_eval_bindings(car(args), new_frame, star);
current = cdr(args); // current is now reused to evaluate expressions
while (current->type == CONS_TYPE) {
result = eval(car(current), new_frame);
current = cdr(current);
}
return result;
LET_ERROR_BAD_FORM:
fprintf(stderr, "Evaluation error: built-in function `%s`: bad form in arguments: ", name);
LET_ERROR_DISPLAY_TREE:
error_display_tree(name, args);
texit(4);
return NULL; // will never return
}
Value *eval_let(Value *args, Frame *frame) {
return let_helper(args, frame, 0, 0);
}
Value *eval_let_star(Value *args, Frame *frame) {
return let_helper(args, frame, 1, 0);
}
Value *eval_letrec(Value *args, Frame *frame) {
return let_helper(args, frame, 0, 1);
}
Value *eval_letrec_star(Value *args, Frame *frame) {
return let_helper(args, frame, 1, 1);
}
Value *eval_quote(Value *args, Frame *frame) {
int argc = length(args);
if (argc != 1) {
fprintf(stderr, "Evaluation error: built-in function `quote`: expected 1 argument, received %d\n", argc);
texit(4);
}
return car(args);
}
Value *eval_display(Value *args, Frame *frame) {
Value *val, *result;
int i, argc;
char c;
argc = length(args);
if (argc != 1) {
fprintf(stderr, "Evaluation error: built-in function `display`: expected 1 argument, received %d\n", argc);
texit(4);
}
val = eval(car(args), frame);
switch (val->type) {
case INT_TYPE:
printf("%d", val->i);
break;
case DOUBLE_TYPE:
printf("%lf", val->d);
break;
case STR_TYPE:
printf("%s", val->s);
break;
case BOOL_TYPE:
if (val->i == 0)
printf("#f");
else
printf("#t");
break;
case VOID_TYPE:
break;
case CLOSURE_TYPE:
printf("#<procedure>");
default:
fprintf(stderr, "Evaluation error: built-in function `display`: cannot display value of type %d\n", val->type);
texit(4);
}
result = makeVoid();
return result;
}
Value *eval_lambda(Value *args, Frame *frame) {
Value *closure, *current, *next;
if (length(args) < 2) {
fprintf(stderr, "Evaluation error: built-in function `lambda`: bad form in arguments: ");
error_display_tree("lambda", args);
texit(4);
}
current = car(args);
closure = talloc(sizeof(Value));
closure->type = CLOSURE_TYPE;
closure->cl.paramNames = current;
closure->cl.functionCode = cdr(args);
closure->cl.frame = frame;
if (current->type != SYMBOL_TYPE) {
while (current->type == CONS_TYPE) {
if (car(current)->type != SYMBOL_TYPE)
goto LAMBDA_BAD_PARAMETERS;
next = cdr(current);
while (next->type == CONS_TYPE) {
if (strcmp(car(current)->s, car(next)->s) == 0)
goto LAMBDA_BAD_PARAMETERS;
next = cdr(next);
}
current = cdr(current);
}
if (current->type != NULL_TYPE)
goto LAMBDA_BAD_PARAMETERS;
}
return closure;
LAMBDA_BAD_PARAMETERS:
fprintf(stderr, "Evaluation error: built-in function `lambda`: bad form in parameters list: ");
error_display_tree("lambda", args);
texit(4);
return NULL; // will never return
}
Value *eval_define(Value *args, Frame *frame) {
Value *var, *expr;
if (length(args) != 2) {
fprintf(stderr, "Evaluation error: built-in function `define`: bad form in arguments: ");
error_display_tree("define", args);
texit(4);
}
var = car(args);
expr = car(cdr(args));
if (var->type == CONS_TYPE) {
expr = eval_lambda(cons(cdr(var), expr), frame);
var = car(var);
} else if (var->type != SYMBOL_TYPE) {
fprintf(stderr, "Evaluation error: built-in function `define`: bad form in arguments: ");
error_display_tree("define", args);
texit(4);
}
frame->bindings = cons(cons(var, eval(expr, frame)), frame->bindings);
return makeVoid();
}
Value *eval_set(Value *args, Frame *frame) {
Value *binding, *pair, *expr;
Frame *current = frame;
int argc = length(args);
if (argc != 2) {
fprintf(stderr, "Evaluation error: built-in function `set!`: expected 2 arguments, received %d\n", argc);
texit(4);
}
expr = car(args);
if (expr->type != SYMBOL_TYPE) {
fprintf(stderr, "Evaluation error: built-in function `set!`: wrong type argument in position 1 (expected SYMBOL_TYPE): ");
display_to_fd(expr, stderr);
texit(4);
}
while (current != NULL) {
binding = current->bindings;
while (binding->type == CONS_TYPE) {
pair = car(binding);
if (strcmp(car(pair)->s, expr->s) == 0) {
pair->c.cdr = eval(car(cdr(args)), frame);
return makeVoid();
}
binding = cdr(binding);
}
current = current->parent;
}
fprintf(stderr, "Evaluation error: built-in function `set!`: unbound variable ");
display_to_fd(expr, stderr);
texit(4);
return NULL;
}
Value *logic_helper(Value *args, Frame *frame, int end_val) {
Value *cond, *current = args;
int arg_num = 1;
while (current->type == CONS_TYPE) {
cond = eval(car(current), frame);
if (cond->type != BOOL_TYPE) {
fprintf(stderr, "Evaluation error: built-in function `and`: wrong type argument in position %d: ", arg_num);
display_to_fd(cond, stderr);
texit(4);
}
if (cond->i == end_val) {
return cond;
}
arg_num++;
current = cdr(current);
}
return makeBool(!end_val);
}
Value *eval_and(Value *args, Frame *frame) {
return logic_helper(args, frame, 0);
}
Value *eval_or(Value *args, Frame *frame) {
return logic_helper(args, frame, 1);
}
////////////////////////////////////////
///////// PRIMITIVE FUNCTIONS //////////
////////////////////////////////////////
enum operation {
PLUS,
MINUS,
MULT,
};
Value *arith_helper(Value *result, Value *args, enum operation op) {
Value *cur_val, *current = args;
char names[3] = {'+', '-', '/'};
char name = names[op];
int arg_num = 1;
while (current->type == CONS_TYPE) {
cur_val = car(current);
switch (cur_val->type) {
case INT_TYPE:
if (result->type == INT_TYPE) {
switch (op) {
case PLUS:
result->i += cur_val->i;
break;
case MINUS:
result->i -= cur_val->i;
break;
case MULT:
result->i *= cur_val->i;
break;
}
} else if (result->type == DOUBLE_TYPE) {
switch (op) {
case PLUS:
result->d += (double)cur_val->i;
break;
case MINUS:
result->d -= (double)cur_val->i;
break;
case MULT:
result->d *= (double)cur_val->i;
break;
}
}
break;
case DOUBLE_TYPE:
if (result->type == INT_TYPE)
result->d = (double)result->i;
result->type = DOUBLE_TYPE;
switch (op) {
case PLUS:
result->d += cur_val->d;
break;
case MINUS:
result->d -= cur_val->d;
break;
case MULT:
result->d *= cur_val->d;
break;
}
break;
default:
fprintf(stderr, "Evaluation error: primitive function `%c`: wrong type argument in position %d: ", name, arg_num);
display_to_fd(cur_val, stderr);
texit(4);
}
current = cdr(current);
arg_num++;
}
return result;
}
Value *prim_add(Value *args) {
Value *result;
result = talloc(sizeof(Value));
result->type = INT_TYPE;
result->i = 0;
return arith_helper(result, args, PLUS);
}
Value *prim_sub(Value *args) {
Value *result;
int argc = length(args);
result = talloc(sizeof(Value));
result->type = INT_TYPE;
result->i = 0;
switch (argc) {
case 0:
fprintf(stderr, "Evaluation error: primitive function `-`: wrong number of arguments\n");
texit(4);
case 1:
return arith_helper(result, args, MINUS);
default:
break;
}
*result = *car(args);
return arith_helper(result, cdr(args), MINUS);
}
Value *prim_mul(Value *args) {
Value *result = talloc(sizeof(Value));
result->type = INT_TYPE;
result->i = 1;
return arith_helper(result, args, MULT);
}
Value *prim_div(Value *args) {
Value *result, *divisor;
int argc = length(args);
if (argc != 2) {
fprintf(stderr, "Evaluation error: primitive function `/`: wrong number of arguments\n");
texit(4);
}
result = talloc(sizeof(Value));
*result = *car(args);
divisor = car(cdr(args));
if (result->type != INT_TYPE && result->type != DOUBLE_TYPE) {
fprintf(stderr, "Evaluation error: primitive function `/`: wrong type argument in position 1: ");
display_to_fd(result, stderr);
texit(4);
}
switch (divisor->type) {
case INT_TYPE:
if (result->type == INT_TYPE) {
if (result->i % divisor->i == 0) {
result->i /= divisor->i;
return result;
}
result->type = DOUBLE_TYPE;
result->d = (double)result->i;
}
divisor->d = (double)divisor->i;
case DOUBLE_TYPE:
if (result->type == INT_TYPE)
result->d = (double)result->i;
result->type = DOUBLE_TYPE;
result->d /= divisor->d;
break;
default:
fprintf(stderr, "Evaluation error: primitive function `/`: wrong type argument in position 2: ");
display_to_fd(divisor, stderr);
texit(4);
}
return result;
}
Value *prim_mod(Value *args) {
Value *result, *first, *second;
if (length(args) != 2) {
fprintf(stderr, "Evaluation error: primitive function `modulo`: wrong number of arguments\n");
texit(4);
}
first = car(args);
second = car(cdr(args));
if (first->type != INT_TYPE) {
fprintf(stderr, "Evaluation error: primitive function `modulo`: wrong type argument in position 1: ");
display_to_fd(first, stderr);
texit(4);
}
if (second->type != INT_TYPE) {
fprintf(stderr, "Evaluation error: primitive function `modulo`: wrong type argument in position 2: ");
display_to_fd(second, stderr);
texit(4);
}
result = talloc(sizeof(Value));
result->type = INT_TYPE;
result->i = first->i % second->i;
return result;
}
enum comparison {
EQ,
GT,
LT,
GEQ,
LEQ,
};
Value *compare_helper(Value *args, enum comparison comp) {
Value *prev, *current, *cur_val;
int arg_num = 2;
if (length(args) <= 1)
return makeBool(1);
prev = car(args); // should already be evaluated
current = cdr(args);
while (current->type == CONS_TYPE) {
cur_val = car(current);
switch (prev->type) {
case INT_TYPE:
switch (cur_val->type) {
case INT_TYPE:
switch (comp) {
case EQ:
if (prev->i != cur_val->i)
return makeBool(0);
break;
case GT:
if (prev->i <= cur_val->i)
return makeBool(0);
break;
case LT:
if (prev->i >= cur_val->i)
return makeBool(0);
break;
case GEQ:
if (prev->i < cur_val->i)
return makeBool(0);
break;
case LEQ:
if (prev->i > cur_val->i)
return makeBool(0);
break;
}
break;
case DOUBLE_TYPE:
switch (comp) {
case EQ:
if (prev->d != cur_val->i)
return makeBool(0);
break;
case GT:
if (prev->d <= cur_val->i)
return makeBool(0);
break;
case LT:
if (prev->d >= cur_val->i)
return makeBool(0);
break;
case GEQ:
if (prev->d < cur_val->i)
return makeBool(0);
break;
case LEQ:
if (prev->d > cur_val->i)
return makeBool(0);
break;
}
break;
default:
fprintf(stderr, "Evaluation error: primitive function `=`: wrong type argument in position %d: ", arg_num);
display_to_fd(prev, stderr);
texit(4);
}
break;
case DOUBLE_TYPE:
switch (cur_val->type) {
case INT_TYPE:
switch (comp) {
case EQ:
if (prev->i != cur_val->d)
return makeBool(0);
break;
case GT:
if (prev->i <= cur_val->d)
return makeBool(0);
break;
case LT:
if (prev->i >= cur_val->d)
return makeBool(0);
break;
case GEQ:
if (prev->i < cur_val->d)
return makeBool(0);
break;
case LEQ:
if (prev->i > cur_val->d)
return makeBool(0);
break;
}
break;
case DOUBLE_TYPE:
switch (comp) {
case EQ:
if (prev->d != cur_val->d)
return makeBool(0);
break;
case GT:
if (prev->d <= cur_val->d)
return makeBool(0);
break;
case LT:
if (prev->d >= cur_val->d)
return makeBool(0);
break;
case GEQ:
if (prev->d < cur_val->d)
return makeBool(0);
break;
case LEQ:
if (prev->d > cur_val->d)
return makeBool(0);
break;
}
break;
default:
fprintf(stderr, "Evaluation error: primitive function `=`: wrong type argument in position %d: ", arg_num);
display_to_fd(prev, stderr);
texit(4);
}
break;
default:
fprintf(stderr, "Evaluation error: primitive function `=`: wrong type argument in position 1: ");
display_to_fd(prev, stderr);
texit(4);
}
arg_num++;
prev = current;
current = cdr(current);
}
return makeBool(1);
}
Value *prim_eqnum(Value *args) {
return compare_helper(args, EQ);
}
Value *prim_gt(Value *args) {
return compare_helper(args, GT);
}
Value *prim_lt(Value *args) {
return compare_helper(args, LT);
}
Value *prim_geq(Value *args) {
return compare_helper(args, GEQ);
}
Value *prim_leq(Value *args) {
return compare_helper(args, LEQ);
}
Value *prim_null(Value *args) {
int argc = length(args);
if (argc != 1) {
fprintf(stderr, "Evaluation error: primitive function `null?`: expected 1 argument, received %d\n", argc);
texit(4);
}
return makeBool(car(args)->type == NULL_TYPE);
}
Value *prim_car(Value *args) {
Value *value;
int argc = length(args);
if (argc != 1) {
fprintf(stderr, "Evaluation error: primitive function `car`: expected 1 argument, received %d\n", argc);
texit(4);
}
value = car(args);
if (value->type != CONS_TYPE) {
fprintf(stderr, "Evaluation error: primitive function `car`: wrong type argument in position 1 (expected CONS_TYPE): ");
display_to_fd(value, stderr);
texit(4);
}
return car(value);
}
Value *prim_cdr(Value *args) {
Value *value;
int argc = length(args);
if (argc != 1) {
fprintf(stderr, "Evaluation error: primitive function `cdr`: expected 1 argument, received %d\n", argc);
texit(4);
}
value = car(args);
if (value->type != CONS_TYPE) {
fprintf(stderr, "Evaluation error: primitive function `cdr`: wrong type argument in position 1 (expected CONS_TYPE): ");
display_to_fd(value, stderr);
texit(4);
}
return cdr(value);
}
Value *prim_cons(Value *args) {
int argc = length(args);
if (argc != 2) {
fprintf(stderr, "Evaluation error: primitive function `cons`: expected 2 arguments, received %d\n", argc);
texit(4);
}
return cons(car(args), car(cdr(args)));
}
Value *prim_list(Value *args) {
return args;
}
Value *prim_append(Value *args) {
Value head, *tail, *current, *current_list = NULL;
int arg_num = 1;
head.c.cdr = NULL;
tail = &head;
current = args;
while (current->type == CONS_TYPE) {
current_list = car(current);
while (current_list->type == CONS_TYPE) {
tail->c.cdr = cons(car(current_list), NULL);
tail = tail->c.cdr;
current_list = cdr(current_list);
}
if (current_list->type != NULL_TYPE && cdr(current)->type != NULL_TYPE) {
fprintf(stderr, "Evaluation error: primitive function `append`: wrong type argument in position %d: ", arg_num);
display_to_fd(current_list, stderr);
texit(4);
}
current = cdr(current);
arg_num++;
}
tail->c.cdr = current_list ? current_list : makeNull();
return head.c.cdr;
}
int equal_helper(Value *first, Value *second) {
int equal = -1;
if (first->type != second->type)
return 0;
switch (first->type) {
case INT_TYPE:
case BOOL_TYPE:
return (first->i == second->i);
case DOUBLE_TYPE:
return (first->d == second->d);
case STR_TYPE:
case SYMBOL_TYPE:
return !strcmp(first->s, second->s);
case CONS_TYPE:
equal = 1;
while (first->type == CONS_TYPE && second->type == CONS_TYPE) {
equal &= equal_helper(car(first), car(second));
if (!equal)
return equal;
first = cdr(first);
second = cdr(second);
}
return equal_helper(first, second); // first/second should usually be NULL_TYPE
case NULL_TYPE:
return 1;
case CLOSURE_TYPE:
equal = 1;
equal &= equal_helper(first->cl.paramNames, second->cl.paramNames);
equal &= equal_helper(first->cl.functionCode, second->cl.functionCode);
equal &= (first->cl.frame == second->cl.frame);
return equal;
case PRIMITIVE_TYPE:
return (first->pf == second->pf);
default:
fprintf(stderr, "Evaluation error: primitive function `equal?`: unexpected value of type %d\n", first->type);
texit(4);
}
return equal;
}
Value *prim_equal(Value *args) {
int argc = length(args);
if (argc != 2) {
fprintf(stderr, "Evaluation error: built-in function `equal?`: expected 2 arguments, received %d\n", argc);
texit(4);
}
return makeBool(equal_helper(car(args), car(cdr(args))));
}
////////////////////////////////////////
///////// EVALUATION FUNCTIONS /////////
////////////////////////////////////////
Value *apply(Value *function, Value *args) {
Value *result, *curr_param, *curr_arg;
Frame *new_frame;
if (function->type == PRIMITIVE_TYPE) {
return function->pf(args);
} else if (function->type != CLOSURE_TYPE) {
fprintf(stderr, "Evaluation error: wrong type to apply: expected type %d (CLOSURE_TYPE), received type %d\n", CLOSURE_TYPE, function->type);
texit(4);
}
new_frame = talloc(sizeof(Frame));
new_frame->bindings = makeNull();
new_frame->parent = function->cl.frame;
curr_param = function->cl.paramNames;
curr_arg = args;
if (curr_param->type == SYMBOL_TYPE) {
new_frame->bindings = cons(cons(curr_param, curr_arg), new_frame->bindings);
} else {
while (curr_param->type == CONS_TYPE) {
if (curr_arg->type != CONS_TYPE) {
goto APPLY_WRONG_NUMBER_ARGS;
}
// lambda assures that parameters list is well-formed
new_frame->bindings = cons(cons(car(curr_param), car(curr_arg)), new_frame->bindings);
curr_param = cdr(curr_param);
curr_arg = cdr(curr_arg);
}
if (curr_arg->type != NULL_TYPE)
goto APPLY_WRONG_NUMBER_ARGS;
}
curr_arg = function->cl.functionCode; // reuse curr_arg, now for body code
while (curr_arg->type == CONS_TYPE) {
result = eval(car(curr_arg), new_frame);
curr_arg = cdr(curr_arg);
}
// lambda assures that body code is a list with at least one element
return result;
APPLY_WRONG_NUMBER_ARGS:
fprintf(stderr, "Evaluation error: possibly wrong number of arguments to apply\n");
fprintf(stderr, "Expected: ");
display_to_fd(function->cl.paramNames, stderr);
fprintf(stderr, "Received: ");
display_to_fd(args, stderr);
texit(4);
return NULL; // will never return
}
void bind_primitive(char *name, Value *(*function)(Value *), Frame *frame){
Value *name_val, *func_val;
name_val = talloc(sizeof(Value));
name_val->type = SYMBOL_TYPE;
name_val->s = talloc(sizeof(name));
strcpy(name_val->s, name);
func_val = talloc(sizeof(Value));
func_val->type = PRIMITIVE_TYPE;
func_val->pf = function;
frame->bindings = cons(cons(name_val, func_val), frame->bindings);
}
Value *eval_all(Value *exprs, Frame *frame) {
Value *current, *tail, head;
switch (exprs->type) {
case CONS_TYPE:
break;
case NULL_TYPE:
return exprs;
default:
fprintf(stderr, "Evaluation error: expected CONS_TYPE or NULL_TYPE in eval_all, received type %d\n", exprs->type);
texit(4);
}
head.c.cdr = NULL;
tail = &head;
current = exprs;
while (current->type != NULL_TYPE) {
tail->c.cdr = cons(eval(car(current), frame), NULL);
tail = tail->c.cdr;
current = cdr(current);
}
tail->c.cdr = current;
return head.c.cdr;