]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/acid/print.c
audiohda: fix syntax error
[plan9front.git] / sys / src / cmd / acid / print.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include <mach.h>
6 #define Extern extern
7 #include "acid.h"
8
9 static char *binop[] =
10 {
11         [OMUL]  "*",
12         [ODIV]  "/",
13         [OMOD]  "%",
14         [OADD]  "+",
15         [OSUB]  "-",
16         [ORSH]  ">>",
17         [OLSH]  "<<",
18         [OLT]   "<",
19         [OGT]   ">",
20         [OLEQ]  "<=",
21         [OGEQ]  ">=",
22         [OEQ]   "==",
23         [ONEQ]  "!=",
24         [OLAND] "&",
25         [OXOR]  "^",
26         [OLOR]  "|",
27         [OCAND] "&&",
28         [OCOR]  "||",
29         [OASGN] " = ",
30 };
31
32 static char *tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
33 char *typenames[] =
34 {
35         [TINT]          "integer",
36         [TFLOAT]        "float",
37         [TSTRING]       "string",
38         [TLIST]         "list",
39         [TCODE]         "code",
40 };
41
42 int
43 cmp(void *va, void *vb)
44 {
45         char **a = va;
46         char **b = vb;
47
48         return strcmp(*a, *b);
49 }
50
51 void
52 fundefs(void)
53 {
54         Lsym *l;
55         char **vec;
56         int i, j, n, max, col, f, g, s;
57
58         max = 0;
59         f = 0;
60         g = 100;
61         vec = malloc(sizeof(char*)*g);
62         if(vec == 0)
63                 fatal("out of memory");
64
65         for(i = 0; i < Hashsize; i++) {
66                 for(l = hash[i]; l; l = l->hash) {
67                         if(l->proc == 0 && l->builtin == 0)
68                                 continue;
69                         n = strlen(l->name);
70                         if(n > max)
71                                 max = n;
72                         if(f >= g) {
73                                 g *= 2;
74                                 vec = realloc(vec, sizeof(char*)*g);
75                                 if(vec == 0)
76                                         fatal("out of memory");
77                         }
78                         vec[f++] = l->name;
79                 }
80         }
81         qsort(vec, f, sizeof(char*), cmp);
82         max++;
83         col = 60/max;
84         s = (f+col-1)/col;
85
86         for(i = 0; i < s; i++) {
87                 for(j = i; j < f; j += s)
88                         Bprint(bout, "%-*s", max, vec[j]);
89                 Bprint(bout, "\n");
90         }
91         free(vec);
92 }
93
94 void
95 whatis(Lsym *l)
96 {
97         int t;
98         int def;
99         Type *ti;
100
101         if(l == 0) {
102                 fundefs();
103                 return;
104         }
105
106         def = 0;
107         if(l->v->set) {
108                 t = l->v->type;
109                 Bprint(bout, "%s variable", typenames[t]);
110                 if(t == TINT || t == TFLOAT)
111                         Bprint(bout, " format %c", l->v->fmt);
112                 if(l->v->comt)
113                         Bprint(bout, " complex %s", l->v->comt->base->name);
114                 Bputc(bout, '\n');
115                 def = 1;
116         }
117         if(l->lt) {
118                 Bprint(bout, "complex %s {\n", l->name);
119                 for(ti = l->lt; ti; ti = ti->next) {
120                         if(ti->type) {
121                                 if(ti->fmt == 'a') {
122                                         Bprint(bout, "\t%s %d %s;\n",
123                                         ti->type->name, ti->offset,
124                                         ti->tag->name);
125                                 }
126                                 else {
127                                         Bprint(bout, "\t'%c' %s %d %s;\n",
128                                         ti->fmt, ti->type->name, ti->offset,
129                                         ti->tag->name);
130                                 }
131                         }
132                         else
133                                 Bprint(bout, "\t'%c' %d %s;\n",
134                                 ti->fmt, ti->offset, ti->tag->name);
135                 }
136                 Bprint(bout, "};\n");
137                 def = 1;
138         }
139         if(l->proc) {
140                 Bprint(bout, "defn %s(", l->name);
141                 pexpr(l->proc->left);
142                 Bprint(bout, ") {\n");
143                 pcode(l->proc->right, 1);
144                 Bprint(bout, "}\n");
145                 def = 1;
146         }
147         if(l->builtin) {
148                 Bprint(bout, "builtin function\n");
149                 def = 1;
150         }
151         if(def == 0)
152                 Bprint(bout, "%s is undefined\n", l->name);
153 }
154
155 void
156 slist(Node *n, int d)
157 {
158         if(n == 0)
159                 return;
160         if(n->op == OLIST)
161                 Bprint(bout, "%.*s{\n", d-1, tabs);
162         pcode(n, d);
163         if(n->op == OLIST)
164                 Bprint(bout, "%.*s}\n", d-1, tabs);
165 }
166
167 void
168 pcode(Node *n, int d)
169 {
170         Node *r, *l;
171
172         if(n == 0)
173                 return;
174
175         r = n->right;
176         l = n->left;
177
178         switch(n->op) {
179         default:
180                 Bprint(bout, "%.*s", d, tabs);
181                 pexpr(n);
182                 Bprint(bout, ";\n");
183                 break;
184         case OLIST:
185                 pcode(n->left, d);
186                 pcode(n->right, d);
187                 break;
188         case OLOCAL:
189                 Bprint(bout, "%.*slocal", d, tabs);
190                 while(l) {
191                         Bprint(bout, " %s", l->sym->name);
192                         l = l->left;
193                         if(l == 0)
194                                 Bprint(bout, ";\n");
195                         else
196                                 Bprint(bout, ",");
197                 }
198                 break;
199         case OCOMPLEX:
200                 Bprint(bout, "%.*scomplex %s %s;\n", d, tabs, n->sym->name, l->sym->name);
201                 break;
202         case OIF:
203                 Bprint(bout, "%.*sif ", d, tabs);
204                 pexpr(l);
205                 d++;
206                 Bprint(bout, " then\n");
207                 if(r && r->op == OELSE) {
208                         slist(r->left, d);
209                         Bprint(bout, "%.*selse\n", d-1, tabs);
210                         slist(r->right, d);
211                 }
212                 else
213                         slist(r, d);
214                 break;
215         case OWHILE:
216                 Bprint(bout, "%.*swhile ", d, tabs);
217                 pexpr(l);
218                 d++;
219                 Bprint(bout, " do\n");
220                 slist(r, d);
221                 break;
222         case ORET:
223                 Bprint(bout, "%.*sreturn ", d, tabs);
224                 pexpr(l);
225                 Bprint(bout, ";\n");
226                 break;
227         case ODO:
228                 Bprint(bout, "%.*sloop ", d, tabs);
229                 pexpr(l->left);
230                 Bprint(bout, ", ");
231                 pexpr(l->right);
232                 Bprint(bout, " do\n");
233                 slist(r, d+1);
234         }
235 }
236
237 void
238 pexpr(Node *n)
239 {
240         Node *r, *l;
241
242         if(n == 0)
243                 return;
244
245         r = n->right;
246         l = n->left;
247
248         switch(n->op) {
249         case ONAME:
250                 Bprint(bout, "%s", n->sym->name);
251                 break;
252         case OCONST:
253                 switch(n->type) {
254                 case TINT:
255                         Bprint(bout, "%lld", n->ival);
256                         break;
257                 case TFLOAT:
258                         Bprint(bout, "%g", n->fval);
259                         break;
260                 case TSTRING:
261                         pstr(n->string);
262                         break;
263                 case TLIST:
264                         break;
265                 }
266                 break;
267         case OMUL:
268         case ODIV:
269         case OMOD:
270         case OADD:
271         case OSUB:
272         case ORSH:
273         case OLSH:
274         case OLT:
275         case OGT:
276         case OLEQ:
277         case OGEQ:
278         case OEQ:
279         case ONEQ:
280         case OLAND:
281         case OXOR:
282         case OLOR:
283         case OCAND:
284         case OCOR:
285                 Bputc(bout, '(');
286                 pexpr(l);
287                 Bprint(bout, binop[n->op]);
288                 pexpr(r);
289                 Bputc(bout, ')');
290                 break;
291         case OASGN:
292                 pexpr(l);
293                 Bprint(bout, binop[n->op]);
294                 pexpr(r);
295                 break;
296         case OINDM:
297                 Bprint(bout, "*");
298                 pexpr(l);
299                 break;
300         case OEDEC:
301                 Bprint(bout, "--");
302                 pexpr(l);
303                 break;
304         case OEINC:
305                 Bprint(bout, "++");
306                 pexpr(l);
307                 break;
308         case OPINC:
309                 pexpr(l);
310                 Bprint(bout, "++");
311                 break;
312         case OPDEC:
313                 pexpr(l);
314                 Bprint(bout, "--");
315                 break;
316         case ONOT:
317                 Bprint(bout, "!");
318                 pexpr(l);
319                 break;
320         case OLIST:
321                 pexpr(l);
322                 if(r) {
323                         Bprint(bout, ",");
324                         pexpr(r);
325                 }
326                 break;
327         case OCALL:
328                 pexpr(l);
329                 Bprint(bout, "(");
330                 pexpr(r);
331                 Bprint(bout, ")");
332                 break;
333         case OCTRUCT:
334                 Bprint(bout, "{");
335                 pexpr(l);
336                 Bprint(bout, "}");
337                 break;
338         case OHEAD:
339                 Bprint(bout, "head ");
340                 pexpr(l);
341                 break;
342         case OTAIL:
343                 Bprint(bout, "tail ");
344                 pexpr(l);
345                 break;
346         case OAPPEND:
347                 Bprint(bout, "append ");
348                 pexpr(l);
349                 Bprint(bout, ",");
350                 pexpr(r);
351                 break;
352         case ODELETE:
353                 Bprint(bout, "delete ");
354                 pexpr(l);
355                 Bprint(bout, ",");
356                 pexpr(r);
357                 break;
358         case ORET:
359                 Bprint(bout, "return ");
360                 pexpr(l);
361                 break;
362         case OINDEX:
363                 pexpr(l);
364                 Bprint(bout, "[");
365                 pexpr(r);
366                 Bprint(bout, "]");
367                 break;
368         case OINDC:
369                 Bprint(bout, "@");
370                 pexpr(l);
371                 break;
372         case ODOT:
373                 pexpr(l);
374                 Bprint(bout, ".%s", n->sym->name);
375                 break;
376         case OFRAME:
377                 Bprint(bout, "%s:%s", n->sym->name, l->sym->name);
378                 break;
379         case OCAST:
380                 Bprint(bout, "(%s)", n->sym->name);
381                 pexpr(l);
382                 break;
383         case OFMT:
384                 pexpr(l);
385                 Bprint(bout, "\\%c", (int)r->ival);
386                 break;
387         case OEVAL:
388                 Bprint(bout, "eval ");
389                 pexpr(l);
390                 break;
391         case OWHAT:
392                 Bprint(bout, "whatis");
393                 if(n->sym)
394                         Bprint(bout, " %s", n->sym->name);
395                 break;
396         }
397 }
398
399 void
400 pstr(String *s)
401 {
402         int i, c;
403
404         Bputc(bout, '"');
405         for(i = 0; i < s->len; i++) {
406                 c = s->string[i];
407                 switch(c) {
408                 case '\0':
409                         c = '0';
410                         break;
411                 case '\n':
412                         c = 'n';
413                         break;
414                 case '\r':
415                         c = 'r';
416                         break;
417                 case '\t':
418                         c = 't';
419                         break;
420                 case '\b':
421                         c = 'b';
422                         break;
423                 case '\f':
424                         c = 'f';
425                         break;
426                 case '\a':
427                         c = 'a';
428                         break;
429                 case '\v':
430                         c = 'v';
431                         break;
432                 case '\\':
433                         c = '\\';
434                         break;
435                 case '"':
436                         c = '"';
437                         break;
438                 default:
439                         Bputc(bout, c);
440                         continue;
441                 }
442                 Bputc(bout, '\\');
443                 Bputc(bout, c);
444         }
445         Bputc(bout, '"');
446 }