summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/AST
diff options
context:
space:
mode:
authorJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-07 16:58:27 +0100
committerJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-07 16:58:27 +0100
commit30e3fb729c1cd8d057d0872a8cb285987432e83b (patch)
tree1ccfca3c11440a7970102f36c9f8e526ba61c196 /Sencha-lang/AST
parent02d96d9891472de552b6518ec30a880f3eff69ce (diff)
downloadsencha-lang-30e3fb729c1cd8d057d0872a8cb285987432e83b.tar.gz
sencha-lang-30e3fb729c1cd8d057d0872a8cb285987432e83b.tar.bz2
sencha-lang-30e3fb729c1cd8d057d0872a8cb285987432e83b.tar.xz
sencha-lang-30e3fb729c1cd8d057d0872a8cb285987432e83b.zip
It's alive and looks like it also works.
Diffstat (limited to 'Sencha-lang/AST')
-rw-r--r--Sencha-lang/AST/AST.cpp5
-rw-r--r--Sencha-lang/AST/AST.h1
-rw-r--r--Sencha-lang/AST/ASTExpression.h1
-rw-r--r--Sencha-lang/AST/BasicExpression.cpp29
-rw-r--r--Sencha-lang/AST/BasicExpression.h8
-rw-r--r--Sencha-lang/AST/BasicStatement.cpp2
-rw-r--r--Sencha-lang/AST/ConstantExpression.cpp6
-rw-r--r--Sencha-lang/AST/ConstantExpression.h2
-rw-r--r--Sencha-lang/AST/PostfixExpression.cpp36
-rw-r--r--Sencha-lang/AST/PostfixExpression.h17
-rw-r--r--Sencha-lang/AST/SenchaObject.cpp78
-rw-r--r--Sencha-lang/AST/SenchaObject.h4
12 files changed, 173 insertions, 16 deletions
diff --git a/Sencha-lang/AST/AST.cpp b/Sencha-lang/AST/AST.cpp
index 5fcf37e..8c36289 100644
--- a/Sencha-lang/AST/AST.cpp
+++ b/Sencha-lang/AST/AST.cpp
@@ -14,6 +14,11 @@ AST::AST() {
level_of_depth = 0;
}
+void AST::delete_all_children()
+{
+ //TODO perform deleting
+}
+
AST::~AST() {
}
diff --git a/Sencha-lang/AST/AST.h b/Sencha-lang/AST/AST.h
index c29207e..113ec4c 100644
--- a/Sencha-lang/AST/AST.h
+++ b/Sencha-lang/AST/AST.h
@@ -16,6 +16,7 @@ public:
ASTNode * current_node;
int number_of_nodes;
int level_of_depth;
+ void delete_all_children();
ASTNode * add_node(ASTNode * node);
virtual ~AST();
};
diff --git a/Sencha-lang/AST/ASTExpression.h b/Sencha-lang/AST/ASTExpression.h
index af5f487..eb56b19 100644
--- a/Sencha-lang/AST/ASTExpression.h
+++ b/Sencha-lang/AST/ASTExpression.h
@@ -14,6 +14,7 @@ class ASTExpression : public ASTNode {
public:
virtual SenchaObject evaluate() = 0;
+ virtual void execute() = 0;
ASTExpression();
virtual ~ASTExpression();
};
diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp
index 5e1dc7a..b297aa9 100644
--- a/Sencha-lang/AST/BasicExpression.cpp
+++ b/Sencha-lang/AST/BasicExpression.cpp
@@ -42,16 +42,34 @@ void BasicExpression::set_right_operand(ASTNode * right)
if(children.size() == 2)
children_set = true;
}
+void BasicExpression::execute()
+{
+ children[0]->execute();
+ children[1]->execute();
+ std::cout << evaluate().repr() << std::endl;
+}
SenchaObject BasicExpression::evaluate()
{
-
+ //TODO refactor it ;)
+ SenchaObject so;
if(oper == "+")
{
- return SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() + static_cast<ASTExpression *>(children[1])->evaluate());
+ so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() + static_cast<ASTExpression *>(children[1])->evaluate());
}
-
- return SenchaObject();
+ else if(oper == "-")
+ {
+ so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() - static_cast<ASTExpression *>(children[1])->evaluate());
+ }
+ else if(oper == "*")
+ {
+ so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() * static_cast<ASTExpression *>(children[1])->evaluate());
+ }
+ else if(oper == "/")
+ {
+ so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() / static_cast<ASTExpression *>(children[1])->evaluate());
+ }
+ return so;
}
BasicExpression::BasicExpression(ASTNode * parent) {
@@ -70,6 +88,7 @@ std::string BasicExpression::debug()
std::string debug_note = "Basic expression:\n";
debug_note += "left operand: \n";
debug_note += this->children[0]->debug();
- debug_note += "\nright operand:\n" + this->children[1]->debug() +"\n###\n";
+ debug_note += "\nright operand:\n" + this->children[1]->debug();
+ debug_note += "\nOperator: " + this->oper +"\n###\n";
return debug_note;
}
diff --git a/Sencha-lang/AST/BasicExpression.h b/Sencha-lang/AST/BasicExpression.h
index c9a79e3..00dcac2 100644
--- a/Sencha-lang/AST/BasicExpression.h
+++ b/Sencha-lang/AST/BasicExpression.h
@@ -8,12 +8,9 @@
#ifndef BASICEXPRESSION_H_
#define BASICEXPRESSION_H_
#include "ASTExpression.h"
-
+#include <iostream>
class BasicExpression : public ASTExpression {
public:
- //TODO change oper to enum
-
-
std::string oper;
@@ -22,7 +19,8 @@ public:
void set_left_operand(ASTNode * left);
void set_right_operand(ASTNode * right);
virtual SenchaObject evaluate();
- virtual void execute() { evaluate(); };
+ virtual void execute();
+
std::string debug() ;
diff --git a/Sencha-lang/AST/BasicStatement.cpp b/Sencha-lang/AST/BasicStatement.cpp
index 3229321..02ac7c3 100644
--- a/Sencha-lang/AST/BasicStatement.cpp
+++ b/Sencha-lang/AST/BasicStatement.cpp
@@ -28,5 +28,5 @@ std::string BasicStatement::debug()
void BasicStatement::execute()
{
- std::cout << children[0]->debug() << std::endl ;
+ children[0]->execute() ;
}
diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp
index 9858bb9..d0ade2e 100644
--- a/Sencha-lang/AST/ConstantExpression.cpp
+++ b/Sencha-lang/AST/ConstantExpression.cpp
@@ -17,6 +17,12 @@ ConstantExpression::~ConstantExpression() {
// TODO Auto-generated destructor stub
}
+ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value)
+{
+ this->parent = parent;
+ this->value = value;
+}
+
SenchaObject ConstantExpression::evaluate()
{
return value;
diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h
index 5ba474c..f74b28f 100644
--- a/Sencha-lang/AST/ConstantExpression.h
+++ b/Sencha-lang/AST/ConstantExpression.h
@@ -15,7 +15,7 @@ public:
ConstantExpression(ASTNode * parent, int number) ;
ConstantExpression(ASTNode * parent, double number) ;
ConstantExpression(ASTNode * parent, std::string text);
-
+ ConstantExpression(ASTNode * parent, SenchaObject value);
SenchaObject value;
std::string debug();
diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp
index 4ceb6b4..82876cd 100644
--- a/Sencha-lang/AST/PostfixExpression.cpp
+++ b/Sencha-lang/AST/PostfixExpression.cpp
@@ -7,8 +7,11 @@
#include "PostfixExpression.h"
-PostfixExpression::PostfixExpression() {
+PostfixExpression::PostfixExpression(ASTNode * parent) {
// TODO Auto-generated constructor stub
+ this->parent = parent;
+ head_set = false;
+ body_set = false;
}
@@ -16,3 +19,34 @@ PostfixExpression::~PostfixExpression() {
// TODO Auto-generated destructor stub
}
+
+void PostfixExpression::set_head(ASTExpression * expression)
+{
+ //How should it look like?
+ head_set = true;
+}
+
+void PostfixExpression::add_argument(ASTExpression * expression)
+{
+ arguments.push_back(expression);
+}
+
+void PostfixExpression::set_body(ASTStatement * statement)
+{
+ children.push_back(statement);
+ body_set = true;
+}
+
+SenchaObject PostfixExpression::evaluate()
+{
+ return SenchaObject();
+}
+
+void PostfixExpression::execute() { evaluate(); }
+
+
+
+std::string PostfixExpression::debug()
+{
+ return "Postfix expression tadadah!";
+}
diff --git a/Sencha-lang/AST/PostfixExpression.h b/Sencha-lang/AST/PostfixExpression.h
index 0a2f94a..4aa5974 100644
--- a/Sencha-lang/AST/PostfixExpression.h
+++ b/Sencha-lang/AST/PostfixExpression.h
@@ -8,10 +8,25 @@
#ifndef POSTFIXEXPRESSION_H_
#define POSTFIXEXPRESSION_H_
#include "ASTExpression.h"
+#include "ASTStatement.h"
class PostfixExpression : public ASTExpression {
public:
- PostfixExpression();
+
+ bool body_set;
+ bool head_set;
+ std::vector<ASTExpression *> arguments;
+ void set_head(ASTExpression * expression);
+ void add_argument(ASTExpression * expression);
+ void set_body(ASTStatement * statement);
+
+ virtual SenchaObject evaluate();
+ virtual void execute();
+
+
+ std::string debug() ;
+
+ PostfixExpression(ASTNode * parent);
virtual ~PostfixExpression();
};
diff --git a/Sencha-lang/AST/SenchaObject.cpp b/Sencha-lang/AST/SenchaObject.cpp
index 46694f3..ed86bf4 100644
--- a/Sencha-lang/AST/SenchaObject.cpp
+++ b/Sencha-lang/AST/SenchaObject.cpp
@@ -63,7 +63,6 @@ SenchaObject SenchaObject::operator+(const SenchaObject& right)const
result.set_value(this->number + right.number);
break;
}
-
}
else
{
@@ -73,3 +72,80 @@ SenchaObject SenchaObject::operator+(const SenchaObject& right)const
return result;
}
+
+SenchaObject SenchaObject::operator-(const SenchaObject& right)const
+{
+ SenchaObject result;
+ if(type == right.type)
+ {
+ switch(type){
+ case string_literal:
+ result.type = invalid;
+ break;
+ case integer_number:
+ result.set_value(this->integer - right.integer);
+ break;
+ case float_number:
+ result.set_value(this->number - right.number);
+ break;
+ }
+ }
+ else
+ {
+ result.type = invalid;
+ }
+
+ return result;
+}
+
+SenchaObject SenchaObject::operator*(const SenchaObject& right)const
+{
+ SenchaObject result;
+ if(type == right.type)
+ {
+ switch(type){
+ case string_literal:
+ result.type = invalid;
+ break;
+ case integer_number:
+ result.set_value(this->integer * right.integer);
+ break;
+ case float_number:
+ result.set_value(this->number * right.number);
+ break;
+ }
+ }
+ else
+ {
+ result.type = invalid;
+ }
+
+ return result;
+}
+
+SenchaObject SenchaObject::operator/(const SenchaObject& right)const
+{
+ SenchaObject result;
+ if(type == right.type)
+ {
+ switch(type){
+ case string_literal:
+ result.type = invalid;
+ break;
+ case integer_number:
+ result.set_value(this->integer / right.integer);
+ break;
+ case float_number:
+ result.set_value(this->number / right.number);
+ break;
+ }
+ }
+ else
+ {
+ result.type = invalid;
+ }
+
+ 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 9a4f1bb..fbaf064 100644
--- a/Sencha-lang/AST/SenchaObject.h
+++ b/Sencha-lang/AST/SenchaObject.h
@@ -62,8 +62,10 @@ public:
//TODO overload operators as it should be done
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 operator-(const SenchaObject& right)const { return SenchaObject(); };
virtual ~SenchaObject();
};