summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-21 16:16:39 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-21 16:16:39 +0100
commit7ac6ae1c224f8ba04e6769f8dbf6fba6881bcaf7 (patch)
tree6d3ee26bae7dc308d807233f8c5cf8443855833b
parent736458f1fd5508b48602feae215749d2db73ebc5 (diff)
downloadsencha-lang-7ac6ae1c224f8ba04e6769f8dbf6fba6881bcaf7.tar.gz
sencha-lang-7ac6ae1c224f8ba04e6769f8dbf6fba6881bcaf7.tar.bz2
sencha-lang-7ac6ae1c224f8ba04e6769f8dbf6fba6881bcaf7.tar.xz
sencha-lang-7ac6ae1c224f8ba04e6769f8dbf6fba6881bcaf7.zip
Inspector can inspect ASTElements, as for now, only basic expressions
and constant expressions :).
-rw-r--r--Sencha-lang/AST/BasicExpression.cpp4
-rw-r--r--Sencha-lang/AST/ConstantExpression.cpp4
-rw-r--r--Sencha-lang/ASTInspector.cpp44
-rw-r--r--Sencha-lang/ASTInspector.h20
-rw-r--r--Sencha-lang/Tests/TestASTInspector.cpp45
-rw-r--r--Sencha-lang/Tests/TestASTInspector.h3
-rw-r--r--Sencha-lang/Visitor.cpp2
-rw-r--r--Sencha-lang/Visitor.h3
8 files changed, 95 insertions, 30 deletions
diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp
index d5bf0f3..3787a72 100644
--- a/Sencha-lang/AST/BasicExpression.cpp
+++ b/Sencha-lang/AST/BasicExpression.cpp
@@ -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() {
diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp
index 3e2ba71..51db3cf 100644
--- a/Sencha-lang/AST/ConstantExpression.cpp
+++ b/Sencha-lang/AST/ConstantExpression.cpp
@@ -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()
diff --git a/Sencha-lang/ASTInspector.cpp b/Sencha-lang/ASTInspector.cpp
index d867eab..2040007 100644
--- a/Sencha-lang/ASTInspector.cpp
+++ b/Sencha-lang/ASTInspector.cpp
@@ -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()
+{
+
}
-ASTInspector::~ASTInspector() {
- // TODO Auto-generated destructor stub
+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;
}
diff --git a/Sencha-lang/ASTInspector.h b/Sencha-lang/ASTInspector.h
index c11db14..92052fd 100644
--- a/Sencha-lang/ASTInspector.h
+++ b/Sencha-lang/ASTInspector.h
@@ -15,24 +15,26 @@
class ASTInspector: public Visitor {
public:
ASTInspector();
- typedef unsigned int NumberOfNodes;
+ 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;
-
- void visit(Visitable * node);
+ 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_ */
diff --git a/Sencha-lang/Tests/TestASTInspector.cpp b/Sencha-lang/Tests/TestASTInspector.cpp
index 0d4c9d1..9469389 100644
--- a/Sencha-lang/Tests/TestASTInspector.cpp
+++ b/Sencha-lang/Tests/TestASTInspector.cpp
@@ -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;
+}
diff --git a/Sencha-lang/Tests/TestASTInspector.h b/Sencha-lang/Tests/TestASTInspector.h
index a9439ff..4720cc1 100644
--- a/Sencha-lang/Tests/TestASTInspector.h
+++ b/Sencha-lang/Tests/TestASTInspector.h
@@ -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;
};
diff --git a/Sencha-lang/Visitor.cpp b/Sencha-lang/Visitor.cpp
index 4f85116..bbb1f18 100644
--- a/Sencha-lang/Visitor.cpp
+++ b/Sencha-lang/Visitor.cpp
@@ -16,7 +16,7 @@ Visitor::~Visitor() {
// TODO Auto-generated destructor stub
}
-Visitable::Visitable()
+Visitable::Visitable() : type("unknown")
{
}
diff --git a/Sencha-lang/Visitor.h b/Sencha-lang/Visitor.h
index 52d9b05..3f365a6 100644
--- a/Sencha-lang/Visitor.h
+++ b/Sencha-lang/Visitor.h
@@ -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();