summaryrefslogtreecommitdiffstats
path: root/Sencha-lang
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-22 18:52:19 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-22 18:52:19 +0100
commit8ef1eb14c59525d55ad3b55786246bb44e204981 (patch)
tree8bc86567b01333a0eb1f47f756b88cf6fb798b3e /Sencha-lang
parent6721f24be8b1504f62bc458dab08e3e74defac30 (diff)
downloadsencha-lang-8ef1eb14c59525d55ad3b55786246bb44e204981.tar.gz
sencha-lang-8ef1eb14c59525d55ad3b55786246bb44e204981.tar.bz2
sencha-lang-8ef1eb14c59525d55ad3b55786246bb44e204981.zip
I do tests for evaluating logical expressions. Sadly I enountered
segmentation fault :(.
Diffstat (limited to 'Sencha-lang')
-rw-r--r--Sencha-lang/AST/ASTNode.h1
-rw-r--r--Sencha-lang/AST/ASTStatement.h1
-rw-r--r--Sencha-lang/AST/BasicStatement.cpp5
-rw-r--r--Sencha-lang/AST/BasicStatement.h1
-rw-r--r--Sencha-lang/AST/ProgramNode.cpp5
-rw-r--r--Sencha-lang/AST/ProgramNode.h2
-rw-r--r--Sencha-lang/Tests/TestParser.cpp58
-rw-r--r--Sencha-lang/Tests/TestParser.h37
-rw-r--r--Sencha-lang/Tests/tests.h4
9 files changed, 91 insertions, 23 deletions
diff --git a/Sencha-lang/AST/ASTNode.h b/Sencha-lang/AST/ASTNode.h
index 6a16f96..f774ae3 100644
--- a/Sencha-lang/AST/ASTNode.h
+++ b/Sencha-lang/AST/ASTNode.h
@@ -20,6 +20,7 @@ public:
virtual void accept(Visitor * visitor){ visitor->visit(this); };
+ virtual SenchaObject evaluate() = 0;
virtual std::string debug() = 0;
virtual void execute() = 0;
virtual ~ASTNode();
diff --git a/Sencha-lang/AST/ASTStatement.h b/Sencha-lang/AST/ASTStatement.h
index f1e9451..aab10b2 100644
--- a/Sencha-lang/AST/ASTStatement.h
+++ b/Sencha-lang/AST/ASTStatement.h
@@ -13,6 +13,7 @@ class ASTStatement : public ASTNode {
public:
ASTStatement();
virtual void execute() = 0;
+ virtual SenchaObject evaluate() { return SenchaObject(); }
virtual ~ASTStatement();
};
diff --git a/Sencha-lang/AST/BasicStatement.cpp b/Sencha-lang/AST/BasicStatement.cpp
index 26d1adf..bcdd5a7 100644
--- a/Sencha-lang/AST/BasicStatement.cpp
+++ b/Sencha-lang/AST/BasicStatement.cpp
@@ -36,6 +36,11 @@ void BasicStatement::execute()
child->execute() ;
}
+SenchaObject BasicStatement::evaluate()
+{
+ return children[children.size() - 1]->evaluate();
+}
+
void BasicStatement::accept(Visitor * visitor)
{
visitor->visit(this);
diff --git a/Sencha-lang/AST/BasicStatement.h b/Sencha-lang/AST/BasicStatement.h
index 1b616dc..840a8e6 100644
--- a/Sencha-lang/AST/BasicStatement.h
+++ b/Sencha-lang/AST/BasicStatement.h
@@ -17,6 +17,7 @@ public:
BasicStatement(ASTNode * parent);
virtual std::string debug();
void add_expression(ASTExpression * expr);
+ virtual SenchaObject evaluate();
virtual void execute();
virtual void accept(Visitor * vistitor);
virtual ~BasicStatement();
diff --git a/Sencha-lang/AST/ProgramNode.cpp b/Sencha-lang/AST/ProgramNode.cpp
index cab01be..43369f0 100644
--- a/Sencha-lang/AST/ProgramNode.cpp
+++ b/Sencha-lang/AST/ProgramNode.cpp
@@ -36,6 +36,11 @@ void ProgramNode::execute_last()
children[children.size() - 1]->execute();
}
+SenchaObject ProgramNode::evaluate_last()
+{
+ return static_cast<ASTStatement * >(children[children.size() - 1])->evaluate();
+}
+
std::string ProgramNode::debug()
{
std::string debug_note = "Program started debugging\n";
diff --git a/Sencha-lang/AST/ProgramNode.h b/Sencha-lang/AST/ProgramNode.h
index 80425df..c54bf07 100644
--- a/Sencha-lang/AST/ProgramNode.h
+++ b/Sencha-lang/AST/ProgramNode.h
@@ -19,6 +19,8 @@ public:
virtual ~ProgramNode();
virtual void execute();
virtual void execute_last();
+ virtual SenchaObject evaluate() { return evaluate_last(); }
+ SenchaObject evaluate_last();
virtual void accept(Visitor * visitor);
};
diff --git a/Sencha-lang/Tests/TestParser.cpp b/Sencha-lang/Tests/TestParser.cpp
index aa40b2c..a65bb71 100644
--- a/Sencha-lang/Tests/TestParser.cpp
+++ b/Sencha-lang/Tests/TestParser.cpp
@@ -16,3 +16,61 @@ TestParser::~TestParser() {
// TODO Auto-generated destructor stub
}
+std::string TestParser::test_parsing_and_evaluating_logical_expressions()
+{
+ std::string test_report = "";
+ std::vector<InputOutputPair> logical_inputs = prepare_logical_input();
+ Lexer lexer;
+ Context context;
+ Parser parser(&context);
+ for(auto logical_case : logical_inputs)
+ {
+ auto tokens = lexer.parse_line(logical_case.first);
+ parser.add_tokens(tokens);
+ auto value = parser.program->evaluate();
+ parser.erase_all();
+ muu_assert("Logical value isn't correct", value.truthy == logical_case.second);
+ }
+ return test_report;
+}
+
+std::vector<TestParser::InputOutputPair> TestParser::prepare_logical_input()
+{
+ std::vector<InputOutputPair> logical_inputs;
+ logical_inputs.push_back(InputOutputPair("false and false", false));
+ logical_inputs.push_back(InputOutputPair("false and true", false));
+ logical_inputs.push_back(InputOutputPair("true and false", false));
+ logical_inputs.push_back(InputOutputPair("true and true", true));
+ logical_inputs.push_back(InputOutputPair("false or false", false));
+ logical_inputs.push_back(InputOutputPair("false or true", true));
+ logical_inputs.push_back(InputOutputPair("true or false", true));
+ logical_inputs.push_back(InputOutputPair("true or true", true));
+ logical_inputs.push_back(InputOutputPair("(true or false) and (true or false)", true));
+ logical_inputs.push_back(InputOutputPair("7 + 2 > 55 or 4-5 == -1", true));
+
+ return logical_inputs;
+}
+
+std::string TestParser::all_tests()
+{
+ std::string test_report = "";
+
+ mu_run_test(test_parsing_and_evaluating_logical_expressions);
+
+
+ return test_report;
+}
+
+/*TODO
+ * write test of evaluating logical expression
+ * possible input:
+ *
+ * some variable == "g" and other != "a"
+ *
+ * some easy expression like:
+ * if("aaa" != "bbb" and 2 < 5) {
+>> print(1);
+>> } else {
+>> print(0);
+>> }
+ */
diff --git a/Sencha-lang/Tests/TestParser.h b/Sencha-lang/Tests/TestParser.h
index 6a11a6c..faae1a9 100644
--- a/Sencha-lang/Tests/TestParser.h
+++ b/Sencha-lang/Tests/TestParser.h
@@ -9,33 +9,26 @@
#define TESTPARSER_H_
#include "TestSuite.h"
+#include "../Lexer.h"
+#include "../Parser.h"
+#include "../ASTInspector.h"
+#include <utility>
+#include <vector>
+
+
class TestParser: public TestSuite {
public:
TestParser();
virtual ~TestParser();
+ typedef std::pair<std::string, bool> InputOutputPair;
+private:
+ std::string test_parsing_and_evaluating_logical_expressions();
+
+ std::vector<InputOutputPair> prepare_logical_input();
+
+ virtual std::string all_tests();
+
};
#endif /* TESTPARSER_H_ */
-
-/*TODO
- * write test of evaluating logical expression
- * possible input:
- * false and false
- * false and true
- * true and false
- * true and true
- * false or false
- * false or true
- * true or false
- * true or true
- * 7 + 2 > 55 or 4-5 == -1
- * some variable == "g" and other != "a"
- *
- * some easy expression like:
- * if("aaa" != "bbb" and 2 < 5) {
->> print(1);
->> } else {
->> print(0);
->> }
- */
diff --git a/Sencha-lang/Tests/tests.h b/Sencha-lang/Tests/tests.h
index 160799f..1089cc1 100644
--- a/Sencha-lang/Tests/tests.h
+++ b/Sencha-lang/Tests/tests.h
@@ -11,6 +11,7 @@
#include <iostream>
#include "TestLexer.h"
#include "TestASTInspector.h"
+#include "TestParser.h"
#include "../Lexer.h"
#include "../Parser.h"
@@ -68,7 +69,8 @@ void run_test_suites()
TestASTInspector test_inspector;
test_inspector.run_tests();
-
+ TestParser test_parser;
+ test_parser.run_tests();
}