-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.cpp
281 lines (243 loc) · 6.26 KB
/
ast.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// definitions for classes in ast.h
#include "ast.h"
// returns the token literal
std::string Node::tokenLiteral(){
return token.literal;
}
// default constructor for Program
Program::Program(){
statements = std::vector<Statement*>();
}
// destructor for Program
Program::~Program(){
for(Statement* stmt : statements){
delete stmt;
}
}
// Overriding tokenLiteral from Node
std::string Program::tokenLiteral() {
if(statements.size() > 0)
return statements[0]->tokenLiteral();
else
return "";
}
// Overriding toString from Node for printing
std::string Program::toString(){
std::string result = "";
for(Statement* stmt : statements){
result += stmt->toString();
}
return result;
}
// default constructor
LetStatement::LetStatement(){
name = nullptr;
}
// Setting the token constructor
LetStatement::LetStatement(Token token){
this->token = token;
}
// Overriding toString from Node for printing
std::string LetStatement::toString(){
std::string output = "";
output += tokenLiteral() + " ";
output += name->toString()+ " = ";
if(expressionValue != nullptr){
output += expressionValue->toString();
}
output += ";";
return output;
}
// default constructor for Identifier
Identifier::Identifier(Token token, std::string value){
this->token = token;
this->value = value;
}
// Overriding toString from Node for printing
std::string Identifier::toString() {
return value;
}
// constructor with token
ReturnStatement::ReturnStatement(Token token){
this->token = token;
}
// Overriding toString from Node for printing
std::string ReturnStatement::toString(){
std::string output = "";
output += tokenLiteral() + " ";
if(expressionValue != nullptr){
output += expressionValue->toString();
}
output += ";";
return output;
}
// default constructor
ExpressionStatement::ExpressionStatement(Token token){
this->token = token;
}
// constructor with expression
ExpressionStatement::ExpressionStatement(Expression* expression){
this->expressionValue = expression;
}
// Overriding toString from Node for printing
std::string ExpressionStatement::toString() {
if(expressionValue != nullptr){
return expressionValue->toString();
}
else
return "";
}
// Token constructor
IntegerLiteral::IntegerLiteral(Token token){
this->token = token;
}
// Overriding toString from Node for printing
std::string IntegerLiteral::toString() {
return token.literal;
}
// toString override
std::string PrefixExpression::toString() {
std::string output = "";
output += "(" + op + " ";
output += right->toString();
output += ")";
return output;
}
// default constructor
PrefixExpression::PrefixExpression(Token token, std::string op){
this->token = token;
this->op = op;
}
// prefix destructor
PrefixExpression::~PrefixExpression(){
if(right)
delete right;
}
// constructor with token, operator, and left expression
InfixExpression::InfixExpression(Token token, std::string op, Expression* left){
this->token = token;
this->op = op;
this->left = left;
}
// infix destructor
InfixExpression::~InfixExpression(){
if(left)
delete left;
if(right)
delete right;
}
// toString override
std::string InfixExpression::toString(){
std::string output = "";
output += "(";
output += left->toString();
output += " " + op + " ";
output += right->toString();
output += ")";
return output;
}
//constructor
Boolean::Boolean(Token token, bool value){
this->token = token;
this->value = value;
}
std::string Boolean::toString() {
return token.literal;
}
// destructor
IfExpression::~IfExpression(){
if(condition)
delete condition;
if(consequence)
delete consequence;
if(alternative)
delete alternative;
}
// prints out if as a string
std::string IfExpression::toString() {
std::string output = "if";
output += condition->toString();
output += " ";
output += consequence->toString();
if(alternative != nullptr){
output += " else " + alternative->toString();
}
return output;
}
// destructor deletes all the Statement* within the block statement
BlockStatement::~BlockStatement(){
for(Statement* stmt: statements){
delete stmt;
}
}
// turns Block statment into a string
std::string BlockStatement::toString(){
std::string output = "";
for(Statement* s : statements){
output += s->toString();
}
return output;
}
// returns a string of the function literal properly formated
std::string FunctionLiteral::toString(){
std::string output = "";
output += tokenLiteral();
output += "(";
for(size_t i = 0; i < parameters.size(); i++){
output += parameters[i]->toString() ;
if(i+1<parameters.size())
output += ", ";
}
output += ")";
output += body->toString();
return output;
}
// to string for printing
std::string CallExpression::toString(){
std::string output = "";
output += function->toString() + "(";
for(size_t i = 0; i < arguments.size(); i++){
output += arguments[i]->toString();
if(i+1 < arguments.size())
output += ", ";
}
output += ")";
return output;
}
// Overriding toString from Node for printing
std::string StringLiteral::toString() {
return token.literal;
}
// Overriding toString from Node for printing
std::string ArrayLiteral::toString() {
std::string output = "[";
for(size_t i = 0; i < elements.size(); i++){
output += elements[i]->toString();
if(i+1 < elements.size())
output += ", ";
}
output += "]";
return output;
}
// to string for printing
std::string IndexExpression::toString(){
std::string output = "(";
output += left->toString();
output += "[";
output += index->toString();
output += "])";
return output;
}
// to string for printing
std::string HashLiteral::toString(){
std::string output = "{";
for(auto it = pairs.begin(); it != pairs.end(); it++){
output += it->first->toString() + ":" + it->second->toString();
auto ahead = it;
ahead++;
if(ahead != pairs.end())
output += ", ";
}
output += "}";
return output;
}