Repository navigation
Expand file tree
/
Copy pathinline.c
More file actions
1298 lines (1154 loc) · 37.5 KB
/
Copy pathinline.c
File metadata and controls
1298 lines (1154 loc) · 37.5 KB
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 (c) 1993-2012 David Gay and Gustav Hållberg
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose, without fee, and without written agreement is hereby granted,
* provided that the above copyright notice and the following two paragraphs
* appear in all copies of this software.
*
* IN NO EVENT SHALL DAVID GAY OR GUSTAV HALLBERG BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DAVID GAY OR
* GUSTAV HALLBERG HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* DAVID GAY AND GUSTAV HALLBERG SPECIFICALLY DISCLAIM ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
* "AS IS" BASIS, AND DAVID GAY AND GUSTAV HALLBERG HAVE NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "calloc.h"
#include "compile.h"
#include "global.h"
#include "inline.h"
#include "tree.h"
#define GEP GLOBAL_ENV_PREFIX
/* helper structure for building a sequence of components and variables */
struct code_state {
struct component *top; /* topmost component; the final result */
struct component **dst; /* where to add new components */
struct clist *clist; /* pending clist */
struct vlist *vlist; /* pending vlist */
};
#define CODE_STATE_UNSET ((struct component *)5L)
static void code_state_flush_block(struct code_state *state)
{
if (state->vlist == NULL && state->clist == NULL)
return;
assert(*state->dst == CODE_STATE_UNSET);
struct clist *tail_clist = new_clist(CODE_STATE_UNSET, state->clist);
*state->dst = new_block_component(
new_codeblock(reverse_vlist(state->vlist),
reverse_clist(tail_clist),
NO_LOC));
state->dst = &tail_clist->c;
state->clist = NULL;
state->vlist = NULL;
}
/* call this first */
UNUSED static void code_state_init(struct code_state *state)
{
*state = (struct code_state){
.top = CODE_STATE_UNSET,
.dst = &state->top
};
}
UNUSED static void code_state_add_comp(struct code_state *state,
struct component *comp)
{
state->clist = new_clist(comp, state->clist);
}
UNUSED static void code_state_add_var(
struct code_state *state, const char *var, typeset_t typeset,
const struct loc *loc)
{
if (state->clist != NULL)
code_state_flush_block(state);
state->vlist = new_vlist(var, typeset, loc, state->vlist);
}
UNUSED static void code_state_add_vlist(struct code_state *state,
struct vlist *vlist)
{
if (vlist == NULL)
return;
if (state->clist != NULL)
code_state_flush_block(state);
while (vlist != NULL)
{
struct vlist *next = vlist->next;
vlist->next = state->vlist;
state->vlist = vlist;
vlist = next;
}
}
/* call this last; returns the result */
UNUSED static struct component *code_state_close(
struct code_state *state, struct component *expr)
{
code_state_flush_block(state);
assert(*state->dst == CODE_STATE_UNSET);
*state->dst = expr;
struct component *result = state->top;
code_state_init(state);
return result;
}
typedef struct component *(*match_err_fn)(void *data);
struct mvar {
const char *name;
const struct loc *loc;
};
#define MVAR(n, l) &(const struct mvar){ .name = (n), .loc = (l) }
/* ... are pointers to const struct mvar */
static struct vlist *build_vlist(size_t n, ...)
{
va_list args;
va_start(args, n);
struct vlist *res = NULL;
for (size_t i = 0; i < n; ++i)
{
const struct mvar *mvar = va_arg(args, const struct mvar *);
if (mvar->name == NULL)
continue;
res = new_vlist(mvar->name, TYPESET_ANY, mvar->loc, res);
}
va_end(args);
return res;
}
static struct clist *build_clist(int n, ...)
{
va_list args;
struct clist *res = NULL;
va_start(args, n);
while (n-- > 0)
{
struct component *c = va_arg(args, struct component *);
if (c == NULL)
continue;
res = new_clist(c, res);
}
va_end(args);
return reverse_clist(res);
}
static struct component *new_int_component(long n)
{
return new_const_component(new_int_constant(n, cstbase_decimal, NO_LOC));
}
static struct component *build_codeblock(struct vlist *vl, struct clist *code)
{
return new_block_component(new_codeblock(vl, code, NO_LOC));
}
static struct component *build_unop(enum builtin_op op, struct component *e)
{
return new_unop_component(NO_LOC, arith_default, op, e);
}
static struct component *build_if(struct component *cond,
struct component *ctrue)
{
return new_binop_component(NO_LOC, arith_default, b_if, cond, ctrue);
}
static struct component *build_unless(struct component *cond,
struct component *otherwise)
{
return build_if(build_unop(b_logical_not, cond), otherwise);
}
static struct component *build_exit(const char *name, struct component *c)
{
return new_exit_component(NO_LOC, name, c);
}
static struct component *build_exec(struct component *f, const struct loc *loc,
int n, ...)
{
va_list args;
struct clist *res = new_clist(f, NULL);
va_start(args, n);
while (n--)
res = new_clist(va_arg(args, struct component *), res);
va_end(args);
return new_execute_component(loc, reverse_clist(res));
}
static inline struct component *set_loc(
const struct loc *loc, struct component *c)
{
c->loc = *loc;
return c;
}
static struct component *build_const_not_equal(const struct loc *loc,
struct constant *cst,
struct component *e)
{
switch (constant_type(cst))
{
case type_null:
case type_integer:
return new_binop_component(loc, arith_default, b_ne, e,
new_const_component(cst));
default:
return set_loc(
loc,
build_unop(b_logical_not,
set_loc(loc,
build_exec(new_recall_component(GEP "equal?", loc),
loc, 2, new_const_component(cst), e))));
}
}
static struct component *build_typecheck(const struct loc *loc,
struct component *e,
enum mudlle_type type)
{
const char *f = NULL;
switch (type)
{
case type_icode:
case type_variable:
case type_internal:
case type_private:
case type_mcode:
case type_weak_ref:
return new_binop_component(
loc, arith_default, b_eq,
build_exec(new_recall_component(GEP "typeof", loc), loc, 1, e),
new_int_component(type));
case type_closure: f = GEP "closure?"; break;
case type_primitive: f = GEP "primitive?"; break;
case type_varargs: f = GEP "varargs?"; break;
case type_secure: f = GEP "secure?"; break;
case type_integer: f = GEP "integer?"; break;
case type_string: f = GEP "string?"; break;
case type_vector: f = GEP "vector?"; break;
case type_pair: f = GEP "pair?"; break;
case type_symbol: f = GEP "symbol?"; break;
case type_table: f = GEP "table?"; break;
case type_object: f = GEP "object?"; break;
case type_character: f = GEP "character?"; break;
case type_gone: f = GEP "gone?"; break;
case type_oport: f = GEP "port?"; break;
case type_float: f = GEP "float?"; break;
case type_bigint: f = GEP "bigint?"; break;
case type_connection: f = GEP "connection?"; break;
case type_cookie: f = GEP "magic_cookie?"; break;
case type_regexp: f = GEP "regexp?"; break;
case type_file: f = GEP "file?"; break;
case stype_list: f = GEP "list?"; break;
case stype_function: f = GEP "function?"; break;
case stype_float_like: f = GEP "float_like?"; break;
case stype_bigint_like: f = GEP "bigint_like?"; break;
case type_null:
return new_binop_component(
loc, arith_default, b_eq, e,
new_const_component(constant_null));
case stype_none:
return component_false;
case stype_any:
return component_true;
case stype_false:
return new_binop_component(
loc, arith_default, b_eq, e,
component_false);
case mudlle_synthetic_types:
abort();
}
assert(f != NULL);
return build_exec(new_recall_component(f, loc), loc, 1, e);
}
/* true if 'c' is a recall of a single-assignment variable */
static bool is_safe_recall(struct component *c)
{
return c->vclass == c_recall && c->u.recall[0] == '%';
}
static struct component *make_local_var(struct vlist **locals, int level,
const struct loc *loc)
{
/* room for '%' '-' <digits> '\0' */
char buf[1 + 1 + BITS_DECIMAL_DIG(CHAR_BIT * sizeof level - 1) + 1];
int r = snprintf(buf, sizeof buf, "%%%d", level);
assert(r >= 0 && (unsigned)r < sizeof buf);
const char *tmpname = heap_allocate_string(parser_heap(), buf);
*locals = new_vlist(tmpname, TYPESET_ANY, loc, *locals);
return new_recall_component(tmpname, loc);
}
static bool parser_expand_paren(struct component **cp)
{
bool result = false;
while ((*cp)->vclass == c_builtin && (*cp)->u.builtin.fn == b_paren)
{
*cp = (*cp)->u.builtin.args->c;
result = true;
}
return result;
}
static struct component *comp_id(struct component *e)
{
return e;
}
static struct component *comp_set_tmp(struct component *e)
{
return new_assign_component(NO_LOC, NO_LOC, "%tmp", e);
}
static struct component *parser_expand_assign(struct component *c)
{
assert(c->vclass == c_builtin && c->u.builtin.fn == b_assign);
struct component *dst = c->u.builtin.args->c;
struct component *val = c->u.builtin.args->next->c;
if (dst->vclass == c_builtin && dst->u.builtin.fn == b_ref)
{
struct clist *args = dst->u.builtin.args;
return new_ternop_component(&c->loc, b_set,
args->c, args->next->c, val);
}
if (dst->vclass == c_recall)
{
if (val->vclass == c_closure)
val->loc = dst->loc;
return new_assign_component(&c->loc, &dst->loc, dst->u.recall, val);
}
abort();
}
static struct component *parser_expand_modify(struct component *c)
{
assert(c->vclass == c_modify);
/* needed for the destination type checks below */
parser_expand_paren(&c->u.modify->dst);
const struct loc *loc = &c->loc;
struct component *(*set_tmp)(struct component *) = comp_id;
struct component *ret = NULL;
const char *tmp_name = NULL;
if (c->u.modify->postfix)
{
set_tmp = comp_set_tmp;
tmp_name = "%tmp";
ret = new_recall_component(tmp_name, loc);
}
struct component *dst = c->u.modify->dst;
bool is_incrementer = c->u.modify->mod == NULL;
struct component *mod = is_incrementer ? component_one : c->u.modify->mod;
enum arith_mode arith_mode = c->u.modify->arith_mode;
enum builtin_op op = c->u.modify->fn;
if (dst->vclass == c_builtin && dst->u.builtin.fn == b_ref)
{
struct clist *args = dst->u.builtin.args;
struct component *lexp = args->c, *lref = args->next->c;
struct vlist *vl;
struct clist *cl;
if (lref->vclass == c_constant && lref->u.cst->vclass == cst_string)
{
vl = build_vlist(
2,
MVAR(tmp_name, &dst->loc),
MVAR("%sym", &lref->loc));
cl = build_clist(
3,
new_assign_component(
loc, NO_LOC, "%sym",
build_exec(new_recall_component(GEP "symbol_ref", &dst->loc),
&dst->loc, 2, lexp, lref)),
build_exec(
new_recall_component(GEP "symbol_set!", loc), loc, 2,
new_recall_component("%sym", loc),
new_binop_component(
loc, arith_mode, op,
set_tmp(build_exec(
new_recall_component(GEP "symbol_get", &dst->loc),
&dst->loc, 1,
new_recall_component("%sym", &dst->loc))),
mod)),
ret);
}
else
{
vl = build_vlist(
3,
MVAR(tmp_name, &dst->loc),
MVAR("%ref", &lref->loc),
MVAR("%exp", &lexp->loc));
cl = build_clist(
4,
new_assign_component(loc, NO_LOC, "%exp", lexp),
new_assign_component(loc, NO_LOC, "%ref", lref),
new_ternop_component(
loc, b_set,
new_recall_component("%exp", loc),
new_recall_component("%ref", loc),
new_binop_component(
loc, arith_mode, op,
set_tmp(new_binop_component(
loc, arith_default, b_ref,
new_recall_component("%exp", loc),
new_recall_component("%ref", loc))),
mod)),
ret);
}
return build_codeblock(vl, cl);
}
if (dst->vclass == c_recall)
{
struct vlist *vl = build_vlist(
1, MVAR(tmp_name, &dst->loc));
struct clist *cl = build_clist(
2,
new_assign_component(
loc, &dst->loc, dst->u.recall,
new_binop_component(loc, arith_mode, op, set_tmp(dst), mod)),
ret);
/* optimize for common case */
if (vl == NULL && cl->next == NULL)
return cl->c;
return build_codeblock(vl, cl);
}
abort();
}
static struct component *make_safe_copy(struct component *e,
struct vlist **locals,
struct component **aexpr,
int level, const struct loc *loc)
{
if (is_safe_recall(e))
return e;
struct component *recall = make_local_var(locals, level, loc);
assert(recall->vclass == c_recall);
*aexpr = new_assign_component(loc, &recall->loc, recall->u.recall, e);
return recall;
}
static struct component *build_match_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data);
static struct component *build_symbol_name_check(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
switch (pat->vclass)
{
case pat_sink:
if (pat->u.var.typeset == TYPESET_ANY)
return NULL;
FALLTHROUGH;
case pat_and:
case pat_or:
case pat_variable:
return build_match_block(pat, e, level, err, err_data);
case pat_expr:
{
struct vlist *locals = NULL;
struct component *lassign = NULL;
struct component *nexp = make_safe_copy(pat->u.expr, &locals, &lassign,
level, &pat->loc);
return build_codeblock(
locals,
build_clist(
2,
lassign,
build_unless(
new_binop_component(
&pat->loc, arith_default, b_logical_and,
build_typecheck(&pat->loc, nexp, type_string),
build_exec(new_recall_component(
GEP "string_iequal?", &pat->loc),
&pat->loc, 2, nexp, e)),
err(err_data))));
}
case pat_const:
{
struct constant *cst = pat->u.constval;
if (cst->vclass == cst_string)
return build_unless(
build_exec(new_recall_component(GEP "string_iequal?", &pat->loc),
&pat->loc, 2, new_const_component(cst), e),
err(err_data));
}
FALLTHROUGH;
case pat_list:
case pat_array:
case pat_symbol:
abort();
}
abort();
}
static struct component *next_error(void *count)
{
++*(int *)count;
return build_exit("%next", component_undefined);
}
static struct component *cause_no_match_error(void *data)
{
struct component *val = data;
const struct loc *loc = &val->loc;
return build_exec(new_recall_component(GEP "fail_no_match", loc),
loc, 1, val);
}
static struct component *parser_expand_match(struct component *c)
{
assert(c->vclass == c_match);
/*
* <$match> [
* | $exp |
*
* $exp = <match-expression>
* <$next> [ \
* | <pattern-variables> | |
* if (<pattern-match>) +- repeat for each match node
* exit<$match> <pattern-expression>; |
* ] /
* false
* ]
*/
struct vlist *vl = build_vlist(
1, MVAR("%exp", &c->u.match->expression->loc));
struct clist *code = build_clist(1, component_false);
bool force = c->u.match->force;
for (struct match_node_list *matches = c->u.match->matches;
matches;
matches = matches->next)
{
int next_count = 0;
match_err_fn err = next_error;
void *err_data = &next_count;
if (force)
{
err = cause_no_match_error;
err_data = new_recall_component("%exp", &c->loc);
force = false;
}
struct component *matchcode = build_match_block(
matches->match->pattern,
new_recall_component("%exp", &matches->match->pattern->loc),
0, err, err_data);
struct component *cexit = set_loc(
&matches->match->loc,
build_exit("%match", matches->match->expression));
struct clist *cl = build_clist(2, matchcode, cexit);
struct component *cblock = build_codeblock(matches->match->locals, cl);
cblock->u.blk->loc = matches->match->loc;
if (next_count)
cblock = new_labeled_component(NO_LOC, "%next", cblock);
code = new_clist(cblock, code);
}
code = new_clist(
new_assign_component(NO_LOC, NO_LOC, "%exp", c->u.match->expression),
code);
return new_labeled_component(
NO_LOC, "%match", build_codeblock(vl, code));
}
static struct component *parser_expand_pattern(struct component *c)
{
assert(c->vclass == c_pattern);
/* Warning: if the match fails, this might leave only some of the variables
* in the pattern filled. But it's a feature, right? */
struct vlist *locals = NULL;
struct component *aexpr = NULL;
struct component *val = make_safe_copy(
c->u.pattern.expression, &locals, &aexpr, 0, &c->loc);
return build_codeblock(
locals,
build_clist(
3,
aexpr,
build_match_block(c->u.pattern.pattern, val,
0, cause_no_match_error, val),
component_undefined));
}
static int patlist_len(struct pattern_list *l)
{
int n = 0;
for (; l != NULL; l = l->next)
++n;
return n;
}
static struct clist *build_patarray_match(
struct clist *code, struct component *elocal,
struct pattern_list *l, int idx, int level,
match_err_fn err, void *err_data)
{
for (; l != NULL; l = l->next)
{
--idx;
struct component *c = build_match_block(
l->pat, new_binop_component(&l->pat->loc, arith_default, b_ref,
elocal, new_int_component(idx)),
level + 1, err, err_data);
if (c != NULL)
code = new_clist(c, code);
}
return code;
}
static struct component *build_vlength(struct component *v,
const struct loc *loc)
{
return build_exec(new_recall_component(GEP "vlength", loc), loc, 1, v);
}
static struct component *build_match_array_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
/*
* [
* | tmp |
* tmp = <expression>;
* (vector?(tmp) &&
* vlength(tmp) == vlength(<pattern>) &&
* tmp[0] == <pattern>[0] &&
* :
* tmp[N] == <pattern>[N])
* ]
*/
struct component *eassign = NULL;
struct vlist *vlocals = NULL;
struct component *elocal = make_safe_copy(e, &vlocals, &eassign, level,
&pat->loc);
int vlen = patlist_len(pat->u.ary.patlist);
int tlen = patlist_len(pat->u.ary.pattail);
struct component *lc = NULL;
if (!pat->u.ary.ellipsis || (vlen + tlen) > 0)
lc = set_loc(
&pat->loc,
build_if(
new_binop_component(
&pat->loc, arith_default, pat->u.ary.ellipsis ? b_lt : b_ne,
build_vlength(elocal, &pat->loc),
new_int_component(vlen + tlen)),
err(err_data)));
struct component *tc = set_loc(
&pat->loc,
build_unless(
build_exec(new_recall_component(GEP "vector?", &pat->loc),
&pat->loc, 1, elocal),
err(err_data)));
struct clist *code = build_patarray_match(
NULL, elocal, pat->u.ary.pattail, 0, level, err, err_data);
code = build_patarray_match(
code, elocal, pat->u.ary.patlist, vlen, level, err,
err_data);
if (lc != NULL)
code = new_clist(lc, code);
code = new_clist(tc, code);
if (eassign != NULL)
code = new_clist(eassign, code);
return build_codeblock(vlocals, code);
}
static struct component *build_match_list_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
/*
* [
* | tmp |
* tmp = <expression>;
* pair?(tmp) &&
* car(tmp) == car(<pattern>) &&
* [
* tmp = cdr(tmp); <pattern> = cdr(<pattern>);
* pair?(tmp) &&
* car(tmp) == car(<pattern>) &&
* :
* [ \ this is done at
* cdr(tmp) == cdr(<pattern>); +- the last pair
* ] /
* ]
* ]
*/
/* the last pair has both car/cdr entries */
for (struct pattern_list *apl = pat->u.lst->next; apl; apl = apl->next)
++level;
int nlevel = level;
struct clist *code = NULL;
struct vlist *locals = NULL;
struct component *exp = NULL;
for (struct pattern_list *apl = pat->u.lst->next; ; )
{
assert(apl != NULL); /* silence clang-tidy warning */
struct component *texp = (apl->next == NULL && is_safe_recall(e)
? e
: make_local_var(&locals, --level,
&pat->loc));
if (exp == NULL)
{
struct component *getcdr = build_exec(
new_recall_component(GEP "cdr", &pat->loc), &pat->loc, 1, texp);
struct component *c =
(pat->u.lst->pat == NULL
? build_if(build_const_not_equal(
&pat->loc, constant_null, getcdr),
err(err_data))
: build_match_block(pat->u.lst->pat, getcdr,
nlevel, err, err_data));
if (c)
code = new_clist(c, code);
}
else
{
code = new_clist(
new_assign_component(
&apl->pat->loc, &exp->loc, exp->u.recall,
build_exec(new_recall_component(GEP "cdr", &apl->pat->loc),
&apl->pat->loc, 1, texp)),
code);
}
exp = texp;
struct component *mc = build_match_block(
apl->pat,
build_exec(new_recall_component(GEP "car", &apl->pat->loc),
&apl->pat->loc, 1, exp),
nlevel, err, err_data);
if (mc != NULL)
code = new_clist(mc, code);
struct component *pc = set_loc(
&apl->pat->loc,
build_unless(
build_exec(new_recall_component(GEP "pair?", &apl->pat->loc),
&apl->pat->loc, 1, exp),
err(err_data)));
code = new_clist(pc, code);
apl = apl->next;
if (apl == NULL)
break;
}
if (!is_safe_recall(e))
code = new_clist(
new_assign_component(NO_LOC, &exp->loc, exp->u.recall, e),
code);
return build_codeblock(reverse_vlist(locals), code);
}
static struct component *build_typeset_check(struct component *e,
typeset_t typeset,
const struct loc *loc)
{
if (typeset == 0)
return component_false;
struct component *tc = NULL;
for (typeset_t i = stype_none; typeset; ++i)
{
enum mudlle_type t;
/* convoluted to test null/integer early */
switch (i)
{
case stype_none: t = type_integer; break;
case mudlle_synthetic_types: t = type_null; break;
case mudlle_synthetic_types + 1: t = i = 0; break;
default: t = i; break;
}
typeset_t tset = type_typeset(t);
if ((typeset & tset) == tset)
{
typeset &= ~tset;
struct component *this = build_typecheck(loc, e, t);
if (tc != NULL)
this = new_binop_component(loc, arith_default, b_logical_or,
tc, this);
tc = this;
}
}
return tc;
}
static struct component *build_match_variable_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
typeset_t typeset = pat->u.var.typeset;
if (typeset == TYPESET_ANY)
{
if (pat->u.var.name == NULL)
return NULL;
return new_assign_component(&pat->loc, &pat->loc, pat->u.var.name, e);
}
struct vlist *locals = NULL;
struct component *aexp = NULL;
e = make_safe_copy(e, &locals, &aexp, level, &pat->loc);
struct component *tc = build_unless(
build_typeset_check(e, typeset, &pat->loc),
err(err_data));
e = (pat->u.var.name == NULL
? NULL /* sink; do nothing */
: new_assign_component(&pat->loc, &pat->loc, pat->u.var.name, e));
return build_codeblock(locals, build_clist(3, aexp, tc, e));
}
static struct component *build_match_symbol_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
struct vlist *locals = NULL;
struct component *aexp = NULL;
e = make_safe_copy(e, &locals, &aexp, level, &pat->loc);
return build_codeblock(
locals,
build_clist(
4,
aexp,
build_unless(build_typecheck(&pat->loc, e, type_symbol),
err(err_data)),
build_symbol_name_check(
pat->u.sym.name,
build_exec(new_recall_component(GEP "symbol_name", &pat->loc),
&pat->loc, 1, e),
level + 1,
err, err_data),
build_match_block(
pat->u.sym.val,
build_exec(new_recall_component(GEP "symbol_get", &pat->loc),
&pat->loc, 1, e),
level + 2,
err, err_data)));
}
static struct component *build_match_or_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
struct vlist *locals = NULL;
struct component *aexp = NULL;
e = make_safe_copy(e, &locals, &aexp, level, &pat->loc);
int lhs_count = 0;
struct component *lhs_comp = build_match_block(
pat->u.or.lhs, e, level + 1, next_error, &lhs_count);
struct component *rhs_comp = NULL;
if (lhs_count == 0)
{
/* lhs always succeeds */
compile_warning(&pat->u.or.rhs->loc, "pattern not reachable");
}
else
rhs_comp = build_match_block(pat->u.or.rhs, e, level + 1, err, err_data);
return new_labeled_component(
&pat->loc,
"%found", build_codeblock(
locals,
build_clist(
3,
aexp,
new_labeled_component(
&pat->loc,
"%next", build_codeblock(
NULL,
build_clist(
2,
lhs_comp,
build_exit("%found", new_const_component(constant_null))))),
rhs_comp)));
}
static struct component *build_match_expr_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
return set_loc(
&pat->loc,
build_unless(
set_loc(&pat->loc,
build_exec(new_recall_component(GEP "equal?", &pat->loc),
&pat->loc, 2, pat->u.expr, e)),
err(err_data)));
}
static struct component *build_match_and_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
return build_codeblock(
NULL,
build_clist(
2,
build_match_block(pat->u.and.pat, e, level, err, err_data),
build_unless(pat->u.and.cond, err(err_data))));
}
static struct component *build_match_const_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
return set_loc(
&pat->loc,
build_if(build_const_not_equal(&pat->loc, pat->u.constval, e),
err(err_data)));
}
typedef struct component *(*build_match_block_fn)(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data);
static struct component *build_match_block(
struct pattern *pat, struct component *e, int level,
match_err_fn err, void *err_data)
{
static const build_match_block_fn fns[] = {
[pat_const] = &build_match_const_block,
[pat_list] = &build_match_list_block,
[pat_array] = &build_match_array_block,
[pat_symbol] = &build_match_symbol_block,
[pat_variable] = &build_match_variable_block,
[pat_sink] = &build_match_variable_block,
[pat_expr] = &build_match_expr_block,
[pat_and] = &build_match_and_block,
[pat_or] = &build_match_or_block,
};
return fns[pat->vclass](pat, e, level, err, err_data);
}
static struct component *parser_expand_const_list(struct constant *c)
{
assert(c->vclass == cst_list);
struct cstlist *const clhead = c->u.constants;
assert(clhead != NULL);
struct cstlist *last_dynamic = NULL;
for (struct cstlist *cl = clhead; cl != NULL; cl = cl->next)
if (cl->cst->is_dynamic)
last_dynamic = cl;
struct component *result = NULL;