More visiting methods. Gonna catch them all!
parent
88ccccc4fe
commit
509a416bf9
|
@ -19,7 +19,6 @@ public:
|
|||
virtual void execute();
|
||||
virtual void execute_quietly();
|
||||
std::string get_operator() { return oper; }
|
||||
|
||||
std::string debug() ;
|
||||
virtual void accept(Visitor * visitor);
|
||||
|
||||
|
|
|
@ -40,13 +40,14 @@ void ASTInspector::visit(Visitable * node)
|
|||
}
|
||||
}
|
||||
|
||||
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";
|
||||
|
||||
write_report(visit_notes);
|
||||
depth_level--;
|
||||
}
|
||||
|
@ -59,30 +60,58 @@ 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->oper + " 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");
|
||||
depth_level--;
|
||||
}
|
||||
|
||||
void ASTInspector::visit(PostfixExpression * node)
|
||||
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 * node)
|
||||
{
|
||||
|
||||
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)
|
||||
|
@ -97,6 +126,21 @@ 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()
|
||||
{
|
||||
std::string indentation = "";
|
||||
|
|
|
@ -30,10 +30,13 @@ 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(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);
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -349,8 +349,6 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
|
|||
|
||||
ASTExpression * Parser::postfix_expr(ASTNode * expression)
|
||||
{
|
||||
//TODO implement postfix expression ASAP
|
||||
|
||||
auto name = tok_value;
|
||||
if(is_function_name())
|
||||
{
|
||||
|
@ -395,7 +393,7 @@ ASTExpression * Parser::mul_expr(ASTNode * expression)
|
|||
be->set_right_operand(mul_expr(be));
|
||||
}
|
||||
|
||||
if(be->oper == "")
|
||||
if(be->get_operator() == "")
|
||||
{
|
||||
ASTExpression * ae;
|
||||
ae = static_cast<ASTExpression *>(be->children[0]);
|
||||
|
@ -426,7 +424,7 @@ ASTExpression * Parser::add_expr(ASTNode * expression)
|
|||
be->set_right_operand(add_expr(be));
|
||||
}
|
||||
|
||||
if(be->oper == "")
|
||||
if(be->get_operator() == "")
|
||||
{
|
||||
ASTExpression * ae;
|
||||
ae = static_cast<ASTExpression *>(be->children[0]);
|
||||
|
@ -457,7 +455,7 @@ ASTExpression * Parser::rel_expr(ASTNode * expression)
|
|||
be->set_right_operand(rel_expr(be));
|
||||
}
|
||||
|
||||
if(be->oper == "")
|
||||
if(be->get_operator() == "")
|
||||
{
|
||||
ASTExpression * ae;
|
||||
ae = static_cast<ASTExpression *>(be->children[0]);
|
||||
|
@ -489,7 +487,7 @@ ASTExpression * Parser::eq_expr(ASTNode * expression)
|
|||
be->set_right_operand(eq_expr(be));
|
||||
}
|
||||
|
||||
if(be->oper == "")
|
||||
if(be->get_operator() == "")
|
||||
{
|
||||
ASTExpression * ae;
|
||||
ae = static_cast<ASTExpression *>(be->children[0]);
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "Parser.h"
|
||||
#include "Tests/TestLexer.h"
|
||||
#include "Context.h"
|
||||
#include "ASTInspector.h"
|
||||
#include "Tests/tests.h"
|
||||
using namespace std;
|
||||
|
||||
|
@ -131,7 +132,7 @@ void interactive()
|
|||
context.register_function("tan", s_tan);
|
||||
|
||||
Parser parser(&context);
|
||||
|
||||
ASTInspector inspector;
|
||||
vector<Token> tokens;
|
||||
|
||||
string input;
|
||||
|
@ -157,7 +158,10 @@ void interactive()
|
|||
//cout << parser.show_tokens() << endl;
|
||||
//cout << "My tree:\n";
|
||||
//cout << parser.program->debug();
|
||||
cout << parser.context->debug();
|
||||
inspector.visit(parser.program);
|
||||
//cout << parser.context->debug();
|
||||
cout << inspector.get_report();
|
||||
inspector.forget_everything();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -188,6 +192,7 @@ int main(int argc, char *argv[])
|
|||
context.register_function("tan", s_tan);
|
||||
|
||||
Parser parser(&context);
|
||||
|
||||
vector<Token> tokens;
|
||||
string line;
|
||||
ifstream input_file (name);
|
||||
|
|
Loading…
Reference in New Issue