summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/ASTInspector.cpp
blob: 2040007f1ae91e1e20c885ac432013485c432b31 (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
/*
 * 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
	{
		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";
	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->oper + " on:\n";
	write_report(visit_notes);

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

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

void ASTInspector::visit(PostfixExpression * node)
{

}

void ASTInspector::visit(WhileNode * node)
{

}

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;
}