-
Notifications
You must be signed in to change notification settings - Fork 0
/
AST.c
218 lines (169 loc) · 3.78 KB
/
AST.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
#include "AST.h"
//////////////////////////////////
// I/O Interface
ostream&
operator <<(ostream &o, const ASTExpression &a)
{
// Concatenates an ASTExpression object to an ostream.
//
// This works by delegating the job back to the ASTExpression node.
a.ASTPrint(o);
return o;
}
//////////////////////////////////
// ASTExpression methods
int
ASTExpression::getline()
{
return linenum;
}
int
ASTExpression::getchar()
{
return charnum;
}
//////////////////////////////////
// ASTInteger methods
ASTInteger::ASTInteger(int i, int line_init, int char_init)
:ASTExpression(line_init,char_init),value(i)
{
// The ASTInteger constructor uses the superclass initializer
// to initialize its superclass linenum and charnum. It
// also initializes value from its argument.
// No other code is necessary.
value = i;
}
void
ASTInteger::ASTPrint(ostream &o) const {
// To print an integer ASTExpression, print the integer
// value associated with it.
o << value;
}
ASTString::ASTString(string i, int line_init, int char_init)
:ASTExpression(line_init,char_init),value(i)
{
// The ASTInteger constructor uses the superclass initializer
// to initialize its superclass linenum and charnum. It
// also initializes value from its argument.
// No other code is necessary.
value = i;
}
void
ASTString::ASTPrint(ostream &o) const {
// To print an integer ASTExpression, print the integer
// value associated with it.
o << value;
}
//GarnetPtr
//ASTInteger::eval() const
//{
// // To evaluate an integer, make a garnet integer object.
// // An integer is a primitive, so I call the make_prim
// return make_object_from_prim(value);
//}
void ASTFloat::ASTPrint(ostream &o) const {
o << value;
}
void ASTIdentifier::ASTPrint(ostream &o) const {
o << id;
}
void ASTExpressionListSimple::ASTPrint(ostream &o) const
{
o << "(" << *expr << ")";
}
void ASTExpressionListCompound::ASTPrint(ostream &o) const
{
o << "(" << *expr_list << "; \n" << *expr << ")";
}
//////////////////////////////////
// ASTArgList methods
ASTExpression *
ASTArgList::head() {
cerr << "Don't know how to take the head of this arglist" << endl;
abort();
}
ASTExpression *
ASTArgList::tail() {
cerr << "Don't know how to take the tail of this arglist" << endl;
abort();
}
//////////////////////////////////
// ASTArgListSimple methods
ASTExpression *
ASTArgListSimple::head()
{
return expr;
}
void
ASTArgListSimple::ASTPrint(ostream &o) const
{
o << *expr;
}
//////////////////////////////////
// ASTArgListCompound methods
ASTExpression *
ASTArgListCompound::head()
{
return list_head;
}
ASTExpression *
ASTArgListCompound::tail()
{
return list_tail;
}
void ASTArgListCompound::ASTPrint(ostream &o) const
{
o << *list_head<< ", " << *list_tail;
}
void ASTArgListEmpty::ASTPrint(ostream &o) const
{
// define appropriately
}
void
ASTMethodCall::ASTPrint(ostream &o) const
{
o << methodName << "(" << *arglist << ")";
}
GarnetPtr ASTEval()
{
//evaluate the GarnetPtr in question
}
GarnetPtr ASTInteger::ASTEval()
{
return make_object_from_prim(value);
}
void
ASTWhile::ASTPrint(ostream &o) const
{
o << "While " << *condition << " " << " do \n" << *body << "\n end" ;
}
void
ASTFor::ASTPrint(ostream &o) const
{
o << "for " << *condition << " in "<< *arrayExp << " do \n" << *body << "\n end" ;
}
void
ASTLoop::ASTPrint(ostream &o) const
{
o << "loop " << "{ \n" << *body << "\n }" ;
}
void
ASTIf::ASTPrint(ostream &o) const
{
o << "if(" << *condition << ") do" << "\n" << *body << "\n end" << *hacky ;
}
void
ASTelsif::ASTPrint(ostream &o) const
{
o << "elsif(" << *condition << ") then" << "{ \n" << *body << "\n}" << *hacky;
}
void
ASTelse::ASTPrint(ostream &o) const
{
o << "else" << "{ \n" << *body << "\n}" ;
}
void
ASTIfNo::ASTPrint(ostream &o) const
{
o << "end" ;
}