diff options
author | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2012-12-22 11:06:55 +0100 |
---|---|---|
committer | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2012-12-22 11:06:55 +0100 |
commit | 4d7205920055139596a07e8a74f7c6e4f79f698f (patch) | |
tree | f54f43afcfe41bd0a84fc9f96c9f48192de1d5e8 | |
parent | 509a416bf9d74cc0bc679dcd9956bc438d142362 (diff) | |
download | sencha-lang-4d7205920055139596a07e8a74f7c6e4f79f698f.tar.gz sencha-lang-4d7205920055139596a07e8a74f7c6e4f79f698f.tar.bz2 sencha-lang-4d7205920055139596a07e8a74f7c6e4f79f698f.tar.xz sencha-lang-4d7205920055139596a07e8a74f7c6e4f79f698f.zip |
Almost everything is ready with visitor pattern and ASTInspector class.
-rw-r--r-- | Sencha-lang/AST/Assignment.cpp | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/BasicStatement.cpp | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/ConstantExpression.cpp | 4 | ||||
-rw-r--r-- | Sencha-lang/AST/DeclarationStatement.cpp | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/IfNode.cpp | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/IfNode.h | 3 | ||||
-rw-r--r-- | Sencha-lang/AST/IncorrectExpression.cpp | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/PostfixExpression.cpp | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/ProgramNode.cpp | 2 | ||||
-rw-r--r-- | Sencha-lang/AST/RepeatStatement.cpp | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/VariableExpression.cpp | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/WhileNode.cpp | 1 | ||||
-rw-r--r-- | Sencha-lang/ASTInspector.cpp | 212 | ||||
-rw-r--r-- | Sencha-lang/ASTInspector.h | 28 | ||||
-rw-r--r-- | Sencha-lang/main.cpp | 1 |
15 files changed, 228 insertions, 31 deletions
diff --git a/Sencha-lang/AST/Assignment.cpp b/Sencha-lang/AST/Assignment.cpp index 20ff698..33f817c 100644 --- a/Sencha-lang/AST/Assignment.cpp +++ b/Sencha-lang/AST/Assignment.cpp @@ -69,6 +69,7 @@ Assignment::Assignment(ASTNode * parent, Context * context) { this->parent = parent; this->context = context; + this->type = "Assignment"; } diff --git a/Sencha-lang/AST/BasicStatement.cpp b/Sencha-lang/AST/BasicStatement.cpp index b8dc1bf..26d1adf 100644 --- a/Sencha-lang/AST/BasicStatement.cpp +++ b/Sencha-lang/AST/BasicStatement.cpp @@ -9,6 +9,7 @@ BasicStatement::BasicStatement(ASTNode * parent) { this->parent = parent; + this->type = "BasicStatement"; } BasicStatement::~BasicStatement() { diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp index 51db3cf..ec8cbd5 100644 --- a/Sencha-lang/AST/ConstantExpression.cpp +++ b/Sencha-lang/AST/ConstantExpression.cpp @@ -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); } diff --git a/Sencha-lang/AST/DeclarationStatement.cpp b/Sencha-lang/AST/DeclarationStatement.cpp index a734cbb..bcfb5c4 100644 --- a/Sencha-lang/AST/DeclarationStatement.cpp +++ b/Sencha-lang/AST/DeclarationStatement.cpp @@ -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() diff --git a/Sencha-lang/AST/IfNode.cpp b/Sencha-lang/AST/IfNode.cpp index ada71af..ba80f3d 100644 --- a/Sencha-lang/AST/IfNode.cpp +++ b/Sencha-lang/AST/IfNode.cpp @@ -11,6 +11,7 @@ IfNode::IfNode(ASTNode * parent) { this->parent = parent; is_else = false; + this->type = "IfNode"; } std::string IfNode::debug() diff --git a/Sencha-lang/AST/IfNode.h b/Sencha-lang/AST/IfNode.h index 9121260..7cc0515 100644 --- a/Sencha-lang/AST/IfNode.h +++ b/Sencha-lang/AST/IfNode.h @@ -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(); }; diff --git a/Sencha-lang/AST/IncorrectExpression.cpp b/Sencha-lang/AST/IncorrectExpression.cpp index 986c1d4..5d1fb71 100644 --- a/Sencha-lang/AST/IncorrectExpression.cpp +++ b/Sencha-lang/AST/IncorrectExpression.cpp @@ -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() { diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp index 7c75def..fb2345e 100644 --- a/Sencha-lang/AST/PostfixExpression.cpp +++ b/Sencha-lang/AST/PostfixExpression.cpp @@ -12,6 +12,7 @@ PostfixExpression::PostfixExpression(ASTNode * parent, Context * context) { this->context = context; name = ""; native = false; + this->type = "PostfixExpression"; } PostfixExpression::~PostfixExpression() { diff --git a/Sencha-lang/AST/ProgramNode.cpp b/Sencha-lang/AST/ProgramNode.cpp index aced5bf..cab01be 100644 --- a/Sencha-lang/AST/ProgramNode.cpp +++ b/Sencha-lang/AST/ProgramNode.cpp @@ -9,7 +9,7 @@ ProgramNode::ProgramNode() { //Do nothing - + this->type = "ProgramNode"; } ProgramNode::~ProgramNode() { diff --git a/Sencha-lang/AST/RepeatStatement.cpp b/Sencha-lang/AST/RepeatStatement.cpp index 3fc96da..404b13a 100644 --- a/Sencha-lang/AST/RepeatStatement.cpp +++ b/Sencha-lang/AST/RepeatStatement.cpp @@ -12,6 +12,7 @@ RepeatStatement::RepeatStatement(ASTNode * parent) this->parent = parent; how_many_times = 0; body = NULL; + this->type = "RepeatStatement"; } RepeatStatement::~RepeatStatement() diff --git a/Sencha-lang/AST/VariableExpression.cpp b/Sencha-lang/AST/VariableExpression.cpp index 93856f0..5ac4a5e 100644 --- a/Sencha-lang/AST/VariableExpression.cpp +++ b/Sencha-lang/AST/VariableExpression.cpp @@ -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() diff --git a/Sencha-lang/AST/WhileNode.cpp b/Sencha-lang/AST/WhileNode.cpp index 84144df..0d10214 100644 --- a/Sencha-lang/AST/WhileNode.cpp +++ b/Sencha-lang/AST/WhileNode.cpp @@ -10,6 +10,7 @@ WhileNode::WhileNode(ASTNode * parent) { body = NULL; this->parent = parent; + this->type = "WhileNode"; } WhileNode::~WhileNode() { diff --git a/Sencha-lang/ASTInspector.cpp b/Sencha-lang/ASTInspector.cpp index b4ab03a..6c92c48 100644 --- a/Sencha-lang/ASTInspector.cpp +++ b/Sencha-lang/ASTInspector.cpp @@ -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,50 +140,175 @@ 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::write_report(std::string visit_notes) + +void ASTInspector::visit(ProgramNode * program) { - std::istringstream stream(visit_notes); - std::string line; - std::string correct_indentation = compute_indent(); + this->occurences["ProgramNode"]++; + std::cout << "Visiting ProgramNode" << std::endl; + depth_level++; + std::string visit_notes = ""; + visit_notes += "ProgramNode:\n"; - while(std::getline(stream, line)) { - inspection_report += correct_indentation + line + "\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(ProgramNode & program) +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(BasicStatement & basic_statement) +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(DeclarationStatement & declaration_statement) +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); + std::string line; + std::string correct_indentation = compute_indent(); + + while(std::getline(stream, line)) { + inspection_report += correct_indentation + line + "\n"; + } + +} + + std::string ASTInspector::compute_indent() { std::string indentation = ""; diff --git a/Sencha-lang/ASTInspector.h b/Sencha-lang/ASTInspector.h index c69d234..88e6a56 100644 --- a/Sencha-lang/ASTInspector.h +++ b/Sencha-lang/ASTInspector.h @@ -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); + + + + + }; diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp index d91ece4..8ae6c0d 100644 --- a/Sencha-lang/main.cpp +++ b/Sencha-lang/main.cpp @@ -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(); |