/
show.c
521 lines (482 loc) · 12.6 KB
/
show.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
/*
* show.c
* Copyright (C) 2013 National University of Singapore
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "log.h"
#include "op.h"
#include "show.h"
#include "term.h"
/*
* Prototypes.
*/
static bool show_needs_brackets(atom_t atom, term_t t, size_t idx,
assoc_t assoc, unsigned priority, bool ac);
/*
* Print a term to a buffer.
*/
extern char *show_buf(char *start, char *end, term_t t)
{
switch (type(t))
{
case NIL:
return show_buf_nil(start, end);
case BOOL:
return show_buf_bool(start, end, boolean(t));
case NUM:
return show_buf_num(start, end, num(t));
case STR:
return show_buf_string(start, end, string(t));
case ATOM:
return show_buf_atom(start, end, atom(t));
case FUNC:
return show_buf_func(start, end, func(t));
case FOREIGN:
return show_buf_foreign(start, end, foreign(t));
case VAR:
return show_buf_var(start, end, var(t));
default:
panic("bad term type (%d)", type(t));
}
}
/*
* Print a term to a file.
*/
extern void show_file(FILE *file, term_t t)
{
char buf[1000];
char *end = show_buf(buf, buf+sizeof(buf), t);
if (end < buf+sizeof(buf))
{
fputs(buf, file);
return;
}
else
{
char buf2[end-buf+1];
show_buf(buf2, buf2+sizeof(buf2), t);
fputs(buf2, file);
return;
}
}
/*
* Print a term to an allocated string.
*/
extern char *show(term_t t)
{
char *end = show_buf(NULL, NULL, t);
size_t len = end - (char *)NULL;
char *str = (char *)gc_malloc(len+1);
show_buf(str, str+len+1, t);
return str;
}
/*
* Write a character.
*/
extern char *show_buf_char(char *start, char *end, char c)
{
if (start < end)
*start = c;
start++;
if (start < end)
*start = '\0';
return start;
}
/*
* Write a string.
*/
extern char *show_buf_str(char *start, char *end, const char *str)
{
for (size_t i = 0; str[i] != '\0'; i++)
start = show_buf_char(start, end, str[i]);
return start;
}
/*
* Write an escaped string.
*/
extern char *show_buf_esc_str(char *start, char *end, const char *str,
size_t str_len)
{
for (size_t i = 0; i < str_len; i++)
{
if (isprint(str[i]) && str[i] != '"' && str[i] != '\\')
start = show_buf_char(start, end, str[i]);
else
{
start = show_buf_char(start, end, '\\');
switch (str[i])
{
case '\0':
start = show_buf_char(start, end, '0');
break;
case '"':
start = show_buf_char(start, end, '"');
break;
case '\\':
start = show_buf_char(start, end, '\\');
break;
case '\n':
start = show_buf_char(start, end, 'n');
break;
case '\r':
start = show_buf_char(start, end, 'r');
break;
case '\t':
start = show_buf_char(start, end, 't');
break;
case '\a':
start = show_buf_char(start, end, 'a');
break;
case '\b':
start = show_buf_char(start, end, 'b');
break;
case '\f':
start = show_buf_char(start, end, 'f');
break;
default:
{
start = show_buf_char(start, end, 'x');
char tmp[3];
snprintf(tmp, sizeof(tmp), "%.2X", str[i] & 0xFF);
start = show_buf_char(start, end, tmp[0]);
start = show_buf_char(start, end, tmp[1]);
break;
}
}
}
}
return start;
}
/*
* Write a functor.
*/
extern char *show_buf_func(char *start, char *end, func_t f)
{
show_buf_func_restart: {}
const char *name = atom_name(f->atom);
uint_t a = atom_arity(f->atom);
assoc_t assoc;
unsigned priority;
bool ac, space;
if (a == 1 && unop_lookup(opinfo_init(), name, &priority, &space))
{
// A unary operator.
bool b2 = show_needs_brackets(f->atom, f->args[0], 0, XFX, priority,
false);
start = show_buf_str(start, end, name);
if (space)
start = show_buf_char(start, end, ' ');
if (!b2 && type(f->args[0]) == FUNC)
{
f = func(f->args[0]);
goto show_buf_func_restart; // Tail call optimization.
}
if (b2)
start = show_buf_char(start, end, '(');
start = show_buf(start, end, f->args[0]);
if (b2)
start = show_buf_char(start, end, ')');
return start;
}
if (a == 2 && binop_lookup(opinfo_init(), name, &assoc, &priority, &ac,
&space))
{
// A binary operator.
bool b1 = show_needs_brackets(f->atom, f->args[0], 0, assoc, priority,
ac);
bool b2 = show_needs_brackets(f->atom, f->args[1], 1, assoc, priority,
ac);
if (b1)
start = show_buf_char(start, end, '(');
start = show_buf(start, end, f->args[0]);
if (b1)
start = show_buf_char(start, end, ')');
if (space)
start = show_buf_char(start, end, ' ');
start = show_buf_str(start, end, name);
if (space)
start = show_buf_char(start, end, ' ');
if (!b2 && type(f->args[1]) == FUNC)
{
f = func(f->args[1]);
goto show_buf_func_restart; // Tail call optimization.
}
if (b2)
start = show_buf_char(start, end, '(');
start = show_buf(start, end, f->args[1]);
if (b2)
start = show_buf_char(start, end, ')');
return start;
}
// Not an operator:
start = show_buf_name(start, end, name);
if (a == 0)
{
start = show_buf_str(start, end, "()");
return start;
}
start = show_buf_char(start, end, '(');
for (uint_t i = 0; i < a-1; i++)
{
start = show_buf(start, end, f->args[i]);
start = show_buf_char(start, end, ',');
}
start = show_buf(start, end, f->args[a-1]);
start = show_buf_char(start, end, ')');
return start;
}
/*
* Show a functor.
*/
extern char *show_func(func_t f)
{
char *end = show_buf_func(NULL, NULL, f);
size_t len = end - (char *)NULL;
char *str = (char *)gc_malloc(len+1);
show_buf_func(str, str+len+1, f);
return str;
}
/*
* Check if an argument needs brackets or not.
*/
static bool show_needs_brackets(atom_t atom, term_t t, size_t idx,
assoc_t assoc, unsigned priority, bool ac)
{
if (type(t) != FUNC)
return false;
func_t f = func(t);
if (atom_arity(f->atom) != 2)
return false;
unsigned priority_1;
if (!binop_lookup(opinfo_init(), atom_name(f->atom), NULL, &priority_1,
NULL, NULL))
return false;
if (priority_1 < priority)
return false;
if (priority_1 > priority)
return true;
if (atom == f->atom && ac)
return false;
switch (assoc)
{
case XFX:
return true;
case XFY:
return (idx == 1);
case YFX:
return (idx == 0);
}
return true;
}
/*
* Write a name.
*/
extern char *show_buf_name(char *start, char *end, const char *name)
{
bool quotes = false;
for (size_t i = 0; name[i] != '\0'; i++)
{
if (name[i] != '_' && !isalnum(name[i]))
{
quotes = true;
break;
}
}
if (quotes)
{
start = show_buf_char(start, end, '\'');
start = show_buf_esc_str(start, end, name, strlen(name));
start = show_buf_char(start, end, '\'');
}
else
start = show_buf_str(start, end, name);
return start;
}
/*
* Write a string.
*/
extern char *show_buf_string(char *start, char *end, str_t s)
{
start = show_buf_char(start, end, '"');
start = show_buf_esc_str(start, end, s->chars, s->len);
start = show_buf_char(start, end, '"');
return start;
}
/*
* Show a string.
*/
extern char *show_string(str_t s)
{
char *end = show_buf_string(NULL, NULL, s);
size_t len = end - (char *)NULL;
char *str = (char *)gc_malloc(len+1);
show_buf_string(str, str+len+1, s);
return str;
}
/*
* Write an atom.
*/
extern char *show_buf_atom(char *start, char *end, atom_t a)
{
start = show_buf_char(start, end, '@');
start = show_buf_str(start, end, atom_name(a));
return start;
}
/*
* Show a Boolean.
*/
extern char *show_atom(atom_t a)
{
char *end = show_buf_atom(NULL, NULL, a);
size_t len = end - (char *)NULL;
char *str = (char *)gc_malloc(len+1);
show_buf_atom(str, str+len+1, a);
return str;
}
/*
* Write a nil.
*/
extern char *show_buf_nil(char *start, char *end)
{
return show_buf_str(start, end, "nil");
}
/*
* Show a nil.
*/
extern char *show_nil(void)
{
char *str = (char *)gc_malloc(4);
show_buf_nil(str, str+4);
return str;
}
/*
* Write a Boolean.
*/
extern char *show_buf_bool(char *start, char *end, bool_t b)
{
return show_buf_str(start, end, (b? "true": "false"));
}
/*
* Show a Boolean.
*/
extern char *show_bool(bool_t b)
{
char *end = show_buf_bool(NULL, NULL, b);
size_t len = end - (char *)NULL;
char *str = (char *)gc_malloc(len+1);
show_buf_bool(str, str+len+1, b);
return str;
}
/*
* Write a number.
*/
#define NUM_MAX_LEN 100
#define NUM_MIN_PRECISION 15
#define NUM_MAX_PRECISION (NUM_MIN_PRECISION+2)
extern char *show_buf_num(char *start, char *end, num_t n)
{
if (n == inf)
return show_buf_str(start, end, "inf");
if (n == -inf)
return show_buf_str(start, end, "-inf");
char tmp[NUM_MAX_LEN];
ssize_t l;
size_t p = NUM_MIN_PRECISION;
num_t n1;
do
{
l = snprintf(tmp, sizeof(tmp), "%.*g", (int)p, n);
if (l < 0)
panic("snprintf failed: %s", strerror(errno));
p++;
if (p >= NUM_MAX_PRECISION)
break;
sscanf(tmp, "%lf", &n1);
n1 = num(make_num(n1));
}
while (n != n1);
if (start >= end)
return start + (size_t)l;
return show_buf_str(start, end, tmp);
}
/*
* Show a number.
*/
extern char *show_num(num_t n)
{
char *end = show_buf_num(NULL, NULL, n);
size_t len = end - (char *)NULL;
char *str = (char *)gc_malloc(len+1);
show_buf_num(str, str+len+1, n);
return str;
}
/*
* Write a variable.
*/
extern char *show_buf_var(char *start, char *end, var_t v)
{
const char *name = v->name;
if (name == NULL)
{
char *buf = (start < end? start: NULL);
size_t len = (start < end? end - start: 0);
size_t idx = gc_objidx(v);
int res = snprintf(buf, len, "_V%zu", idx);
if (res < 0)
panic("snprintf failed: %s", strerror(errno));
return start + (size_t)res;
}
else
return show_buf_name(start, end, name);
}
/*
* Show a variable.
*/
extern char *show_var(var_t v)
{
char *end = show_buf_var(NULL, NULL, v);
size_t len = end - (char *)NULL;
char *str = (char *)gc_malloc(len+1);
show_buf_var(str, str+len+1, v);
return str;
}
/*
* Write a foriegn value.
*/
extern char *show_buf_foreign(char *start, char *end, foreign_t f)
{
char *buf = (start < end? start: NULL);
size_t len = (start < end? end - start: 0);
int res = snprintf(buf, len, "#%.16llX", (unsigned long long)f);
if (res < 0)
panic("snprintf failed: %s", strerror(errno));
return start + (size_t)res;
}
/*
* Show a foreign.
*/
extern char *show_foreign(foreign_t f)
{
char *end = show_buf_foreign(NULL, NULL, f);
size_t len = end - (char *)NULL;
char *str = (char *)gc_malloc(len+1);
show_buf_foreign(str, str+len+1, f);
return str;
}