sencha-lang/Sencha-lang/ASTInspector.cpp

110 lines
2.1 KiB
C++

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