From 93a63d43e950c53230c1658e86111ee962faf7f9 Mon Sep 17 00:00:00 2001 From: Justyna Ilczuk Date: Fri, 28 Dec 2012 17:39:43 +0100 Subject: [PATCH] Silly mistake, and lots of fun, heh :P --- Sencha-lang/AST/PostfixExpression.cpp | 9 +-- Sencha-lang/AST/PostfixExpression.h | 2 +- Sencha-lang/AST/WhileNode.cpp | 18 ++---- Sencha-lang/AST/WhileNode.h | 4 +- Sencha-lang/ASTInspector.cpp | 2 - Sencha-lang/Parser.cpp | 89 ++++++++------------------ Sencha-lang/Tests/TestASTInspector.cpp | 24 +++---- Sencha-lang/Tests/TestLexer.h | 5 +- Sencha-lang/Tests/TestParser.cpp | 3 +- Sencha-lang/Tests/minunit.h | 4 ++ Sencha-lang/Tests/tests.h | 49 ++------------ 11 files changed, 57 insertions(+), 152 deletions(-) diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp index 737b287..7ca535f 100644 --- a/Sencha-lang/AST/PostfixExpression.cpp +++ b/Sencha-lang/AST/PostfixExpression.cpp @@ -8,9 +8,9 @@ #include "PostfixExpression.h" PostfixExpression::PostfixExpression(std::string name, Context * context): - context(context), name(name), native(false), type("PostfixExpression") + name(name), context(context), native(false) { - + type= "PostfixExpression"; } PostfixExpression::~PostfixExpression() { @@ -22,11 +22,6 @@ PostfixExpression::~PostfixExpression() { } -void PostfixExpression::set_name(std::string name) -{ - this->name = name; -} - void PostfixExpression::add_argument(ASTExpression * expression) { arguments.push_back(expression); diff --git a/Sencha-lang/AST/PostfixExpression.h b/Sencha-lang/AST/PostfixExpression.h index 4677a56..42b8fbe 100644 --- a/Sencha-lang/AST/PostfixExpression.h +++ b/Sencha-lang/AST/PostfixExpression.h @@ -18,8 +18,8 @@ class PostfixExpression : public ASTExpression { public: std::string name; - bool native; Context * context; + bool native; std::vector arguments; void add_argument(ASTExpression * expression); diff --git a/Sencha-lang/AST/WhileNode.cpp b/Sencha-lang/AST/WhileNode.cpp index 11ebba8..0865555 100644 --- a/Sencha-lang/AST/WhileNode.cpp +++ b/Sencha-lang/AST/WhileNode.cpp @@ -7,9 +7,11 @@ #include "WhileNode.h" -WhileNode::WhileNode() { - body = NULL; - this->type = "WhileNode"; +WhileNode::WhileNode(ASTExpression * condition, ASTStatement * body): + body(body) +{ + type = "WhileNode"; + children.push_back(condition); } WhileNode::~WhileNode() { @@ -21,16 +23,6 @@ WhileNode::~WhileNode() { } } -void WhileNode::add_condition(ASTExpression * expression) -{ - children.push_back(expression); -} - -void WhileNode::add_body(ASTStatement * statement) -{ - body = statement; -} - void WhileNode::execute() { diff --git a/Sencha-lang/AST/WhileNode.h b/Sencha-lang/AST/WhileNode.h index bc41139..8d672b1 100644 --- a/Sencha-lang/AST/WhileNode.h +++ b/Sencha-lang/AST/WhileNode.h @@ -17,11 +17,9 @@ */ class WhileNode: public ASTStatement { public: - WhileNode(); + WhileNode(ASTExpression * condition, ASTStatement * body); virtual ~WhileNode(); ASTStatement * body; - void add_condition(ASTExpression * expression); - void add_body(ASTStatement * statement); virtual void execute(); bool evaluate_condition(); diff --git a/Sencha-lang/ASTInspector.cpp b/Sencha-lang/ASTInspector.cpp index 5dd32da..834e0a5 100644 --- a/Sencha-lang/ASTInspector.cpp +++ b/Sencha-lang/ASTInspector.cpp @@ -307,10 +307,8 @@ void ASTInspector::write_report(std::string visit_notes) while(std::getline(stream, line)) { inspection_report += correct_indentation + line + "\n"; } - } - std::string ASTInspector::compute_indent() { std::string indentation = ""; diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp index ca52866..9a862fe 100644 --- a/Sencha-lang/Parser.cpp +++ b/Sencha-lang/Parser.cpp @@ -69,16 +69,10 @@ bool Parser::read_next() void Parser::interpret() { read_next(); - while(tok_value!= "") - { - program->add_statement(statement()); - } + while(tok_value!= "") program->add_statement(statement()); } -bool Parser::peek(string s) -{ - return tok_value == s; -} +bool Parser::peek(string s) {return tok_value == s;} bool Parser::accept(string s) { @@ -110,10 +104,7 @@ bool Parser::is_type() read_next(); return true; } - else - { - return false; - } + else return false; } bool Parser::is_function_name() @@ -123,21 +114,16 @@ bool Parser::is_function_name() read_next(); return true; } - else - { - return false; - } + else return false; } ASTStatement * Parser::statement() -{ - BasicStatement * stat = new BasicStatement(); +{ if(accept("{")) - { - while(!accept("}")) - { - stat->children.push_back( statement()); - } + { + BasicStatement * stat = new BasicStatement(); + while(!accept("}")) stat->children.push_back(statement()); + return stat; } else if(is_type()) { @@ -158,23 +144,14 @@ ASTStatement * Parser::statement() { argc++; is_type(); - declaration->add_argument(tok_value); read_next(); - if(peek(")")) - { - break; - } + if(peek(")")) break; expect(","); } expect(")"); - if(!accept(";")) - { - - declaration->add_body(statement()); - } + if(!accept(";")) declaration->add_body(statement()); } - delete stat; return declaration; } else if(accept("if")) @@ -182,11 +159,7 @@ ASTStatement * Parser::statement() IfNode * ifStatement = new IfNode(); ifStatement->add_condition(expr()); ifStatement->add_body(statement()); - if(accept("else")) - { - ifStatement->add_else_block(statement()); - } - delete stat; + if(accept("else")) ifStatement->add_else_block(statement()); return ifStatement; } else if(accept("repeat")) @@ -194,34 +167,27 @@ ASTStatement * Parser::statement() RepeatStatement * repeat = new RepeatStatement(); repeat->add_iteration_number(expr()->evaluate()); repeat->add_body(statement()); - delete stat; return repeat; } else if(accept("while")) { - WhileNode * while_node = new WhileNode(); - while_node->add_condition(expr()); - while_node->add_body(statement()); - delete stat; + WhileNode * while_node = new WhileNode(expr(), statement()); return while_node; } else if(accept("return")) - { - if(!peek(";")) - { - stat->add_expression(expr()); - } + { + BasicStatement * stat = new BasicStatement(); + if(!peek(";")) stat->add_expression(expr()); expect(";"); return stat; } else - { + { + BasicStatement * stat = new BasicStatement(); stat->add_expression(expr()); while(!expect(";") && tok_value != "") read_next(); return stat; - - } - return stat; + } } ASTExpression * Parser::prim_expr() @@ -262,18 +228,15 @@ ASTExpression * Parser::prim_expr() else if(current_token.get_type() == t_symbol) { string name = current_token.value; - VariableExpression * ve; - ve = new VariableExpression(name, context); + VariableExpression * ve = new VariableExpression(name, context); read_next(); return ve; } else if(accept("(")) { - BasicExpression * be; - be = static_cast(expr()); - + ASTExpression * expression = expr(); expect(")"); - return be; + return expression; } else { @@ -294,7 +257,6 @@ ASTExpression * Parser::postfix_expr() { if(!accept(")")) { - function_call->add_argument(expr()); while(accept(",")) { @@ -350,9 +312,8 @@ ASTExpression * Parser::eq_expr() ASTExpression * left = rel_expr(); if(peek("==") || peek("!=")) { - string oper; - if(accept("==")) oper = "=="; - else oper = "!="; + string oper = tok_value; + read_next(); return new BasicExpression(left, eq_expr(), oper); } else return left; @@ -365,7 +326,7 @@ ASTExpression * Parser::log_expr() { string oper; if(accept("and")) oper = "&&"; - else oper = "||"; + else if(accept("or")) oper = "||"; ASTExpression * right = log_expr(); return new BasicExpression(left, right, oper); } diff --git a/Sencha-lang/Tests/TestASTInspector.cpp b/Sencha-lang/Tests/TestASTInspector.cpp index d5fbf42..f38c7cc 100644 --- a/Sencha-lang/Tests/TestASTInspector.cpp +++ b/Sencha-lang/Tests/TestASTInspector.cpp @@ -19,8 +19,9 @@ TestASTInspector::~TestASTInspector() { std::string TestASTInspector::test_inspecting_basic_expression() { std::string test_report = ""; + BasicExpression * be = new BasicExpression(new ConstantExpression(SenchaObject(9)), + new ConstantExpression(SenchaObject(122)), "+"); - BasicExpression * be = build_basic_expression("+", SenchaObject(9), SenchaObject(122)); be->accept(&inspector); std::string assert_report = "Report:\n" + inspector.get_report(); muu_assert("ASTInspector didn\'t write any report", inspector.get_report() != ""); @@ -37,8 +38,8 @@ 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 * left = build_basic_expression("-", SenchaObject(88), SenchaObject(-19)); + BasicExpression * 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(); @@ -56,25 +57,20 @@ 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; + mu_run_test(test_inspecting_basic_expression); + mu_run_test(test_inspecting_simple_AST); + return test_report; } BasicExpression * TestASTInspector::build_basic_expression(std::string oper, SenchaObject arg1, SenchaObject arg2) { - BasicExpression * be = new BasicExpression(); - be->set_operator(oper); - be->set_left_operand(new ConstantExpression( arg1)); - be->set_right_operand(new ConstantExpression( arg2)); + BasicExpression * be = new BasicExpression(new ConstantExpression( arg1), new ConstantExpression( arg2), oper); return be; } BasicExpression * TestASTInspector::build_simple_AST(std::string oper, BasicExpression * left, BasicExpression * right) { - BasicExpression * be = new BasicExpression(); - be->set_operator(oper); - be->set_left_operand(left); - be->set_right_operand(right); + BasicExpression * be = new BasicExpression(left, right, oper); + return be; } diff --git a/Sencha-lang/Tests/TestLexer.h b/Sencha-lang/Tests/TestLexer.h index fa30d1b..02876cb 100644 --- a/Sencha-lang/Tests/TestLexer.h +++ b/Sencha-lang/Tests/TestLexer.h @@ -9,7 +9,10 @@ #define TESTLEXER_H_ #include "TestSuite.h" #include "../Lexer.h" - +/** + * TextLexer uses small test framework (TestSuite) + * to test some methods of Lexer class. + */ class TestLexer : public TestSuite { public: TestLexer(); diff --git a/Sencha-lang/Tests/TestParser.cpp b/Sencha-lang/Tests/TestParser.cpp index 3f432c9..9b40de8 100644 --- a/Sencha-lang/Tests/TestParser.cpp +++ b/Sencha-lang/Tests/TestParser.cpp @@ -28,11 +28,10 @@ std::string TestParser::test_parsing_and_evaluating_logical_expressions() auto tokens = lexer.parse_line(logical_case.first); parser.add_tokens(tokens); parser.interpret(); - SenchaObject value = parser.program->evaluate(); + SenchaObject value = parser.program->evaluate_last(); parser.erase_all(); muu_assert("Logical value isn't correct", value.truthy == logical_case.second); } - return test_report; } diff --git a/Sencha-lang/Tests/minunit.h b/Sencha-lang/Tests/minunit.h index b3917fb..9344d94 100644 --- a/Sencha-lang/Tests/minunit.h +++ b/Sencha-lang/Tests/minunit.h @@ -8,6 +8,10 @@ #ifndef MINUNIT_H_ #define MINUNIT_H_ +/** + * For my small testing framework I define three macro's + * Here they are: + */ #define mu_assert(message, test) do { if (!(test)) return message; } while (0) #define muu_assert(message, test) do { if (!(test)) test_report += message "\n"; } while (0) #define mu_run_test(test) do { std::string message = test(); tests++;\ diff --git a/Sencha-lang/Tests/tests.h b/Sencha-lang/Tests/tests.h index 1089cc1..49700d1 100644 --- a/Sencha-lang/Tests/tests.h +++ b/Sencha-lang/Tests/tests.h @@ -18,53 +18,11 @@ using namespace std; -void test_lexer() -{ - string test_line = "dupa"; - string test_line2 = "def how_many_trees = 1; how_many_trees + 3 == 2; num cut_tree( num how_many) {return how_many -1; != <=}"; - Lexer lexer; - vector tokens = lexer.parse_line(test_line); - - - for(unsigned int i=0; i< tokens.size(); i++) - { - cout << tokens[i].get_value() << " type: " << tokens[i].get_type() << endl; - } - - - tokens = lexer.parse_line(test_line2); - -} - -void test_parser() -{ - vector lines; - lines.push_back("def i; bulb; i + 3; string banan = \"kartofel\"; banan = \"banan\"; string kaboom(num how_many_times) { def z; }"); - lines.push_back("num pun"); - lines.push_back("def how_many_trees = 1; how_many_trees + 3 == 2; num cut_tree(num how_many) {return how_many -1}"); - Lexer lexer; - Context context; - vector tokens; - - for(unsigned int i=0; i>>" << endl; - cout << "Instructions: " << endl ; - cout << lines[i] << endl << endl; - //cout << parser.report_message; - cout << parser.error_message << endl; - } - -} void run_test_suites() { - TestLexer test_l; - test_l.run_tests(); + TestLexer test_lexer; + test_lexer.run_tests(); TestASTInspector test_inspector; test_inspector.run_tests(); @@ -76,8 +34,9 @@ void run_test_suites() void run_tests() { - run_test_suites(); cout << "derp!" << endl; + run_test_suites(); + }