sencha-lang/Sencha-lang/ASTInspector.cpp

154 lines
2.9 KiB
C++
Raw Normal View History

2012-12-17 22:59:16 +00:00
/*
* ASTInspector.cpp
*
* Created on: Dec 17, 2012
* Author: att
*/
#include "ASTInspector.h"
ASTInspector::ASTInspector()
: number_of_visits(0), depth_level(0)
{
}
ASTInspector::~ASTInspector()
{
2012-12-17 22:59:16 +00:00
}
unsigned int ASTInspector::how_many_occurences_of(std::string type)
{
return this->occurences[type];
2012-12-17 22:59:16 +00:00
}
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.get_operator() + " 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 & postfix_expression)
{
this->occurences["PostfixExpression"]++;
depth_level++;
std::string visit_notes = "";
visit_notes += "Postfix expression:\n";
visit_notes += postfix_expression.name;
visit_notes += " operating on arguments:\n";
for(auto argument : postfix_expression.arguments)
{
argument->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";
for(auto child: while_node.children)
{
child->accept(this);
}
write_report("End of while node\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";
}
}
void ASTInspector::visit(ProgramNode & program)
{
}
void ASTInspector::visit(BasicStatement & basic_statement)
{
}
void ASTInspector::visit(DeclarationStatement & declaration_statement)
{
}
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;
}