-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.c
8004 lines (6881 loc) · 236 KB
/
eval.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
/* Copyright 2010-2024
* Kaz Kylheku <kaz@kylheku.com>
* Vancouver, Canada
* All rights reserved.
*
* 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.
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <wchar.h>
#include <signal.h>
#include <time.h>
#include <assert.h>
#include "config.h"
#include "alloca.h"
#include "lib.h"
#include "gc.h"
#include "args.h"
#include "arith.h"
#include "signal.h"
#include "unwind.h"
#include "regex.h"
#include "stream.h"
#include "y.tab.h"
#include "parser.h"
#include "hash.h"
#include "debug.h"
#include "match.h"
#include "txr.h"
#include "combi.h"
#include "autoload.h"
#include "struct.h"
#include "cadr.h"
#include "filter.h"
#include "tree.h"
#include "vm.h"
#include "buf.h"
#include "eval.h"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#if CONFIG_SMALL_MEM
#define MAP_ALLOCA_LIMIT 1024
#else
#define MAP_ALLOCA_LIMIT 4096
#endif
typedef val (*opfun_t)(val, val);
struct c_var {
val *loc;
val bind;
};
val top_vb, top_fb, top_mb, top_smb, special, builtin;
val op_table, pm_table;
val dyn_env;
val eval_error_s, case_error_s;
val dwim_s, progn_s, prog1_s, prog2_s, progv_s, sys_blk_s;
val let_s, let_star_s, lambda_s, call_s, dvbind_s;
val sys_catch_s, handler_bind_s, cond_s, if_s, iflet_s, when_s, usr_var_s;
val defvar_s, defvarl_s, defparm_s, defparml_s, defun_s, defmacro_s, macro_s;
val tree_case_s, tree_bind_s, mac_param_bind_s, mac_env_param_bind_s;
val sys_mark_special_s;
val caseq_s, caseql_s, casequal_s;
val caseq_star_s, caseql_star_s, casequal_star_s;
val memq_s, memql_s, memqual_s;
val eq_s, eql_s, equal_s, less_s;
val car_s, cdr_s, not_s, vecref_s;
val setq_s, setqf_s, sys_lisp1_value_s, sys_lisp1_setq_s;
val sys_l1_val_s, sys_l1_setq_s;
val inc_s;
val for_s, for_star_s, each_s, each_star_s, collect_each_s, collect_each_star_s;
val for_op_s, each_op_s;
val append_each_s, append_each_star_s, while_s, while_star_s, until_star_s;
val dohash_s;
val uw_protect_s, return_s, return_from_s, sys_abscond_from_s, block_star_s;
val list_s, list_star_s, append_s, apply_s, sys_apply_s, iapply_s;
val gen_s, gun_s, generate_s, rest_s;
val promise_s, promise_forced_s, promise_inprogress_s, force_s;
val op_s, identity_s;
val hash_lit_s, hash_construct_s, struct_lit_s, qref_s, uref_s;
val vector_lit_s, vec_list_s, tree_lit_s, tree_construct_s;
val macro_time_s, macrolet_s;
val defsymacro_s, symacrolet_s, prof_s, switch_s, struct_s;
val fbind_s, lbind_s, flet_s, labels_s;
val load_path_s, load_hooks_s, load_recursive_s, load_search_dirs_s;
val load_args_s, load_time_s, load_time_lit_s;
val eval_only_s, compile_only_s, compiler_let_s;
val const_foldable_s;
val pct_fun_s;
val special_s, unbound_s;
val whole_k, form_k, symacro_k, macro_k;
val last_form_evaled;
val call_f, iter_begin_f, iter_from_binding_f, iter_more_f;
val iter_item_f, iter_step_f;
val join_f;
val origin_hash;
static val unused_arg_s;
static val const_foldable_hash;
val make_env(val vbindings, val fbindings, val up_env)
{
val env = make_obj();
env->e.type = ENV;
env->e.vbindings = vbindings;
env->e.fbindings = fbindings;
env->e.up_env = up_env;
return env;
}
val copy_env(val oenv)
{
type_check(lit("copy-env"), oenv, ENV);
{
val vb = copy_alist(oenv->e.vbindings);
val fb = copy_alist(oenv->e.fbindings);
val nenv = make_obj();
nenv->e.type = ENV;
nenv->e.vbindings = vb;
nenv->e.fbindings = fb;
nenv->e.up_env = oenv->e.up_env;
return nenv;
}
}
val deep_copy_env(val oenv)
{
type_check(lit("deep-copy-env"), oenv, ENV);
{
val vb = copy_alist(oenv->e.vbindings);
val fb = copy_alist(oenv->e.fbindings);
val up_env = if2(oenv->e.up_env != nil,
deep_copy_env(oenv->e.up_env));
val nenv = make_obj();
nenv->e.type = ENV;
nenv->e.vbindings = vb;
nenv->e.fbindings = fb;
nenv->e.up_env = up_env;
return nenv;
}
}
/*
* Wrapper for performance reasons: don't make make_env
* process default arguments.
*/
static val make_env_intrinsic(val vbindings, val fbindings, val up_env)
{
vbindings = default_null_arg(vbindings);
fbindings = default_null_arg(fbindings);
up_env = default_null_arg(up_env);
return make_env(vbindings, fbindings, up_env);
}
val env_fbind(val env, val sym, val fun)
{
val self = lit("env-fbind");
if (env) {
val cell;
type_check(self, env, ENV);
cell = acons_new_c(sym, nulloc, mkloc(env->e.fbindings, env));
return rplacd(cell, fun);
} else {
return sethash(top_fb, sym, fun);
}
}
val env_vbind(val env, val sym, val obj)
{
val self = lit("env-vbind");
if (env) {
val cell;
type_check(self, env, ENV);
cell = acons_new_c(sym, nulloc, mkloc(env->e.vbindings, env));
return rplacd(cell, obj);
} else {
return sethash(top_vb, sym, obj);
}
}
static val env_vbindings(val env)
{
val self = lit("env-vbindings");
type_check(self, env, ENV);
return env->e.vbindings;
}
static val env_fbindings(val env)
{
val self = lit("env-fbindings");
type_check(self, env, ENV);
return env->e.fbindings;
}
static val env_next(val env)
{
val self = lit("env-next");
type_check(self, env, ENV);
return env->e.up_env;
}
static void env_vb_to_fb(val env)
{
if (env) {
type_check(lit("expand"), env, ENV);
env->e.fbindings = env->e.vbindings;
env->e.vbindings = nil;
}
}
static val env_to_menv(val env, val self, val menv)
{
if (env == nil) {
return menv;
} else {
val iter;
type_check(self, env, ENV);
if (!menv)
menv = make_env(nil, nil, nil);
if (env->e.up_env)
menv = env_to_menv(env->e.up_env, self, menv);
for (iter = env->e.vbindings; iter; iter = cdr(iter)) {
val binding = car(iter);
env_vbind(menv, car(binding), special_s);
}
for (iter = env->e.fbindings; iter; iter = cdr(iter)) {
val binding = car(iter);
env_fbind(menv, car(binding), special_s);
}
return menv;
}
}
val ctx_form(val obj)
{
if (consp(obj))
return obj;
if (interp_fun_p(obj))
return obj->f.f.interp_fun;
return nil;
}
val ctx_name(val obj)
{
if (consp(obj)) {
if (car(obj) == lambda_s)
return list(lambda_s, second(obj), nao);
else
return car(obj);
}
if (interp_fun_p(obj))
return func_get_name(obj, obj->f.env);
return nil;
}
static void eval_exception(val sym, val ctx, val fmt, int ex_time, va_list vl)
{
uses_or2;
val form = ctx_form(ctx);
val stream = make_string_output_stream();
val loc = or2(source_loc_str(form, nil),
source_loc_str(last_form_evaled, nil));
val msg;
if (loc)
format(stream, lit("~a: "), loc, nao);
(void) vformat(stream, fmt, vl);
msg = get_string_from_stream(stream);
if (ex_time) {
val loading = cdr(lookup_var(nil, load_recursive_s));
val error_caught = uw_find_frame(error_s, catch_frame_s);
if (loading && !error_caught) {
uw_dump_deferred_warnings(std_error);
put_line(msg, std_error);
}
}
uw_rthrow(sym, msg);
}
NORETURN val eval_error(val ctx, val fmt, ...)
{
va_list vl;
va_start (vl, fmt);
eval_exception(eval_error_s, ctx, fmt, 0, vl);
va_end (vl);
abort();
}
static val eval_warn(val ctx, val fmt, ...)
{
va_list vl;
uw_catch_begin (cons(continue_s, nil), exsym, exvals);
va_start (vl, fmt);
eval_exception(warning_s, ctx, scat2(lit("warning: "), fmt), 0, vl);
va_end (vl);
uw_catch(exsym, exvals) { (void) exsym; (void) exvals; }
uw_unwind;
uw_catch_end;
return nil;
}
static val eval_defr_warn(val ctx, val tag, val fmt, ...)
{
uses_or2;
va_list vl;
va_start (vl, fmt);
uw_catch_begin (cons(continue_s, nil), exsym, exvals);
{
val form = ctx_form(ctx);
val stream = make_string_output_stream();
val loc = or2(source_loc_str(form, nil),
source_loc_str(last_form_evaled, nil));
if (loc)
format(stream, lit("~a: "), loc, nao);
(void) vformat(stream, scat2(lit("warning: "), fmt), vl);
uw_rthrow(defr_warning_s,
cons(get_string_from_stream(stream), cons(tag, nil)));
}
uw_catch(exsym, exvals) { (void) exsym; (void) exvals; }
uw_unwind;
uw_catch_end;
va_end (vl);
return nil;
}
static NORETURN val expand_error(val ctx, val fmt, ...)
{
va_list vl;
va_start (vl, fmt);
eval_exception(eval_error_s, ctx, fmt, 1, vl);
va_end (vl);
abort();
}
val lookup_origin(val form)
{
return gethash(origin_hash, form);
}
static val set_origin(val form, val origin)
{
if (origin && form != origin && is_ptr(form) &&
(!symbolp(form) || !symbol_package(form)) &&
is_ptr(origin) && (!symbolp(origin) || !symbol_package(origin)))
sethash(origin_hash, form, origin);
return form;
}
val set_last_form_evaled(val form)
{
val prev = last_form_evaled;
last_form_evaled = form;
return prev;
}
void error_trace(val exsym, val exvals, val out_stream, val prefix)
{
val last = last_form_evaled;
val xlast = uw_last_form_expanded();
val info = source_loc_str(last, nil);
val max_length = nil, max_depth = nil;
val saved_de = set_dyn_env(make_env(nil, nil, dyn_env));
env_vbind(dyn_env, print_circle_s, t);
uw_dump_deferred_warnings(out_stream);
if (uw_exception_subtype_p(exsym, stack_overflow_s)) {
max_length = set_max_length(out_stream, num_fast(5));
max_depth = set_max_depth(out_stream, num_fast(5));
}
if (cdr(exvals) || !stringp(car(exvals)))
format(out_stream, lit("~a exception args: ~!~s\n"),
prefix, exvals, nao);
else
format(out_stream, lit("~a ~!~a\n"), prefix, car(exvals), nao);
if (info) {
val first, origin, oinfo = nil;
for (first = t; last; last = origin, first = nil) {
info = oinfo ? oinfo : info;
origin = lookup_origin(last);
oinfo = source_loc_str(origin, nil);
if (first) {
if (origin)
format(out_stream, lit("~a during evaluation of form ~!~s\n"),
prefix, last, nao);
else if (!uw_exception_subtype_p(exsym, eval_error_s))
format(out_stream, lit("~a during evaluation at ~a of form ~!~s\n"),
prefix, info, last, nao);
}
if (origin) {
format(out_stream, lit("~a ... an expansion of ~!~s\n"),
prefix, origin, nao);
} else if (!first) {
if (info)
format(out_stream, lit("~a which is located at ~a\n"), prefix,
info, nao);
else
format(out_stream, lit("~a whose location is unavailable\n"), prefix,
nao);
}
if (origin == last)
break;
}
}
if (xlast) {
val ex_info = source_loc_str(xlast, nil);
val form = xlast;
if (ex_info)
format(out_stream, lit("~a during expansion at ~a of form ~!~s\n"),
prefix, ex_info, xlast, nao);
else
format(out_stream, lit("~a during expansion of form ~!~s\n"),
prefix, xlast, nao);
if (info)
format(out_stream, lit("~a by macro code located at ~a\n"), prefix,
info, nao);
for (;;) {
val origin = lookup_origin(xlast);
val oinfo = source_loc_str(origin, nil);
if (origin) {
if (oinfo)
format(out_stream, lit("~a ... an expansion at ~a of ~!~s\n"),
prefix, oinfo, origin, nao);
else
format(out_stream, lit("~a ... an expansion of ~!~s\n"),
prefix, origin, nao);
if (origin == form)
break;
form = origin;
continue;
}
break;
}
}
#if CONFIG_DEBUG_SUPPORT
if (dbg_backtrace) {
format(out_stream, lit("~a backtrace:\n"), prefix, nao);
debug_dump_backtrace(out_stream, prefix);
}
#endif
if (max_length) {
set_max_length(out_stream, max_length);
set_max_depth(out_stream, max_depth);
}
dyn_env = saved_de;
}
val lookup_global_var(val sym)
{
uses_or2;
return or2(gethash_d(top_vb, sym),
if2(autoload_try_var(sym), gethash_d(top_vb, sym)));
}
val lookup_global_fun(val sym)
{
return lookup_fun(nil, sym);
}
val lookup_var(val env, val sym)
{
if (env) {
type_check(lit("variable lookup"), env, ENV);
for (; env; env = env->e.up_env) {
val binding = assoc(sym, env->e.vbindings);
if (binding) {
if (cdr(binding) == unbound_s)
break;
return binding;
}
}
}
for (env = dyn_env; env; env = env->e.up_env) {
val binding = assoc(sym, env->e.vbindings);
if (binding)
return if3(us_cdr(binding) == unbound_s, nil, binding);
}
return lookup_global_var(sym);
}
val lookup_sym_lisp1(val env, val sym)
{
uses_or2;
if (env) {
type_check(lit("lisp-1-style lookup"), env, ENV);
for (; env; env = env->e.up_env) {
val binding = or2(assoc(sym, env->e.vbindings),
assoc(sym, env->e.fbindings));
if (binding) {
if (cdr(binding) == unbound_s)
break;
return binding;
}
}
}
for (env = dyn_env; env; env = env->e.up_env) {
val binding = or2(assoc(sym, env->e.vbindings),
assoc(sym, env->e.fbindings));
if (binding)
return binding;
}
return or3(gethash_d(top_vb, sym),
if2(autoload_try_fun_var(sym),
gethash_d(top_vb, sym)),
gethash_d(top_fb, sym));
}
loc lookup_var_l(val env, val sym)
{
val binding = lookup_var(env, sym);
if (binding)
return cdr_l(binding);
uw_throwf(error_s, lit("variable ~s unexpectedly unbound"), sym, nao);
}
val lookup_dynamic_var(val sym)
{
return lookup_var(nil, sym);
}
val lookup_dynamic_sym_lisp1(val sym)
{
return lookup_sym_lisp1(nil, sym);
}
static val lookup_mac(val menv, val sym);
val lookup_fun(val env, val sym)
{
uses_or2;
if (consp(sym)) {
if (car(sym) == meth_s) {
val strct = cadr(sym);
val slot = caddr(sym);
val type = or2(find_struct_type(strct),
if2(autoload_try_struct(strct),
find_struct_type(strct)));
if (slot == init_k) {
return cons(sym, struct_get_initfun(type));
} else if (slot == postinit_k) {
return cons(sym, struct_get_postinitfun(type));
} else {
return if2(and2(type, static_slot_p(type, slot)),
cons(sym, static_slot(type, slot)));
}
} else if (car(sym) == macro_s) {
return lookup_mac(nil, cadr(sym));
} else if (car(sym) == lambda_s) {
return cons(sym, func_interp(env, expand(sym, nil)));
} else {
return nil;
}
}
if (env) {
type_check(lit("function lookup"), env, ENV);
for (; env; env = env->e.up_env) {
val binding = assoc(sym, env->e.fbindings);
if (binding)
return binding;
}
}
return or2(gethash_d(top_fb, sym),
if2(autoload_try_fun(sym), gethash_d(top_fb, sym)));
}
val func_get_name(val fun, val env)
{
val self = lit("func-get-name");
env = default_null_arg(env);
type_check(self, fun, FUN);
if (env) {
type_check(self, env, ENV);
{
val iter;
for (; env; env = env->e.up_env) {
for (iter = env->e.fbindings; iter; iter = cdr(iter)) {
val binding = car(iter);
if (cdr(binding) == fun)
return car(binding);
}
}
}
}
{
val name;
if ((name = hash_revget(top_fb, fun, eq_f, nil)))
return name;
if ((name = hash_revget(top_mb, fun, eq_f, nil)))
return list(macro_s, name, nao);
if ((name = method_name(fun)))
return name;
if (interp_fun_p(fun))
return func_get_form(fun);
}
return nil;
}
static val lookup_mac(val menv, val sym)
{
uses_or2;
if (nilp(menv)) {
return or2(gethash_d(top_mb, sym),
if2(autoload_try_fun(sym), gethash_d(top_mb, sym)));
} else {
type_check(lit("macro lookup"), menv, ENV);
{
val binding = assoc(sym, menv->e.fbindings);
if (binding) /* special_s: see make_fun_shadowing_env */
return (cdr(binding) == special_s) ? nil : binding;
return lookup_mac(menv->e.up_env, sym);
}
}
}
static val lookup_symac(val menv, val sym)
{
uses_or2;
if (nilp(menv)) {
return or2(gethash_d(top_smb, sym),
if2(autoload_try_var(sym), gethash_d(top_smb, sym)));
} else {
type_check(lit("symacro lookup"), menv, ENV);
{
val binding = assoc(sym, menv->e.vbindings);
if (binding) /* special_s: see make_var_shadowing_env */
return (cdr(binding) == special_s) ? nil : binding;
return lookup_symac(menv->e.up_env, sym);
}
}
}
static val lookup_symac_lisp1(val menv, val sym)
{
uses_or2;
if (nilp(menv)) {
return or2(gethash_d(top_smb, sym),
if2(autoload_try_var(sym), gethash_d(top_smb, sym)));
} else {
type_check(lit("symacro lookup"), menv, ENV);
/* Of course, we are not looking for symbol macros in the operator macro
* name space. Rather, the object of the lookup rule implemented by this
* function is to allow lexical function bindings to shadow symbol macros.
*/
{
val vbinding = assoc(sym, menv->e.vbindings);
if (vbinding) {
return (cdr(vbinding) == special_s) ? nil : vbinding;
} else {
val fbinding = assoc(sym, menv->e.fbindings);
if (fbinding && cdr(fbinding) == special_s)
return nil;
}
return lookup_symac(menv->e.up_env, sym);
}
}
}
static val reparent_env(val child, val parent)
{
set(mkloc(child->e.up_env, child), parent);
return child;
}
static val special_var_p(val sym)
{
uses_or2;
return or2(gethash(special, sym),
if2(autoload_try_var(sym), gethash(special, sym)));
}
static val lexical_binding_kind(val menv, val sym)
{
if (nilp(menv)) {
return nil;
} else {
type_check(lit("lexical-binding-kind"), menv, ENV);
{
val binding = assoc(sym, menv->e.vbindings);
if (binding) {
/* special_s: see make_var_shadowing_env */
if (cdr(binding) != special_s)
return symacro_k;
else if (special_var_p(sym))
return nil;
return var_k;
}
}
return lexical_binding_kind(menv->e.up_env, sym);
}
}
static val lexical_fun_binding_kind(val menv, val sym)
{
if (nilp(menv)) {
return nil;
} else {
type_check(lit("lexical-fun-binding-kind"), menv, ENV);
{
val binding = assoc(sym, menv->e.fbindings);
/* special_s: see make_var_shadowing_env */
if (binding)
return if3(cdr(binding) == special_s,
fun_k, macro_k);
}
return lexical_fun_binding_kind(menv->e.up_env, sym);
}
}
static val lexical_var_p(val menv, val sym)
{
return eq(lexical_binding_kind(menv, sym), var_k);
}
static val lexical_symacro_p(val menv, val sym)
{
return eq(lexical_binding_kind(menv, sym), symacro_k);
}
static val old_lexical_var_p(val menv, val sym)
{
if (nilp(menv)) {
return nil;
} else {
type_check(lit("lexical-var-p"), menv, ENV);
{
val binding = assoc(sym, menv->e.vbindings);
if (binding)
return tnil(cdr(binding) == special_s);
return lexical_var_p(menv->e.up_env, sym);
}
}
}
static val lexical_fun_p(val menv, val sym)
{
return eq(lexical_fun_binding_kind(menv, sym), fun_k);
}
static val lexical_macro_p(val menv, val sym)
{
return eq(lexical_fun_binding_kind(menv, sym), macro_k);
}
static val lexical_lisp1_binding(val menv, val sym)
{
if (nilp(menv)) {
return nil;
} else {
type_check(lit("lexical-lisp1-binding"), menv, ENV);
{
val binding = assoc(sym, menv->e.vbindings);
if (binding) {
/* special_s: see make_var_shadowing_env */
if (cdr(binding) != special_s)
return symacro_k;
else if (special_var_p(sym))
return nil;
return var_k;
}
}
{
val binding = assoc(sym, menv->e.fbindings);
if (binding && cdr(binding) == special_s)
return fun_k;
return lexical_lisp1_binding(menv->e.up_env, sym);
}
}
}
static val mark_special(val sym)
{
assert (sym != nil);
return sethash(special, sym, t);
}
static void copy_env_handler(mem_t *ptr)
{
val *penv = coerce(val *, ptr);
*penv = copy_env(*penv);
}
static val squash_menv_deleting_range(val menv, val upto_menv)
{
val varshadows = nil, funshadows = nil;
val iter, next, out_env;
if (!upto_menv)
return nil;
out_env = make_env(nil, nil, nil);
for (iter = menv; iter && iter != upto_menv; iter = next) {
type_check(lit("expand-with-free-refs"), iter, ENV);
varshadows = append2(varshadows, mapcar(car_f, iter->e.vbindings));
funshadows = append2(funshadows, mapcar(car_f, iter->e.fbindings));
next = iter->e.up_env;
}
if (!iter)
return nil;
for (; iter; iter = next) {
val viter, fiter;
for (viter = iter->e.vbindings; viter; viter = cdr(viter)) {
val binding = car(viter);
val sym = car(binding);
if (memq(sym, varshadows))
continue;
if (cdr(binding) != special_s)
continue;
push(sym, &varshadows);
env_vbind(out_env, sym, special_s);
}
for (fiter = iter->e.fbindings; fiter; fiter = cdr(fiter)) {
val binding = car(fiter);
val sym = car(binding);
if (memq(sym, funshadows))
continue;
if (cdr(binding) != special_s)
continue;
push(sym, &funshadows);
env_fbind(out_env, sym, special_s);
}
next = iter->e.up_env;
}
return out_env;
}
INLINE val lex_or_dyn_bind_seq(val *dyn_made, val lex_env, val sym, val obj)
{
if (special_var_p(sym)) {
if (!*dyn_made) {
dyn_env = make_env(nil, nil, dyn_env);
*dyn_made = t;
}
env_vbind(dyn_env, sym, obj);
} else {
lex_env = make_env(nil, nil, lex_env);
env_vbind(lex_env, sym, obj);
}
return lex_env;
}
INLINE void lex_or_dyn_bind(val *dyn_made, val lex_env, val sym, val obj)
{
if (special_var_p(sym)) {
if (!*dyn_made) {
dyn_env = make_env(nil, nil, dyn_env);
*dyn_made = t;
}
env_vbind(dyn_env, sym, obj);
} else {
env_vbind(lex_env, sym, obj);
}
}
static val bind_args(val env, val params, varg args, val ctx)
{
val new_env = make_env(nil, nil, env);
val dyn_env_made = nil;
val optargs = nil;
cnum index = 0;
uw_frame_t uw_cc;
uw_push_cont_copy(&uw_cc, coerce(mem_t *, &new_env), copy_env_handler);
for (; args_more(args, index) && consp(params); params = cdr(params)) {
val param = car(params);
val arg;
val initform = nil;
val presentsym = nil;
if (param == colon_k) {
optargs = t;