-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlolcode.c
1977 lines (1916 loc) · 70.7 KB
/
lolcode.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
/*
* lolcode.c
*
* This program parses and executes the commands in a LOLCODE file. If a file
* is not syntactically valid, execution will halt at whatever point precedes
* the error. Variables are assigned and cast dynamically (at run time).
*
* See < http://lolcode.com/specs/1.2 > for more information.
*
* MAINTAINER
*
* Justin J. Meza < justin dot meza at gmail dot com >
*
* LICENSE
*
* Copyright (c) 2007-2010 Justin J. Meza
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* TODO
*
* - Make sure variables, loops, and functions do not share names
* - Add scoping to other control structures
* - For the love of god please make everything 80 columns wide at most!
* - For BIGGR OF and SMALLR OF, what should the return type be? The
* bigger (or smaller) of the two values, or NUMBR if both NUMBRs and
* NUMBAR if any NUMBARs. Specifically, what if two different types are
* the same? Which value is to be returned in this case of a tie?
* - Character hex value interpolation, Unicode?
* - Pick a good value for eps to ensure 0.1 * 10 = 0.99999... is
* interpreted as 1.
* - Are ending AN's evil in the get_args function?
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "hash.h"
#include "list.h"
#include "state.h"
#include "parser.h"
#include "args.h"
#include "loltypes.h"
#include "lolfunc.h"
/* Symbol access types and symbol definition */
enum access
{
READONLY,
READWRITE,
};
struct symbol
{
struct value *value;
struct value *scope;
enum access access;
};
/* Various helper functions for deleting objects */
void
data_delete_list(void *DATA)
/* Deletes pointers to lists */
{
list_delete(DATA);
}
void
data_delete_null(void *DATA)
/* Does not delete anything (used with ACCESS list) */
{ }
void *
data_copy_null(const void *DATA)
/* Does not copy anything (used with ACCESS list) */
{
return NULL;
}
int
parser_rules(char *BUF, size_t LEN, unsigned int *START, unsigned int *POS)
/* The order of these rules is important! */
{
/* String literals */
if (BUF[*POS] == '"') {
do if (BUF[(*POS)++] == '\n') return 1;
while (*POS < LEN && (BUF[*POS] != '"'
|| ((*POS > 0 && BUF[*POS - 1] == ':')
&& (*POS > 1 && BUF[*POS - 2] != ':'))));
}
/* Single-line comments */
if (!strncmp(BUF + *POS, "BTW", 3) && !(*POS > 0 && BUF[*POS - 1] == 'O')) {
*POS += 3;
while (*POS < LEN && BUF[*POS] != '\n') *START = ++(*POS);
}
return 0;
}
void
error(struct parser *PARSER, char *MESSAGE)
/* Prints error messages and sets the state of the ERROR structure */
{
struct token *token;
assert(PARSER);
token = (struct token *)list_head(PARSER->tokens);
if (token) {
if (token->null) fprintf(stderr, "%s:%d: %s: at end of line.\n",
PARSER->name,
token->line + 1,
MESSAGE);
else fprintf(stderr, "%s:%d: %s: at `%s'.\n",
PARSER->name,
token->line + 1,
MESSAGE,
token->data);
}
else fprintf(stderr, "%s: %s: at end of file.\n",
PARSER->name,
MESSAGE);
}
struct value *
token_to_troof(struct token *TOKEN)
/* Casts TOKEN's value to a TROOF. On success, returns the new value and
* frees the token, otherwise, returns NULL and the token remains
* allocated. */
{
troof data;
assert(TOKEN);
assert(TOKEN->data);
if (!strcmp(TOKEN->data, "WIN"))
data = WIN;
else if (!strcmp(TOKEN->data, "FAIL"))
data = FAIL;
else {
return NULL;
}
token_delete(TOKEN);
return value_create_troof(data);;
}
struct value *
token_to_numbr(struct token *TOKEN)
{
numbr data = 0;
int pos, neg = 0;
assert(TOKEN);
assert(TOKEN->data);
for (pos = 0; TOKEN->data[pos] != 0; pos++) {
if (pos == 0 && TOKEN->data[pos] == '-') {
neg = 1;
pos++;
}
if (isdigit(TOKEN->data[pos])) {
data *= 10;
data += (int)(TOKEN->data[pos] - '0');
}
else return NULL;
}
if (neg) data *= -1;
token_delete(TOKEN);
return value_create_numbr(data);
}
struct value *
token_to_numbar(struct token *TOKEN)
{
numbar data = 0.0;
int pos, neg = 0, dec = 0;
assert(TOKEN);
assert(TOKEN->data);
for (pos = 0; TOKEN->data[pos] != 0; pos++) {
if (pos == 0 && TOKEN->data[pos] == '-') {
neg = 1;
pos++;
}
if (isdigit(TOKEN->data[pos])) {
if (dec) {
int n;
numbar frac = (numbar)(TOKEN->data[pos] - '0');
for (n = 0; n < dec; n++)
frac /= 10;
data += frac;
dec++;
}
else {
data *= 10.0;
data += (numbar)(TOKEN->data[pos] - '0');
}
}
else if (TOKEN->data[pos] == '.' && !dec) dec = 1;
else return NULL;
}
if (neg) data *= -1.0;
token_delete(TOKEN);
return value_create_numbar(data);
}
struct symbol *token_to_symbol(struct value *, struct list *, struct token *);
struct value *
token_to_yarn(struct parser *PARSER, struct value *STATE, struct list *ACCESS,
struct token *TOKEN)
{
struct value *value = NULL;
yarn temp; /* Constructs the YARN */
int len; /* Token string length */
int n; /* Tracks token position */
int i; /* Tracks value position */
/* Check for YARN format */
assert(TOKEN);
assert(TOKEN->data);
if (TOKEN->data[0] != '"' || !(len = strlen(TOKEN->data))
|| TOKEN->data[len - 1] != '"')
return NULL;
/* Copy characters, removing quotes */
temp = malloc(sizeof(char) * (len - 1));
for (n = 1, i = 0; n < len - 1; n++) {
if (TOKEN->data[n] == '"' && n != 0 && n < len - 1) {
/* Rogue quote character */
error(PARSER, "Rogue quote character within YARN");
return NULL;
}
else if (TOKEN->data[n] == ':') {
/* Escape characters */
if (TOKEN->data[n + 1] == ')') { /* Newline */
temp[i++] = '\n';
n++;
}
else if (TOKEN->data[n + 1] == '>') { /* Tab */
temp[i++] = '\t';
n++;
}
else if (TOKEN->data[n + 1] == 'o') { /* Alarm */
temp[i++] = '\a';
n++;
}
else if (TOKEN->data[n + 1] == '"') { /* Double quote */
temp[i++] = '"';
n++;
}
else if (TOKEN->data[n + 1] == ':') { /* Colon */
temp[i++] = ':';
n++;
}
else if (TOKEN->data[n + 1] == '{') { /* Variable */
yarn data = NULL;
struct value *value = NULL;
int start = n + 2;
int end = start;
struct token *token = NULL;
struct symbol *symbol = NULL;
enum access access;
/* Seek to end brace */
while (end < len - 1 && TOKEN->data[end] != '}') end++;
if (end == len - 1) {
error(PARSER, "Unclosed variable interpolation block");
free(temp);
token_delete(TOKEN);
return NULL;
}
TOKEN->data[end] = '\0';
/* Retrieve variable value */
token = token_create_str(TOKEN->data + start);
symbol = token_to_symbol(STATE, ACCESS, token);
value = symbol->value;
access = symbol->access;
free(symbol);
token_delete(token);
if (!value) {
error(PARSER, "Invalid interpolation expression");
free(temp);
token_delete(TOKEN);
return NULL;
}
if (access != READONLY && access != READWRITE) {
error(PARSER, "Interpolation expression is not readable");
free(temp);
token_delete(TOKEN);
}
value = value_cast_yarn(value);
data = value_get_yarn(value);
/* Reallocate temp, adding data's size */
temp = realloc(temp, sizeof(char) * (len + strlen(data) - 2));
/* Place the contents of data in temp */
strcpy(temp + i, data);
i += strlen(data);
/* Clean up */
value_delete(value);
n = end;
}
else {
error(PARSER, "Rogue escape character within YARN");
free(temp);
token_delete(TOKEN);
return NULL;
}
}
/* Otherwise, just copy over all other characters */
else temp[i++] = TOKEN->data[n];
}
temp[i] = '\0';
/* Clean up and return */
value = value_create_yarn(temp);
free(temp);
token_delete(TOKEN);
return value;
}
struct symbol *
token_to_symbol(struct value *STATE, struct list *ACCESS, struct token *TOKEN)
/* Resolves the value corresponding to TOKEN in STATE, searching through
* parent scopes if required until the root scope is reached whereupon
* NULL is returned if the value does not exist. If the value is found
* in the current scope (STATE) or a pointer to the value is on the ACCESS
* list, ACCESS is set to READWRITE, otherwise ACCESS is set to READONLY.
*
* +===========================+
* | symbol-> |
* +========+============+=======+=======+===========+
* | SCOPE | IN ACCESS? | value | scope | access |
* +========+============+=======+=======+===========+
* | NONE | N/A | NULL | N/A | N/A |
* +========+------------+-------+-------+-----------+
* | | YES | * | x | READWRITE |
* | STATE +------------+-------+-------+-----------+
* | | NO | * | x | READWRITE |
* +========+------------+-------+-------+-----------+
* | | YES | * | x | READWRITE |
* | PARENT +------------+-------+-------+-----------+
* | | NO | * | x | READONLY |
* +========+------------+-------+-------+-----------+
* * = the value which was found
* x = the scope in which the value was found
*/
{
enum access access = READONLY;
struct value *value = STATE, *state = NULL, *scope = STATE;
char *start = NULL, *end = NULL;
struct symbol *symbol = NULL;
assert(TOKEN);
assert(ACCESS);
assert(STATE);
/* If ``I'', we're done */
if (strcmp(TOKEN->data, "I")) {
do {
value = scope;
start = TOKEN->data;
do {
char *middle = NULL, *key = NULL;
/* Find the next ``!'' */
end = strchr(start, '!');
/* If none found, seek to end */
if (!end) end = strchr(start, '\0');
/* Extract the middle portion */
middle = malloc(sizeof(char) * (end - start + 1));
strncpy(middle, start, end - start);
middle[end - start] = '\0';
/* If at start or ``!'', read from state */
if (start == TOKEN->data || *(start - 1) == '!') key = middle;
/* Else, if not at start and ``?'', read from STATE */
else if (start != TOKEN->data && *(start - 1) == '?') {
struct value *read = NULL;
struct value *temp = NULL;
struct token *token = token_create_str(middle);
struct symbol *symbol = token_to_symbol(STATE, ACCESS,
token);
enum access access;
token_delete(token);
read = symbol->value;
access = symbol->access;
free(symbol);
if (!read) {
value = NULL;
break;
}
if (access != READONLY && access != READWRITE) {
value = NULL;
break;
}
temp = value_cast_yarn(read);
if (!temp) {
value = NULL;
break;
}
free(middle);
key = malloc(sizeof(char)
* (strlen(value_get_yarn(temp)) + 1));
strcpy(key, value_get_yarn(temp));
value_delete(temp);
start = end + 2;
}
else {
value = NULL;
break;
}
start = end + 2;
/* Resolve the next level */
value = state_read(value_get_bukkit(state = value), key);
if (!value) {
struct token *token = token_create_str(key);
struct value *numbr = token_to_numbr(token);
/* Create final NUMBR accesses on-the-fly */
if (start != TOKEN->data && !(*end) && numbr) {
state_write(value_get_bukkit(state), key,
value = value_create_noob());
value_delete(numbr);
}
else {
if (numbr) value_delete(numbr);
else token_delete(token);
value = NULL;
break;
}
}
/* Check for access */
else if (!list_empty(ACCESS)) {
void *head = list_head(ACCESS);
do {
struct value *item = (struct value *)list_head(ACCESS);
if (item == value) access = READWRITE;
list_shift_down(ACCESS);
}
while (list_head(ACCESS) != head);
}
free(key);
}
while (*end);
if (value) break;
scope = scope->parent;
}
while (scope);
}
/* Apply default access permissions if at local scope */
if (scope == STATE) access = READWRITE;
symbol = malloc(sizeof(struct symbol));
symbol->value = value;
symbol->scope = state;
symbol->access = access;
return symbol;
}
struct value *evaluate_expr(struct parser *, struct value *, struct list *,
struct list *);
struct list *
args_get(struct parser *PARSER, struct value *STATE, struct list *BREAKS,
struct list *ACCESS, int NUM)
/* Removes NUM arguments from parsers token stream, optionally seperated by
* ANs. NUM < 0 retrieves as many arguments as possible. Caller code
* should check to make sure the number of arguments actually returned
* matches the arity of the function. */
{
struct list *args = NULL;
struct value *arg = NULL;
int start = NUM;
assert(PARSER);
assert(STATE);
assert(BREAKS);
args = list_create(data_delete_value, data_copy_value);
while (NUM--) {
if ((arg = evaluate_expr(PARSER, STATE, BREAKS, ACCESS)) == NULL) break;
list_push_back(args, arg);
if (NUM) parser_cmp(PARSER, "AN");
}
return args;
}
struct list *
args_convert(struct list *LIST, int *TYPES, unsigned int SIZE)
/* Converts a list of mixed-type values into a list of values conforming to
* a particular typing scheme. The exact scheme is determined using the
* TYPES array and a simple two step process, described below. SIZE is the
* size of the TYPES array. */
{
struct list *list = list_create(data_delete_value, data_copy_value);
/* For each value in LIST, */
while (!list_empty(LIST)) {
struct value *value = (struct value *)list_head(LIST);
unsigned int n;
/* Check if native type matches one in TYPES */
for (n = 0; n < SIZE; n++) {
if (value->type == TYPES[n]) {
list_push_back(list, value_copy(value));
break;
}
}
/* If type matches, move to next value */
if (n < SIZE) {
list_pop_front(LIST);
continue;
}
/* Otherwise, check if the type can be cast */
for (n = 0; n < SIZE; n++) {
struct value *cast = NULL;
switch (TYPES[n]) {
case NOOB:
cast = value_create_noob();
break;
case TROOF:
cast = value_cast_troof(value);
break;
case NUMBR:
cast = value_cast_numbr(value);
break;
case NUMBAR:
cast = value_cast_numbar(value);
break;
case YARN:
cast = value_cast_yarn(value);
break;
default:
fprintf(stderr, "Incorrect cast type.\n");
return NULL;
break;
}
if (cast) {
list_push_back(list, cast);
list_pop_front(LIST);
break;
}
}
/* If unable to cast, bail */
if (n == SIZE) {
list_delete(list);
list_delete(LIST);
return NULL;
}
}
list_delete(LIST);
return list;
}
int
evaluate_parser(struct parser *PARSER, struct value *STATE, struct list *BREAKS,
struct list *ACCESS)
/* Evaluates all of the remaining tokens in PARSER and returns the result
* of the last evaluated expression. The arguments which are used for the
* state of the program are not modified but are visible to the evaluated
* code. Returns 0 if the entire parser was evaluated, and 1 otherwise. */
{
assert(PARSER);
assert(STATE);
assert(BREAKS);
while (!parser_empty(PARSER)) {
struct value *value = NULL;
/* Skip over any null tokens */
while (parser_cmp(PARSER, NULL));
/* OBTW ... TLDR */
/* Note that this needs to be here because it cannot appear wherever an
* expression appears but rathar at the beginning of a new line */
if (parser_cmp(PARSER, "OBTW"))
list_delete(parser_seek(PARSER, "TLDR"));
/* Evaluate an expression */
else if (!(value = evaluate_expr(PARSER, STATE, BREAKS, ACCESS)))
return 1;
/* Update IT */
if (value) state_write(value_get_bukkit(STATE), "IT", value);
/* We should be left with a null token */
if (!parser_cmp(PARSER, NULL)) {
error(PARSER, "Unexpected token");
/* TODO: try returning the unexpected token and deleting it
* later */
return 1;
}
}
return 0;
}
struct value *
evaluate_expr(struct parser *PARSER, struct value *STATE, struct list *BREAKS,
struct list *ACCESS)
/* Evaluates the next valid expression present in PARSER's token stream */
{
struct token *token = NULL;
struct value *value = NULL;
struct symbol *symbol = NULL;
assert(PARSER);
assert(STATE);
assert(BREAKS);
/* CAN HAS STDIO? */
if (parser_cmp(PARSER, "CAN")) {
if (!parser_cmp(PARSER, "HAS")) {
error(PARSER, "Expected `HAS' after `CAN'");
return NULL;
}
if (!parser_cmp(PARSER, "STDIO?")) {
error(PARSER, "Expected `STDIO?' after `CAN HAS'");
return NULL;
}
/* Do nothing */
return value_create_noob();
}
/* HAI */
if (parser_cmp(PARSER, "HAI")) {
struct value *scope = NULL;
struct list *body = NULL;
struct parser *parser = NULL;
if (!parser_cmp(PARSER, "1.2") && !parser_cmp(PARSER, "1.3")
&& !parser_cmp_peek(PARSER, NULL)) {
error(PARSER, "Expected version after `HAI'");
return NULL;
}
scope = value_create_bukkit(STATE);
body = parser_seek(PARSER, "KTHXBYE");
list_pop_front(body); /* <NULL> */
list_pop_back(body); /* KTHXBYE */
parser = parser_create_bind(PARSER->name, body);
if (evaluate_parser(parser, scope, BREAKS, ACCESS)) {
parser_delete(parser);
list_delete(body);
return NULL;
}
parser_delete(parser);
list_delete(body);
/* We cannot access scope from anywhere else, so don't name it */
state_write(value_get_bukkit(STATE), "", scope);
return value_create_noob();
}
/* VISIBLE */
if (parser_cmp(PARSER, "VISIBLE")) {
/* Retrieve all arguments */
struct list *args = args_get(PARSER, STATE, BREAKS, ACCESS, -1);
/* Check for at least one argument */
if (list_size(args) == 0) {
error(PARSER, "No arguments supplied to VISIBLE");
list_delete(args);
return NULL;
}
/* Convert and print arguments */
while (!list_empty(args)) {
struct value *arg = (struct value *)list_head(args);
if (arg->type != YARN) {
struct value *val = NULL;
val = value_cast_yarn(arg);
if (!val) {
error(PARSER, "Invalid argument to VISIBLE");
list_delete(args);
return NULL;
}
printf("%s", value_get_yarn(val));
value_delete(val);
}
else printf("%s", value_get_yarn(arg));
list_pop_front(args);
}
list_delete(args);
/* Append optional newline */
if (!parser_cmp(PARSER, "!")) printf("\n");
return value_create_noob();
}
/* SUM OF */
if (parser_cmp(PARSER, "SUM")) {
struct list *args = NULL;
struct list *values = NULL;
int types[2] = { NUMBR, NUMBAR };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `SUM'");
return NULL;
}
/* Retrieve all arguments */
args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
/* Check for correct number of arguments */
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to SUM OF");
list_delete(args);
return NULL;
}
/* Convert arguments to correct types */
values = args_convert(args, types, 2);
if (!values) {
error(PARSER, "Invalid argument to SUM OF");
return NULL;
}
/* Apply the appropriate operation */
return func_foldl(values, func_sumof);
}
/* DIFF OF */
if (parser_cmp(PARSER, "DIFF")) {
struct list *args = NULL;
struct list *values = NULL;
int types[2] = { NUMBR, NUMBAR };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `DIFF'");
return NULL;
}
args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to DIFF OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 2);
if (!values) {
error(PARSER, "Invalid argument to DIFF OF");
return NULL;
}
return func_foldl(values, func_diffof);
}
/* PRODUKT OF */
if (parser_cmp(PARSER, "PRODUKT")) {
struct list *args = NULL;
struct list *values = NULL;
int types[2] = { NUMBR, NUMBAR };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `PRODUKT'");
return NULL;
}
args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to PRODUKT OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 2);
if (!values) {
error(PARSER, "Invalid argument to PRODUKT OF");
return NULL;
}
return func_foldl(values, func_produktof);
}
/* QUOSHUNT OF */
if (parser_cmp(PARSER, "QUOSHUNT")) {
struct list *args = NULL;
struct list *values = NULL;
int types[2] = { NUMBR, NUMBAR };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `QUOSHUNT'");
return NULL;
}
args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to QUOSHUNT OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 2);
if (!values) {
error(PARSER, "Invalid argument to QUOSHUNT OF");
return NULL;
}
return func_foldl(values, func_quoshuntof);
}
/* MOD OF */
if (parser_cmp(PARSER, "MOD")) {
struct list *args = NULL;
struct list *values = NULL;
int types[1] = { NUMBR };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `MOD'");
return NULL;
}
args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to MOD OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 1);
if (!values) {
error(PARSER, "Invalid argument to MOD OF");
return NULL;
}
return func_foldl(values, func_modof);
}
/* BIGGR OF */
if (parser_cmp(PARSER, "BIGGR")) {
struct list *args = NULL;
struct list *values = NULL;
int types[2] = { NUMBR, NUMBAR };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `BIGGR'");
return NULL;
}
args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to BIGGR OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 2);
if (!values) {
error(PARSER, "Invalid argument to BIGGR OF");
return NULL;
}
return func_foldl(values, func_biggrof);
}
/* SMALLR OF */
if (parser_cmp(PARSER, "SMALLR")) {
struct list *args = NULL;
struct list *values = NULL;
int types[2] = { NUMBR, NUMBAR };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `BIGGR'");
return NULL;
}
args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to SMALLR OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 2);
if (!values) {
error(PARSER, "Invalid argument to SMALLR OF");
return NULL;
}
return func_foldl(values, func_smallrof);
}
/* BOTH OF, BOTH SAEM */
if (parser_cmp(PARSER, "BOTH")) {
if (parser_cmp(PARSER, "OF")) {
struct list *args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
struct list *values = NULL;
int types[1] = { TROOF };
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to BOTH OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 1);
if (!values) {
error(PARSER, "Invalid argument to BOTH OF");
return NULL;
}
return func_foldl(values, func_bothof);
}
else if (parser_cmp(PARSER, "SAEM")) {
struct list *args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
struct list *values = NULL;
void *head = NULL;
int types[2] = { NUMBR, NUMBAR };
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to BOTH SAEM");
list_delete(args);
return NULL;
}
head = list_head(args);
do {
struct value *value = (struct value *)list_head(args);
list_shift_down(args);
if (value->type != NUMBR && value->type != NUMBAR) {
list_delete(args);
return value_create_troof(FAIL);
}
}
while (head != list_head(args));
values = args_convert(args, types, 2);
if (!values) {
error(PARSER, "Invalid argument to BOTH SAEM");
return NULL;
}
return func_foldl(values, func_bothsaem);
}
else {
error(PARSER, "Expected token after `BOTH'");
return NULL;
}
}
/* EITHER OF */
if (parser_cmp(PARSER, "EITHER")) {
struct list *args = NULL;
struct list *values = NULL;
int types[1] = { TROOF };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `EITHER'");
return NULL;
}
args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to EITHER OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 1);
if (!values) {
error(PARSER, "Invalid argument to EITHER OF");
return NULL;
}
return func_foldl(values, func_eitherof);
}
/* WON OF */
if (parser_cmp(PARSER, "WON")) {
struct list *args = NULL;
struct list *values = NULL;
int types[1] = { TROOF };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `WON'");
return NULL;
}
args = args_get(PARSER, STATE, BREAKS, ACCESS, 2);
if (list_size(args) != 2) {
error(PARSER, "Wrong number of arguments to WON OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 1);
if (!values) {
error(PARSER, "Invalid argument to WON OF");
return NULL;
}
return func_foldl(values, func_wonof);
}
/* NOT */
if (parser_cmp(PARSER, "NOT")) {
/* Retrieve one argument */
struct list *args = args_get(PARSER, STATE, BREAKS, ACCESS, 1);
struct list *values = NULL;
struct value *value = NULL;
struct value *result = NULL;
int types[1] = { TROOF };
/* Check for correct number of arguments */
if (list_size(args) != 1) {
error(PARSER, "Wrong number of arguments to NOT");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 1);
if (!values) {
error(PARSER, "Invalid argument to NOT");
return NULL;
}
/* Apply the NOT operation */
value = (struct value *)list_head(values);
if (value->type != TROOF) {
error(PARSER, "Expected TROOF");
list_delete(values);
return NULL;
}
if (value_get_troof(value) == WIN) result = value_create_troof(FAIL);
else result = value_create_troof(WIN);
list_delete(values);
return result;
}
/* ALL OF */
if (parser_cmp(PARSER, "ALL")) {
struct list *args = NULL;
struct list *values = NULL;
int types[1] = { TROOF };
if (!parser_cmp(PARSER, "OF")) {
error(PARSER, "Expected `OF' after `ALL'");
return NULL;
}
args = args_get(PARSER, STATE, BREAKS, ACCESS, -1);
if (list_size(args) == 0) {
error(PARSER, "No arguments supplied to ALL OF");
list_delete(args);
return NULL;
}
values = args_convert(args, types, 1);
if (!values) {
error(PARSER, "Invalid argument to ALL OF");
return NULL;
}
parser_cmp(PARSER, "MKAY");
/* Short-circuit operation */
return func_foldl_short(values,
func_bothof,
value_create_troof(FAIL));
}
/* ANY OF */
if (parser_cmp(PARSER, "ANY")) {
struct list *args = NULL;
struct list *values = NULL;
int types[1] = { TROOF };