Repository navigation
Expand file tree
/
Copy pathparser.y
More file actions
1574 lines (1364 loc) · 40.8 KB
/
Copy pathparser.y
File metadata and controls
1574 lines (1364 loc) · 40.8 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 "mudlle-config.h"
#include <math.h>
#include <string.h>
#include "alloc.h"
#include "array.h"
#include "calloc.h"
#include "compile.h"
#include "global.h"
#include "mparser.h"
#include "print.h"
#include "strbuf.h"
#include "tree.h"
#include "types.h"
#include "utils.h"
#ifdef __clang__
/* generated code doesn't handle this */
#pragma clang diagnostic ignored "-Wmissing-variable-declarations"
#endif
/* needed to copy the fname field */
#define YYLLOC_DEFAULT(cur, rhs, n) do { \
(cur) = YYRHSLOC(rhs, n); \
if (n > 1) \
(cur).first = YYRHSLOC(rhs, 1).first; \
} while (0)
%}
%define api.pure full
%define parse.error custom
%parse-param {const struct parser_config *pconfig}
%initial-action {
@$ = (YYLTYPE){
.first = {
.col = 1,
.line = 1,
.fname = &pconfig->filename
},
.last_column = 1,
.last_line = 1
};
(void)yynerrs; /* silence unused-variable warning */
}
%union {
struct cstrlen string;
const char *symbol;
struct int_and_base integer;
enum builtin_op operator;
struct float_and_base mudlle_float;
struct bigint_const *bigint;
struct constant *tconstant;
enum constant_class cstclass;
struct block *tblock;
struct function *tfunction;
struct function_args *tfnargs;
struct function_arg *tfnarg;
struct clist *tclist;
struct vlist *tvlist;
struct cstlist *tcstlist;
struct cstpair *tcstpair;
struct component *tcomponent;
enum mudlle_type tmtype;
unsigned tmtypeset; /* bitset of 1 << type_xxx */
struct mfile *tfile;
struct pattern *tpattern;
struct pattern_list *tpatternlist;
struct match_node *tmatchnode;
struct match_node_list *tmatchnodelist;
enum builtin_op bop;
struct module_head {
enum file_class fclass;
const char *name;
struct vlist *defines, *requires;
} *tmodule_head;
struct {
bool space_suffix;
} tcomma;
bool force_match;
bool boolean;
enum arith_mode arith_mode;
enum rwmode rwmode;
}
%locations
%token END 0 "end of input"
%token START_CONSTANT
%token START_STORABLE
%token START_ANY_EXPR
%token START_PRIMARY_EXPR
%token START_PAREN_EXPR
%token EQ "'=='"
%token GE "'>='"
%token LE "'<='"
%token NE "'!='"
%token DECREMENT "'--'"
%token INCREMENT "'++'"
%token LOGICAL_OR "'||'"
%token LOGICAL_XOR "'^^'"
%token LOGICAL_AND "'&&'"
%token SHIFT_LEFT "'<<'"
%token SHIFT_RIGHT "'>>'"
%token PATTERN_MATCH "'=>'"
%token ELLIPSIS "'...'"
%token ASSIGN_OP "assignment operator"
%token ELSE "'else'"
%token FOR "'for'"
%token FUNCTION "'fn'"
%token IF_KEYWORD "'if'"
%token LOOP "'loop'"
%token LOOP_EXIT "'exit'"
%token MATCH "'match'"
%token MATCH_FORCE "'match!'"
%token WHILE "'while'"
%token DEFINES "'defines'"
%token LIBRARY "'library'"
%token MODULE "'module'"
%token READS "'reads'"
%token REQUIRES "'requires'"
%token STATIC "'static'"
%token WRITES "'writes'"
%token BIGINT "bigint constant"
%token FLOAT "floating-point constant"
%token INTEGER "integer constant" /* warning: can be -MIN_TAGGED_INT */
%token COMMA "','"
%token STRING "string constant"
%token SYMBOL "variable name"
%token ARITH "'#arith'"
%token HASH_GONE "'#gone'"
%token HASH_RO "'#ro'"
%token HASH_RW "'#rw'"
%token HASH_IM "'#im'"
%token '\'' "apostrophe (')"
%precedence NOT_ELSE
%right '.'
%left LOGICAL_OR
%left LOGICAL_XOR
%left LOGICAL_AND
%left EQ NE '<' LE '>' GE
%left '|'
%left '^'
%left '&'
%left SHIFT_LEFT SHIFT_RIGHT
%left '+' '-'
%left '*' '/' '%'
%precedence ELSE
%type <tclist> expr_list simple_expr_list opt_call_list call_list
%type <tcomponent> expr e0 e1 e2 e3 e4 e4_safe primary_expr loop exit match for
%type <tcomponent> unary_expr assign_expr logical_and_expr
%type <tcomponent> control_expr opt_expr paren_expr variable_recall
%type <tcomponent> if while one_expression
%type <tvlist> variable_list
%type <tvlist> opt_local_vars local_vars
%type <tvlist> requires defines reads writes statics
%type <tmtype> type
%type <tmtypeset> typeset typelist typeset_braced opt_typeset_braced
%type <string> help STRING
%type <symbol> variable label opt_label opt_symbol plain_symbol
%type <symbol> SYMBOL
%type <integer> INTEGER
%type <mudlle_float> FLOAT
%type <bigint> BIGINT
%type <cstclass> table_class
%type <force_match> match_type
%type <tconstant> constant constant_container constant_expr constant_expr_tail
%type <tconstant> constant_no_prefix
%type <tconstant> constant_symbol constant_symbol_name constant_table_entry
%type <tconstant> constant_table_entry_no_prefix opt_constant_list_tail
%type <tconstant> force_constant
%type <tconstant> simple_constant unary_constant
%type <tconstant> float_constant int_constant string_constant
%type <tconstant> special_float_constant
%type <tblock> maybe_arith_code_block code_block opt_implicit_code_block
%type <tfunction> function_expr f0 f1
%type <tfnargs> opt_arglist arglist
%type <tfnarg> arg argname
%type <tcstlist> constant_list opt_constant_list constant_table_entry_list
%type <tfile> file simple module
%type <tmodule_head> module_head;
%type <tpattern> pattern pattern_list pattern_array pattern_atom pattern_cdr
%type <tpattern> pattern_atom_expr pattern_symbol pattern_and
%type <tpattern> pattern_or opt_pattern_or
%type <tpatternlist> opt_pattern_sequence
%type <tmatchnode> match_node
%type <tmatchnodelist> match_list
%type <operator> incrementer
%type <bop> ASSIGN_OP
%type <tcomma> COMMA
%type <boolean> opt_ellipsis
%type <rwmode> HASH_IM HASH_RO HASH_RW constant_string_rwmode_prefix
%type <rwmode> im_ro_rwmode_prefix constant_container_rwmode_prefix
%{
#include "lexer.h"
/* here to catch accidental increase of the size; it's not really harmful to
change the size */
CASSERT_SIZEOF(YYSTYPE, sizeof (struct cstrlen));
#define BINOP(dst, op, loc, e0, l0, e1, l1) do { \
struct component *__c = make_binary( \
b_ ## op, &(loc), (e0), &(l0), (e1), &(l1)); \
if (__c == NULL) \
YYABORT; \
dst = __c; \
} while (0)
#define UNOP(dst, op, loc, e) do { \
struct component *__c = make_unary(b_ ## op, &(loc), (e)); \
if (__c == NULL) \
YYABORT; \
dst = __c; \
} while (0)
static enum arith_mode cur_arith_mode;
static enum rwmode cur_rwmode;
static struct mfile *parsed_code;
static struct constant *parsed_constant;
static struct component *parsed_expr;
static int parsed_expr_end_pos;
static enum parser_mode parser_mode;
bool const_rwmode_is_dynamic(void)
{
assert(parser_mode != parser_mode_undefined);
return parser_mode != parser_mode_storable;
}
static const char *varlist_name;
static struct vlist *seen_user_vars;
static void add_user_var(const char *vname)
{
for (struct vlist **varp = &seen_user_vars; ; varp = &(*varp)->next)
{
struct vlist *var = *varp;
if (var == NULL)
{
var = new_vlist(vname, TYPESET_ANY, NO_LOC, NULL);
var->was_read = var->was_written = true;
*varp = var;
return;
}
if (strcasecmp(vname, var->var) == 0)
return;
}
}
static void maybe_compile_error(const YYLTYPE *loc, const char *s)
{
if (!erred)
compile_error(LLOC_LOC(*loc), "%s", s);
}
static void yyerror(YYLTYPE *locp, const struct parser_config *pconfig,
const char *s)
{
maybe_compile_error(locp, s);
}
struct bracket {
char c;
YYLTYPE loc;
};
static ARRAY_T(struct bracket) bracket_stack;
static void push_bracket(char c, const YYLTYPE *loc)
{
ARRAY_ADD(bracket_stack, ((struct bracket){ .c = c, .loc = *loc }));
}
static void pop_bracket(char c)
{
assert(!ARRAY_IS_EMPTY(bracket_stack));
struct bracket b = ARRAY_POP(bracket_stack);
assert(b.c == c);
}
static bool lookup_arith_mode(enum arith_mode *dst, const char *src,
const YYLTYPE *loc)
{
static const char *const mode_names[] = {
#define AM(name, lname) #name
FOR_ARITH_MODES(AM, SEP_COMMA)
#undef AM
};
size_t slen = strlen(src);
for (enum arith_mode am = 0; am < VLENGTH(mode_names); ++am)
if (strncmp(mode_names[am], src, slen) == 0)
{
#ifndef USE_GMP
if (am == arith_bigint)
{
compile_error(LLOC_LOC(*loc), "#arith(%s) is unsupported", src);
return false;
}
#endif
*dst = am;
return true;
}
compile_error(LLOC_LOC(*loc), "unknown arithmetic mode");
return false;
}
static void check_constant_expr(const YYLTYPE *loc)
{
if (!allow_comma_expression())
maybe_compile_error(loc, "comma-prefixed expression not allowed");
}
static void check_space(const YYLTYPE *loc, bool warn, const char *where)
{
if (warn && !erred)
compile_error(LLOC_LOC(*loc),
"comma must not be followed by whitespace in %s",
where);
}
static struct constant *parse_special_float(const YYLTYPE *loc,
bool neg, const char *s)
{
double d;
if (strcasecmp(s, "inf") == 0 || strcasecmp(s, "infinity") == 0)
d = neg ? -INFINITY : INFINITY;
else if (!neg && strcasecmp(s, "nan") == 0)
d = nan("");
else
{
compile_error(LLOC_LOC(*loc), "syntax error, unexpected variable name");
return NULL;
}
return new_float_constant(d, cstbase_decimal, LLOC_LOC(*loc));
}
/* true if c is a string constant */
static bool is_string(struct component *c)
{
return c->vclass == c_constant && c->u.cst->vclass == cst_string;
}
/* true if c is an addition of two strings */
static bool is_string_add(struct component *c)
{
return (c->vclass == c_builtin && c->u.builtin.fn == b_add
&& is_string(c->u.builtin.args->c)
&& is_string(c->u.builtin.args->next->c));
}
/* collapse the string addition in c into a string constant */
static struct component *perform_string_add(struct component *c)
{
assert(is_string_add(c));
const struct cstrlen *a = &c->u.builtin.args->c->u.cst->u.string;
const struct cstrlen *b = &c->u.builtin.args->next->c->u.cst->u.string;
size_t len = a->len + b->len;
char *nbuf = allocate(parser_heap(), len);
if (a->len > 0)
memcpy(nbuf, a->str, a->len);
if (b->len > 0)
memcpy(nbuf + a->len, b->str, b->len);
struct cstrlen s = {
.len = len,
.str = nbuf,
};
return new_const_component(
new_string_constant(&s, &c->u.builtin.args->c->loc));
}
static struct component *make_binary(
enum builtin_op op, const YYLTYPE *locop,
struct component *arg1, const YYLTYPE *locarg1,
struct component *arg2, const YYLTYPE *locarg2)
{
if (op == b_add && cur_arith_mode == arith_default)
{
/* check if this is a collapsible string addition */
if (is_string_add(arg1))
{
if (is_string(arg2))
arg1 = perform_string_add(arg1);
else if (is_string_add(arg2))
{
arg1 = perform_string_add(arg1);
arg2 = perform_string_add(arg2);
}
}
else if (is_string(arg1) && is_string_add(arg2))
arg2 = perform_string_add(arg2);
}
enum prio_class {
pc_undefined,
pc_logical_or,
pc_logical_xor,
pc_logical_and,
pc_relation,
pc_bitor,
pc_bitxor,
pc_bitand,
pc_bitshift,
pc_arith,
pc_arith_unary,
prio_class_types
};
static const enum prio_class prio_classes[] = {
[b_logical_or] = pc_logical_or,
[b_logical_xor] = pc_logical_xor,
[b_logical_and] = pc_logical_and,
[b_lt] = pc_relation,
[b_le] = pc_relation,
[b_ge] = pc_relation,
[b_gt] = pc_relation,
[b_bitor] = pc_bitor,
[b_bitxor] = pc_bitxor,
[b_bitand] = pc_bitand,
[b_shift_left] = pc_bitshift,
[b_shift_right] = pc_bitshift,
[b_add] = pc_arith,
[b_subtract] = pc_arith,
[b_multiply] = pc_arith,
[b_divide] = pc_arith,
[b_remainder] = pc_arith,
[b_negate] = pc_arith_unary,
[b_bitnot] = pc_arith_unary,
[b_assign] = pc_undefined
};
CASSERT_VLEN(prio_classes, parser_builtins);
enum {
pc_logical_warnings = (P(pc_bitor) | P(pc_bitxor) | P(pc_bitshift)
| P(pc_arith) | P(pc_arith_unary))
};
/* warn if either argument is of a class in these bitsets */
static const unsigned prio_class_warnings[] = {
/* do not warn for _a && b_ || c; too commonly used */
[pc_logical_or] = P(pc_logical_xor) | pc_logical_warnings,
[pc_logical_xor] = P(pc_logical_and) | pc_logical_warnings,
[pc_logical_and] = pc_logical_warnings,
/* do not warn for _a & b_ == c */
[pc_relation] = P(pc_bitor) | P(pc_bitxor) | P(pc_bitshift),
[pc_bitor] = (P(pc_bitxor) | P(pc_bitand) | P(pc_bitshift)
| P(pc_arith)),
[pc_bitxor] = P(pc_bitand) | P(pc_bitshift) | P(pc_arith),
[pc_bitand] = P(pc_bitshift) | P(pc_arith),
[pc_bitshift] = P(pc_bitshift) | P(pc_arith),
[pc_arith_unary] = 0
};
CASSERT_VLEN(prio_class_warnings, prio_class_types);
unsigned warn = prio_class_warnings[prio_classes[op]];
for (struct component *arg = arg1; arg; arg = arg == arg2 ? NULL : arg2)
if (arg->vclass == c_builtin
&& (warn & P(prio_classes[arg->u.builtin.fn])))
compile_warning(
LLOC_LOC(*(arg == arg1 ? locarg1 : locarg2)),
"suggest parentheses around %s in argument to %s",
builtin_op_long_names[arg->u.builtin.fn],
builtin_op_long_names[op]);
return new_binop_component(LLOC_LOC(*locop), cur_arith_mode, op, arg1, arg2);
}
static bool check_int_range(long l, const struct loc *loc)
{
if (l == -MIN_TAGGED_INT)
{
compile_error(loc, "integer constant out of range");
return false;
}
return true;
}
static bool check_comp_int_range(struct component *c)
{
if (c->vclass == c_constant && c->u.cst->vclass == cst_int)
return check_int_range(c->u.cst->u.integer.i, &c->loc);
return true;
}
static bool is_const_unop(enum builtin_op op, struct component *arg)
{
if (arg->vclass != c_constant)
return false;
struct constant *cst = arg->u.cst;
if (cst->vclass == cst_int)
return op == b_negate || op == b_logical_not || op == b_bitnot;
return cst->vclass == cst_float && op == b_negate;
}
static struct component *make_unary(enum builtin_op op,
const YYLTYPE *loc,
struct component *arg)
{
if (is_const_unop(op, arg))
return new_const_component(
new_unary_constant(op, arg->u.cst, LLOC_LOC(*loc)));
return new_unop_component(LLOC_LOC(*loc), cur_arith_mode, op, arg);
}
static bool check_opt_arg(struct function_args *args,
const struct loc *loc)
{
if (args->noptargs == 0 || args->last_arg->default_val != NULL)
return true;
compile_error(loc, "default value required");
return false;
}
static bool find_type(const YYLTYPE *loc, const char *name,
enum mudlle_type *type)
{
if (strcasecmp(name, "int") == 0)
{
*type = type_integer;
return true;
}
for (enum mudlle_type t = 0; t < mudlle_synthetic_types; ++t)
if (strcasecmp(name, mudlle_type_names[t]) == 0)
{
*type = t;
return true;
}
compile_error(LLOC_LOC(*loc), "unknown type %s", name);
return false;
}
static struct module_head *make_module(
enum file_class fclass, const char *name,
struct vlist *requires, struct vlist *defines)
{
struct module_head *m = allocate(parser_heap(), sizeof *m);
*m = (struct module_head){
.fclass = fclass, .name = name, .defines = defines, .requires = requires
};
return m;
}
%}
%%
start:
file { parsed_code = $1; } |
force_constant { parsed_constant = $1; } |
one_expression {
parsed_expr = $1;
parsed_expr_end_pos = @1.last_pos;
} |
%empty {
if (!pconfig->allow_empty)
{
maybe_compile_error(&yylloc, "does not contain code");
return false;
}
} ;
file:
simple |
module ;
simple:
opt_local_vars expr_list {
const struct loc *loc = $1 ? LLOC_LOC(@1) : LLOC_LOC(@2);
$$ = new_file(f_plain, NULL, NULL, NULL, NULL, NULL, NULL, seen_user_vars,
new_codeblock($1, $2, loc), loc, LLOC_LAST_LOC(@2));
} ;
module:
module_head reads writes statics code_block opt_semi {
$$ = new_file($1->fclass, $1->name, $1->requires,
$1->defines, $2, $3, $4, seen_user_vars, $5, LLOC_LOC(@5),
LLOC_LAST_LOC(@5));
} ;
module_head:
MODULE opt_symbol requires {
$$ = make_module(f_module, $2, $3, NULL);
} |
LIBRARY plain_symbol requires defines {
$$ = make_module(f_library, $2, $3, $4);
} ;
opt_symbol:
plain_symbol |
%empty { $$ = NULL; } ;
requires:
REQUIRES { varlist_name = "required module"; } variable_list { $$ = $3; } |
%empty { $$ = NULL; } ;
defines:
DEFINES { varlist_name = "defined variable"; } variable_list { $$ = $3; } ;
reads:
READS { varlist_name = "read variable"; } variable_list { $$ = $3; } |
%empty { $$ = NULL; } ;
writes:
WRITES { varlist_name = "written variable"; } variable_list { $$ = $3; } |
%empty { $$ = NULL; } ;
statics:
STATIC { varlist_name = "static variable"; } variable_list { $$ = $3; } |
%empty { $$ = NULL; } ;
open_brace:
'{' { push_bracket('{', &@1); } ;
close_brace:
'}' { pop_bracket('{'); } ;
open_lt:
'<' { push_bracket('<', &@1); } ;
close_lt:
'>' { pop_bracket('<'); } ;
open_paren:
'(' { push_bracket('(', &@1); } ;
close_paren:
')' { pop_bracket('('); } ;
open_sq_bracket:
'[' { push_bracket('[', &@1); } ;
close_sq_bracket:
']' { pop_bracket('['); } ;
open_vbar:
'|' { push_bracket('|', &@1); } ;
close_vbar:
'|' { pop_bracket('|'); } ;
expr_list:
simple_expr_list ';' opt_implicit_code_block {
struct clist *cl = $1;
if ($3 != NULL) {
struct component *comp = new_block_component($3);
cl = new_clist(comp, cl);
}
$$ = reverse_clist(cl);
} |
simple_expr_list {
$$ = reverse_clist($1);
} ;
opt_implicit_code_block:
%empty { $$ = NULL; } |
local_vars expr_list {
$$ = new_codeblock($1, $2, LLOC_LOC(@1));
} ;
simple_expr_list:
simple_expr_list ';' expr {
$$ = new_clist($3, $1);
} |
simple_expr_list ';' ELSE {
maybe_compile_error(&@2, "there must be no semicolon before 'else'");
YYABORT;
} |
expr {
$$ = new_clist($1, NULL);
} ;
opt_semi: %empty | ';' ;
opt_expr:
expr |
%empty { $$ = NULL; } ;
one_expression:
START_ANY_EXPR e0 { $$ = $2; } |
START_PRIMARY_EXPR primary_expr { $$ = $2; } |
START_PAREN_EXPR paren_expr { $$ = $2; } ;
expr:
label expr {
$$ = new_labeled_component(LLOC_LOC(@1), $1, $2);
} |
e0 ;
label: open_lt plain_symbol close_lt { $$ = $2; } ;
e0:
control_expr |
function_expr {
$$ = new_closure_component(LLOC_LOC(@1), $1);
} |
assign_expr |
e1 ;
control_expr: if | while | loop | exit | match | for ;
if:
IF_KEYWORD open_paren expr close_paren expr ELSE expr {
$$ = new_ternop_component(LLOC_LOC(@1), b_ifelse, $3, $5, $7);
} |
IF_KEYWORD open_paren expr close_paren expr %prec NOT_ELSE {
$$ = new_binop_component(LLOC_LOC(@1), arith_default, b_if, $3, $5);
} ;
while:
WHILE open_paren expr close_paren expr {
$$ = new_binop_component(LLOC_LOC(@1), arith_default, b_while, $3, $5);
} ;
loop:
LOOP expr {
$$ = new_unop_component(LLOC_LOC(@1), arith_default, b_loop, $2);
} ;
for:
FOR open_paren opt_local_vars opt_expr ';' opt_expr ';' opt_expr
close_paren expr {
$$ = new_for_component($3, $4, $6, $8, $10, LLOC_LOC(@1));
} ;
match:
match_type open_paren expr close_paren
open_sq_bracket match_list opt_semi close_sq_bracket {
$$ = new_match_component($1, $3, $6, LLOC_LOC(@1));
} ;
match_type:
MATCH { $$ = false; } |
MATCH_FORCE { $$ = true; } ;
match_list:
match_node { $$ = new_match_list($1, NULL); } |
match_list ';' match_node { $$ = new_match_list($3, $1); } ;
match_node:
pattern_or PATTERN_MATCH expr {
struct vlist *vl = NULL;
if (pattern_has_collisions($1, &vl, NULL))
YYABORT;
$$ = new_match_node($1, $3, vl, LLOC_LOC(@1));
} ;
pattern_or:
pattern_and opt_pattern_or {
$$ = $1;
if ($2 != NULL)
$$ = new_pattern_or($1, $2, LLOC_LOC(@1));
} ;
pattern_and:
pattern_atom LOGICAL_AND logical_and_expr {
$$ = new_pattern_and_expr($1, $3);
} |
pattern_atom ;
opt_label:
label { $$ = $1; } |
%empty { $$ = NULL; } ;
exit:
LOOP_EXIT opt_label e0 {
$$ = new_exit_component(LLOC_LOC(@1), $2, $3);
} ;
function_expr:
typeset FUNCTION f0 {
@$ = @2;
$$ = $3;
$$->loc = *LLOC_LOC(@2);
$$->return_typeset = $1;
} |
FUNCTION f0 {
$$ = $2;
$$->loc = *LLOC_LOC(@1);
} ;
f0:
help f1 {
$$ = $2;
$$->help = $1;
} |
f1 ;
f1:
open_paren opt_arglist close_paren expr {
$$ = new_fn($4, $2, LLOC_LOC(@4), LLOC_LAST_LOC(@4));
if (!check_function_arguments($$))
YYABORT;
} ;
help:
help '+' STRING {
$$.len = $1.len + $3.len;
char *nbuf = allocate(parser_heap(), $$.len);
memcpy(nbuf, $1.str, $1.len);
memcpy(nbuf + $1.len, $3.str, $3.len);
$$.str = nbuf;
} | STRING ;
opt_arglist:
%empty { $$ = new_function_args(); } |
arglist opt_ellipsis {
$$ = $1;
if ($2)
{
++$$->noptargs;
$$->last_arg->is_vararg = true;
if ($$->last_arg->typeset != TYPESET_ANY)
{
compile_error(
&$$->last_arg->loc,
"variable-length argument cannot have a type specified");
YYABORT;
}
if ($$->last_arg->default_val != NULL)
{
compile_error(
&$$->last_arg->default_val->loc,
"variable-length argument cannot have a default value");
YYABORT;
}
}
else if (!check_opt_arg($1, LLOC_LAST_LOC(@1)))
YYABORT;
};
opt_ellipsis:
%empty { $$ = false; } |
ELLIPSIS { $$ = true; } ;
arglist:
arglist COMMA arg {
if (!check_opt_arg($1, LLOC_LOC(@2)))
YYABORT;
$1->last_arg->next = $3;
$1->last_arg = $3;
++$1->nargs;
if ($3->default_val)
++$1->noptargs;
if ($1->nargs > MAX_FUNCTION_ARGS)
{
compile_error(LLOC_LOC(@3),
"functions can have at most %d formal arguments",
MAX_FUNCTION_ARGS);
YYABORT;
}
$$ = $1;
} |
arg {
$$ = new_function_args();
$$->args = $$->last_arg = $1;
$$->nargs = 1;
if ($1->default_val)
$$->noptargs = 1;
};
arg:
argname |
argname '=' expr {
$1->default_val = $3;
$1->is_optional = true;
$$ = $1;
};
argname:
typeset plain_symbol {
$$ = new_function_arg($2, $1, NULL, LLOC_LOC(@2));
} |
plain_symbol {
$$ = new_function_arg($1, TYPESET_ANY, NULL, LLOC_LOC(@1));
} |
'@' pattern_or {
$$ = new_function_arg(NULL, TYPESET_ANY, NULL, LLOC_LOC(@1));
$$->pat = $2;
} ;
typeset:
type { $$ = type_typeset($1); } |
typeset_braced ;
opt_typeset_braced:
%empty { $$ = TYPESET_ANY; } |
typeset_braced;
typeset_braced:
open_brace typelist close_brace { $$ = $2; } ;
typelist:
typelist COMMA type {
typeset_t tset = type_typeset($3);
if (($$ & tset) == tset)
compile_warning(
LLOC_LOC(@3),
"redundant type %s%s%s",
CMARKUP(type, mudlle_type_names[$3]));
else if (($$ & tset) == $$)
compile_warning(
LLOC_LOC(@3),
"type %s%s%s makes previous types redundant",
CMARKUP(type, mudlle_type_names[$3]));
$$ = $1 | tset;
typeset_t mask = TSET(integer) | TYPESET_FALSE;
if (($$ & mask) == mask)
{
compile_error(
LLOC_LOC(@3),
"specifying types false and int together is invalid");
YYABORT;
}
} |
type { $$ = type_typeset($1); } ;
type:
plain_symbol { if (!find_type(&@1, $1, &$$)) YYABORT; } ;
assign_expr:
unary_expr ASSIGN_OP expr {
$$ = new_assign_modify_expr(cur_arith_mode, $2, $1, $3, LLOC_LOC(@2));
if ($$ == NULL)
YYABORT;
} |
unary_expr '=' expr {
$$ = new_assign_expression($1, $3, LLOC_LOC(@2), LLOC_LOC(@1));
if ($$ == NULL)
YYABORT;
} |
'@' pattern {
if (pattern_has_collisions($2, NULL, NULL))
YYABORT;
} '=' expr {
$$ = new_pattern_component($2, $5);
} ;
e1:
e1 '.' e1 { BINOP($$, cons, @2, $1, @1, $3, @3); } |
e1 LOGICAL_OR e1 { BINOP($$, logical_or, @2, $1, @1, $3, @3); } |
e1 LOGICAL_XOR e1 { BINOP($$, logical_xor, @2, $1, @1, $3, @3); } |
logical_and_expr ;
logical_and_expr:
logical_and_expr LOGICAL_AND e2 {
BINOP($$, logical_and, @2, $1, @1, $3, @3);
} |
e2 ;