summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-10 12:37:29 +0100
committerJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-10 12:37:29 +0100
commitbc04b973776d1f78343862698e5450372360140f (patch)
tree33d6582fe76a7b1e9f560e6cf98788db3f93de56
parent1eee8a00393958a83d316c5aa404a50c82f65704 (diff)
downloadsencha-lang-bc04b973776d1f78343862698e5450372360140f.tar.gz
sencha-lang-bc04b973776d1f78343862698e5450372360140f.tar.bz2
sencha-lang-bc04b973776d1f78343862698e5450372360140f.tar.xz
sencha-lang-bc04b973776d1f78343862698e5450372360140f.zip
While node works! != operator
-rw-r--r--Sencha-lang/AST/AllTypesOfASTNodes.h1
-rw-r--r--Sencha-lang/AST/BasicExpression.cpp4
-rw-r--r--Sencha-lang/AST/SenchaObject.cpp13
-rw-r--r--Sencha-lang/AST/SenchaObject.h2
-rw-r--r--Sencha-lang/AST/WhileNode.cpp32
-rw-r--r--Sencha-lang/AST/WhileNode.h11
-rw-r--r--Sencha-lang/Parser.cpp6
-rw-r--r--Sencha-lang/main.cpp1
8 files changed, 65 insertions, 5 deletions
diff --git a/Sencha-lang/AST/AllTypesOfASTNodes.h b/Sencha-lang/AST/AllTypesOfASTNodes.h
index bb22126..22c4abb 100644
--- a/Sencha-lang/AST/AllTypesOfASTNodes.h
+++ b/Sencha-lang/AST/AllTypesOfASTNodes.h
@@ -21,6 +21,7 @@
#include "Assignment.h"
#include "DeclarationStatement.h"
#include "IfNode.h"
+#include "WhileNode.h"
#include "RepeatStatement.h"
//And probably more
//TODO actualize it
diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp
index ed77e10..06c3f3b 100644
--- a/Sencha-lang/AST/BasicExpression.cpp
+++ b/Sencha-lang/AST/BasicExpression.cpp
@@ -86,6 +86,10 @@ SenchaObject BasicExpression::evaluate()
{
so = SenchaObject(right == left);
}
+ else if(oper == "!=")
+ {
+ so = SenchaObject(right != left);
+ }
return so;
}
diff --git a/Sencha-lang/AST/SenchaObject.cpp b/Sencha-lang/AST/SenchaObject.cpp
index b49ac20..8a75e0a 100644
--- a/Sencha-lang/AST/SenchaObject.cpp
+++ b/Sencha-lang/AST/SenchaObject.cpp
@@ -223,4 +223,17 @@ SenchaObject SenchaObject::operator==(const SenchaObject& right)const
return result;
}
+SenchaObject SenchaObject::operator!=(const SenchaObject& right)const
+{
+ SenchaObject result;
+ result.type = boolean;
+
+ result = (*this == right);
+ if(result.truthy)
+ result.truthy = false;
+ else result.truthy = true;
+
+ return result;
+}
+
//TODO change code above to something more generic
diff --git a/Sencha-lang/AST/SenchaObject.h b/Sencha-lang/AST/SenchaObject.h
index c105863..3d5eec2 100644
--- a/Sencha-lang/AST/SenchaObject.h
+++ b/Sencha-lang/AST/SenchaObject.h
@@ -89,6 +89,8 @@ public:
virtual SenchaObject operator*(const SenchaObject& right)const;
virtual SenchaObject operator/(const SenchaObject& right)const;
virtual SenchaObject operator==(const SenchaObject& right)const;
+ virtual SenchaObject operator!=(const SenchaObject& right)const;
+
virtual ~SenchaObject();
};
diff --git a/Sencha-lang/AST/WhileNode.cpp b/Sencha-lang/AST/WhileNode.cpp
index b5b3ec4..98e0710 100644
--- a/Sencha-lang/AST/WhileNode.cpp
+++ b/Sencha-lang/AST/WhileNode.cpp
@@ -7,12 +7,42 @@
#include "WhileNode.h"
-WhileNode::WhileNode() {
+WhileNode::WhileNode(ASTNode * parent) {
// TODO Auto-generated constructor stub
+ this->parent = parent;
}
WhileNode::~WhileNode() {
// TODO Auto-generated destructor stub
}
+void WhileNode::add_condition(ASTExpression * expression)
+{
+ children.push_back(expression);
+}
+
+void WhileNode::add_body(ASTStatement * statement)
+{
+ body = statement;
+}
+
+std::string WhileNode::debug()
+{
+ std::string debug_note = "While";
+ return debug_note;
+}
+
+void WhileNode::execute()
+{
+ while(evaluate_condition())
+ {
+ body->execute();
+ }
+}
+
+bool WhileNode::evaluate_condition()
+{
+ auto condition = static_cast<ASTExpression * >(children[0])->evaluate();
+ return condition.is_true();
+}
diff --git a/Sencha-lang/AST/WhileNode.h b/Sencha-lang/AST/WhileNode.h
index a7f026b..bfd77de 100644
--- a/Sencha-lang/AST/WhileNode.h
+++ b/Sencha-lang/AST/WhileNode.h
@@ -9,11 +9,18 @@
#define WHILENODE_H_
#include "ASTStatement.h"
-
+#include "ASTExpression.h"
class WhileNode: public ASTStatement {
public:
- WhileNode();
+ WhileNode(ASTNode * parent);
virtual ~WhileNode();
+ ASTStatement * body;
+ void add_condition(ASTExpression * expression);
+ void add_body(ASTStatement * statement);
+ virtual std::string debug();
+ virtual void execute();
+ bool evaluate_condition();
+
};
#endif /* WHILENODE_H_ */
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index b0f8910..70b775c 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -240,8 +240,12 @@ ASTStatement * Parser::statement(ASTNode * parent)
}
else if(accept("while"))
{
+ WhileNode * while_node = new WhileNode(parent);
+ while_node->add_condition(expr(while_node));
+ while_node->add_body(statement(while_node));
- return stat;
+ delete stat;
+ return while_node;
}
else if(accept("return"))
{
diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp
index 6806849..805af2d 100644
--- a/Sencha-lang/main.cpp
+++ b/Sencha-lang/main.cpp
@@ -12,7 +12,6 @@ using namespace std;
SenchaObject print(vector<ASTExpression *> arguments)
{
- std::cout << "Program says: ";
for (auto argument: arguments)
{
auto value = argument->evaluate();