@@ -9,6 +9,16 @@ extern int rsparse();
9
9
static const int PUSHBACK_LEN = 4 ;
10
10
11
11
static char pushback [PUSHBACK_LEN ];
12
+ static int verbose ;
13
+
14
+ void print (const char * format , ...) {
15
+ va_list args ;
16
+ va_start (args , format );
17
+ if (verbose ) {
18
+ vprintf (format , args );
19
+ }
20
+ va_end (args );
21
+ }
12
22
13
23
// If there is a non-null char at the head of the pushback queue,
14
24
// dequeue it and shift the rest of the queue forwards. Otherwise,
@@ -54,7 +64,7 @@ struct node *mk_node(char const *name, int n, ...) {
54
64
unsigned sz = sizeof (struct node ) + (n * sizeof (struct node * ));
55
65
struct node * nn , * nd = (struct node * )malloc (sz );
56
66
57
- printf ("# New %d-ary node: %s = %p\n" , n , name , nd );
67
+ print ("# New %d-ary node: %s = %p\n" , n , name , nd );
58
68
59
69
nd -> own_string = 0 ;
60
70
nd -> prev = NULL ;
@@ -70,8 +80,8 @@ struct node *mk_node(char const *name, int n, ...) {
70
80
va_start (ap , n );
71
81
while (i < n ) {
72
82
nn = va_arg (ap , struct node * );
73
- printf ("# arg[%d]: %p\n" , i , nn );
74
- printf ("# (%s ...)\n" , nn -> name );
83
+ print ("# arg[%d]: %p\n" , i , nn );
84
+ print ("# (%s ...)\n" , nn -> name );
75
85
nd -> elems [i ++ ] = nn ;
76
86
}
77
87
va_end (ap );
@@ -95,8 +105,8 @@ struct node *ext_node(struct node *nd, int n, ...) {
95
105
unsigned sz = sizeof (struct node ) + (c * sizeof (struct node * ));
96
106
struct node * nn ;
97
107
98
- printf ("# Extending %d-ary node by %d nodes: %s = %p" ,
99
- nd -> n_elems , c , nd -> name , nd );
108
+ print ("# Extending %d-ary node by %d nodes: %s = %p" ,
109
+ nd -> n_elems , c , nd -> name , nd );
100
110
101
111
if (nd -> next ) {
102
112
nd -> next -> prev = nd -> prev ;
@@ -110,13 +120,13 @@ struct node *ext_node(struct node *nd, int n, ...) {
110
120
nodes -> prev = nd ;
111
121
nodes = nd ;
112
122
113
- printf (" ==> %p\n" , nd );
123
+ print (" ==> %p\n" , nd );
114
124
115
125
va_start (ap , n );
116
126
while (i < n ) {
117
127
nn = va_arg (ap , struct node * );
118
- printf ("# arg[%d]: %p\n" , i , nn );
119
- printf ("# (%s ...)\n" , nn -> name );
128
+ print ("# arg[%d]: %p\n" , i , nn );
129
+ print ("# (%s ...)\n" , nn -> name );
120
130
nd -> elems [nd -> n_elems ++ ] = nn ;
121
131
++ i ;
122
132
}
@@ -129,9 +139,9 @@ int const indent_step = 4;
129
139
void print_indent (int depth ) {
130
140
while (depth ) {
131
141
if (depth -- % indent_step == 0 ) {
132
- printf ("|" );
142
+ print ("|" );
133
143
} else {
134
- printf (" " );
144
+ print (" " );
135
145
}
136
146
}
137
147
}
@@ -140,24 +150,29 @@ void print_node(struct node *n, int depth) {
140
150
int i = 0 ;
141
151
print_indent (depth );
142
152
if (n -> n_elems == 0 ) {
143
- printf ("%s\n" , n -> name );
153
+ print ("%s\n" , n -> name );
144
154
} else {
145
- printf ("(%s\n" , n -> name );
155
+ print ("(%s\n" , n -> name );
146
156
for (i = 0 ; i < n -> n_elems ; ++ i ) {
147
157
print_node (n -> elems [i ], depth + indent_step );
148
158
}
149
159
print_indent (depth );
150
- printf (")\n" );
160
+ print (")\n" );
151
161
}
152
162
}
153
163
154
- int main () {
164
+ int main (int argc , char * * argv ) {
165
+ if (argc == 2 && strcmp (argv [1 ], "-v" ) == 0 ) {
166
+ verbose = 1 ;
167
+ } else {
168
+ verbose = 0 ;
169
+ }
155
170
int ret = 0 ;
156
171
struct node * tmp ;
157
172
memset (pushback , '\0' , PUSHBACK_LEN );
158
173
/* rsdebug = 1; */
159
174
ret = rsparse ();
160
- printf ("--- PARSE COMPLETE: ret:%d, n_nodes:%d ---\n" , ret , n_nodes );
175
+ print ("--- PARSE COMPLETE: ret:%d, n_nodes:%d ---\n" , ret , n_nodes );
161
176
if (nodes ) {
162
177
print_node (nodes , 0 );
163
178
}
0 commit comments