-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.c
More file actions
156 lines (153 loc) · 4.75 KB
/
printer.c
File metadata and controls
156 lines (153 loc) · 4.75 KB
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
#include "printer.h"
#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void lizard_print_indent(int depth) {
for (int i = 0; i < depth; i++) {
printf(" ");
}
}
void lizard_fprint_ast(FILE *fp, lizard_ast_node_t *node, int depth) {
if (!node) {
fprintf(fp, "NULL\n");
return;
}
lizard_print_indent(depth);
switch (node->type) {
case AST_STRING:
fprintf(fp, "String: \"%s\"\n", node->data.string);
break;
case AST_NUMBER:
fprintf(fp, "Number: ");
gmp_printf("%Zd", node->data.number);
fprintf(fp, "\n");
break;
case AST_SYMBOL:
fprintf(fp, "Symbol: %s\n", node->data.variable);
break;
case AST_BOOL:
fprintf(fp, "Boolean: %s\n", node->data.boolean ? "#t" : "#f");
break;
case AST_PAIR:
fprintf(fp, "Pair:\n");
lizard_print_indent(depth + 1);
fprintf(fp, "Car:\n");
lizard_fprint_ast(fp, node->data.pair.car, depth + 2);
lizard_print_indent(depth + 1);
fprintf(fp, "Cdr:\n");
lizard_fprint_ast(fp, node->data.pair.cdr, depth + 2);
break;
case AST_NIL:
fprintf(fp, "Nil\n");
break;
case AST_QUOTE:
fprintf(fp, "Quote:\n");
lizard_fprint_ast(fp, node->data.quoted, depth + 1);
break;
case AST_QUASIQUOTE:
fprintf(fp, "Quasiquote:\n");
lizard_fprint_ast(fp, node->data.quoted, depth + 1);
break;
case AST_UNQUOTE:
fprintf(fp, "Unquote:\n");
lizard_fprint_ast(fp, node->data.quoted, depth + 1);
break;
case AST_UNQUOTE_SPLICING:
fprintf(fp, "Unquote-splicing:\n");
lizard_fprint_ast(fp, node->data.quoted, depth + 1);
break;
case AST_ASSIGNMENT:
fprintf(fp, "Assignment:\n");
lizard_fprint_ast(fp, node->data.assignment.variable, depth + 1);
lizard_fprint_ast(fp, node->data.assignment.value, depth + 1);
break;
case AST_DEFINITION:
fprintf(fp, "Definition:\n");
lizard_fprint_ast(fp, node->data.definition.variable, depth + 1);
lizard_fprint_ast(fp, node->data.definition.value, depth + 1);
break;
case AST_IF:
fprintf(fp, "If:\n");
lizard_fprint_ast(fp, node->data.if_clause.pred, depth + 1);
lizard_fprint_ast(fp, node->data.if_clause.cons, depth + 1);
if (node->data.if_clause.alt)
lizard_fprint_ast(fp, node->data.if_clause.alt, depth + 1);
break;
case AST_LAMBDA:
fprintf(fp, "Lambda:\n");
if (node->data.lambda.parameters) {
list_node_t *iter = node->data.lambda.parameters->head;
while (iter != node->data.lambda.parameters->nil) {
lizard_fprint_ast(fp, ((lizard_ast_list_node_t *)iter)->ast, depth + 1);
iter = iter->next;
}
}
break;
case AST_BEGIN:
fprintf(fp, "Begin:\n");
{
list_node_t *iter = node->data.begin_expressions->head;
while (iter != node->data.begin_expressions->nil) {
lizard_fprint_ast(fp, ((lizard_ast_list_node_t *)iter)->ast, depth + 1);
iter = iter->next;
}
}
break;
case AST_APPLICATION:
fprintf(fp, "Application:\n");
{
list_node_t *iter = node->data.application_arguments->head;
if (iter == node->data.application_arguments->nil) {
lizard_print_indent(depth + 1);
fprintf(fp, "Empty application\n");
}
while (iter != node->data.application_arguments->nil) {
lizard_fprint_ast(fp, ((lizard_ast_list_node_t *)iter)->ast, depth + 1);
iter = iter->next;
}
}
break;
case AST_MACRO:
fprintf(fp, "Macro:\n");
lizard_fprint_ast(fp, node->data.macro_def.variable, depth + 1);
lizard_fprint_ast(fp, node->data.macro_def.transformer, depth + 1);
break;
case AST_PROMISE:
fprintf(fp, "Promise (forced=%s)\n",
node->data.promise.forced ? "true" : "false");
if (node->data.promise.forced)
lizard_fprint_ast(fp, node->data.promise.value, depth + 1);
else
lizard_fprint_ast(fp, node->data.promise.expr, depth + 1);
break;
case AST_CONTINUATION:
fprintf(fp, "Continuation: %p\n", node->data.continuation.captured_cont);
break;
case AST_CALLCC:
fprintf(fp, "Call/cc:\n");
{
list_node_t *iter = node->data.application_arguments->head;
while (iter != node->data.application_arguments->nil) {
lizard_fprint_ast(fp, ((lizard_ast_list_node_t *)iter)->ast, depth + 1);
iter = iter->next;
}
}
break;
case AST_ERROR:
fprintf(fp, "Error (code %d):\n", node->data.error.code);
{
list_node_t *iter = node->data.error.data->head;
while (iter != node->data.error.data->nil) {
lizard_fprint_ast(fp, ((lizard_ast_list_node_t *)iter)->ast, depth + 1);
iter = iter->next;
}
}
break;
default:
fprintf(fp, "Unknown AST node type.\n");
}
}
void lizard_print_ast(lizard_ast_node_t *node, int depth) {
lizard_fprint_ast(stdout, node, depth);
}