Almost everything is ready with visitor pattern and ASTInspector class.
parent
509a416bf9
commit
4d72059200
|
@ -69,6 +69,7 @@ Assignment::Assignment(ASTNode * parent, Context * context)
|
|||
{
|
||||
this->parent = parent;
|
||||
this->context = context;
|
||||
this->type = "Assignment";
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
BasicStatement::BasicStatement(ASTNode * parent) {
|
||||
this->parent = parent;
|
||||
this->type = "BasicStatement";
|
||||
}
|
||||
|
||||
BasicStatement::~BasicStatement() {
|
||||
|
|
|
@ -11,6 +11,7 @@ ConstantExpression::ConstantExpression(ASTNode * parent) //Constructor which set
|
|||
{
|
||||
this->parent = parent;
|
||||
value = SenchaObject();
|
||||
this->type = "ConstantExpression";
|
||||
}
|
||||
|
||||
ConstantExpression::~ConstantExpression() {
|
||||
|
@ -46,16 +47,19 @@ void ConstantExpression::accept(Visitor * visitor)
|
|||
ConstantExpression::ConstantExpression(ASTNode * parent, int number)
|
||||
{
|
||||
this->parent = parent; value = SenchaObject(number);
|
||||
this->type = "ConstantExpression";
|
||||
}
|
||||
|
||||
ConstantExpression::ConstantExpression(ASTNode * parent, double number)
|
||||
{
|
||||
this->parent = parent;
|
||||
this->type = "ConstantExpression";
|
||||
value = SenchaObject(number);
|
||||
}
|
||||
|
||||
ConstantExpression::ConstantExpression(ASTNode * parent, std::string text)
|
||||
{
|
||||
this->type = "ConstantExpression";
|
||||
this->parent = parent; value = SenchaObject(text);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ DeclarationStatement::DeclarationStatement(ASTNode * parent, Context * context)
|
|||
body = NULL;
|
||||
right_value = SenchaObject();
|
||||
children.push_back(new ConstantExpression(this));
|
||||
this->type = "DeclarationStatement";
|
||||
}
|
||||
|
||||
std::string DeclarationStatement::debug()
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
IfNode::IfNode(ASTNode * parent) {
|
||||
this->parent = parent;
|
||||
is_else = false;
|
||||
this->type = "IfNode";
|
||||
}
|
||||
|
||||
std::string IfNode::debug()
|
||||
|
|
|
@ -21,6 +21,9 @@ public:
|
|||
virtual std::string debug();
|
||||
virtual void execute();
|
||||
bool evaluate_condition();
|
||||
ASTNode * condition() { return children[0]; }
|
||||
ASTNode * then_block() { return children[1]; }
|
||||
ASTNode * else_block() { return children[2]; }
|
||||
IfNode(ASTNode * parent);
|
||||
virtual ~IfNode();
|
||||
};
|
||||
|
|
|
@ -11,6 +11,7 @@ IncorrectExpression::IncorrectExpression(ASTNode * parent, std::string error_mes
|
|||
{
|
||||
this->parent = parent;
|
||||
this->error_message = error_message;
|
||||
this->type = "IncorrectExpression";
|
||||
}
|
||||
|
||||
IncorrectExpression::~IncorrectExpression() {
|
||||
|
|
|
@ -12,6 +12,7 @@ PostfixExpression::PostfixExpression(ASTNode * parent, Context * context) {
|
|||
this->context = context;
|
||||
name = "";
|
||||
native = false;
|
||||
this->type = "PostfixExpression";
|
||||
}
|
||||
|
||||
PostfixExpression::~PostfixExpression() {
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
ProgramNode::ProgramNode() {
|
||||
//Do nothing
|
||||
|
||||
this->type = "ProgramNode";
|
||||
}
|
||||
|
||||
ProgramNode::~ProgramNode() {
|
||||
|
|
|
@ -12,6 +12,7 @@ RepeatStatement::RepeatStatement(ASTNode * parent)
|
|||
this->parent = parent;
|
||||
how_many_times = 0;
|
||||
body = NULL;
|
||||
this->type = "RepeatStatement";
|
||||
}
|
||||
|
||||
RepeatStatement::~RepeatStatement()
|
||||
|
|
|
@ -12,6 +12,7 @@ VariableExpression::VariableExpression(ASTNode * parent, Context * context, std:
|
|||
this->name = name;
|
||||
this->parent = parent;
|
||||
this->context = context;
|
||||
this->type = "VariableExpression";
|
||||
}
|
||||
|
||||
std::string VariableExpression::debug()
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
WhileNode::WhileNode(ASTNode * parent) {
|
||||
body = NULL;
|
||||
this->parent = parent;
|
||||
this->type = "WhileNode";
|
||||
}
|
||||
|
||||
WhileNode::~WhileNode() {
|
||||
|
|
|
@ -34,20 +34,62 @@ void ASTInspector::visit(Visitable * node)
|
|||
{
|
||||
visit(static_cast<BasicExpression *>(node));
|
||||
}
|
||||
else if (node->type == "PostfixExpression")
|
||||
{
|
||||
visit(static_cast<PostfixExpression *>(node));
|
||||
}
|
||||
else if (node->type == "WhileNode")
|
||||
{
|
||||
visit(static_cast<WhileNode *>(node));
|
||||
}
|
||||
else if (node->type == "ProgramNode")
|
||||
{
|
||||
visit(static_cast<ProgramNode *>(node));
|
||||
}
|
||||
else if (node->type == "BasicStatement")
|
||||
{
|
||||
visit(static_cast<BasicStatement *>(node));
|
||||
}
|
||||
else if (node->type == "DeclarationStatement")
|
||||
{
|
||||
visit(static_cast<DeclarationStatement *>(node));
|
||||
}
|
||||
else if (node->type == "VariableExpression")
|
||||
{
|
||||
visit(static_cast<VariableExpression *>(node));
|
||||
}
|
||||
else if (node->type == "Assignment")
|
||||
{
|
||||
visit(static_cast<Assignment *>(node));
|
||||
}
|
||||
else if (node->type == "IfNode")
|
||||
{
|
||||
visit(static_cast<IfNode *>(node));
|
||||
}
|
||||
else if (node->type == "RepeatStatement")
|
||||
{
|
||||
visit(static_cast<RepeatStatement *>(node));
|
||||
}
|
||||
else if (node->type == "IncorrectExpression")
|
||||
{
|
||||
visit(static_cast<IncorrectExpression *>(node));
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
std::cout << "Visiting unknown node" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ASTInspector::visit(ConstantExpression & constant_expression)
|
||||
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";
|
||||
visit_notes += constant_expression->value.repr() + "\n";
|
||||
|
||||
visit_notes += "End of ConstantExpression";
|
||||
write_report(visit_notes);
|
||||
depth_level--;
|
||||
}
|
||||
|
@ -60,33 +102,34 @@ void ASTInspector::forget_everything()
|
|||
number_of_visits = 0;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(BasicExpression & basic_expression)
|
||||
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";
|
||||
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);
|
||||
basic_expression->children[0]->accept(this);
|
||||
basic_expression->children[1]->accept(this);
|
||||
|
||||
write_report("End of basic expression\n");
|
||||
write_report("End of BasicExpression\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(PostfixExpression & postfix_expression)
|
||||
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 += postfix_expression->name;
|
||||
visit_notes += " operating on arguments:\n";
|
||||
|
||||
for(auto argument : postfix_expression.arguments)
|
||||
write_report(visit_notes);
|
||||
for(auto argument : postfix_expression->arguments)
|
||||
{
|
||||
argument->accept(this);
|
||||
}
|
||||
|
@ -97,23 +140,162 @@ void ASTInspector::visit(PostfixExpression & postfix_expression)
|
|||
}
|
||||
|
||||
|
||||
void ASTInspector::visit(WhileNode & while_node)
|
||||
void ASTInspector::visit(WhileNode * while_node)
|
||||
{
|
||||
this->occurences["WhileNode"]++;
|
||||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "WhileNode:\n";
|
||||
write_report(visit_notes);
|
||||
|
||||
|
||||
for(auto child: while_node.children)
|
||||
for(auto child: while_node->children)
|
||||
{
|
||||
child->accept(this);
|
||||
}
|
||||
|
||||
while_node->body->accept(this);
|
||||
|
||||
|
||||
write_report("End of while node\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
|
||||
void ASTInspector::visit(ProgramNode * program)
|
||||
{
|
||||
this->occurences["ProgramNode"]++;
|
||||
std::cout << "Visiting ProgramNode" << std::endl;
|
||||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "ProgramNode:\n";
|
||||
|
||||
write_report(visit_notes);
|
||||
for(auto child: program->children)
|
||||
{
|
||||
child->accept(this);
|
||||
}
|
||||
|
||||
write_report("End of ProgramNode\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(BasicStatement * basic_statement)
|
||||
{
|
||||
this->occurences["BasicStatement"]++;
|
||||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "BasicStatement:\n";
|
||||
|
||||
write_report(visit_notes);
|
||||
|
||||
for(auto child: basic_statement->children)
|
||||
{
|
||||
child->accept(this);
|
||||
}
|
||||
|
||||
write_report("End of BasicStatement\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(DeclarationStatement * declaration_statement)
|
||||
{
|
||||
this->occurences["DeclarationStatement"]++;
|
||||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "DeclarationStatement:\n";
|
||||
|
||||
visit_notes += "It declares: " + declaration_statement->name + "to be:\n";
|
||||
visit_notes += declaration_statement->right_value.repr();
|
||||
|
||||
write_report(visit_notes);
|
||||
write_report("End of DeclarationStatement\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(Assignment * assignment)
|
||||
{
|
||||
this->occurences["Assignment"]++;
|
||||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "Assignment:\n";
|
||||
|
||||
visit_notes += "It assigns to: " + assignment->name + " expression:\n";
|
||||
write_report(visit_notes);
|
||||
|
||||
assignment->children[1]->accept(this);
|
||||
|
||||
write_report("End of DeclarationStatement\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(IfNode * if_node)
|
||||
{
|
||||
this->occurences["IfNode"]++;
|
||||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "IfNode:\n";
|
||||
visit_notes += "checks condition:";
|
||||
write_report(visit_notes);
|
||||
|
||||
if_node->condition()->accept(this);
|
||||
|
||||
write_report("if above is true, it executes:");
|
||||
|
||||
if_node->then_block()->accept(this);
|
||||
if(if_node->is_else)
|
||||
{
|
||||
write_report("if it's not, it executes:");
|
||||
if_node->else_block()->accept(this);
|
||||
}
|
||||
|
||||
|
||||
write_report("End of IfNode\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(IncorrectExpression * incorrect_expression)
|
||||
{
|
||||
this->occurences["IncorrectExpression"]++;
|
||||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "IncorrectExpression:\n";
|
||||
|
||||
visit_notes += incorrect_expression->error_message;
|
||||
write_report(visit_notes);
|
||||
|
||||
write_report("End of IncorrectExpression\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(RepeatStatement * repeat_statement)
|
||||
{
|
||||
this->occurences["RepeatStatement"]++;
|
||||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "RepeatStatement:\n";
|
||||
|
||||
visit_notes += "It executes " + to_string(repeat_statement->how_many_times) + "times\n";
|
||||
write_report(visit_notes);
|
||||
repeat_statement->body->accept(this);
|
||||
write_report("End of RepeatStatement\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(VariableExpression * variable)
|
||||
{
|
||||
this->occurences["VariableExpression"]++;
|
||||
depth_level++;
|
||||
std::string visit_notes = "";
|
||||
visit_notes += "VariableExpression:\n";
|
||||
|
||||
visit_notes += "Name: " + variable->name + "\n";
|
||||
write_report(visit_notes);
|
||||
|
||||
write_report("End of VariableExpression\n");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
|
||||
void ASTInspector::write_report(std::string visit_notes)
|
||||
{
|
||||
std::istringstream stream(visit_notes);
|
||||
|
@ -126,20 +308,6 @@ void ASTInspector::write_report(std::string visit_notes)
|
|||
|
||||
}
|
||||
|
||||
void ASTInspector::visit(ProgramNode & program)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ASTInspector::visit(BasicStatement & basic_statement)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ASTInspector::visit(DeclarationStatement & declaration_statement)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string ASTInspector::compute_indent()
|
||||
{
|
||||
|
|
|
@ -18,9 +18,11 @@ public:
|
|||
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();
|
||||
void visit(Visitable * node);
|
||||
void visit(ProgramNode * node);
|
||||
|
||||
private:
|
||||
unsigned int number_of_visits;
|
||||
|
@ -30,13 +32,23 @@ private:
|
|||
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);
|
||||
void visit(ProgramNode & node);
|
||||
void visit(BasicStatement & node);
|
||||
void visit(DeclarationStatement & node);
|
||||
void visit(ConstantExpression * node);
|
||||
void visit(BasicExpression * node);
|
||||
void visit(PostfixExpression * node);
|
||||
void visit(WhileNode * node);
|
||||
|
||||
void visit(BasicStatement * node);
|
||||
void visit(DeclarationStatement * node);
|
||||
void visit(Assignment * node);
|
||||
void visit(IfNode * node);
|
||||
void visit(IncorrectExpression * node);
|
||||
void visit(RepeatStatement * node);
|
||||
void visit(VariableExpression * node);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -159,6 +159,7 @@ void interactive()
|
|||
//cout << "My tree:\n";
|
||||
//cout << parser.program->debug();
|
||||
inspector.visit(parser.program);
|
||||
|
||||
//cout << parser.context->debug();
|
||||
cout << inspector.get_report();
|
||||
inspector.forget_everything();
|
||||
|
|
Loading…
Reference in New Issue