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
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go.h"
enum
{
PFIXME = 0,
};
void
exprlistfmt(Fmt *f, NodeList *l)
{
for(; l; l=l->next) {
exprfmt(f, l->n, 0);
if(l->next)
fmtprint(f, ", ");
}
}
void
exprfmt(Fmt *f, Node *n, int prec)
{
int nprec;
char *p;
nprec = 0;
if(n == nil) {
fmtprint(f, "<nil>");
return;
}
if(n->implicit) {
exprfmt(f, n->left, prec);
return;
}
switch(n->op) {
case OAPPEND:
case ONAME:
case ONONAME:
case OPACK:
case OLITERAL:
case ODOT:
case ODOTPTR:
case ODOTINTER:
case ODOTMETH:
case ODOTTYPE:
case ODOTTYPE2:
case OXDOT:
case OARRAYBYTESTR:
case OCAP:
case OCLOSE:
case OCOPY:
case OLEN:
case OMAKE:
case ONEW:
case OPANIC:
case OPRINT:
case OPRINTN:
case OCALL:
case OCALLMETH:
case OCALLINTER:
case OCALLFUNC:
case OCONV:
case OCONVNOP:
case OMAKESLICE:
case ORUNESTR:
case OADDR:
case OCOM:
case OIND:
case OMINUS:
case ONOT:
case OPLUS:
case ORECV:
case OCONVIFACE:
case OTPAREN:
case OINDEX:
case OINDEXMAP:
nprec = 7;
break;
case OMUL:
case ODIV:
case OMOD:
case OLSH:
case ORSH:
case OAND:
case OANDNOT:
nprec = 6;
break;
case OADD:
case OSUB:
case OOR:
case OXOR:
nprec = 5;
break;
case OEQ:
case OLT:
case OLE:
case OGE:
case OGT:
case ONE:
nprec = 4;
break;
case OSEND:
nprec = 3;
break;
case OANDAND:
nprec = 2;
break;
case OOROR:
nprec = 1;
break;
case OTYPE:
if(n->sym != S)
nprec = 7;
break;
}
if(prec > nprec)
fmtprint(f, "(");
switch(n->op) {
default:
bad:
fmtprint(f, "(node %O)", n->op);
break;
case OREGISTER:
fmtprint(f, "%R", n->val.u.reg);
break;
case OLITERAL:
if(n->sym != S) {
fmtprint(f, "%S", n->sym);
break;
}
switch(n->val.ctype) {
default:
goto bad;
case CTINT:
fmtprint(f, "%B", n->val.u.xval);
break;
case CTBOOL:
if(n->val.u.bval)
fmtprint(f, "true");
else
fmtprint(f, "false");
break;
case CTCPLX:
fmtprint(f, "%.17g+%.17gi",
mpgetflt(&n->val.u.cval->real),
mpgetflt(&n->val.u.cval->imag));
break;
case CTFLT:
fmtprint(f, "%.17g", mpgetflt(n->val.u.fval));
break;
case CTSTR:
fmtprint(f, "\"%Z\"", n->val.u.sval);
break;
case CTNIL:
fmtprint(f, "nil");
break;
}
break;
case ONAME:
case OPACK:
case ONONAME:
fmtprint(f, "%S", n->sym);
break;
case OTYPE:
if(n->type == T && n->sym != S) {
fmtprint(f, "%S", n->sym);
break;
}
fmtprint(f, "%T", n->type);
break;
case OTARRAY:
fmtprint(f, "[]");
exprfmt(f, n->left, PFIXME);
break;
case OTPAREN:
fmtprint(f, "(");
exprfmt(f, n->left, 0);
fmtprint(f, ")");
break;
case OTMAP:
fmtprint(f, "map[");
exprfmt(f, n->left, 0);
fmtprint(f, "] ");
exprfmt(f, n->right, 0);
break;
case OTCHAN:
if(n->etype == Crecv)
fmtprint(f, "<-");
fmtprint(f, "chan");
if(n->etype == Csend) {
fmtprint(f, "<- ");
exprfmt(f, n->left, 0);
} else {
fmtprint(f, " ");
if(n->left->op == OTCHAN && n->left->sym == S && n->left->etype == Crecv) {
fmtprint(f, "(");
exprfmt(f, n->left, 0);
fmtprint(f, ")");
} else
exprfmt(f, n->left, 0);
}
break;
case OTSTRUCT:
fmtprint(f, "<struct>");
break;
case OTINTER:
fmtprint(f, "<inter>");
break;
case OTFUNC:
fmtprint(f, "<func>");
break;
case OAS:
exprfmt(f, n->left, 0);
fmtprint(f, " = ");
exprfmt(f, n->right, 0);
break;
case OASOP:
exprfmt(f, n->left, 0);
fmtprint(f, " %#O= ", n->etype);
exprfmt(f, n->right, 0);
break;
case OAS2:
case OAS2DOTTYPE:
case OAS2FUNC:
case OAS2MAPR:
case OAS2MAPW:
case OAS2RECV:
exprlistfmt(f, n->list);
fmtprint(f, " = ");
exprlistfmt(f, n->rlist);
break;
case OADD:
case OANDAND:
case OANDNOT:
case ODIV:
case OEQ:
case OGE:
case OGT:
case OLE:
case OLT:
case OLSH:
case OMOD:
case OMUL:
case ONE:
case OOR:
case OOROR:
case ORSH:
case OSEND:
case OSUB:
case OXOR:
exprfmt(f, n->left, nprec);
fmtprint(f, " %#O ", n->op);
exprfmt(f, n->right, nprec+1);
break;
case OADDR:
case OCOM:
case OIND:
case OMINUS:
case ONOT:
case OPLUS:
case ORECV:
fmtprint(f, "%#O", n->op);
if((n->op == OMINUS || n->op == OPLUS) && n->left->op == n->op)
fmtprint(f, " ");
exprfmt(f, n->left, 0);
break;
case OCLOSURE:
fmtprint(f, "func literal");
break;
case OCOMPLIT:
fmtprint(f, "composite literal");
break;
case OARRAYLIT:
if(isslice(n->type))
fmtprint(f, "slice literal");
else
fmtprint(f, "array literal");
break;
case OMAPLIT:
fmtprint(f, "map literal");
break;
case OSTRUCTLIT:
fmtprint(f, "struct literal");
break;
case OXDOT:
case ODOT:
case ODOTPTR:
case ODOTINTER:
case ODOTMETH:
exprfmt(f, n->left, 7);
if(n->right == N || n->right->sym == S)
fmtprint(f, ".<nil>");
else {
// skip leading type· in method name
p = utfrrune(n->right->sym->name, 0xb7);
if(p)
p+=2;
else
p = n->right->sym->name;
fmtprint(f, ".%s", p);
}
break;
case ODOTTYPE:
case ODOTTYPE2:
exprfmt(f, n->left, 7);
fmtprint(f, ".(");
if(n->right != N)
exprfmt(f, n->right, 0);
else
fmtprint(f, "%T", n->type);
fmtprint(f, ")");
break;
case OINDEX:
case OINDEXMAP:
exprfmt(f, n->left, 7);
fmtprint(f, "[");
exprfmt(f, n->right, 0);
fmtprint(f, "]");
break;
case OSLICE:
case OSLICESTR:
case OSLICEARR:
exprfmt(f, n->left, 7);
fmtprint(f, "[");
if(n->right->left != N)
exprfmt(f, n->right->left, 0);
fmtprint(f, ":");
if(n->right->right != N)
exprfmt(f, n->right->right, 0);
fmtprint(f, "]");
break;
case OCALL:
case OCALLFUNC:
case OCALLINTER:
case OCALLMETH:
exprfmt(f, n->left, 7);
fmtprint(f, "(");
exprlistfmt(f, n->list);
if(n->isddd)
fmtprint(f, "...");
fmtprint(f, ")");
break;
case OCOMPLEX:
fmtprint(f, "complex(");
exprfmt(f, n->left, 0);
fmtprint(f, ", ");
exprfmt(f, n->right, 0);
fmtprint(f, ")");
break;
case OREAL:
fmtprint(f, "real(");
exprfmt(f, n->left, 0);
fmtprint(f, ")");
break;
case OIMAG:
fmtprint(f, "imag(");
exprfmt(f, n->left, 0);
fmtprint(f, ")");
break;
case OCONV:
case OCONVIFACE:
case OCONVNOP:
case OARRAYBYTESTR:
case ORUNESTR:
if(n->type == T || n->type->sym == S)
fmtprint(f, "(%T)(", n->type);
else
fmtprint(f, "%T(", n->type);
if(n->left == N)
exprlistfmt(f, n->list);
else
exprfmt(f, n->left, 0);
fmtprint(f, ")");
break;
case OAPPEND:
case OCAP:
case OCLOSE:
case OLEN:
case OCOPY:
case OMAKE:
case ONEW:
case OPANIC:
case OPRINT:
case OPRINTN:
fmtprint(f, "%#O(", n->op);
if(n->left)
exprfmt(f, n->left, 0);
else
exprlistfmt(f, n->list);
fmtprint(f, ")");
break;
case OMAKESLICE:
fmtprint(f, "make(%#T, ", n->type);
exprfmt(f, n->left, 0);
if(count(n->list) > 2) {
fmtprint(f, ", ");
exprfmt(f, n->right, 0);
}
fmtprint(f, ")");
break;
case OMAKEMAP:
fmtprint(f, "make(%#T)", n->type);
break;
}
if(prec > nprec)
fmtprint(f, ")");
}
|