-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
executable file
·195 lines (188 loc) · 5.66 KB
/
parser.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
#include "tokenizer.h"
#include "value.h"
#include "linkedlist.h"
#include "talloc.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
/*
* Prints a simple error trace and exits the program upon discovering unparsable input
*/
void raiseParseError(char* message, bool condition) {
if (!condition) {
printf("Syntax error: %s\n", message);
texit(1);
}
}
/*
* Adds a symbol to the parse tree, encapsulating symbols contained within parentheses
* in sub-lists.
*/
Value *addToParseTree(Value *tree, int *depth, Value *token) {
if (token->type == CLOSE_TYPE) {
// recurse up the level
(*depth)--;
raiseParseError("too many close parentheses.", (*depth) >= 0);
Value *tmpList = makeNull();
Value *top = car(tree);
// Pop off the stack
while (top->type != OPEN_TYPE) {
tmpList = cons(top, tmpList);
tree = cdr(tree);
top = car(tree);
}
// skip past open type we just read
tree = cdr(tree);
if (tree->type != NULL_TYPE && car(tree)->type == QUOTE_TYPE) {
//if the tree is not null, we need to check for a quote
tree = cdr(tree); //skip past the quote
Value *quoteExpansion = makeNull();
quoteExpansion = cons(tmpList, quoteExpansion);
Value *quoteSymbol = makeNull();
quoteSymbol->type = SYMBOL_TYPE;
quoteSymbol->s = talloc(sizeof(char) * 6);
strcpy(quoteSymbol->s, "quote");
tmpList = cons(quoteSymbol, quoteExpansion);
}
// Push tmpList onto the tree
tree = cons(tmpList, tree);
} else if (token->type == OPEN_TYPE) {
// push to tree
tree = cons(token, tree);
// increment depth
(*depth)++;
} else {
// continue push to tree, checking for quotes
if (tree->type != NULL_TYPE && car(tree)->type == QUOTE_TYPE) {
//skip past the quote
tree = cdr(tree);
Value *quoteExpansion = makeNull();
quoteExpansion = cons(token, quoteExpansion);
Value *quoteSymbol = makeNull();
quoteSymbol->type = SYMBOL_TYPE;
quoteSymbol->s = talloc(sizeof(char) * 6);
strcpy(quoteSymbol->s, "quote");
quoteExpansion = cons(quoteSymbol, quoteExpansion);
tree = cons(quoteExpansion, tree);
} else {
tree = cons(token, tree);
}
}
return tree;
}
/*
* Takes a linked list of tokens as a parameter.
* Returns the pointer to a parse tree representing the program.
*/
Value *parse(Value *tokens) {
Value *tree = makeNull();
int depth = 0;
Value *current = tokens;
assert(current != NULL && "Error (parse): null pointer");
while (current->type != NULL_TYPE) {
Value *token = car(current);
tree = addToParseTree(tree, &depth, token);
current = cdr(current);
}
raiseParseError("not enough close parentheses.", depth == 0);
return reverse(tree);
}
/*
* Recursively prints a parse tree
*/
void printTreeHelper(Value *tree, bool prefixWithSpace, bool expectingList, bool atBeginning) {
assert(tree != NULL);
if (prefixWithSpace && !atBeginning && (tree->type != NULL_TYPE || !expectingList) && tree->type != CONS_TYPE) {
printf(" ");
}
if (tree->type == INT_TYPE) {
if (expectingList) {
printf(". %i", tree->i);
} else {
printf("%i",tree->i);
}
}
else if (tree->type == DOUBLE_TYPE) {
if (expectingList) {
printf(". %f", tree->d);
} else {
printf("%f",tree->d);
}
}
else if (tree->type == STR_TYPE) {
if (expectingList) {
printf(". \"%s\"", tree->s);
} else {
printf("\"%s\"",tree->s);
}
}
else if (tree->type == SYMBOL_TYPE) {
if (expectingList) {
printf(". %s", tree->s);
} else {
printf("%s",tree->s);
}
}
else if (tree->type == BOOL_TYPE) {
if (expectingList) {
printf(". %s", tree->s);
} else {
printf("%s",tree->s);
}
}
else if (tree->type == CONS_TYPE) {
if (expectingList) {
if(atBeginning) {
printTreeHelper(tree->c.car, true, false, true);
printTreeHelper(tree->c.cdr, true, true, false);
}
else {
printTreeHelper(tree->c.car, true, false, false);
printTreeHelper(tree->c.cdr, true, true, false);
}
} else {
if (atBeginning) {
printf("(");
} else {
printf(" (");
}
printTreeHelper(tree->c.car, false, false, false);
printTreeHelper(tree->c.cdr, true, true, false);
printf(")");
}
}
else if (tree->type == NULL_TYPE) {
if (!expectingList) {
printf("()");
}
}
else if (tree->type == CLOSURE_TYPE) {
if (expectingList) {
printf(". #procedure");
} else {
printf("#procedure");
}
}
else if (tree->type == PRIMITIVE_TYPE) {
if (expectingList) {
printf(". #procedure");
} else {
printf("#procedure");
}
}
else if (tree->type == QUOTE_TYPE) {
printf("'");
}
}
/*
* Prints a parse tree to the command line, using parentheses
* to denote tree structure (ie it looks like scheme code)
*/
void printTree(Value *tree) {
assert(tree != NULL);
if (tree->type == NULL_TYPE) {
printf("()");
} else {
printTreeHelper(tree, false, tree->type == CONS_TYPE, true);
}
}