-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslip.c
1481 lines (1176 loc) · 26.6 KB
/
slip.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
/*
Bootstrap slip.
Ideas from bootstrap scheme from Peter Michaux (http://peter.michaux.ca/)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <ctype.h>
#include <stdarg.h>
#include <assert.h>
#include "dlist.h"
#include "slip.h"
#include "slip_parser.h"
#include "slip_primitives.h"
static pSlipObject slip_eval(pSlip gd, pSlipObject exp, pSlipEnvironment env);
///////////////////////////////////////////////////////////////////////////////////////////
void FreeToken(void *t)
{
pToken tt = t;
//LogInfo("free token (%i)[%s]\n", tt->id, tt->z);
if (tt == NULL)
return;
if (tt->z != NULL)
free(tt->z);
free(tt);
}
pToken re2c_NewToken(pSlip ctx, char *z, int len, int id)
{
pToken x;
assert(ctx != NULL);
x = calloc(1, sizeof(uToken));
x->id = id;
x->z = calloc(1, len + 4);
x->line = ctx->parse_data.current_line;
memmove(x->z, z, len);
return x;
}
///////////////////////////////////////////////////////////////////////////////////////////
static void FreeEnvironmentVariable(void *data)
{
pSlipValue v = data;
// dont free contents, juts container
free(v);
}
static void FreeEnvironment(void *data)
{
pSlipEnvironment env;
env = data;
FreeDList(env->lstVars);
free(env);
}
static pSlipValue NewValue(void)
{
pSlipValue v;
v = calloc(1, sizeof(uSlipValue));
return v;
}
static pSlipEnvironment NewEnvironment(void)
{
pSlipEnvironment env;
env = calloc(1, sizeof(uSlipEnvironment));
env->lstVars = NewDList(FreeEnvironmentVariable);
return env;
}
// gd can be null here, as we initialise the singleton TRUE/FALSE objects
pSlipObject s_NewObject(pSlip gd)
{
pSlipObject o;
o = calloc(1, sizeof(uSlipObject));
if (o == NULL)
{
fflush(stdout);
fflush(stdin);
fprintf(stderr, "out of memory\n");
exit(1);
}
o->id = gd->obj_id++;
o->type = eType_NIL;
assert(gd->lstObjects != NULL);
dlist_ins(gd->lstObjects, o);
return o;
}
static int s_IsSingleton(pSlip gd, pSlipObject obj)
{
if (obj->id < USER_OBJECT_ID_START)
return S_TRUE;
return S_FALSE;
}
static void s_ReleaseObject(pSlip gd, pSlipObject obj)
{
if (obj == NULL)
return;
assert(obj->type >= 1 && obj->type < eType_MAX_TYPES);
// dont free up our base objects if context is in run mode.
if (gd->running == SLIP_RUNNING && obj->id < USER_OBJECT_ID_START)
return;
switch (obj->type)
{
case eType_STRING:
free(obj->data.string.data);
break;
case eType_SYMBOL:
free(obj->data.symbol.value);
break;
case eType_PAIR:
case eType_EMPTY_LIST:
case eType_INTNUM:
case eType_NIL:
case eType_BOOL:
case eType_CHARACTER:
case eType_PRIMITIVE_PROC:
case eType_COMPOUND_PROC:
break;
default:
fflush(stdout);
fflush(stdin);
fprintf(stderr, "cannot free unknown type %i\n", obj->type);
exit(1);
}
memset(obj, 0x0, sizeof(uSlipObject));
free(obj);
}
pSlipObject s_NewInteger(pSlip gd, int64_t value)
{
pSlipObject obj;
obj = s_NewObject(gd);
obj->type = eType_INTNUM;
obj->data.intnum.value = value;
return obj;
}
pSlipObject s_NewCharacter(pSlip gd, int64_t value)
{
pSlipObject obj;
obj = s_NewObject(gd);
obj->type = eType_CHARACTER;
obj->data.character.value = value;
return obj;
}
pSlipObject s_NewString(pSlip gd, uint8_t *data, int length)
{
pSlipObject obj;
DLElement *e;
e = dlist_head(gd->lstStrings);
while (e != NULL)
{
obj = dlist_data(e);
e = dlist_next(e);
if (obj->data.string.length == length)
{
if ( memcmp(obj->data.string.data, data, length) == 0)
return obj;
}
}
obj = s_NewObject(gd);
obj->type = eType_STRING;
obj->data.string.data = malloc(length + 4);
memmove(obj->data.string.data, data, 1+length);
obj->data.string.length = length;
dlist_ins(gd->lstStrings, obj);
return obj;
}
pSlipObject s_NewSymbol(pSlip gd, uint8_t *data)
{
pSlipObject obj;
DLElement *e;
e = dlist_head(gd->lstSymbols);
while (e != NULL)
{
obj = dlist_data(e);
e = dlist_next(e);
if (strcmp(obj->data.symbol.value, data) == 0)
return obj;
}
obj = s_NewObject(gd);
obj->type = eType_SYMBOL;
obj->data.symbol.value = strdup(data);
dlist_ins(gd->lstSymbols, obj);
return obj;
}
pSlipObject s_NewBool(pSlip gd, int value)
{
pSlipObject obj;
assert(value == S_TRUE || value == S_FALSE);
obj = s_NewObject(gd);
obj->type = eType_BOOL;
obj->data.boolean.value = value;
return obj;
}
pSlipObject s_NewCompoundProc(pSlip gd, pSlipObject params, pSlipObject code, pSlipEnvironment env)
{
pSlipObject obj;
obj = s_NewObject(gd);
obj->type = eType_COMPOUND_PROC;
obj->data.comp_proc.params = params;
obj->data.comp_proc.code = code;
obj->data.comp_proc.env = env;
return obj;
}
static int IsObjectX(pSlipObject o, int type)
{
if (o->type == type)
return S_TRUE;
else
return S_FALSE;
}
int sIsObject_CompoundProc(pSlipObject obj)
{
return IsObjectX(obj, eType_COMPOUND_PROC);
}
int sIsObject_EmptyList(pSlip gd, pSlipObject obj)
{
return IsObjectX(obj, eType_EMPTY_LIST);
}
int sIsObject_String(pSlipObject obj)
{
return IsObjectX(obj, eType_STRING);
}
int sIsObject_Character(pSlipObject obj)
{
return IsObjectX(obj, eType_CHARACTER);
}
int sIsObject_Pair(pSlipObject obj)
{
return IsObjectX(obj, eType_PAIR);
}
int sIsObject_Boolean(pSlipObject obj)
{
return IsObjectX(obj, eType_BOOL);
}
int sIsObject_Symbol(pSlipObject obj)
{
return IsObjectX(obj, eType_SYMBOL);
}
int sIsObject_Integer(pSlipObject obj)
{
return IsObjectX(obj, eType_INTNUM);
}
pSlipObject s_NewPair(pSlip gd, pSlipObject car, pSlipObject cdr)
{
pSlipObject obj;
obj = s_NewObject(gd);
obj->type = eType_PAIR;
obj->data.pair.car = car;
obj->data.pair.cdr = cdr;
return obj;
}
pSlipObject cons(pSlip gd, pSlipObject car, pSlipObject cdr)
{
return s_NewPair(gd, car, cdr);
}
pSlipObject car(pSlipObject pair)
{
return pair->data.pair.car;
}
void set_car(pSlipObject obj, pSlipObject value)
{
obj->data.pair.car = value;
}
pSlipObject cdr(pSlipObject pair)
{
return pair->data.pair.cdr;
}
void set_cdr(pSlipObject obj, pSlipObject value)
{
obj->data.pair.cdr = value;
}
static pSlipEnvironment enclosing_environment(pSlipEnvironment env)
{
return env->parent;
}
static pSlipValue ScanForVar(pSlipObject o, pSlipEnvironment env)
{
DLElement *e;
pSlipValue v;
e = dlist_head(env->lstVars);
while (e != NULL)
{
v = dlist_data(e);
e = dlist_next(e);
if (v->var == o)
{
return v;
}
}
return NULL;
}
static pSlipObject lookup_variable_value(pSlip gd, pSlipObject var, pSlipEnvironment env)
{
pSlipValue v;
do
{
v = ScanForVar(var, env);
if (v != NULL)
return v->val;
env = enclosing_environment(env);
}while (env != NULL);
throw_error(gd, "unbound variable %s for lookup\n", (char*)var->data.symbol.value);
return NULL;
}
static void set_variable_value(pSlip gd, pSlipObject var, pSlipObject val, pSlipEnvironment env)
{
pSlipValue v;
do
{
v = ScanForVar(var, env);
if (v != NULL)
{
v->val = val;
return;
}
env = enclosing_environment(env);
}while (env != NULL);
throw_error(gd, "unbound variable %s for assignment\n", (char*)var->data.symbol.value);
}
static pSlipObject make_primitive_proc(pSlip gd, pSlipObject (*func)(pSlip gd, pSlipObject args))
{
pSlipObject obj;
obj = s_NewObject(gd);
obj->type = eType_PRIMITIVE_PROC;
obj->data.prim_proc.func = func;
return obj;
}
static void define_variable(pSlip gd, pSlipObject var, pSlipObject val, pSlipEnvironment env)
{
pSlipValue v;
v = ScanForVar(var, env);
if (v != NULL)
{
v->val = val;
return;
}
v = NewValue();
v->var = var;
v->val = val;
dlist_ins(env->lstVars, v);
}
void slip_add_procedure(pSlip gd, pSlipEnvironment env, char *sym, pSlipObject (*func)(pSlip gd, pSlipObject args))
{
define_variable(gd, s_NewSymbol(gd, sym), make_primitive_proc(gd, func), env);
}
static pSlipEnvironment setup_environment(pSlip gd, pSlipEnvironment parent, pSlipObject params, pSlipObject args)
{
pSlipEnvironment env;
env = NewEnvironment();
dlist_ins(gd->lstGlobalEnvironment, env);
env->parent = parent;
// propagate args+params across time and space!
while (args != gd->singleton_EmptyList && params != gd->singleton_EmptyList)
{
define_variable(gd, car(params), car(args), env);
args = cdr(args);
params = cdr(params);
}
return env;
}
///////////////////////////////////////////////////////////////////////////
static int is_self_evaluating(pSlipObject exp)
{
return sIsObject_Boolean(exp) == S_TRUE || sIsObject_Integer(exp) == S_TRUE || sIsObject_Character(exp) == S_TRUE || sIsObject_String(exp) == S_TRUE ;
}
static int is_tagged_list(pSlipObject expression, pSlipObject tag)
{
pSlipObject the_car;
if (sIsObject_Pair(expression) == S_TRUE)
{
the_car = car(expression);
if (sIsObject_Symbol(the_car) == S_TRUE && (the_car == tag))
return S_TRUE;
}
return S_FALSE;
}
static int is_quoted(pSlip gd, pSlipObject expression)
{
return is_tagged_list(expression, gd->singleton_QuoteSymbol);
}
static int is_variable(pSlipObject expression)
{
return sIsObject_Symbol(expression);
}
static pSlipObject text_of_quotation(pSlipObject exp)
{
return cadr(exp);
}
static int is_assignment(pSlip gd, pSlipObject exp)
{
return is_tagged_list(exp, gd->singleton_SetSymbol);
}
static pSlipObject assignment_variable(pSlipObject exp)
{
return car(cdr(exp));
}
static pSlipObject assignment_value(pSlipObject exp)
{
return car(cdr(cdr(exp)));
}
static char is_definition(pSlip gd, pSlipObject exp)
{
return is_tagged_list(exp, gd->singleton_DefineSymbol);
}
static pSlipObject definition_variable(pSlipObject exp)
{
if (sIsObject_Symbol(cadr(exp)) == S_TRUE)
{
return cadr(exp);
}
else
{
return caadr(exp);
}
}
static pSlipObject make_lambda(pSlip gd, pSlipObject params, pSlipObject code)
{
return cons(gd, gd->singleton_Lambda, cons(gd, params, code));
}
static int is_lambda(pSlip gd, pSlipObject exp)
{
return is_tagged_list(exp, gd->singleton_Lambda);
}
static pSlipObject lambda_parameters(pSlipObject exp)
{
return cadr(exp);
}
static pSlipObject lambda_body(pSlipObject exp)
{
return cddr(exp);
}
static pSlipObject make_application(pSlip gd, pSlipObject op, pSlipObject operands)
{
return cons(gd, op, operands);
}
static int is_let(pSlip gd, pSlipObject exp)
{
return is_tagged_list(exp, gd->singleton_Let);
}
static pSlipObject let_bindings(pSlipObject exp)
{
return cadr(exp);
}
static pSlipObject let_body(pSlipObject exp)
{
return cddr(exp);
}
static pSlipObject binding_parameter(pSlipObject binding)
{
return car(binding);
}
static pSlipObject binding_argument(pSlipObject binding)
{
return cadr(binding);
}
static pSlipObject bindings_parameters(pSlip gd, pSlipObject bindings)
{
return sIsObject_EmptyList(gd, bindings) ? gd->singleton_EmptyList : cons(gd, binding_parameter(car(bindings)), bindings_parameters(gd, cdr(bindings)));
}
static pSlipObject bindings_arguments(pSlip gd, pSlipObject bindings)
{
return sIsObject_EmptyList(gd, bindings) ? gd->singleton_EmptyList : cons(gd, binding_argument(car(bindings)), bindings_arguments(gd, cdr(bindings)));
}
static pSlipObject let_parameters(pSlip gd, pSlipObject exp)
{
return bindings_parameters(gd, let_bindings(exp));
}
static pSlipObject let_arguments(pSlip gd, pSlipObject exp)
{
return bindings_arguments(gd, let_bindings(exp));
}
static pSlipObject let_to_application(pSlip gd, pSlipObject exp)
{
return make_application(gd, make_lambda(gd, let_parameters(gd, exp), let_body(exp)), let_arguments(gd, exp));
}
static pSlipObject definition_value(pSlip gd, pSlipObject exp, pSlipEnvironment env)
{
if (sIsObject_Symbol(cadr(exp)) == S_TRUE)
{
return caddr(exp);
}
else
{
return make_lambda(gd, cdadr(exp), cddr(exp));
}
}
void throw_error(pSlip gd, char *s, ...)
{
va_list args;
char *x;
x = calloc(1, 1024);
va_start(args, s);
vsnprintf(x, 1023, s, args);
va_end(args);
fflush(stdout);
fflush(stdin);
fprintf(stderr, "%s", x);
fflush(stderr);
free(x);
gd->running = SLIP_ERROR;
}
static int is_last_exp(pSlip gd, pSlipObject seq)
{
return sIsObject_EmptyList(gd, cdr(seq));
}
static pSlipObject first_exp(pSlipObject seq)
{
return car(seq);
}
static pSlipObject rest_exps(pSlipObject seq)
{
return cdr(seq);
}
static int sIsObject_PrimitiveProc(pSlipObject obj)
{
if (obj->type == eType_PRIMITIVE_PROC)
return S_TRUE;
else
return S_FALSE;
}
static pSlipObject make_begin(pSlip gd, pSlipObject exp)
{
return cons(gd, gd->singleton_Begin, exp);
}
static pSlipObject make_if(pSlip gd, pSlipObject predicate, pSlipObject consequent, pSlipObject alternative)
{
return cons(gd, gd->singleton_IFSymbol, cons(gd, predicate, cons(gd, consequent, cons(gd, alternative, gd->singleton_EmptyList))));
}
pSlipObject eval_assignment(pSlip gd, pSlipObject exp, pSlipEnvironment env)
{
pSlipObject a1;
pSlipObject z1;
pSlipObject z2;
z1 = assignment_value(exp);
assert(z1 != NULL);
a1 = slip_eval(gd, z1, env);
assert(a1 != NULL);
z2 = assignment_variable(exp);
assert(z2 != NULL);
set_variable_value(gd, z2, a1, env);
if (gd->running == SLIP_RUNNING)
return gd->singleton_OKSymbol;
else
return gd->singleton_False;
}
pSlipObject eval_definition(pSlip gd, pSlipObject exp, pSlipEnvironment env)
{
pSlipObject a2;
pSlipObject x1;
pSlipObject x2;
x1 = definition_value(gd, exp, env);
assert(x1 != NULL);
a2 = slip_eval(gd, x1, env);
assert(a2 != NULL);
x2 = definition_variable(exp);
assert(x2 != NULL);
define_variable(gd, x2, a2, env);
if (gd->running == SLIP_RUNNING)
return gd->singleton_OKSymbol;
else
return gd->singleton_False;
}
static int is_if(pSlip gd, pSlipObject expression)
{
return is_tagged_list(expression, gd->singleton_IFSymbol);
}
static pSlipObject if_predicate(pSlipObject exp)
{
return cadr(exp);
}
static pSlipObject if_consequent(pSlipObject exp)
{
return caddr(exp);
}
static int is_true(pSlip gd, pSlipObject obj)
{
// all values are true except for false itself.
if (gd->singleton_False == obj)
return S_FALSE;
else
return S_TRUE;
}
static pSlipObject if_alternative(pSlip gd, pSlipObject exp)
{
if (sIsObject_EmptyList(gd, cdddr(exp)) == S_TRUE)
{
return gd->singleton_False;
}
else
{
return cadddr(exp);
}
}
static int is_begin(pSlip gd, pSlipObject exp)
{
return is_tagged_list(exp, gd->singleton_Begin);
}
static pSlipObject begin_actions(pSlipObject exp)
{
return cdr(exp);
}
static int is_cond(pSlip gd, pSlipObject exp)
{
return is_tagged_list(exp, gd->singleton_Cond);
}
static pSlipObject cond_clauses(pSlipObject exp)
{
return cdr(exp);
}
static pSlipObject cond_predicate(pSlipObject clause)
{
return car(clause);
}
static pSlipObject cond_actions(pSlipObject clause)
{
return cdr(clause);
}
static int is_cond_else_clause(pSlip gd, pSlipObject clause)
{
if (cond_predicate(clause) == gd->singleton_Else)
return S_TRUE;
else
return S_FALSE;
}
static pSlipObject sequence_to_exp(pSlip gd, pSlipObject seq)
{
if (sIsObject_EmptyList(gd, seq) == S_TRUE)
{
return seq;
}
else if (is_last_exp(gd, seq) == S_TRUE)
{
return first_exp(seq);
}
else
{
return make_begin(gd, seq);
}
}
static pSlipObject expand_clauses(pSlip gd, pSlipObject clauses)
{
pSlipObject first;
pSlipObject rest;
if (sIsObject_EmptyList(gd, clauses) == S_TRUE)
{
return gd->singleton_False;
}
else
{
first = car(clauses);
rest = cdr(clauses);
if (is_cond_else_clause(gd, first) == S_TRUE)
{
if (sIsObject_EmptyList(gd, rest) == S_TRUE)
{
return sequence_to_exp(gd, cond_actions(first));
}
else
{
throw_error(gd, "else clause isn't last cond->if");
return gd->singleton_False;
}
}
else
{
return make_if(gd, cond_predicate(first), sequence_to_exp(gd, cond_actions(first)), expand_clauses(gd, rest));
}
}
}
static pSlipObject cond_to_if(pSlip gd, pSlipObject exp)
{
return expand_clauses(gd, cond_clauses(exp));
}
static int is_application(pSlipObject exp)
{
return sIsObject_Pair(exp);
}
static pSlipObject slip_operator(pSlipObject exp)
{
return car(exp);
}
static pSlipObject operands(pSlipObject exp)
{
return cdr(exp);
}
int is_no_operands(pSlip gd, pSlipObject ops)
{
return sIsObject_EmptyList(gd, ops);
}
static pSlipObject first_operand(pSlipObject ops)
{
return car(ops);
}
static pSlipObject rest_operands(pSlipObject ops)
{
return cdr(ops);
}
static pSlipObject list_of_values(pSlip gd, pSlipObject exps, pSlipEnvironment env)
{
if (is_no_operands(gd, exps) == S_TRUE)
{
return gd->singleton_EmptyList;
}
else
{
pSlipObject x;
x = slip_eval(gd, first_operand(exps), env);
if (x == NULL)
x = gd->singleton_Nil;
return cons(gd, x, list_of_values(gd, rest_operands(exps), env));
}
}
static pSlipObject slip_eval(pSlip gd, pSlipObject exp, pSlipEnvironment env)
{
pSlipObject proc;
pSlipObject args;
tailcall:
if (is_self_evaluating(exp) == S_TRUE)
{
return exp;
}
else if (is_variable(exp) == S_TRUE)
{
return lookup_variable_value(gd, exp, env);
}
else if (is_quoted(gd, exp) == S_TRUE)
{
return text_of_quotation(exp);
}
else if (is_assignment(gd, exp) == S_TRUE)
{
return eval_assignment(gd, exp, env);
}
else if (is_definition(gd, exp) == S_TRUE)
{
return eval_definition(gd, exp, env);
}
else if (is_if(gd, exp) == S_TRUE)
{
exp = is_true(gd, slip_eval(gd, if_predicate(exp), env)) == S_TRUE ? if_consequent(exp) : if_alternative(gd, exp);
goto tailcall;
}
else if (is_lambda(gd, exp) == S_TRUE)
{
return s_NewCompoundProc(gd, lambda_parameters(exp), lambda_body(exp), env);
}
else if (is_begin(gd, exp) == S_TRUE)
{
exp = begin_actions(exp);
while (!is_last_exp(gd, exp))
{
slip_eval(gd, first_exp(exp), env);
exp = rest_exps(exp);
}
exp = first_exp(exp);
goto tailcall;
}
else if (is_cond(gd, exp) == S_TRUE)
{
exp = cond_to_if(gd, exp);
goto tailcall;
}
else if (is_let(gd, exp) == S_TRUE)
{
exp = let_to_application(gd, exp);
goto tailcall;
}
else if (is_application(exp) == S_TRUE)
{
proc = slip_eval(gd, slip_operator(exp), env);
if (proc == NULL)
return gd->singleton_False;
if (proc->type == eType_PRIMITIVE_PROC || proc->type == eType_COMPOUND_PROC)
{
args = list_of_values(gd, operands(exp), env);
if (args == NULL)
return gd->singleton_False;
if (sIsObject_PrimitiveProc(proc) == S_TRUE)
{
return proc->data.prim_proc.func(gd, args);
}
else if (sIsObject_CompoundProc(proc) == S_TRUE)
{
env = setup_environment(gd, proc->data.comp_proc.env, proc->data.comp_proc.params, args);
exp = make_begin(gd, proc->data.comp_proc.code);
goto tailcall;
}
else
{
throw_error(gd, "unknown procedure type\n");
return gd->singleton_False;
}
}
else
return proc;
}