-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwrite.c
142 lines (128 loc) · 3.19 KB
/
write.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
/*
* module : write.c
* version : 1.2
* date : 10/11/24
*/
#include "globals.h"
/*
* writefactor - print a factor in readable format to stdout.
*/
void writefactor(pEnv env, Index n, FILE *fp)
{
int i;
uint64_t set, j;
char *ptr, buf[BUFFERMAX], tmp[MAXNUM];
#if 0
/*
* This cannot happen. writefactor has a small number of customers: writeterm,
* main, put, fput. They all check that the stack is not empty, so this code
* only serves as a reminder for future customers.
*/
if (!n)
execerror(env, "non-empty stack", "writefactor");
#endif
switch (nodetype(n)) {
case USR_:
fprintf(fp, "%s", vec_at(env->symtab, nodevalue(n).ent).name);
break;
case ANON_FUNCT_:
fprintf(fp, "%s", opername(operindex(env, nodevalue(n).proc)));
break;
case BOOLEAN_:
fprintf(fp, "%s", nodevalue(n).num ? "true" : "false");
break;
case CHAR_:
if (nodevalue(n).num >= 8 && nodevalue(n).num <= 13)
fprintf(fp, "'\\%c", "btnvfr"[nodevalue(n).num - 8]);
else if (iscntrl(nodevalue(n).num) || nodevalue(n).num == 32)
fprintf(fp, "'\\%03d", (int)nodevalue(n).num);
else
fprintf(fp, "'%c", (int)nodevalue(n).num);
break;
case INTEGER_:
fprintf(fp, "%" PRId64, nodevalue(n).num);
break;
case SET_:
putc('{', fp);
for (i = 0, j = 1, set = nodevalue(n).set; i < SETSIZE; i++, j <<= 1)
if (set & j) {
fprintf(fp, "%d", i);
set &= ~j;
if (set)
putc(' ', fp);
}
putc('}', fp);
break;
case STRING_:
putc('"', fp);
#ifdef NOBDW
for (ptr = (char *)&nodevalue(n); *ptr; ptr++)
#else
for (ptr = nodevalue(n).str; *ptr; ptr++)
#endif
if (*ptr == '"')
fprintf(fp, "\\\"");
else if (*ptr >= 8 && *ptr <= 13)
fprintf(fp, "\\%c", "btnvfr"[*ptr - 8]);
else if (iscntrl((int)*ptr))
fprintf(fp, "\\%03d", *ptr);
else
putc(*ptr, fp);
putc('"', fp);
break;
case LIST_:
putc('[', fp);
writeterm(env, nodevalue(n).lis, fp);
putc(']', fp);
break;
case FLOAT_:
snprintf(buf, BUFFERMAX, "%g", nodevalue(n).dbl); /* exponent char e */
if ((ptr = strchr(buf, '.')) == 0) { /* locate decimal point */
if ((ptr = strchr(buf, 'e')) == 0) {/* locate start of exponent */
i = buf[strlen(buf) - 1];
if (isdigit(i)) /* check digit present */
strcat(buf, ".0"); /* add decimal point and 0 */
} else {
strcpy(tmp, ptr); /* save exponent */
strcpy(ptr, ".0"); /* add decimal point and 0 */
strcat(buf, tmp); /* restore exponent */
}
}
fprintf(fp, "%s", buf);
break;
case FILE_:
if (!nodevalue(n).fil)
fprintf(fp, "NULL");
else if (nodevalue(n).fil == stdin)
fprintf(fp, "stdin");
else if (nodevalue(n).fil == stdout)
fprintf(fp, "stdout");
else if (nodevalue(n).fil == stderr)
fprintf(fp, "stderr");
else
fprintf(fp, "%p", (void *)nodevalue(n).fil);
break;
case BIGNUM_:
#ifdef NOBDW
fprintf(fp, "%s", (char *)&nodevalue(n));
#else
fprintf(fp, "%s", nodevalue(n).str);
#endif
break;
default:
error("a factor cannot begin with this symbol");
break;
}
}
/*
* writeterm - print the contents of a list in readable format to fp.
*/
void writeterm(pEnv env, Index n, FILE *fp)
{
while (n) {
writefactor(env, n, fp);
POP(n);
if (n)
putc(' ', fp);
}
}