-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint.c
270 lines (225 loc) · 5.56 KB
/
print.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
#include "print.h"
/* evaluator info printing */
void print_info(char* main_label) {
labels_passed_count++;
if (!INFO || LIB) return;
NL;
printf("@ %s", main_label);
NL;
PRDIV;
print_registers();
print_stack();
if (STEP) getchar();
NL;
return;
}
void print_final_val(void) {
if (LIB) return;
NL;
printf("VALUE: ");
print_obj(val);
NL;
NL;
// is this step needed?
if (STEP) getchar();
return;
}
void print_registers(void) {
print_register(expr, EXPR_NAME);
print_register(val, VAL_NAME);
print_register(cont, CONT_NAME);
print_register(func, FUNC_NAME);
print_register(arglist, ARGLIST_NAME);
print_register(unev, UNEV_NAME);
print_register(env, ENV_NAME);
}
void print_stack(void) {
PRDIV;
// printf("%s\n", "printing stack...");
List* temp = stack;
int count = 0;
if (!temp) printf("%s\n", "-- EMPTY STACK --");
while (temp) {
NL_PRINT("-- STACK ENTRY %d --", count);
print_obj(temp->car);
NL;
temp = temp->cdr;
count++;
}
PRDIV;
}
void print_base_env(void) {
if (!INFO || LIB) return;
NL;
NL;
NL_PRINT("base_env: %p", base_env);
}
void print_unbound(void) {
NL;
NL;
NL_PRINT("UNBOUND VARIABLE: \"%s\"!", GETNAME(expr));
}
/* info helpers */
void print_register(Obj reg, char* name) {
NL_PRINT("-- %s --", name);
print_obj(reg);
NL;
}
void print_obj(Obj obj) {
switch (GETTAG(obj)) {
case NUM:
printf("%ld ", GETNUM(obj));
break;
case NAME:
printf("%s ", GETNAME(obj));
break;
case BOOL_:
printf("%s ", GETBOOL(obj) == 0 ? "#f" : "#t");
break;
case LIST:
printf("%s", "( ");
print_list(GETLIST(obj));
break;
case PRIM:
printf("%s ", obj.val.prim.name);
break;
case ENV:
printf("%p ", GETENV(obj));
break;
case LABEL:
print_label(GETLABEL(obj));
break;
case COMP:
printf("COMPILED FUNCTION");
break;
case DUMMY:
printf("%s ", "DUMMY");
break;
case ERROR:
printf("%s\n", "ERROR!!!");
break;
case UNINIT:
printf("%s ", "***");
break;
case tag_count:
printf("%s ", "tag_count...huh?");
break;
}
return;
}
void print_list(List* list) {
if (list == NULL) {
printf("%s", ") ");
return;
}
print_obj(list->car);
print_list(list->cdr);
}
void print_label(Label label) {
switch (label) {
case _DONE:
printf("DONE ");
break;
case _IF_DECIDE:
printf("IF_DECIDE ");
break;
case _DID_ASS_VAL:
printf("DID_ASS_VAL ");
break;
case _DID_DEF_VAL:
printf("DID_DEF_VAL ");
break;
case _DID_FUNC:
printf("DID_FUNC ");
break;
case _ACC_ARG:
printf("ACC_ARG ");
break;
case _DID_SIMPLE_ARG:
printf("DID_SIMPLE_ARG ");
break;
case _REST_SIMPLE_ARGS:
printf("REST_SIMPLE_ARGS ");
break;
case _DID_LAST_ARG:
printf("DID_LAST_ARG ");
break;
case _SEQ_CONT:
printf("SEQ_CONT ");
break;
case _ALT_SEQ_CONT:
printf("ALT_SEQ_CONT ");
break;
default:
printf("UNKNOWN LABEL ");
}
}
/* user interface printing */
void print_intro(void) {
NL;
NL_PRINT("Welcome to lispinc!");
NL;
NL_PRINT("Nick Drozd, 2016");
NL_PRINT("github.com/nickdrozd/lispinc");
NL;
NL_PRINT("Enter .help for help and enter .quit to quit.");
NL;
NL_PRINT("Now, the time has come for you to lispinc...for your life!");
NL;
}
void print_help(void) {
NL;
NL_PRINT("*** HELP ***");
TABNL_PRINT("-- enter .info to toggle evaluator info mode");
TABNL_PRINT(
"-- enter .step to toggle step mode (pauses between each step of the "
"evaluator in info mode)");
TABNL_PRINT("-- enter .stats to toggle stack stats mode");
TABNL_PRINT(
"-- enter .tail to toggle tail recursion mode (turning this off is "
"really only of any interest in conjunction with stats mode)");
TABNL_PRINT("-- enter .debug to toggle debug mode");
TABNL_PRINT("-- enter .quit to quit");
TABNL_PRINT("-- enter .repl to turn off info, stats, and step modes");
TABNL_PRINT("-- enter .demo to turn on info, stats, and step modes");
}
void print_flags(void) {
if (!INFO) return;
NL;
NL_PRINT("*** FLAGS ***");
print_flag(INFO, INFO_NAME);
print_flag(STEP, STEP_NAME);
print_flag(STATS, STATS_NAME);
print_flag(TAIL, TAIL_NAME);
print_flag(DEBUG, DEBUG_NAME);
}
void print_exit(void) {
NL;
NL_PRINT("exiting lispinc...");
NL;
NL_PRINT("Byeeeeee!");
NL;
}
void print_flag(int flag, char* name) {
TABNL_PRINT("%s :%s", name, flag ? "ON" : "OFF");
}
/* debugging */
void debug_print(char* statement) {
if (DEBUG) {
NL;
NL;
NL_PRINT("DEBUG: %s", statement);
NL;
}
}
// is there a better way to include the register name?
void debug_register(Obj reg, char* name) {
if (DEBUG) {
NL;
printf("DEBUG -- register: %s", name);
NL;
print_obj(reg);
NL;
NL;
}
}