-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_rules.y
2205 lines (2023 loc) · 67.9 KB
/
parse_rules.y
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
%{
#define MAX_IDENT_LEN 128
#define MAX_STR_LEN 1024
#define MAX_INCLUDE_LEVELS 64
#define FUNC_PREFIX "_"
#define VAR_PREFIX "_"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include "configs.h"
#include "strlist.h"
#include "keyval.h"
#include "funcinfo.h"
#include "strutils.h"
kvmap included_files;
strlist namespace_list;
strlist using_list;
strlist namespaces_active;
strlist inits_list;
funclist funcs_list;
funclist externs_list;
kvmap global_consts;
kvmap global_vars;
strlist vardecl_list;
kvmap scoping_vars_used;
kvmaplist scoping_consts;
kvmaplist scoping_vars;
FILE *yyin=NULL;
FILE *outf=NULL;
int yylineno = 1;
const char *yydirname = NULL;
const char *yyfilename = NULL;
int yylex(void);
int yyparse(void);
void yyerror(char *s);
void muv_warn(char *arg);
char *decl_new_variable(const char *name);
const char *includes_dir = MUV_INCLUDES_DIR;
int debugging_level = 0;
int do_optimize = 1;
int allow_nonsys_includes = 1;
int has_tuple_check = 0;
const char *tuple_check =
": tuple_check[ arr expect pos -- ]\n"
" arr @ array? not if\n"
" \"Cannot unpack from non-array in \" pos @ strcat abort\n"
" then\n"
" arr @ array_count expect @ = not if\n"
" \"Wrong number of values to unpack in \" pos @ strcat abort\n"
" then\n"
";\n";
struct bookmark_t {
const char *dname;
const char *fname;
long pos;
long lineno;
} bookmarks[MAX_INCLUDE_LEVELS];
int bookmark_count = 0;
int bookmark_push(const char *fname, int doinit);
int bookmark_pop();
/* compiler state exception flag */
%}
%union {
int token;
char *str;
int num_int;
strlist list;
keyval keyval;
funcinfo prim;
accessor getset;
}
%token <num_int> INTEGER
%token <prim> DECLARED_FUNC
%token <str> FLOAT STR IDENT VAR CONST
%token <keyval> DECLARED_CONST DECLARED_VAR
%token <token> INCLUDE UNARY NAMESPACE
%token <token> D_LANGUAGE D_INCLUDE D_AUTHOR
%token <token> D_VERSION D_LIBVERSION D_PRAGMA
%token <token> D_WARN D_ERROR D_NOTE D_ECHO
%token <token> IF ELSE UNLESS PUBLIC
%token <token> FUNC RETURN TRY CATCH
%token <token> SWITCH USING CASE DEFAULT
%token <token> DO WHILE UNTIL FOR BY
%token <token> CONTINUE BREAK
%token <token> TOP PUSH MUF DEL FMTSTRING
%token <token> EXTERN VOID SINGLE MULTIPLE
%right BARE ELSE SUFFIX
%left ',' KEYVAL
%right ASGN PLUSASGN MINUSASGN MULTASGN DIVASGN MODASGN BITLEFTASGN BITRIGHTASGN BITANDASGN BITORASGN BITXORASGN
%left DECR INCR
%right '?' ':'
%left OR XOR
%left AND
%left BITOR
%left BITXOR
%left BITAND
%left EQEQ NEQ STREQ
%left GT GTE LT LTE IN
%left BITLEFT BITRIGHT
%left PLUS MINUS
%left MULT DIV MOD
%right UNARY NOT BITNOT
%left '[' ']' '(' ')' APPEND DOT
%type <str> globalstatement ns_ident directive
%type <str> proposed_funcname maybe_bad_funcname
%type <str> proposed_varname maybe_bad_varname
%type <str> simple_statement statement statements paren_expr
%type <str> compr_loop compr_cond subscript
%type <str> function_call expr attribute settable
%type <str> using_clause case_clause case_clauses default_clause
%type <list> arglist arglist_or_null dictlist argvarlist
%type <list> index_parts tuple_parts
%type <getset> lvalue
%type <num_int> ret_count_type opt_varargs opt_public
%start program
%%
program: /* nothing */ { }
| program globalstatement { if (outf) fprintf(outf, "%s", $2); free($2); }
| program nsdecl '{' program '}' {
strlist_pop(&namespace_list);
}
;
nsdecl: NAMESPACE ns_ident {
char full[MAX_IDENT_LEN];
const char *ns = strlist_top(&namespace_list);
if (ns && *ns) {
snprintf(full, sizeof(full), "%s::%s", ns, $2);
} else {
strcpy(full, $2);
}
strlist_add(&namespace_list, full);
free($2);
}
;
globalstatement:
MUF '(' STR ')' ';' { $$ = savefmt("%s\n", $3); free($3); }
| directive { $$ = $1; }
| USING NAMESPACE ns_ident ';' {
strlist_add(&namespaces_active, $3);
$$ = savestring("");
free($3);
}
| INCLUDE STR ';' {
if (!bookmark_push($2, 0))
YYERROR;
$$ = savestring("");
free($2);
}
| CONST proposed_varname ASGN expr ';' {
char *vname;
const char *ns = strlist_top(&namespace_list);
if (ns && *ns) {
vname = savefmt("%s::%s", ns, $2);
} else {
vname = savestring($2);
}
$$ = savestring("");
kvmap_add(&global_consts, vname, $4);
free(vname);
free($2); free($4);
}
| VAR proposed_varname ';' {
char *vname;
char *code;
const char *ns = strlist_top(&namespace_list);
if (ns && *ns) {
vname = savefmt("%s::%s", ns, $2);
code = savefmt("%s::%s", ns, $2);
} else {
vname = savestring($2);
code = savefmt("%s%s", VAR_PREFIX, $2);
}
$$ = savefmt("lvar %s\n", code);
kvmap_add(&global_vars, vname, code);
free(vname);
free(code);
free($2);
}
| VAR proposed_varname ASGN expr ';' {
char *vname, *code, *init;
const char *ns = strlist_top(&namespace_list);
if (ns && *ns) {
vname = savefmt("%s::%s", ns, $2);
code = savefmt("%s::%s", ns, $2);
} else {
vname = savestring($2);
code = savefmt("%s%s", VAR_PREFIX, $2);
}
$$ = savefmt("lvar %s\n", code);
kvmap_add(&global_vars, vname, code);
init = savefmt("%s %s !", $4, code);
strlist_add(&inits_list, init);
free(init);
free(vname);
free(code);
free($2);
free($4);
}
| EXTERN su ret_count_type proposed_funcname '(' argvarlist opt_varargs ')' sd ';' {
char *fname;
const char *ns = strlist_top(&namespace_list);
if (ns && *ns) {
fname = savefmt("%s::%s", ns, $4);
} else {
fname = savestring($4);
}
funclist_add(&externs_list, fname, $4, $6.count - ($7?1:0), $3, $7);
$$ = savestring("");
free(fname);
free($4);
strlist_free(&$6);
strlist_clear(&vardecl_list);
kvmap_clear(&scoping_vars_used);
}
| EXTERN su ret_count_type proposed_funcname '(' argvarlist opt_varargs ')' ASGN STR sd ';' {
char *fname;
const char *ns = strlist_top(&namespace_list);
if (ns && *ns) {
fname = savefmt("%s::%s", ns, $4);
} else {
fname = savestring($4);
}
funclist_add(&externs_list, fname, $10, $6.count - ($7?1:0), $3, $7);
$$ = savestring("");
free(fname);
free($4);
strlist_free(&$6);
free($10);
strlist_clear(&vardecl_list);
kvmap_clear(&scoping_vars_used);
}
| opt_public FUNC su proposed_funcname '(' argvarlist opt_varargs ')' {
/* Mid-rule action to make sure function is declared
* before statements, to allow possible recursion. */
char *fname;
char *code;
const char *ns = strlist_top(&namespace_list);
if (ns && *ns) {
fname = savefmt("%s::%s", ns, $4);
if ($1) {
char *p;
for (p = fname; *p; p++) {
if (*p == ':') *p = '_';
}
}
code = savestring(fname);
} else {
fname = savestring($4);
if ($1) {
code = savestring($4);
} else {
code = savefmt("%s%s", FUNC_PREFIX, $4);
}
}
funclist_add(&funcs_list, fname, code, $6.count - ($7?1:0), 1, $7);
free(fname);
free(code);
strlist_clear(&vardecl_list);
} '{' statements '}' sd {
char *fname;
const char *ns = strlist_top(&namespace_list);
char *body, *vars, *decls, *idecls;
if (ns && *ns) {
fname = savefmt("%s::%s", ns, $4);
if ($1) {
char *p;
for (p = fname; *p; p++) {
if (*p == ':') *p = '_';
}
}
} else {
if ($1) {
fname = savestring($4);
} else {
fname = savefmt("%s%s", FUNC_PREFIX, $4);
}
}
body = indent($11);
vars = strlist_join(&$6, " ", 0, -1);
decls = strlist_wrap(&vardecl_list, 0, -1);
idecls = indent(decls);
if (*idecls) {
idecls = appendstr(idecls, "\n", NULL);
}
free(decls);
if (endswith(body, " exit")) {
body[strlen(body)-5] = '\0';
$$ = savefmt(": %s[ %s -- ret ]\n%s%s\n;\n", fname, vars, idecls, body);
} else {
$$ = savefmt(": %s[ %s -- ret ]\n%s%s\n 0\n;\n", fname, vars, idecls, body);
}
if ($1) {
$$ = appendfmt($$, "public %s\n", fname);
$$ = appendfmt($$, "$libdef %s\n", fname);
}
strlist_clear(&vardecl_list);
kvmap_clear(&scoping_vars_used);
free(idecls);
free(vars);
free(body);
free(fname);
free($4);
strlist_free(&$6);
free($11);
}
;
directive:
D_LANGUAGE STR {
if (strcmp($2, "muv")) {
yyerror("Only $language \"muv\" allowed.");
YYERROR;
}
$$ = savestring("");
free($2);
}
| D_VERSION FLOAT { $$ = savefmt("$version %s\n", $2); free($2); }
| D_LIBVERSION FLOAT { $$ = savefmt("$lib-version %s\n", $2); free($2); }
| D_AUTHOR STR { $$ = savefmt("$author %s\n", $2); free($2); }
| D_NOTE STR { $$ = savefmt("$note %s\n", $2); free($2); }
| D_ECHO STR { $$ = savefmt("$echo %s\n", $2); free($2); }
| D_PRAGMA STR { $$ = savefmt("$pragma %s\n", $2); free($2); }
| D_INCLUDE STR { $$ = savefmt("$include %s\n", $2); free($2); }
| D_ERROR STR { yyerror($2); $$ = savestring(""); free($2); YYERROR; }
| D_WARN STR { muv_warn($2); $$ = savestring(""); free($2); }
;
opt_public: /* nothing */ { $$ = 0; }
| PUBLIC { $$ = 1; }
;
ns_ident: IDENT { $$ = savestring($1); free($1); }
| DECLARED_VAR { $$ = savestring($1.key); keyval_free(&$1); }
| DECLARED_CONST { $$ = savestring($1.key); keyval_free(&$1); }
| DECLARED_FUNC { $$ = savestring($1.name); }
;
maybe_bad_funcname:
DECLARED_VAR { $$ = savestring($1.key); keyval_free(&$1); }
| DECLARED_CONST { $$ = savestring($1.key); keyval_free(&$1); }
| DECLARED_FUNC { $$ = savestring($1.name); }
;
proposed_funcname: IDENT { $$ = savestring($1); free($1); }
| maybe_bad_funcname {
char *vname;
const char *ns = strlist_top(&namespace_list);
if (ns && *ns) {
vname = savefmt("%s%s::%s", VAR_PREFIX, ns, $1);
} else {
vname = savefmt("%s%s", VAR_PREFIX, $1);
}
if (
kvmap_get(&global_consts, vname) ||
kvmap_get(&global_vars, vname) ||
funclist_find(&funcs_list, vname) ||
funclist_find(&externs_list, vname)
) {
char *err = savefmt("Indentifier '%s' already declared.", $1);
yyerror(err);
free(err);
YYERROR;
}
free(vname);
$$ = $1;
}
;
maybe_bad_varname:
DECLARED_VAR { $$ = savestring($1.key); keyval_free(&$1); }
| DECLARED_CONST { $$ = savestring($1.key); keyval_free(&$1); }
| DECLARED_FUNC { $$ = savestring($1.name); }
;
proposed_varname: IDENT { $$ = savestring($1); free($1); }
| maybe_bad_varname {
char *vname;
const char *ns = strlist_top(&namespace_list);
if (ns && *ns) {
vname = savefmt("%s%s::%s", VAR_PREFIX, ns, $1);
} else {
vname = savefmt("%s%s", VAR_PREFIX, $1);
}
if (
kvmap_get(&global_consts, vname) ||
kvmap_get(&global_vars, vname) ||
funclist_find(&funcs_list, vname) ||
funclist_find(&externs_list, vname)
) {
char *err = savefmt("Indentifier '%s' already declared.", $1);
yyerror(err);
free(err);
YYERROR;
}
free(vname);
$$ = $1;
}
;
ret_count_type:
VOID { $$ = 0; }
| SINGLE { $$ = 1; }
| MULTIPLE { $$ = 99; }
;
opt_varargs: /* nothing */ { $$ = 0; }
| MULT { $$ = 1; }
;
argvarlist: /* nothing */ { strlist_init(&$$); }
| proposed_varname {
char *vname = decl_new_variable($1);
if (!vname) {
char *err = savefmt("Indentifier '%s' already declared.", $1);
yyerror(err);
free(err);
YYERROR;
}
strlist_init(&$$);
strlist_add(&$$, vname);
free(vname);
free($1);
}
| argvarlist ',' proposed_varname {
char *vname = decl_new_variable($3);
if (!vname) {
char *err = savefmt("Indentifier '%s' already declared.", $3);
yyerror(err);
free(err);
YYERROR;
}
$$ = $1;
strlist_add(&$$, vname);
free(vname);
free($3);
}
;
su: /* nothing */ { /* scope up */ kvmaplist_add(&scoping_consts); kvmaplist_add(&scoping_vars); } ;
sd: /* nothing */ { /* scope down */ kvmaplist_pop(&scoping_consts); kvmaplist_pop(&scoping_vars); } ;
simple_statement:
RETURN { $$ = savestring("0 exit"); }
| RETURN expr { $$ = savefmt("%s exit", $2); free($2); }
| BREAK { $$ = savestring("break"); }
| CONTINUE { $$ = savestring("continue"); }
| expr {
if (!*$1) {
$$ = savestring("");
} else {
char *out;
struct optims_t {
const char *pat;
const char *repl;
} optims[] = {
{"0 pop", ""},
{"1 pop", ""},
{"dup %1 ! pop", "%1 !"},
{"%1 ! %1 @", "dup %1 !"},
{"%1 @ %1 @ %1 @", "%1 @ dup dup"},
{"%1 @ %1 @", "%1 @ dup"},
{"%1 @ ++ dup %1 ! pop", "%1 ++"},
{"%1 @ -- dup %1 ! pop", "%1 --"},
{"%1 @ dup ++ %1 ! pop", "%1 ++"},
{"%1 @ dup -- %1 ! pop", "%1 --"},
{"%1 @ ++ dup %1 !", "%1 ++ %1 @"},
{"%1 @ -- dup %1 !", "%1 -- %1 @"},
{"dup 4 rotate 4 rotate ->[] %1 ! pop", "rot rot ->[] %1 !"},
{"dup 4 rotate 4 rotate array_nested_set %1 ! pop", "rot rot array_nested_set %1 !"},
{NULL, NULL}
};
out = savefmt("%s pop", $1);
if (do_optimize) {
int i;
for (i = 0; optims[i].pat; i++) {
char *tmp = replace_words(out, optims[i].pat, optims[i].repl);
free(out);
out = tmp;
}
}
$$ = out;
}
free($1);
}
;
statement: ';' { $$ = savestring(""); }
| simple_statement ';' { $$ = $1; }
| simple_statement IF paren_expr ';' {
char *body = wrapit("if", $1, "then");
$$ = appendstr(savestring($3), body, NULL);
free($1); free($3);
free(body);
}
| simple_statement UNLESS paren_expr ';' {
char *body = wrapit("not if", $1, "then");
$$ = appendstr(savestring($3), body, NULL);
free($1); free($3);
free(body);
}
| CONST proposed_varname ASGN expr ';' {
kvmap *m = kvmaplist_top(&scoping_consts);
kvmap_add(m, $2, $4);
$$ = savestring("");
free($2); free($4);
}
| IF paren_expr statement %prec BARE {
char *pfx = savefmt("%s if", $2);
$$ = wrapit(pfx, $3, "then");
free(pfx);
free($2); free($3);
}
| IF paren_expr statement ELSE statement {
char *pfx = savefmt("%s if", $2);
$$ = wrapit2(pfx, $3, "else", $5, "then");
free(pfx);
free($2); free($3); free($5);
}
| WHILE su paren_expr statement sd {
if (!strcmp($3, "1")) {
$$ = wrapit("begin", $4, "repeat");
} else {
$$ = wrapit2("begin", $3, "while", $4, "repeat");
}
free($3); free($4);
}
| UNTIL su paren_expr statement sd {
if (!strcmp($3, "0")) {
$$ = wrapit("begin", $4, "repeat");
} else {
$3 = appendstr($3, "not", NULL);
$$ = wrapit2("begin", $3, "while", $4, "repeat");
}
free($3); free($4);
}
| DO su statement WHILE paren_expr sd ';' {
if (!strcmp($5, "1")) {
$$ = wrapit("begin", $3, "repeat");
} else {
$3 = appendstr($3, $5, "not", NULL);
$$ = wrapit("begin", $3, "until");
}
free($3); free($5);
}
| DO su statement UNTIL paren_expr sd ';' {
if (!strcmp($5, "0")) {
$$ = wrapit("begin", $3, "repeat");
} else {
$3 = appendstr($3, $5, NULL);
$$ = wrapit("begin", $3, "until");
}
free($3); free($5);
}
| FOR '(' su settable IN expr KEYVAL expr ')' statement sd {
char *pfx = appendstr(NULL, $6, $8, "1", "for", NULL);
char *body = appendstr(NULL, $4, "\n", $10, NULL);
$$ = wrapit(pfx, body, "repeat");
free(body);
free(pfx);
free($4); free($6); free($8); free($10);
}
| FOR '(' su settable IN expr KEYVAL expr BY expr ')' statement sd {
char *pfx = appendstr(NULL, $6, $8, $10, "for", NULL);
char *body = appendstr(NULL, $4, "\n", $12, NULL);
$$ = wrapit(pfx, body, "repeat");
free(body);
free(pfx);
free($4); free($6); free($8); free($10); free($12);
}
| FOR '(' su settable IN expr ')' statement sd {
char *pfx = appendstr(NULL, $6, "foreach", NULL);
char *ind = appendstr(NULL, $4, "pop\n", $8, NULL);
$$ = wrapit(pfx, ind, "repeat");
free(ind);
free(pfx);
free($4); free($6); free($8);
}
| FOR '(' su settable KEYVAL settable IN expr ')' statement sd {
char *pfx = appendstr(NULL, $8, "foreach", NULL);
char *ind = appendstr(NULL, $6, $4, "\n", $10, NULL);
$$ = wrapit(pfx, ind, "repeat");
free(ind);
free(pfx);
free($4); free($6); free($8); free($10);
}
| TRY statement CATCH '(' ')' statement {
$$ = wrapit2("0 try", $2, "catch pop", $6, "endcatch");
free($2); free($6);
}
| TRY statement CATCH '(' su lvalue ')' statement sd {
char *mid = savefmt("catch_detailed %s", $6.set);
$$ = wrapit2("0 try", $2, mid, $8, "endcatch");
free(mid);
free($2); getset_free(&$6); free($8);
}
| SWITCH '(' expr using_clause ')' '{' case_clauses default_clause '}' {
$3 = appendstr($3, "\n", $7, $8, NULL);
$$ = wrapit("0 begin pop (switch)", $3, "repeat pop");
strlist_pop(&using_list);
free($3); free($4); free($7); free($8);
}
| '{' su statements sd '}' { $$ = $3; }
;
using_clause: /* nothing */ { $$ = savestring("="); strlist_add(&using_list, $$); }
| USING MUF '(' STR ')' { $$ = savestring($4); strlist_add(&using_list, $$); free($4); }
| USING EQEQ { $$ = savestring("="); strlist_add(&using_list, $$); }
| USING NEQ { $$ = savestring("= not"); strlist_add(&using_list, $$); }
| USING LT { $$ = savestring("<"); strlist_add(&using_list, $$); }
| USING LTE { $$ = savestring("<="); strlist_add(&using_list, $$); }
| USING GT { $$ = savestring(">"); strlist_add(&using_list, $$); }
| USING GTE { $$ = savestring(">="); strlist_add(&using_list, $$); }
| USING STREQ { $$ = savestring("strcmp not"); strlist_add(&using_list, $$); }
| USING IN { $$ = savestring("swap array_findval"); strlist_add(&using_list, $$); }
| USING DECLARED_FUNC {
if ($2.expects != 2) {
yyerror("Using clause expects instruction or function that takes 2 args.");
YYERROR;
}
$$ = savestring($2.code);
if (!strcmp($$, "stringcmp")) {
strlist_add(&using_list, "stringcmp not");
} else if (!strcmp($$, "strcmp")) {
strlist_add(&using_list, "strcmp not");
} else {
strlist_add(&using_list, $2.code);
}
}
;
case_clauses: case_clause { $$ = $1; }
| case_clauses case_clause { $$ = savefmt("%s%s", $1, $2); free($1); free($2); }
;
case_clause: CASE paren_expr statement {
char *pfx = appendstr(NULL, "dup", $2, using_list.list[using_list.count-1], "if", NULL);
$3 = appendstr($3, "break", NULL);
$$ = wrapit(pfx, $3, "then\n");
free(pfx);
free($2); free($3);
} ;
default_clause: /* nothing */ { $$ = savestring("break"); }
| DEFAULT statement { $$ = savefmt("(default)\n%s break", $2); free($2); }
;
paren_expr: '(' expr ')' { $$ = $2; } ;
statements: /* nothing */ { $$ = savestring(""); }
| statements statement {
$$ = savestring($1);
if (*$$ && *$2) {
$$ = appendstr($$, "\n", NULL);
}
char *dbg = savestring("");
if (debugging_level == 1) {
int lineno = bookmark_count? bookmarks[0].lineno : yylineno;
dbg = appendfmt(dbg, "(MUV:L%d) ", lineno);
} else if (debugging_level == 2) {
dbg = appendfmt(dbg, "\"%s:%d\" pop ", (*yyfilename?yyfilename:"muv"), yylineno);
}
$$ = appendfmt($$, "%s%s", dbg, $2);
free(dbg);
free($1); free($2);
}
;
function_call: DECLARED_FUNC '(' arglist_or_null ')' {
char *basecall;
if ($1.hasvarargs) {
if ($3.count < $1.expects) {
char *err = savefmt(
"Function '%s' expects at least %d args, but was provided %d args.",
$1.name, $1.expects, $3.count
);
yyerror(err);
strlist_free(&$3);
free(err);
YYERROR;
}
} else {
if ($3.count != $1.expects) {
char *err = savefmt(
"Function '%s' expects %d args, but was provided %d args.",
$1.name, $1.expects, $3.count
);
yyerror(err);
strlist_free(&$3);
free(err);
YYERROR;
}
}
if ($1.hasvarargs) {
char *vargs = strlist_wrap(&$3, $1.expects, -1);
char *vlist = wrapit("{", vargs, "}list");
basecall = appendstr(strlist_wrap(&$3, 0, $1.expects), vlist, $1.code, NULL);
free(vlist);
free(vargs);
} else {
basecall = appendstr(strlist_wrap(&$3, 0, -1), $1.code, NULL);
}
if ($1.returns == 0) {
$$ = savefmt("%s 0", basecall);
} else if ($1.returns == 1) {
$$ = savestring(basecall);
} else {
$$ = wrapit("{", basecall, "}list");
}
free(basecall);
strlist_free(&$3);
} ;
lvalue: IDENT {
char *errstr = savefmt("Undeclared identifier '%s'.", $1);
yyerror(errstr);
free(errstr);
free($1);
YYERROR;
}
| VAR proposed_varname {
char *vname = decl_new_variable($2);
if (!vname) {
char *err = savefmt("Indentifier '%s' already declared at this scope level.", $2);
yyerror(err);
free(err);
free($2);
YYERROR;
}
$$.get = savestring("");
$$.set = savefmt("%s !", vname);
$$.del = savefmt("0 %s !", vname);
$$.oper_pre = savestring("0");
$$.oper_post = savefmt("%s !", vname);
$$.call = savestring("");
free(vname);
free($2);
}
| DECLARED_VAR %prec BARE {
$$.get = savefmt("%s @", $1.val);
$$.set = savefmt("%s !", $1.val);
$$.del = savefmt("0 %s !", $1.val);
$$.oper_pre = savefmt("%s @", $1.val);
$$.oper_post = savefmt("%s !", $1.val);
$$.call = savefmt("%s\ndup address? if\n execute\nelse\n } popn \"Tried to execute a non-address in %s:%d\" abort\nthen", $$.get, yyfilename, yylineno);
keyval_free(&$1);
}
| DECLARED_VAR index_parts %prec SUFFIX {
char *idx = strlist_wrap(&$2, 0, -1);
char *idxlist = strlist_wrapit("{", &$2, "}list");
if ($2.count == 1) {
$$.get = savefmt("%s @ %s []", $1.val, idx);
$$.set = savefmt("%s @ %s ->[] %s !", $1.val, idx, $1.val);
$$.del = savefmt("%s @ %s array_delitem %s !", $1.val, idx, $1.val);
$$.oper_pre = savefmt("%s @ %s over over []", $1.val, idx);
$$.oper_post = savefmt("4 rotate 4 rotate ->[] %s !", $1.val);
} else {
$$.get = savefmt("%s @ %s array_nested_get", $1.val, idxlist);
$$.set = savefmt("%s @ %s array_nested_set %s !", $1.val, idxlist, $1.val);
$$.del = savefmt("%s @ %s array_nested_del %s !", $1.val, idxlist, $1.val);
$$.oper_pre = savefmt("%s @ %s over over array_nested_get", $1.val, idxlist);
$$.oper_post = savefmt("4 rotate 4 rotate array_nested_set %s !", $1.val);
}
$$.call = savefmt("%s\ndup address? if\n execute\nelse\n } popn \"Tried to execute a non-address in %s:%d\" abort\nthen", $$.get, yyfilename, yylineno);
free(idx);
free(idxlist);
keyval_free(&$1);
strlist_free(&$2);
}
;
settable: lvalue { $$ = savestring($1.set); getset_free(&$1); }
| LT tuple_parts GT {
int i;
if (debugging_level) {
if (!has_tuple_check) {
if (outf) {
fprintf(outf, "%s", tuple_check);
}
has_tuple_check = 1;
}
$$ = savefmt("dup %d \"%s:%d\" tuple_check", $2.count, yyfilename, yylineno);
} else {
$$ = savestring("");
}
for (i = 0; i < $2.count; i++) {
$$ = appendfmt($$, "dup %d [] %s", i, $2.list[i]);
}
$$ = appendstr($$, "pop", NULL);
strlist_free(&$2);
};
tuple_parts: lvalue {
strlist_init(&$$);
strlist_add(&$$, $1.set);
getset_free(&$1);
}
| tuple_parts ',' lvalue {
$$ = $1;
strlist_add(&$$, $3.set);
getset_free(&$3);
}
;
index_parts:
subscript { strlist_init(&$$); strlist_add(&$$, $1); free($1); }
| attribute { strlist_init(&$$); strlist_add(&$$, $1); free($1); }
| index_parts subscript { $$ = $1; strlist_add(&$$, $2); free($2); }
| index_parts attribute { $$ = $1; strlist_add(&$$, $2); free($2); }
;
subscript: '[' expr ']' { $$ = $2; } ;
attribute: DOT IDENT { $$ = format_muv_str($2); free($2); }
| DOT DECLARED_CONST { $$ = format_muv_str($2.key); keyval_free(&$2); }
| DOT DECLARED_VAR { $$ = format_muv_str($2.key); keyval_free(&$2); }
| DOT DECLARED_FUNC { $$ = format_muv_str($2.name); }
;
compr_cond: /* nothing */ { $$ = savestring(""); }
| IF paren_expr {
$$ = appendstr(NULL, $2, "if", NULL);
free($2);
}
| UNLESS paren_expr {
$$ = appendstr(NULL, $2, "not", "if", NULL);
free($2);
}
;
compr_loop:
'(' settable IN expr KEYVAL expr ')' {
char *ind = indent($2);
$$ = appendstr(NULL, $4, $6, "1", "for\n", ind, NULL);
free(ind);
free($2); free($4); free($6);
}
| '(' settable IN expr KEYVAL expr BY expr ')' {
char *ind = indent($2);
$$ = appendstr(NULL, $4, $6, $8, "for\n", ind, NULL);
free(ind);
free($2); free($4); free($6); free($8);
}
| '(' settable IN expr ')' {
char *ind = indent($2);
$$ = appendstr(NULL, $4, "foreach\n", ind, "pop", NULL);
free(ind);
free($2); free($4);
}
| '(' settable KEYVAL settable IN expr ')' {
char *set = appendstr(NULL, $4, $2, NULL);
char *iset = indent(set);
$$ = appendstr(NULL, $6, "foreach\n", iset, NULL);
free(iset); free(set);
free($2); free($4); free($6);
}
;
expr: paren_expr { $$ = $1; }
| INTEGER { $$ = savefmt("%d", $1); }
| FLOAT { $$ = $1; }
| STR { $$ = format_muv_str($1); free($1); }
| '#' MINUS INTEGER { $$ = savefmt("#-%d", $3); }
| '#' INTEGER { $$ = savefmt("#%d", $2); }
| DECLARED_CONST { $$ = savestring($1.val); keyval_free(&$1); }
| lvalue %prec BARE { $$ = savestring($1.get); getset_free(&$1); }
| lvalue '(' arglist_or_null ')' %prec SUFFIX {
char *body = appendstr(strlist_wrap(&$3, 0, -1), $1.call, NULL);
$$ = appendstr(wrapit("{", body, "}list"), "dup array_count", "2 < if 0 [] then", NULL);
free(body);
getset_free(&$1);
strlist_free(&$3);
}
| function_call { $$ = $1; }
| TOP { $$ = savestring(""); }
| PUSH '(' arglist ')' { strlist_add(&$3, "dup"), $$ = strlist_wrap(&$3, 0, -1); strlist_free(&$3); }
| MUF '(' STR ')' { $$ = $3; }
| DEL '(' lvalue ')' { $$ = savefmt("%s 0", $3.del); getset_free(&$3); }
| FMTSTRING '(' arglist ')' {
const char *ptr = $3.list[0];
int expect = 0;
while (*ptr) {
if (*ptr == '%') {
ptr++;
if (!*ptr) {
break;
}
if (*ptr != '%') {
expect++;
}
}
ptr++;
}
if ($3.count != expect+1) {
char *err = savefmt("fmtstring(fmt ...) format string expects %d args, but got %d.", expect, $3.count-1);
yyerror(err);
free(err);
YYERROR;
}
strlist_reverse(&$3);
$$ = appendstr(strlist_wrap(&$3, 0, -1), "fmtstring", NULL);
strlist_free(&$3);
}
| expr '?' expr ':' expr { $$ = savefmt("%s if %s else %s then", $1, $3, $5); free($1); free($3); free($5); }
| expr '[' expr ']' { $$ = savefmt("%s %s []", $1, $3); free($1); free($3); }
| APPEND { $$ = savestring("{ }list"); }
| '[' arglist_or_null ']' { $$ = strlist_wrapit("{", &$2, "}list"); strlist_free(&$2); }
| '[' FOR compr_loop compr_cond expr ']' {
/* list comprehension */
char *body = appendstr(NULL, $5, "swap []<-", NULL);
char *pfx;
if (*$4) {
char *cond = wrapit($4, body, "then");
free(body);
body = cond;
}
pfx = appendstr(NULL, "{ }list", $3, NULL);
$$ = wrapit(pfx, body, "repeat");
free(pfx);
free(body);
free($3); free($4); free($5);
}
| '[' dictlist ']' {
/* dictionary initializer */
if ($2.count == 0) {
$$ = savestring("{ }dict");
} else {
$$ = strlist_wrapit("{", &$2, "}dict");
}
strlist_free(&$2);
}
| '[' FOR compr_loop compr_cond expr KEYVAL expr ']' {
/* dictionary comprehension */
char *body = appendstr(NULL, $7, "swap", $5, "->[]", NULL);
char *pfx;
if (*$4) {
char *cond = wrapit($4, body, "then");
free(body);
body = cond;
}
pfx = appendstr(NULL, "{ }dict", $3, NULL);
$$ = wrapit(pfx, body, "repeat");
free(pfx); free(body);
free($3); free($4); free($5); free($7);
}
| PLUS expr %prec UNARY { $$ = savestring($2); free($2); }
| MINUS expr %prec UNARY {
if (isint($2)) {
$$ = savefmt("-%s", $2);
} else {
$$ = savefmt("0 %s -", $2);
}
free($2);
}
| NOT expr %prec UNARY { $$ = appendstr(NULL, $2, "not", NULL); free($2); }
| BITNOT expr %prec UNARY { $$ = appendstr(NULL, $2, "-1", "bitxor", NULL); free($2); }
| BITAND DECLARED_FUNC %prec UNARY { $$ = savefmt("'%s%s", FUNC_PREFIX, $2.name); }
| expr PLUS expr {
if (!strcmp($3, "1")) {
$$ = appendstr(NULL, $1, "++", NULL);
} else {
$$ = appendstr(NULL, $1, $3, "+", NULL);
}
free($1); free($3);
}
| expr MINUS expr {
if (!strcmp($3, "1")) {
$$ = appendstr(NULL, $1, "--", NULL);
} else {
$$ = appendstr(NULL, $1, $3, "-", NULL);
}