summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/ASTInspector.cpp
blob: 59d53b7c479784f1c07210bbf767acd4bb514763 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * ASTInspector.cpp
 *
 *  Created on: Dec 17, 2012
 *      Author: att
 */

#include "ASTInspector.h"

ASTInspector::ASTInspector()
	:  number_of_visits(0), depth_level(0)
{

}

ASTInspector::~ASTInspector()
{

}

unsigned int ASTInspector::how_many_occurences_of(std::string type)
{
	return this->occurences[type];
}

void ASTInspector::visit(Visitable * node)
{
	number_of_visits++;
	if(node->type == "ConstantExpression")
	{
		visit(static_cast<ConstantExpression *>(node));
	}
	else if (node->type == "BasicExpression")
	{
		visit(static_cast<BasicExpression *>(node));
	}
	else if (node->type == "UnaryExpression")
	{
		visit(static_cast<UnaryExpression *>(node));
	}
	else if (node->type == "PostfixExpression")
	{
		visit(static_cast<PostfixExpression *>(node));
	}
	else if (node->type == "WhileNode")
	{
		visit(static_cast<WhileNode *>(node));
	}
	else if (node->type == "ProgramNode")
	{
		visit(static_cast<ProgramNode *>(node));
	}
	else if (node->type == "BasicStatement")
	{
		visit(static_cast<BasicStatement *>(node));
	}
	else if (node->type == "DeclarationStatement")
	{
		visit(static_cast<DeclarationStatement *>(node));
	}
	else if (node->type == "ImportStatement")
	{
		visit(static_cast<ImportStatement *>(node));
	}
	else if (node->type == "VariableExpression")
	{
		visit(static_cast<VariableExpression *>(node));
	}
	else if (node->type == "Assignment")
	{
		visit(static_cast<Assignment *>(node));
	}
	else if (node->type == "IfNode")
	{
		visit(static_cast<IfNode *>(node));
	}
	else if (node->type == "RepeatStatement")
	{
		visit(static_cast<RepeatStatement *>(node));
	}
	else if (node->type == "IncorrectExpression")
	{
		visit(static_cast<IncorrectExpression *>(node));
	}

	else
	{
		std::cout << "Visiting unknown node" << std::endl;
	}
}

void ASTInspector::visit(ConstantExpression * constant_expression)
{
	this->occurences["ConstantExpression"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "Constant expression of value:\n";
	visit_notes += constant_expression->value.repr() + "\n";

	visit_notes += "End of ConstantExpression";
	write_report(visit_notes);
	depth_level--;
}

void ASTInspector::forget_everything()
{
	depth_level = 0;
	inspection_report = "";
	occurences.clear();
	number_of_visits = 0;
}

void ASTInspector::visit(BasicExpression * basic_expression)
{
	this->occurences["BasicExpression"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "Basic expression:\n";
	visit_notes += "Executing " + basic_expression->get_operator() + " on:\n";
	write_report(visit_notes);

	basic_expression->children[0]->accept(this);
	basic_expression->children[1]->accept(this);

	write_report("End of BasicExpression\n");
	depth_level--;
}

void ASTInspector::visit(UnaryExpression * unary_expression)
{
	this->occurences["UnaryExpression"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "Unary expression:\n";
	visit_notes += "Executing " + unary_expression->get_operator() + " on:\n";
	write_report(visit_notes);
	unary_expression->children[0]->accept(this);

	write_report("End of UnaryExpression\n");
	depth_level--;
}

void ASTInspector::visit(PostfixExpression * postfix_expression)
{
	this->occurences["PostfixExpression"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "Postfix expression:\n";
	if(postfix_expression->operation == "call")
	{
		visit_notes += postfix_expression->name;
		visit_notes += " operating on arguments:\n";

		write_report(visit_notes);
		for(auto argument : postfix_expression->arguments)
		{
			argument->accept(this);
		}
	}
	else if (postfix_expression->operation == "access")
	{
		visit_notes += "Accessing " + postfix_expression->name;
		visit_notes += "by index:\n";
		write_report(visit_notes);
		postfix_expression->access_index_expr->accept(this);
	}


	write_report("End of postfix expression\n");
	depth_level--;
}


void ASTInspector::visit(WhileNode * while_node)
{
	this->occurences["WhileNode"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "WhileNode:\n";
	write_report(visit_notes);

	for(auto child: while_node->children)
	{
		child->accept(this);
	}

	while_node->body->accept(this);


	write_report("End of while node\n");
	depth_level--;
}


void ASTInspector::visit(ProgramNode * program)
{
	this->occurences["ProgramNode"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "ProgramNode:\n";
	write_report(visit_notes);

	for(auto child: program->children) child->accept(this);
	write_report("End of ProgramNode\n");
	depth_level--;
}

void ASTInspector::visit(BasicStatement * basic_statement)
{
	this->occurences["BasicStatement"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "BasicStatement:\n";

	write_report(visit_notes);

	for(auto child: basic_statement->children)
	{
		child->accept(this);
	}

	write_report("End of BasicStatement\n");
	depth_level--;
}

void ASTInspector::visit(DeclarationStatement * declaration_statement)
{
	this->occurences["DeclarationStatement"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "DeclarationStatement:\n";
	visit_notes += "It declares: " + declaration_statement->name + " to be:\n";
	if(!declaration_statement->is_function )
	{
		visit_notes += declaration_statement->right_value.repr();
		write_report(visit_notes);
	}
	else
	{
		visit_notes += "function";
		write_report(visit_notes);
		declaration_statement->body->accept(this);
	}
	write_report("End of DeclarationStatement\n");
	depth_level--;
}

void ASTInspector::visit(Assignment * assignment)
{
	this->occurences["Assignment"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "Assignment:\n";

	visit_notes += "It assigns to: " + assignment->name + " expression:\n";
	write_report(visit_notes);

	assignment->children[1]->accept(this);

	write_report("End of DeclarationStatement\n");
	depth_level--;
}

void ASTInspector::visit(IfNode * if_node)
{
	this->occurences["IfNode"]++;
		depth_level++;
		std::string visit_notes = "";
		visit_notes += "IfNode:\n";
		visit_notes += "checks condition:";
		write_report(visit_notes);

		if_node->condition()->accept(this);

		write_report("if above is true, it executes:");

		if_node->then_block()->accept(this);
		if(if_node->is_else)
		{
			write_report("if it's not, it executes:");
			if_node->else_block()->accept(this);
		}


		write_report("End of IfNode\n");
		depth_level--;
}

void ASTInspector::visit(IncorrectExpression * incorrect_expression)
{
	this->occurences["IncorrectExpression"]++;
		depth_level++;
		std::string visit_notes = "";
		visit_notes += "IncorrectExpression:\n";

		visit_notes += incorrect_expression->error_message;
		write_report(visit_notes);

		write_report("End of IncorrectExpression\n");
		depth_level--;
}

void ASTInspector::visit(ImportStatement * import)
{
	this->occurences["ImportStatement"]++;
		depth_level++;
		std::string visit_notes = "";
		visit_notes += "ImportStatement:\n";

		visit_notes += "It imports " + import->name_of_module + " and import is:\n";
		if(import->correctly_imported) visit_notes += "success\n";
		else visit_notes += "failure\n";
		visit_notes += "End of ImportStatement";
		write_report(visit_notes);
		depth_level--;
}

void ASTInspector::visit(RepeatStatement * repeat_statement)
{
	this->occurences["RepeatStatement"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "RepeatStatement:\n";

	visit_notes += "It executes " + to_string(repeat_statement->how_many_times_expr->evaluate().integer) + "times\n";
	write_report(visit_notes);
	repeat_statement->body->accept(this);
	write_report("End of RepeatStatement\n");
	depth_level--;
}

void ASTInspector::visit(VariableExpression * variable)
{
	this->occurences["VariableExpression"]++;
	depth_level++;
	std::string visit_notes = "";
	visit_notes += "VariableExpression:\n";

	visit_notes += "Name: " + variable->name + "\n";
	visit_notes += "Currently has a value:\n";
	visit_notes += variable->evaluate().repr();
	write_report(visit_notes);

	write_report("End of VariableExpression\n");
	depth_level--;
}


void ASTInspector::write_report(std::string visit_notes)
{
	std::istringstream stream(visit_notes);
	std::string line;
	std::string correct_indentation = compute_indent();

	while(std::getline(stream, line)) {
		inspection_report += correct_indentation + line + "\n";
	}
}

std::string ASTInspector::compute_indent()
{
	std::string indentation = "";
	std::string basic_indent = "    ";
	for(unsigned int i = 0; i < depth_level; i++)
	{
		indentation += basic_indent;
	}
	return indentation;
}