forked from arminbiere/aiger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmvtoaig.c
3868 lines (3089 loc) · 77.6 KB
/
smvtoaig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
Copyright (c) 2006-2011, Armin Biere, Johannes Kepler University.
Copyright (c) 2011, Siert Wieringa, Aalto University, Finland.
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.
***************************************************************************/
#include "aiger.h"
/*------------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <unistd.h>
#include <sys/wait.h>
/*------------------------------------------------------------------------*/
#define NEW(p) \
do { \
size_t bytes = sizeof (*(p)); \
(p) = malloc (bytes); \
memset ((p), 0, bytes); \
} while (0)
/*------------------------------------------------------------------------*/
#define NEWN(p,n) \
do { \
size_t bytes = (n) * sizeof (*(p)); \
(p) = malloc (bytes); \
memset ((p), 0, bytes); \
} while (0)
/*------------------------------------------------------------------------*/
#define TRUE ((AIG*)(long)1)
#define FALSE ((AIG*)(long)-1)
/*------------------------------------------------------------------------*/
#define VALID_SYMBOL "AIGER_VALID"
#define INVALID_SYMBOL "AIGER_INVALID"
#define INITIALIZED_SYMBOL "AIGER_INITIALIZED"
#define NEVER_SYMBOL "AIGER_NEVER"
#define INVAR_SYMBOL "AIGER_INVAR"
#define FAIR_SYMBOL "AIGER_FAIR" /* SW110131 */
#define JUST_SYMBOL "AIGER_JUST" /* SW110530 */
#define NEXT_PREFIX "AIGER_NEXT_"
#define NOT_PREFIX "AIGER_NOT_"
#define LTL_SYMBOL "IGNORE_LTL_"
/*------------------------------------------------------------------------*/
enum Tag
{
/* The first group of tags are tokens, which are encoded in the same way
* as their ASCII representation. This is just for convenience and
* debugging purposes.
*/
AND = '&',
CLAUSE = ':',
NOT = '!',
ONE = '1',
OR = '|',
ZERO = '0',
LTL_FINALLY = 'F',
LTL_GLOBALLY = 'G',
LTL_HISTORICALLY = 'H',
LTL_ONCE = 'O',
LTL_SINCE = 'S',
LTL_TRIGGERED = 'T',
LTL_UNTIL = 'U',
LTL_RELEASES = 'V',
LTL_NEXT = 'X',
LTL_YESTERDAY = 'Y',
LTL_WEAKYESTERDAY = 'Z',
AG = 256,
ASSIGN = 257,
BECOMES = 258,
boolean = 259,
CASE = 260,
DEFINE = 261,
ESAC = 262,
IFF = 263,
IMPLIES = 264,
init = 265,
INIT = 266,
INVAR = 267,
MODULE = 268,
next = 269,
NOTEQ = 270,
SPEC = 271,
SYMBOL = 272,
TRANS = 273,
VAR = 274,
FAIRNESS = 275, /* SW110131 */
XOR = 276, /* SW110307 */
XNOR = 277, /* SW110307 */
LTLSPEC = 288 /* SW110527 */
};
typedef enum Tag Tag;
/*------------------------------------------------------------------------*/
typedef struct AIG AIG;
typedef struct Symbol Symbol;
typedef struct Expr Expr;
/*------------------------------------------------------------------------*/
struct Symbol
{
char *name;
unsigned declared:1;
unsigned nondet:1;
unsigned latch:1;
unsigned input:1;
unsigned flipped:1;
unsigned mark;
Expr *init_expr;
Expr *next_expr;
Expr *def_expr;
AIG *init_aig;
AIG *next_aig;
AIG *def_aig;
Symbol *chain;
Symbol *order;
};
/*------------------------------------------------------------------------*/
struct Expr
{
Tag tag;
Symbol *symbol;
Expr *c0;
Expr *c1;
Expr *next;
AIG * cache[2];
};
/*------------------------------------------------------------------------*/
struct AIG
{
Symbol *symbol;
unsigned slice; /* actually only 0 or 1 */
AIG *c0;
AIG *c1;
unsigned idx; /* Tseitin index */
AIG *next; /* collision chain */
AIG *cache; /* cache for shifting and elaboration */
unsigned id; /* unique id for hashing/comparing purposes */
};
/*------------------------------------------------------------------------*/
static const char *ltl2smv;
static const char *input_name;
static int close_input;
static FILE *input;
static int verbose;
static int ascii;
static aiger *writer;
static const char *output_name;
static int strip_symbols;
static int lineno;
static int saved_char;
static int char_has_been_saved;
static Tag token;
static unsigned count_buffer;
static char *buffer;
static unsigned size_buffer;
static int next_allowed;
static int temporal_operators_allowed;
/*------------------------------------------------------------------------*/
static Symbol *first_symbol;
static Symbol *last_symbol;
static unsigned size_symbols;
static unsigned count_symbols;
static Symbol **symbols;
static Symbol *initialized_symbol;
static Symbol *invalid_symbol;
static Symbol *valid_symbol;
/*------------------------------------------------------------------------*/
static Expr *first_expr;
static Expr *last_expr;
static unsigned count_exprs;
static unsigned size_expr_stack;
static unsigned count_expr_stack;
static Expr **expr_stack;
static Expr *trans_expr;
static Expr *last_trans_expr;
static Expr *ltl_trans_expr;
static Expr *init_expr;
static Expr *last_init_expr;
static Expr *ltl_init_expr;
/* SW110606 Modified invar handling such that each separate INVAR expression
will lead to a separate constraint */
static Expr **invar_expr;
static int invar_expr_cnt;
/* SW110131 Changed the expression pointer "spec_expr" into a pointer to
a pointer, which forms an array of expression pointers of length
spec_expr_cnt. Then added the same for fair expressions.
*/
static Expr **spec_expr;
static Expr **ltlspec_expr;
static Expr **fair_expr;
static int *justice_groups;
static int fair_expr_cnt;
static int ltlspec_expr_cnt;
static int spec_expr_cnt;
static int ltlspec;
static int constantinitialized;
/*------------------------------------------------------------------------*/
static unsigned size_aigs;
static unsigned count_aigs;
static AIG **aigs;
static unsigned size_cached;
static unsigned count_cached;
static AIG **cached;
static AIG *trans_aig;
static AIG *init_aig;
/* SW110131 Now there's an array of "bad AIGs" and an array of "fair AIGs"
SW110606 Now also for "invar AIGs"
*/
static AIG **invar_aig;
static AIG **bad_aig;
static AIG **fair_aig;
/*------------------------------------------------------------------------*/
static unsigned nondets;
static unsigned inputs;
static unsigned latches;
static unsigned ands;
static unsigned idx;
/*------------------------------------------------------------------------*/
static int optimize = 4;
/*------------------------------------------------------------------------*/
static unsigned primes[] = { 21433, 65537, 332623, 1322963, 200000123 };
static unsigned *eoprimes = primes + sizeof (primes) / sizeof (primes[0]);
/*------------------------------------------------------------------------*/
static void
die (const char *msg, ...)
{
va_list ap;
fputs ("*** [smvtoaig] ", stderr);
va_start (ap, msg);
vfprintf (stderr, msg, ap);
va_end (ap);
fputc ('\n', stderr);
fflush (stderr);
exit (1);
}
/*------------------------------------------------------------------------*/
static void
perr (const char *msg, ...)
{
va_list ap;
fprintf (stderr, "%s:%d: ", input_name, lineno);
va_start (ap, msg);
vfprintf (stderr, msg, ap);
va_end (ap);
fputc ('\n', stderr);
fflush (stderr);
exit (1);
}
/*------------------------------------------------------------------------*/
static void
msg (int level, const char *msg, ...)
{
va_list ap;
if (level > verbose)
return;
fprintf (stderr, "[smvtoaig] ");
va_start (ap, msg);
vfprintf (stderr, msg, ap);
va_end (ap);
fputc ('\n', stderr);
fflush (stderr);
}
/*------------------------------------------------------------------------*/
static void
enlarge_buffer (void)
{
size_buffer = size_buffer ? 2 * size_buffer : 1;
buffer = realloc (buffer, size_buffer);
}
/*------------------------------------------------------------------------*/
static int
full_buffer (void)
{
return count_buffer >= size_buffer;
}
/*------------------------------------------------------------------------*/
static void
push_buffer (int ch)
{
if (full_buffer ())
enlarge_buffer ();
buffer[count_buffer++] = ch;
}
/*------------------------------------------------------------------------*/
static void
pop_buffer (void)
{
assert (count_buffer > 0);
count_buffer--;
}
/*------------------------------------------------------------------------*/
static void
enlarge_expr_stack (void)
{
size_expr_stack = size_expr_stack ? 2 * size_expr_stack : 1;
expr_stack = realloc (expr_stack, size_expr_stack * sizeof (expr_stack[0]));
}
/*------------------------------------------------------------------------*/
static void
push_expr (Expr * expr)
{
if (count_expr_stack >= size_expr_stack)
enlarge_expr_stack ();
expr_stack[count_expr_stack++] = expr;
}
/*------------------------------------------------------------------------*/
static Expr *
pop_expr (void)
{
assert (count_expr_stack > 0);
return expr_stack[--count_expr_stack];
}
/*------------------------------------------------------------------------*/
static int
next_char (void)
{
int res;
if (char_has_been_saved)
{
res = saved_char;
char_has_been_saved = 0;
}
else
res = getc (input);
if (res != EOF)
push_buffer (res);
if (res == '\n')
lineno++;
return res;
}
/*------------------------------------------------------------------------*/
static void
save_char (int ch)
{
assert (!char_has_been_saved);
if (ch == '\n')
{
assert (lineno > 0);
lineno--;
}
saved_char = ch;
char_has_been_saved = 1;
if (ch != EOF && count_buffer)
pop_buffer ();
}
/*------------------------------------------------------------------------*/
static void
invalid_token (void)
{
push_buffer (0);
perr ("invalid token '%s'", buffer);
}
/*------------------------------------------------------------------------*/
static unsigned char issymbol[256];
/*------------------------------------------------------------------------*/
static void
init_issymbol (void)
{
unsigned char ch;
memset (issymbol, 0, sizeof (issymbol));
for (ch = '0'; ch <= '9'; ch++)
issymbol[ch] = 1;
for (ch = 'a'; ch <= 'z'; ch++)
issymbol[ch] = 1;
for (ch = 'A'; ch <= 'Z'; ch++)
issymbol[ch] = 1;
issymbol['-'] = 1;
issymbol['_'] = 1;
issymbol['.'] = 1;
issymbol['@'] = 1;
issymbol['['] = 1;
issymbol[']'] = 1;
}
/* SW110527 */
static int
is_temporal_operator (Tag tag)
{
return ( tag == AG ) ||
( tag == LTL_NEXT ) ||
( tag == LTL_GLOBALLY ) ||
( tag == LTL_FINALLY ) ||
( tag == LTL_YESTERDAY ) ||
( tag == LTL_WEAKYESTERDAY ) ||
( tag == LTL_HISTORICALLY ) ||
( tag == LTL_ONCE ) ||
( tag == LTL_UNTIL ) ||
( tag == LTL_SINCE ) ||
( tag == LTL_TRIGGERED ) ||
( tag == LTL_RELEASES );
}
/*------------------------------------------------------------------------*/
static Tag
get_next_token (void)
{
int ch;
SKIP_WHITE_SPACE:
while (isspace (ch = next_char ()))
;
save_char (ch);
count_buffer = 0; /* start of new token */
ch = next_char ();
assert (!isspace (ch));
if (ch == '-')
{
ch = next_char ();
if (ch == '-')
{
while ((ch = next_char ()) != '\n' && ch != EOF)
;
goto SKIP_WHITE_SPACE;
}
else if (ch != '>')
invalid_token ();
return IMPLIES;
}
if (ch == '!')
{
ch = next_char ();
if (ch == '=')
return NOTEQ;
save_char (ch);
return '!';
}
if (ch == '=' || ch == '|' || ch == '(' || ch == ')' ||
ch == ';' || ch == '&' || ch == '0' || ch == '1')
return ch;
if (ch == ':')
{
ch = next_char ();
if (ch == '=')
return BECOMES;
save_char (ch);
return ':';
}
if (ch == '<')
{
if (next_char () != '-' || next_char () != '>')
invalid_token ();
return IFF;
}
if (issymbol[ch])
{
while (issymbol[ch = next_char ()])
;
save_char (ch);
push_buffer (0);
if ( strlen(buffer) == 1 &&
is_temporal_operator(buffer[0]) )
return buffer[0];
if (!strcmp (buffer, "AG"))
return AG;
if (!strcmp (buffer, "ASSIGN"))
return ASSIGN;
if (!strcmp (buffer, "boolean"))
return boolean;
if (!strcmp (buffer, "case"))
return CASE;
if (!strcmp (buffer, "DEFINE"))
return DEFINE;
if (!strcmp (buffer, "esac"))
return ESAC;
if (!strcmp (buffer, "INIT"))
return INIT;
if (!strcmp (buffer, "init"))
return init;
if (!strcmp (buffer, "INVAR"))
return INVAR;
if (!strcmp (buffer, "MODULE"))
return MODULE;
if (!strcmp (buffer, "next"))
return next;
if (!strcmp (buffer, "SPEC") ||
/* SW110309 Added CTLSPEC which is a synonym for SPEC
*/
!strcmp (buffer, "CTLSPEC"))
return SPEC;
if (!strcmp (buffer, "LTLSPEC"))
return LTLSPEC;
if (!strcmp (buffer, "TRANS"))
return TRANS;
if (!strcmp (buffer, "TRUE"))
return ONE;
/* SW110308 Added IVAR as synonym for VAR
TODO Handle IVAR seperately to include restrictions imposed on
input variables (e.g. can't be left hand side of assignment).
*/
if (!strcmp (buffer, "VAR") || !strcmp (buffer, "IVAR"))
return VAR;
if (!strcmp (buffer, "FALSE"))
return ZERO;
/* SW110131
*/
if (!strcmp (buffer, "FAIRNESS") || !strcmp(buffer, "JUSTICE"))
return FAIRNESS;
/* SW110307
*/
if (!strcmp (buffer, "xor"))
return XOR;
if (!strcmp (buffer, "xnor"))
return XNOR;
return SYMBOL;
}
if (ch != EOF)
{
if (isprint (ch))
perr ("invalid character '%c'", ch);
else
perr ("invalid character");
}
return EOF;
}
/*------------------------------------------------------------------------*/
void
print_token (FILE *out, Tag tag)
{
assert( tag != SYMBOL );
switch (tag)
{
case AG:
fputs ("AG",out);
break;
case ASSIGN:
fputs ("ASSIGN",out);
break;
case BECOMES:
fputs (":=",out);
break;
case boolean:
fputs ("boolean",out);
break;
case CASE:
fputs ("case",out);
break;
case DEFINE:
fputs ("DEFINE",out);
break;
case ESAC:
fputs ("esac",out);
break;
case IFF:
fputs ("<->",out);
break;
case IMPLIES:
fputs ("->",out);
break;
case INIT:
fputs ("INIT",out);
break;
case init:
fputs ("init",out);
break;
case INVAR:
fputs ("INVAR",out);
break;
case MODULE:
fputs ("MODULE",out);
break;
case next:
fputs ("next",out);
break;
case SPEC:
fputs ("SPEC",out);
break;
case TRANS:
fputs ("TRANS",out);
break;
case VAR:
fputs ("VAR",out);
break;
/* SW110131
*/
case FAIRNESS:
fputs ("FAIRNESS",out);
break;
/* SW110307
*/
case XOR:
fputs ("xor",out);
break;
case XNOR:
fputs ("xnor",out);
break;
/* SW110527 */
case LTLSPEC:
fputs ("LTLSPEC",out);
break;
default:
fprintf (out,"%c", tag);
break;
case EOF:
fputs ("<EOF>",out);
break;
}
}
static void
next_token (void)
{
token = get_next_token ();
#if 0
/* Dump all tokens to 'stdout' to debug scanner and parser.
*/
fprintf (stderr, "%s:%d: ", input_name, lineno);
if (token == SYMBOL)
fputs (buffer,stderr);
else
print_token(stderr, token);
fprintf(stderr,"\n");
fflush(stderr);
#endif
}
/*------------------------------------------------------------------------*/
static unsigned
hash_str (const char *str)
{
const unsigned *q;
unsigned char ch;
const char *p;
unsigned res;
res = 0;
p = str;
q = primes;
while ((ch = *p++))
{
res *= *q++;
res += ch;
if (q >= eoprimes)
q = primes;
}
res *= *q;
return res;
}
/*------------------------------------------------------------------------*/
static unsigned
hash_symbol (const char *str)
{
unsigned res = hash_str (str) & (size_symbols - 1);
assert (res < size_symbols);
return res;
}
/*------------------------------------------------------------------------*/
static void
enlarge_symbols (void)
{
Symbol **old_symbols, *p, *chain;
unsigned old_size_symbols, h, i;
old_size_symbols = size_symbols;
old_symbols = symbols;
size_symbols = size_symbols ? 2 * size_symbols : 1;
NEWN (symbols, size_symbols);
for (i = 0; i < old_size_symbols; i++)
{
for (p = old_symbols[i]; p; p = chain)
{
chain = p->chain;
h = hash_symbol (p->name);
p->chain = symbols[h];
symbols[h] = p;
}
}
free (old_symbols);
}
/*------------------------------------------------------------------------*/
static Symbol **
find_symbol (const char * name)
{
Symbol **res, *s;
for (res = symbols + hash_symbol (name);
(s = *res) && strcmp (s->name, name); res = &s->chain)
;
return res;
}
/*------------------------------------------------------------------------*/
static Symbol *
have_symbol (const char * name)
{
return *find_symbol (name);
}
/*------------------------------------------------------------------------*/
static Symbol *
new_symbol (const char * name)
{
Symbol **p, *res;
if (size_symbols <= count_symbols)
enlarge_symbols ();
p = find_symbol (name);
res = *p;
if (!res)
{
NEW (res);
res->name = strdup (name);
if (last_symbol)
last_symbol->order = res;
else
first_symbol = res;
last_symbol = res;
*p = res;
count_symbols++;
}
return res;
}
/*------------------------------------------------------------------------*/
static Symbol *
new_internal_symbol (const char *name)
{
if (have_symbol (name))
die ("duplicate internal symbol '%s'", name);
return new_symbol (name);
}
/*------------------------------------------------------------------------*/
static void
enqueue_expr (Expr * expr)
{
if (last_expr)
last_expr->next = expr;
else
first_expr = expr;
last_expr = expr;
count_exprs++;
}
/*------------------------------------------------------------------------*/
static Expr *
sym2expr (Symbol * symbol)
{
Expr *res;
assert (symbol);
NEW (res);
res->tag = SYMBOL;
res->symbol = symbol;
enqueue_expr (res);
return res;
}
/*------------------------------------------------------------------------*/
static Expr *
new_expr (Tag tag, Expr * c0, Expr * c1)
{
Expr *res;
NEW (res);
res->tag = tag;
res->c0 = c0;
res->c1 = c1;
enqueue_expr (res);
return res;
}
/*------------------------------------------------------------------------*/
static void
eat_token (Tag expected)
{
if (token != expected)
perr ("expected '%c'", expected);
next_token ();
}
/*------------------------------------------------------------------------*/
static void
eat_symbolic_token (Tag expected, const char *str)
{
if (token != expected)
perr ("expected '%s'", str);
next_token ();
}
/*------------------------------------------------------------------------*/
static Symbol *
eat_symbol (void)
{
Symbol *res;
if (token != SYMBOL)
perr ("expected variable");
res = new_symbol (buffer);
next_token ();
return res;
}
/*------------------------------------------------------------------------*/
static void
print_expr(FILE *out, Expr *expr)
{
if ( expr->c0 && expr->c1 )
{
fprintf(out, "( ");
print_expr(out, expr->c0);
}
assert( expr->tag != SYMBOL ||
(expr->symbol && expr->symbol->name) );
if ( expr->tag != SYMBOL )
print_token(out, expr->tag);
else
fputs(expr->symbol->name, out);
fputc(' ', out);
if ( expr->c0 && !expr->c1 )
print_expr(out, expr->c0);
if ( expr->c0 && expr->c1 )
{