-
Notifications
You must be signed in to change notification settings - Fork 4
/
printer.c
186 lines (177 loc) · 5 KB
/
printer.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
#include <assert.h>
#include <gc.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashmap.h"
#include "printer.h"
#include "types.h"
#include "util.h"
char *BLANK_LINE = "";
char* pr_str(MalType *val, int print_readably) {
char *str;
switch (val->type) {
case MAL_ATOM_TYPE:
return pr_atom(val, print_readably);
case MAL_BLANK_LINE_TYPE:
if (print_readably == 2) {
return BLANK_LINE;
} else {
str = GC_MALLOC(1);
str = 0;
return str;
}
case MAL_CONS_TYPE:
return pr_list(val, print_readably);
case MAL_CONTINUATION_TYPE:
return mal_sprintf("<continuation %p>", val)->str;
case MAL_EMPTY_TYPE:
return string("()");
case MAL_FALSE_TYPE:
return string("false");
case MAL_HASHMAP_TYPE:
return pr_hashmap(val, print_readably);
case MAL_KEYWORD_TYPE:
return mal_sprintf(":%s", val->keyword)->str;
case MAL_LAMBDA_TYPE:
return string("<lambda>");
case MAL_NIL_TYPE:
return string("nil");
case MAL_NUMBER_TYPE:
return long_long_to_string(val->number);
case MAL_REGEX_TYPE:
return pr_regex(val, print_readably);
case MAL_STRING_TYPE:
return pr_string(val, print_readably);
case MAL_SYMBOL_TYPE:
return string(val->symbol);
case MAL_TRUE_TYPE:
return string("true");
case MAL_VECTOR_TYPE:
return pr_vector(val, print_readably);
case MAL_ERROR_TYPE:
printf("This shouldn't happen; pr_str() got mal_error(%s)\n", pr_str(val->error_val, 1));
assert(0);
default:
printf("I don't know how to print this type: %d\n", val->type);
assert(0);
}
}
char* pr_atom(MalType *val, int print_readably) {
return mal_sprintf("(atom %s)", pr_str(val->atom_val, print_readably))->str;
}
char* pr_list(MalType *val, int print_readably) {
if (is_empty(val)) {
return string("()");
}
char *item_str;
MalType *str = mal_string("(");
do {
if (is_cons(val)) {
item_str = pr_str(mal_car(val), print_readably);
mal_string_append(str, item_str);
mal_string_append_char(str, ' ');
} else {
mal_string_append(str, ". ");
mal_string_append(str, pr_str(val, print_readably));
mal_string_append_char(str, ' ');
break;
}
} while (!is_empty(val = mal_cdr(val)));
str->str[strlen(str->str) - 1] = ')';
return str->str;
}
char* pr_vector(MalType *val, int print_readably) {
size_t len = mal_vector_len(val);
if (len == 0) {
return string("[]");
}
MalType *str = mal_string("[");
char *item_str;
for(size_t i=0; i<len; i++) {
item_str = pr_str(mal_vector_ref(val, i), print_readably);
mal_string_append(str, item_str);
if (i < len-1) mal_string_append_char(str, ' ');
}
mal_string_append_char(str, ']');
return str->str;
}
char* pr_hashmap(MalType *val, int print_readably) {
char *key_str, *val_str;
if (mal_hashmap_size(val) == 0) {
return string("{}");
}
MalType *str = mal_string("{");
struct hashmap_iter *iter;
for (iter = hashmap_iter(&val->hashmap); iter; iter = hashmap_iter_next(&val->hashmap, iter)) {
key_str = (char*)hashmap_iter_get_key(iter);
val_str = pr_str((MalType*)hashmap_iter_get_data(iter), print_readably);
mal_string_append(str, key_str);
mal_string_append_char(str, ' ');
mal_string_append(str, val_str);
mal_string_append_char(str, ' ');
}
str->str[strlen(str->str) - 1] = '}';
return str->str;
}
char* pr_string(MalType *val, int print_readably) {
assert(strlen(val->str) == val->str_len);
if (print_readably) {
size_t len = val->str_len;
char *orig = val->str;
MalType *repr = mal_string("\"");
for (size_t i=0; i<len; i++) {
switch (orig[i]) {
case '\n':
mal_string_append(repr, "\\n");
break;
case '"':
mal_string_append(repr, "\\\"");
break;
case '\\':
mal_string_append(repr, "\\\\");
break;
default:
mal_string_append_char(repr, orig[i]);
}
}
mal_string_append_char(repr, '"');
return repr->str;
} else {
size_t len = val->str_len + 1;
char *str = GC_MALLOC(len);
snprintf(str, len, "%s", val->str);
return str;
}
}
char* pr_regex(MalType *val, int print_readably) {
assert(strlen(val->regex) == val->regex_len);
if (print_readably) {
size_t len = val->regex_len;
char *orig = val->regex;
MalType *repr = mal_string("/");
for (size_t i=0; i<len; i++) {
switch (orig[i]) {
case '\n':
mal_string_append(repr, "\\n");
break;
case '/':
mal_string_append(repr, "\\/");
break;
case '\\':
mal_string_append(repr, "\\\\");
break;
default:
mal_string_append_char(repr, orig[i]);
}
}
mal_string_append_char(repr, '/');
return repr->str;
} else {
size_t len = val->regex_len + 1;
char *str = GC_MALLOC(len);
snprintf(str, len, "%s", val->regex);
return str;
}
}