-
Notifications
You must be signed in to change notification settings - Fork 0
/
Printer.H
255 lines (239 loc) · 5.88 KB
/
Printer.H
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
#ifndef PRINTER_HEADER
#define PRINTER_HEADER
#include "Absyn.H"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Certain applications may improve performance by changing the buffer size */
#define BUFFER_INITIAL 2000
/* You may wish to change _L_PAREN or _R_PAREN */
#define _L_PAREN '('
#define _R_PAREN ')'
class PrintAbsyn : public Visitor
{
protected:
int _n_, _i_;
/* The following are simple heuristics for rendering terminals */
/* You may wish to change them */
void render(Char c);
void render(String s);
void render(char* s);
void indent(void);
void backup(void);
public:
PrintAbsyn(void);
~PrintAbsyn(void);
char* print(Visitable* v);
void visitProgram(Program *p); /* abstract class */
void visitProg(Prog *p);
void visitFunction(Function *p); /* abstract class */
void visitGlobal(Global *p);
void visitFun(Fun *p);
void visitDecl(Decl *p); /* abstract class */
void visitDec(Dec *p);
void visitFDecl(FDecl *p); /* abstract class */
void visitFDec(FDec *p);
void visitListFunction(ListFunction* p);
void visitListStm(ListStm* p);
void visitListFDecl(ListFDecl* p);
void visitListIdent(ListIdent* p);
void visitStm(Stm *p); /* abstract class */
void visitSDecl(SDecl *p);
void visitSExp(SExp *p);
void visitSBlock(SBlock *p);
void visitSWhile(SWhile *p);
void visitSRepeat(SRepeat *p);
void visitSIf(SIf *p);
void visitSIfThenElse(SIfThenElse *p);
void visitSIfThen(SIfThen *p);
void visitSFor(SFor *p);
void visitSForScoped(SForScoped *p);
void visitSReturn(SReturn *p);
void visitExp(Exp *p); /* abstract class */
void visitEAss(EAss *p);
void visitELt(ELt *p);
void visitEGt(EGt *p);
void visitEAdd(EAdd *p);
void visitESub(ESub *p);
void visitEMul(EMul *p);
void visitCall(Call *p);
void visitEVar(EVar *p);
void visitEStr(EStr *p);
void visitEInt(EInt *p);
void visitEDouble(EDouble *p);
void visitListExp(ListExp* p);
void visitType(Type *p); /* abstract class */
void visitTInt(TInt *p);
void visitTDouble(TDouble *p);
void visitInteger(Integer i);
void visitDouble(Double d);
void visitChar(Char c);
void visitString(String s);
void visitIdent(String s);
protected:
void inline bufAppend(const char* s)
{
int len = strlen(s);
while (cur_ + len > buf_size)
{
buf_size *= 2; /* Double the buffer size */
resizeBuffer();
}
for(int n = 0; n < len; n++)
{
buf_[cur_ + n] = s[n];
}
cur_ += len;
buf_[cur_] = 0;
}
void inline bufAppend(const char c)
{
if (cur_ == buf_size)
{
buf_size *= 2; /* Double the buffer size */
resizeBuffer();
}
buf_[cur_] = c;
cur_++;
buf_[cur_] = 0;
}
void inline bufAppend(String str)
{
const char* s = str.c_str();
bufAppend(s);
}
void inline bufReset(void)
{
cur_ = 0;
buf_size = BUFFER_INITIAL;
resizeBuffer();
memset(buf_, 0, buf_size);
}
void inline resizeBuffer(void)
{
char* temp = (char*) malloc(buf_size);
if (!temp)
{
fprintf(stderr, "Error: Out of memory while attempting to grow buffer!\n");
exit(1);
}
if (buf_)
{
strcpy(temp, buf_);
free(buf_);
}
buf_ = temp;
}
char *buf_;
int cur_, buf_size;
};
class ShowAbsyn : public Visitor
{
public:
ShowAbsyn(void);
~ShowAbsyn(void);
char* show(Visitable* v);
void visitProgram(Program *p); /* abstract class */
void visitProg(Prog *p);
void visitFunction(Function *p); /* abstract class */
void visitGlobal(Global *p);
void visitFun(Fun *p);
void visitDecl(Decl *p); /* abstract class */
void visitDec(Dec *p);
void visitFDecl(FDecl *p); /* abstract class */
void visitFDec(FDec *p);
void visitListFunction(ListFunction* p);
void visitListStm(ListStm* p);
void visitListFDecl(ListFDecl* p);
void visitListIdent(ListIdent* p);
void visitStm(Stm *p); /* abstract class */
void visitSDecl(SDecl *p);
void visitSExp(SExp *p);
void visitSBlock(SBlock *p);
void visitSWhile(SWhile *p);
void visitSRepeat(SRepeat *p);
void visitSIf(SIf *p);
void visitSIfThenElse(SIfThenElse *p);
void visitSIfThen(SIfThen *p);
void visitSFor(SFor *p);
void visitSForScoped(SForScoped *p);
void visitSReturn(SReturn *p);
void visitExp(Exp *p); /* abstract class */
void visitEAss(EAss *p);
void visitELt(ELt *p);
void visitEGt(EGt *p);
void visitEAdd(EAdd *p);
void visitESub(ESub *p);
void visitEMul(EMul *p);
void visitCall(Call *p);
void visitEVar(EVar *p);
void visitEStr(EStr *p);
void visitEInt(EInt *p);
void visitEDouble(EDouble *p);
void visitListExp(ListExp* p);
void visitType(Type *p); /* abstract class */
void visitTInt(TInt *p);
void visitTDouble(TDouble *p);
void visitInteger(Integer i);
void visitDouble(Double d);
void visitChar(Char c);
void visitString(String s);
void visitIdent(String s);
protected:
void inline bufAppend(const char* s)
{
int len = strlen(s);
while (cur_ + len > buf_size)
{
buf_size *= 2; /* Double the buffer size */
resizeBuffer();
}
for(int n = 0; n < len; n++)
{
buf_[cur_ + n] = s[n];
}
cur_ += len;
buf_[cur_] = 0;
}
void inline bufAppend(const char c)
{
if (cur_ == buf_size)
{
buf_size *= 2; /* Double the buffer size */
resizeBuffer();
}
buf_[cur_] = c;
cur_++;
buf_[cur_] = 0;
}
void inline bufAppend(String str)
{
const char* s = str.c_str();
bufAppend(s);
}
void inline bufReset(void)
{
cur_ = 0;
buf_size = BUFFER_INITIAL;
resizeBuffer();
memset(buf_, 0, buf_size);
}
void inline resizeBuffer(void)
{
char* temp = (char*) malloc(buf_size);
if (!temp)
{
fprintf(stderr, "Error: Out of memory while attempting to grow buffer!\n");
exit(1);
}
if (buf_)
{
strcpy(temp, buf_);
free(buf_);
}
buf_ = temp;
}
char *buf_;
int cur_, buf_size;
};
#endif