summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-05 21:31:06 +0100
committerJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-05 21:31:06 +0100
commitd40b7c2397b807fcd3df2489b88d5d163326055b (patch)
tree88d7915a05254c8431e1199590bd22090c15624f
parentc5cf3e2fc93a640c7d75fa007e094f3ed1dffa0b (diff)
downloadsencha-lang-d40b7c2397b807fcd3df2489b88d5d163326055b.tar.gz
sencha-lang-d40b7c2397b807fcd3df2489b88d5d163326055b.tar.bz2
sencha-lang-d40b7c2397b807fcd3df2489b88d5d163326055b.tar.xz
sencha-lang-d40b7c2397b807fcd3df2489b88d5d163326055b.zip
More work on AST structure. I created a general type of object calle SenchaObject to store primitives.
-rw-r--r--Sencha-lang/AST/ASTNode.h3
-rw-r--r--Sencha-lang/AST/BasicExpression.cpp5
-rw-r--r--Sencha-lang/AST/BasicExpression.h7
-rw-r--r--Sencha-lang/AST/ConstantExpression.cpp6
-rw-r--r--Sencha-lang/AST/ConstantExpression.h7
-rw-r--r--Sencha-lang/AST/PostfixExpression.h3
-rw-r--r--Sencha-lang/AST/SenchaObject.cpp46
-rw-r--r--Sencha-lang/AST/SenchaObject.h68
-rw-r--r--Sencha-lang/Parser.cpp20
9 files changed, 147 insertions, 18 deletions
diff --git a/Sencha-lang/AST/ASTNode.h b/Sencha-lang/AST/ASTNode.h
index b0145a3..eb077d2 100644
--- a/Sencha-lang/AST/ASTNode.h
+++ b/Sencha-lang/AST/ASTNode.h
@@ -8,6 +8,8 @@
#ifndef ASTNODE_H_
#define ASTNODE_H_
#include <vector>
+#include <string>
+#include "SenchaObject.h"
class ASTNode {
public:
@@ -17,6 +19,7 @@ public:
void add_children(ASTNode *);
void remove_most_right_children();
void set_parent(ASTNode *);
+ virtual std::string debug() = 0;
virtual void execute() = 0;
virtual ~ASTNode();
};
diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp
index 5065de3..b370619 100644
--- a/Sencha-lang/AST/BasicExpression.cpp
+++ b/Sencha-lang/AST/BasicExpression.cpp
@@ -24,7 +24,7 @@ void BasicExpression::set_right_operand(ASTNode * right)
this->right = static_cast<ASTExpression *>(children[1]);
}
-virtual int BasicExpression::evaluate()
+virtual SenchaObject BasicExpression::evaluate()
{
if(oper == "+")
{
@@ -34,7 +34,8 @@ virtual int BasicExpression::evaluate()
return 0;
}
-BasicExpression::BasicExpression() {
+BasicExpression::BasicExpression(ASTNode * parent) {
+ this->parent = parent;
// initialize pointers
this->add_children(0);
this->add_children(0);
diff --git a/Sencha-lang/AST/BasicExpression.h b/Sencha-lang/AST/BasicExpression.h
index cf6d80e..ee67a54 100644
--- a/Sencha-lang/AST/BasicExpression.h
+++ b/Sencha-lang/AST/BasicExpression.h
@@ -8,7 +8,6 @@
#ifndef BASICEXPRESSION_H_
#define BASICEXPRESSION_H_
#include "ASTExpression.h"
-#include <string>
class BasicExpression : public ASTExpression {
public:
@@ -20,9 +19,11 @@ public:
void set_operator(std::string op);
void set_left_operand(ASTNode * left);
void set_right_operand(ASTNode * right);
- virtual int evaluate();
+ virtual SenchaObject evaluate();
- BasicExpression();
+ virtual std::string debug();
+
+ BasicExpression(ASTNode * parent);
virtual ~BasicExpression();
};
diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp
index 702dfe0..0e5e134 100644
--- a/Sencha-lang/AST/ConstantExpression.cpp
+++ b/Sencha-lang/AST/ConstantExpression.cpp
@@ -9,10 +9,14 @@
ConstantExpression::ConstantExpression() {
// TODO Auto-generated constructor stub
-
+ value = SenchaObject();
}
ConstantExpression::~ConstantExpression() {
// TODO Auto-generated destructor stub
}
+virtual SenchaObject ConstantExpression::evaluate()
+{
+ return value;
+}
diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h
index cc96adc..aafb38a 100644
--- a/Sencha-lang/AST/ConstantExpression.h
+++ b/Sencha-lang/AST/ConstantExpression.h
@@ -12,8 +12,13 @@
class ConstantExpression : public ASTExpression {
public:
ConstantExpression();
+ ConstantExpression(int number) { value = SenchaObject(number);};
+ ConstantExpression(double number) { value = SenchaObject(number); };
+ ConstantExpression(std::string text){ value = SenchaObject(text);};
+
+ SenchaObject value;
virtual ~ConstantExpression();
- virtual int evaluate();
+ virtual SenchaObject evaluate();
};
#endif /* CONSTANTEXPRESSION_H_ */
diff --git a/Sencha-lang/AST/PostfixExpression.h b/Sencha-lang/AST/PostfixExpression.h
index 855de51..0a2f94a 100644
--- a/Sencha-lang/AST/PostfixExpression.h
+++ b/Sencha-lang/AST/PostfixExpression.h
@@ -7,8 +7,9 @@
#ifndef POSTFIXEXPRESSION_H_
#define POSTFIXEXPRESSION_H_
+#include "ASTExpression.h"
-class PostfixExpression {
+class PostfixExpression : public ASTExpression {
public:
PostfixExpression();
virtual ~PostfixExpression();
diff --git a/Sencha-lang/AST/SenchaObject.cpp b/Sencha-lang/AST/SenchaObject.cpp
new file mode 100644
index 0000000..8b15489
--- /dev/null
+++ b/Sencha-lang/AST/SenchaObject.cpp
@@ -0,0 +1,46 @@
+/*
+ * SenchaObject.cpp
+ *
+ * Created on: Dec 5, 2012
+ * Author: attero
+ */
+
+#include "SenchaObject.h"
+
+SenchaObject::SenchaObject() {
+ set_null_value();
+
+ // TODO Auto-generated constructor stub
+
+}
+
+SenchaObject::~SenchaObject() {
+ // TODO Auto-generated destructor stub
+}
+
+SenchaObject SenchaObject::operator+(const SenchaObject& right)const
+{
+ SenchaObject result;
+ if(type == right.type)
+ {
+ switch(type){
+ case string_literal:
+ result.set_value(this->text + right.text);
+ 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;
+
+}
diff --git a/Sencha-lang/AST/SenchaObject.h b/Sencha-lang/AST/SenchaObject.h
new file mode 100644
index 0000000..70cadfc
--- /dev/null
+++ b/Sencha-lang/AST/SenchaObject.h
@@ -0,0 +1,68 @@
+/*
+ * SenchaObject.h
+ *
+ * Created on: Dec 5, 2012
+ * Author: attero
+ */
+
+#ifndef SENCHAOBJECT_H_
+#define SENCHAOBJECT_H_
+#include <string>
+class SenchaObject {
+
+public:
+ typedef enum {
+ string_literal,
+ integer_number,
+ float_number,
+ null,
+ invalid
+ } Type;
+
+ Type type;
+
+
+ std::string text;
+ int integer;
+ double number;
+
+ void set_value(std::string text)
+ {
+ this->text = text;
+ type = string_literal;
+ }
+
+ void set_null_value()
+ {
+ this->type = null;
+ this->integer = 0;
+ this->number = 0;
+ this->text = "";
+ }
+
+ void set_value(int integer)
+ {
+ this->integer = integer;
+ type = integer_number;
+ }
+
+ void set_value(double number)
+ {
+ this->number = number;
+ type = float_number;
+ }
+
+ SenchaObject();
+ SenchaObject(int integer) { set_value(integer); }
+ SenchaObject(float number) { set_value(number); }
+ SenchaObject(std::string text) { set_value(text); }
+
+ //TODO overload operators as it should be done
+
+ virtual SenchaObject operator+(const SenchaObject& right)const;
+
+ virtual SenchaObject operator-(const SenchaObject& right)const { return SenchaObject(); };
+ virtual ~SenchaObject();
+};
+
+#endif /* SENCHAOBJECT_H_ */
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index 3972af7..baca878 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -114,7 +114,7 @@ bool Parser::is_type()
else return false;
}
-void Parser::statement()
+ASTNode * Parser::statement(ASTNode * statement)
{
if(accept("{"))
{
@@ -209,7 +209,7 @@ void Parser::statement()
}
}
-void Parser::prim_expr()
+ASTNode * Parser::prim_expr(ASTNode * expression)
{
if(current_token.get_type() == t_integer)
{
@@ -242,7 +242,7 @@ void Parser::prim_expr()
}
-void Parser::postfix_expr()
+ASTNode * Parser::postfix_expr(ASTNode * expression)
{
prim_expr();
if(accept("["))
@@ -270,7 +270,7 @@ void Parser::postfix_expr()
}
}
-void Parser::mul_expr()
+ASTNode * Parser::mul_expr(ASTNode * expression)
{
postfix_expr();
while(peek("*") || peek("/"))
@@ -287,7 +287,7 @@ void Parser::mul_expr()
}
}
-void Parser::add_expr()
+ASTNode * Parser::add_expr(ASTNode * expression)
{
mul_expr();
while(peek("+") || peek("-"))
@@ -304,7 +304,7 @@ void Parser::add_expr()
}
}
-void Parser::rel_expr()
+ASTNode * Parser::rel_expr(ASTNode * expression)
{
add_expr();
while(peek("<"))
@@ -315,7 +315,7 @@ void Parser::rel_expr()
}
}
-void Parser::eq_expr()
+ASTNode * Parser::eq_expr(ASTNode * expression)
{
rel_expr();
while(peek("==") || peek("!="))
@@ -333,9 +333,9 @@ void Parser::eq_expr()
}
}
-void Parser::expr()
-{
- eq_expr();
+ASTNode * Parser::expr(ASTNode * expression)
+{
+ eq_expr(expression);
if(accept("="))
{
expr();