Inspector can inspect ASTElements, as for now, only basic expressions

and constant expressions :).
functions
Justyna Ilczuk 2012-12-21 16:16:39 +01:00
parent 736458f1fd
commit 7ac6ae1c22
8 changed files with 98 additions and 33 deletions

View File

@ -93,9 +93,9 @@ SenchaObject BasicExpression::evaluate()
return so;
}
BasicExpression::BasicExpression(ASTNode * parent) {
BasicExpression::BasicExpression(ASTNode * parent) : children_set(false){
this->parent = parent;
children_set = false;
this->type= "BasicExpression";
}
BasicExpression::~BasicExpression() {

View File

@ -17,10 +17,10 @@ ConstantExpression::~ConstantExpression() {
}
ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value)
ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value) : value(value)
{
this->parent = parent;
this->value = value;
this->type = "ConstantExpression";
}
SenchaObject ConstantExpression::evaluate()

View File

@ -7,22 +7,42 @@
#include "ASTInspector.h"
ASTInspector::ASTInspector() {
// TODO Auto-generated constructor stub
depth_level = 0;
ASTInspector::ASTInspector()
: number_of_visits(0), depth_level(0)
{
}
ASTInspector::~ASTInspector() {
// TODO Auto-generated destructor stub
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";
@ -31,9 +51,17 @@ void ASTInspector::visit(ConstantExpression * constant_expression)
depth_level--;
}
void ASTInspector::forget_everything()
{
depth_level = 0;
inspection_report = "";
occurences.clear();
number_of_visits = 0;
}
void ASTInspector::visit(BasicExpression * basic_expression)
{
std::cout << "Inspector visits basic expression!" << std::endl;
this->occurences["BasicExpression"]++;
depth_level++;
std::string visit_notes = "";
visit_notes += "Basic expression:\n";
@ -73,7 +101,7 @@ std::string ASTInspector::compute_indent()
{
std::string indentation = "";
std::string basic_indent = " ";
for(int i = 0; i < depth_level; i++)
for(unsigned int i = 0; i < depth_level; i++)
{
indentation += basic_indent;
}

View File

@ -15,24 +15,26 @@
class ASTInspector: public Visitor {
public:
ASTInspector();
typedef unsigned int NumberOfNodes;
std::map<std::string, NumberOfNodes> occurences;
std::string inspection_report;
unsigned int depth_level;
unsigned int how_many_visits() { return number_of_visits; }
unsigned int how_many_occurences_of(std::string type);
std::string get_report() { return this->inspection_report; }
void visit(Visitable * node);
virtual ~ASTInspector();
void forget_everything();
private:
unsigned int number_of_visits;
typedef unsigned int NumberOfNodes;
std::map<std::string, NumberOfNodes> occurences;
std::string inspection_report;
unsigned int depth_level;
std::string compute_indent();
void write_report(std::string visit_notes);
void visit(ConstantExpression * node);
void visit(BasicExpression * node);
void visit(PostfixExpression * node);
void visit(WhileNode * node);
std::string compute_indent();
void write_report(std::string visit_notes);
virtual ~ASTInspector();
};
#endif /* ASTINSPECTOR_H_ */

View File

@ -18,16 +18,40 @@ TestASTInspector::~TestASTInspector() {
std::string TestASTInspector::test_inspecting_basic_expression()
{
std::string test_report = "";
BasicExpression * be = build_basic_expression("+", SenchaObject(9), SenchaObject(122));
be->accept(&inspector);
std::string assert_report = "Report: " + inspector.inspection_report;
muu_assert("dd", inspector.inspection_report == "");
std::cout << assert_report << " reports..." << std::endl;
std::string assert_report = "Report:\n" + inspector.get_report();
muu_assert("ASTInspector didn\'t write any report", inspector.get_report() != "");
muu_assert("Some of nodes were missed in visit", inspector.how_many_visits() == 3);
muu_assert("Some of nodes were missed in visit", inspector.how_many_occurences_of("BasicExpression") == 1);
muu_assert("Some of nodes were missed in visit", inspector.how_many_occurences_of("ConstantExpression") == 2);
delete be;
inspector.forget_everything();
return test_report;
}
std::string TestASTInspector::test_inspecting_simple_AST()
{
std::string test_report = "";
auto left = build_basic_expression("-", SenchaObject(88), SenchaObject(-19));
auto right = build_basic_expression("*", SenchaObject(9), SenchaObject(12));
BasicExpression * be = build_simple_AST("+", left, right);
be->accept(&inspector);
std::string assert_report = "Report:\n" + inspector.get_report();
muu_assert("ASTInspector didn\'t write any report", inspector.get_report() != "");
muu_assert("Some of nodes were missed in visit", inspector.how_many_visits() == 7);
muu_assert("Some of nodes were missed in visit", inspector.how_many_occurences_of("BasicExpression") == 3);
muu_assert("Some of nodes were missed in visit", inspector.how_many_occurences_of("ConstantExpression") == 4);
delete be;
inspector.forget_everything();
return test_report;
@ -38,7 +62,7 @@ std::string TestASTInspector::all_tests()
std::string test_report = "";
mu_run_test(test_inspecting_basic_expression);
mu_run_test(test_inspecting_simple_AST);
return test_report;
}
@ -50,3 +74,12 @@ BasicExpression * TestASTInspector::build_basic_expression(std::string oper, Sen
be->set_right_operand(new ConstantExpression(be, arg2));
return be;
}
BasicExpression * TestASTInspector::build_simple_AST(std::string oper, BasicExpression * left, BasicExpression * right)
{
BasicExpression * be = new BasicExpression(NULL);
be->set_operator(oper);
be->set_left_operand(left);
be->set_right_operand(right);
return be;
}

View File

@ -20,10 +20,11 @@ public:
virtual ~TestASTInspector();
std::string test_inspecting_basic_expression();
std::string test_inspecting_simple_AST();
virtual std::string all_tests();
private:
BasicExpression * build_basic_expression(std::string oper, SenchaObject arg1, SenchaObject arg2);
BasicExpression * build_simple_AST(std::string oper, BasicExpression * left, BasicExpression * right);
ASTInspector inspector;
};

View File

@ -16,7 +16,7 @@ Visitor::~Visitor() {
// TODO Auto-generated destructor stub
}
Visitable::Visitable()
Visitable::Visitable() : type("unknown")
{
}

View File

@ -7,7 +7,7 @@
#ifndef VISITOR_H_
#define VISITOR_H_
#include <string>
class Visitable;
@ -21,6 +21,7 @@ public:
class Visitable
{
public:
std::string type;
Visitable();
virtual void accept(Visitor * visitor) = 0;
virtual ~Visitable();