-
Notifications
You must be signed in to change notification settings - Fork 11
/
Printer.HC
155 lines (146 loc) · 3.53 KB
/
Printer.HC
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
#ifndef PRINTER_HC
#define PRINTER_HC
#include "MalListVector.HC"
#include "String.HC"
#include "Types.HC"
String *unescapeString(String *s)
{
INT i;
/* CHAR c; */
INT c; // suppress warning about 64-bit register
String *unescaped = StringEmpty;
for (i = 0; i < StringLen(s); ++i) {
c = StringGet(s, i);
switch (c) {
case '"':
StringAppend(unescaped, '\\');
StringAppend(unescaped, '"');
break;
case '\n':
StringAppend(unescaped, '\\');
StringAppend(unescaped, 'n');
break;
case '\\':
StringAppend(unescaped, '\\');
StringAppend(unescaped, '\\');
break;
default:
StringAppend(unescaped, c);
};
}
return unescaped;
}
String *PrintAtom(Atom *a, BOOL print_readably, BOOL print_quotes)
{
CHAR buf[32];
String *s, *unescaped;
NullCheck(a, "atom");
switch (a->_tag) {
case ATOM_NIL:
return StringMk("nil");
case ATOM_FALSE:
return StringMk("false");
case ATOM_TRUE:
return StringMk("true");
case ATOM_SYMBOL:
case ATOM_KEYWORD:
return StringCopy(a->_str);
case ATOM_STRING:
if (print_quotes) s = StringMk("\"");
else s = StringEmpty;
if (print_readably) {
unescaped = unescapeString(a->_str);
StringConcat(s, unescaped);
}
else {
StringConcat(s, a->_str);
}
if (print_quotes) StringAppend(s, '"');
return s;
case ATOM_NUMBER:
StrPrint(buf, "%ld", a->_num);
return StringMk(buf);
default:
PrintError("unknown atom tag");
return NULL;
};
}
String *PrintFun(Fun *f)
{
return StringMk("#<function>");
}
String *PrintRef(Malval *m, BOOL print_readably)
{
String *s = StringMk("(atom ");
String *_s = PrintMalval(m->_ref, print_readably, TRUE);
StringConcat(s, _s);
StringAppend(s, ')');
return s;
}
VOID _printSeqAux(Malval *seq, String *str, BOOL print_readably)
{
INT i;
Malval *_m;
String *_s;
for (i = 0; i < MalSize(seq); ++i) {
_m = MalNth(seq, i);
// this is on purpose (passing print_readably for print_quotes)
_s = PrintMalval(_m, print_readably, print_readably);
StringConcat(str, _s);
if (i < MalSize(seq) - 1)
StringAppend(str, ' ');
}
}
String *PrintMalval(Malval *m, BOOL print_readably=TRUE, BOOL print_quotes=TRUE)
{
NullCheck(m, "malval");
String *s;
INT i;
Hashmap *tab;
PArray *keys;
Malval *key, *val;
switch (m->_tag) {
case ATOM:
return PrintAtom(m->_atom, print_readably, print_quotes);
case LIST:
s = StringMk("(");
_printSeqAux(m, s, print_readably);
StringAppend(s, ')');
return s;
case VEC:
s = StringMk("[");
_printSeqAux(m, s, print_readably);
StringAppend(s, ']');
return s;
case FUN:
return PrintFun(m->_fun);
case HASH:
s = StringMk("{");
tab = HashmapOfMalval(m);
keys = RemoveDuplicateMalvals(HashmapKeys(tab));
for (i = 0; i < PArraySize(keys); ++i) {
key = PArrayGet(keys, i);
val = HashmapGet(tab, key);
StringConcat(s, PrintMalval(key, print_readably, print_readably));
StringConcatC(s, " ");
StringConcat(s, PrintMalval(val, print_readably, print_readably));
if (i < PArraySize(keys) - 1)
StringAppend(s, ' ');
}
StringAppend(s, '}');
return s;
case REF:
return PrintRef(m, print_readably);
default:
"unknown malval tag: %d\n", m->_tag;
return throws("print malval error");
}
}
VOID PrintfMalval(Malval *m, BOOL print_readably, BOOL print_quotes)
{
String *s = PrintMalval(m, print_readably, print_quotes);
CHAR *c = StringCString(s);
"%s\n", c;
Free(c);
}
#endif