-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrinter.H
263 lines (247 loc) · 6.16 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
255
256
257
258
259
260
261
262
#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 visitPDefs(PDefs *p);
void visitDfn(Dfn *p); /* abstract class */
void visitDFun(DFun *p);
void visitDmem(Dmem *p); /* abstract class */
void visitDMember(DMember *p);
void visitDef(Def *p); /* abstract class */
void visitDClass(DClass *p);
void visitListDef(ListDef* p);
void visitListDfn(ListDfn* p);
void visitListDmem(ListDmem* p);
void visitArg(Arg *p); /* abstract class */
void visitADecl(ADecl *p);
void visitListArg(ListArg* p);
void visitStm(Stm *p); /* abstract class */
void visitSExp(SExp *p);
void visitSInit(SInit *p);
void visitSMemInit(SMemInit *p);
void visitSReturn(SReturn *p);
void visitSWhile(SWhile *p);
void visitSIfElse(SIfElse *p);
void visitSException(SException *p);
void visitSThrow(SThrow *p);
void visitListStm(ListStm* p);
void visitExp(Exp *p); /* abstract class */
void visitEInt(EInt *p);
void visitEDouble(EDouble *p);
void visitEString(EString *p);
void visitEId(EId *p);
void visitEApp(EApp *p);
void visitSNewClass(SNewClass *p);
void visitSClassMember(SClassMember *p);
void visitSClassMemberVar(SClassMemberVar *p);
void visitETimes(ETimes *p);
void visitEDiv(EDiv *p);
void visitEPlus(EPlus *p);
void visitEMinus(EMinus *p);
void visitELt(ELt *p);
void visitEGt(EGt *p);
void visitEEq(EEq *p);
void visitENEq(ENEq *p);
void visitEAss(EAss *p);
void visitListExp(ListExp* p);
void visitListId(ListId* p);
void visitInteger(Integer i);
void visitDouble(Double d);
void visitChar(Char c);
void visitString(String s);
void visitIdent(String s);
void visitId(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 visitPDefs(PDefs *p);
void visitDfn(Dfn *p); /* abstract class */
void visitDFun(DFun *p);
void visitDmem(Dmem *p); /* abstract class */
void visitDMember(DMember *p);
void visitDef(Def *p); /* abstract class */
void visitDClass(DClass *p);
void visitListDef(ListDef* p);
void visitListDfn(ListDfn* p);
void visitListDmem(ListDmem* p);
void visitArg(Arg *p); /* abstract class */
void visitADecl(ADecl *p);
void visitListArg(ListArg* p);
void visitStm(Stm *p); /* abstract class */
void visitSExp(SExp *p);
void visitSInit(SInit *p);
void visitSMemInit(SMemInit *p);
void visitSReturn(SReturn *p);
void visitSWhile(SWhile *p);
void visitSIfElse(SIfElse *p);
void visitSException(SException *p);
void visitSThrow(SThrow *p);
void visitListStm(ListStm* p);
void visitExp(Exp *p); /* abstract class */
void visitEInt(EInt *p);
void visitEDouble(EDouble *p);
void visitEString(EString *p);
void visitEId(EId *p);
void visitEApp(EApp *p);
void visitSNewClass(SNewClass *p);
void visitSClassMember(SClassMember *p);
void visitSClassMemberVar(SClassMemberVar *p);
void visitETimes(ETimes *p);
void visitEDiv(EDiv *p);
void visitEPlus(EPlus *p);
void visitEMinus(EMinus *p);
void visitELt(ELt *p);
void visitEGt(EGt *p);
void visitEEq(EEq *p);
void visitENEq(ENEq *p);
void visitEAss(EAss *p);
void visitListExp(ListExp* p);
void visitListId(ListId* p);
void visitInteger(Integer i);
void visitDouble(Double d);
void visitChar(Char c);
void visitString(String s);
void visitIdent(String s);
void visitId(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