summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-09 21:17:13 +0100
committerJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-09 21:17:13 +0100
commit2042c35d8502ecd66c7cdb49fff3a3a8887b01c9 (patch)
tree7ea93db2a4b6432834619db91a8a7494ba57ae30
parentfc7f656bc27ea146f2d28f721a4cb4a9aac6e266 (diff)
downloadsencha-lang-2042c35d8502ecd66c7cdb49fff3a3a8887b01c9.tar.gz
sencha-lang-2042c35d8502ecd66c7cdb49fff3a3a8887b01c9.tar.bz2
sencha-lang-2042c35d8502ecd66c7cdb49fff3a3a8887b01c9.tar.xz
sencha-lang-2042c35d8502ecd66c7cdb49fff3a3a8887b01c9.zip
Better assigning and usage in functions.
-rw-r--r--Sencha-lang/AST/Assignment.cpp11
-rw-r--r--Sencha-lang/AST/Assignment.h3
-rw-r--r--Sencha-lang/AST/BasicStatement.cpp3
-rw-r--r--Sencha-lang/AST/ConstantExpression.cpp17
-rw-r--r--Sencha-lang/AST/ConstantExpression.h4
-rw-r--r--Sencha-lang/AST/RepeatStatement.cpp58
-rw-r--r--Sencha-lang/AST/RepeatStatement.h28
-rw-r--r--Sencha-lang/Parser.cpp10
8 files changed, 125 insertions, 9 deletions
diff --git a/Sencha-lang/AST/Assignment.cpp b/Sencha-lang/AST/Assignment.cpp
index 05f5bc5..c83ab8a 100644
--- a/Sencha-lang/AST/Assignment.cpp
+++ b/Sencha-lang/AST/Assignment.cpp
@@ -12,17 +12,22 @@ SenchaObject Assignment::evaluate()
return static_cast<ASTExpression *>(children[1])->evaluate();
}
+void Assignment::set_name(std::string name)
+{
+ this->name = name;
+}
+
void Assignment::execute()
{
auto left_value = static_cast<ASTExpression *>(children[0])->evaluate();
auto right_value = static_cast<ASTExpression *>(children[1])->evaluate();
static_cast<ASTExpression *>(children[1])->execute_quietly();
- if(left_value.name != "")
+
+ if(name != "")
{
right_value.name = left_value.name;
- context->set(left_value.name, right_value);
+ context->set(name, right_value);
}
- std::cout << right_value.repr() << std::endl;
}
void Assignment::execute_quietly()
diff --git a/Sencha-lang/AST/Assignment.h b/Sencha-lang/AST/Assignment.h
index c851ccf..74721c9 100644
--- a/Sencha-lang/AST/Assignment.h
+++ b/Sencha-lang/AST/Assignment.h
@@ -15,7 +15,10 @@ class Assignment : public ASTExpression {
public:
Context * context;
+ std::string name;
+
SenchaObject evaluate();
+ void set_name(std::string name);
void execute();
void execute_quietly();
void add_lvalue(ASTExpression *);
diff --git a/Sencha-lang/AST/BasicStatement.cpp b/Sencha-lang/AST/BasicStatement.cpp
index ffdfe1f..03fe6d8 100644
--- a/Sencha-lang/AST/BasicStatement.cpp
+++ b/Sencha-lang/AST/BasicStatement.cpp
@@ -27,5 +27,6 @@ std::string BasicStatement::debug()
void BasicStatement::execute()
{
- children[0]->execute() ;
+ for(auto child: children)
+ child->execute() ;
}
diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp
index 3874a52..74ac04f 100644
--- a/Sencha-lang/AST/ConstantExpression.cpp
+++ b/Sencha-lang/AST/ConstantExpression.cpp
@@ -10,6 +10,7 @@
ConstantExpression::ConstantExpression(ASTNode * parent) {
this->parent = parent;
value = SenchaObject();
+ this->context = NULL;
}
ConstantExpression::~ConstantExpression() {
@@ -21,16 +22,29 @@ ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value, std
this->parent = parent;
this->value = value;
this->value.name = name;
+ this->context = NULL;
+}
+
+ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value, Context * context)
+{
+ this->context = context;
+ this->parent = parent;
+ this->value = value;
}
ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value)
{
this->parent = parent;
this->value = value;
+ this->context = NULL;
}
SenchaObject ConstantExpression::evaluate()
{
+ if(value.name != "" && context != NULL)
+ {
+ value = context->get(value.name);
+ }
return value;
}
@@ -48,17 +62,20 @@ void ConstantExpression::execute_quietly()
ConstantExpression::ConstantExpression(ASTNode * parent, int number)
{
this->parent = parent; value = SenchaObject(number);
+ this->context = NULL;
}
ConstantExpression::ConstantExpression(ASTNode * parent, double number)
{
this->parent = parent;
value = SenchaObject(number);
+ this->context = NULL;
}
ConstantExpression::ConstantExpression(ASTNode * parent, std::string text)
{
this->parent = parent; value = SenchaObject(text);
+ this->context = NULL;
}
std::string ConstantExpression::debug() { return "Constant expression:\n" + value.repr() + "\n"; }
diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h
index 8d7c122..285972e 100644
--- a/Sencha-lang/AST/ConstantExpression.h
+++ b/Sencha-lang/AST/ConstantExpression.h
@@ -9,6 +9,7 @@
#define CONSTANTEXPRESSION_H_
#include "ASTExpression.h"
#include <iostream>
+#include "../Context.h"
class ConstantExpression : public ASTExpression {
public:
@@ -17,10 +18,11 @@ public:
ConstantExpression(ASTNode * parent, double number) ;
ConstantExpression(ASTNode * parent, std::string text);
ConstantExpression(ASTNode * parent, SenchaObject value, std::string name);
+ ConstantExpression(ASTNode * parent, SenchaObject value, Context * context);
ConstantExpression(ASTNode * parent, SenchaObject value);
SenchaObject value;
-
+ Context * context;
std::string debug();
diff --git a/Sencha-lang/AST/RepeatStatement.cpp b/Sencha-lang/AST/RepeatStatement.cpp
new file mode 100644
index 0000000..18f7ca9
--- /dev/null
+++ b/Sencha-lang/AST/RepeatStatement.cpp
@@ -0,0 +1,58 @@
+/*
+ * RepeatStatement.cpp
+ *
+ * Created on: Dec 9, 2012
+ * Author: attero
+ */
+
+#include "RepeatStatement.h"
+
+RepeatStatement::RepeatStatement(ASTNode * parent)
+{
+ this->parent = parent;
+ how_many_times = 0;
+ body = NULL;
+}
+
+RepeatStatement::~RepeatStatement()
+{
+
+}
+
+void RepeatStatement::execute()
+{
+ execute_quietly();
+}
+
+void RepeatStatement::add_body(ASTStatement * statement)
+{
+ body = statement;
+}
+
+void RepeatStatement::execute_quietly()
+{
+ for(int i = 0; i < how_many_times; i++)
+ {
+ body->execute();
+ }
+}
+
+
+std::string RepeatStatement::debug()
+{
+ return "Repeats!";
+}
+
+void RepeatStatement::add_iteration_number(SenchaObject so)
+{
+
+ /*if(so.type == SenchaObject::integer_number)
+ {
+ how_many_times = so.integer;
+ }
+ else
+ {
+ how_many_times = 0;
+ }*/
+ how_many_times = 3;
+}
diff --git a/Sencha-lang/AST/RepeatStatement.h b/Sencha-lang/AST/RepeatStatement.h
new file mode 100644
index 0000000..89d6b89
--- /dev/null
+++ b/Sencha-lang/AST/RepeatStatement.h
@@ -0,0 +1,28 @@
+/*
+ * RepeatStatement.h
+ *
+ * Created on: Dec 9, 2012
+ * Author: attero
+ */
+
+#ifndef REPEATSTATEMENT_H_
+#define REPEATSTATEMENT_H_
+
+#include "ASTStatement.h"
+
+class RepeatStatement: public ASTStatement {
+public:
+ RepeatStatement(ASTNode * parent);
+ int how_many_times;
+ ASTStatement * body;
+ void add_iteration_number(SenchaObject so);
+ virtual void execute();
+ virtual void execute_quietly();
+ void add_body(ASTStatement * statement);
+
+
+ virtual std::string debug() ;
+ virtual ~RepeatStatement();
+};
+
+#endif /* REPEATSTATEMENT_H_ */
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index 3f4b03c..7ac8fc6 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -149,7 +149,7 @@ ASTStatement * Parser::statement(ASTNode * parent)
{
while(!accept("}"))
{
- stat = static_cast<BasicStatement * >(statement(parent));
+ stat->children.push_back( statement(parent));
}
}
@@ -290,9 +290,10 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
//TODO is it right?
string name = current_token.value;
- variable = context->get(name);
+ //variable = context->get(name);
variable.name = name;
- ce = new ConstantExpression(expression, variable, name);
+
+ ce = new ConstantExpression(expression, variable, context);
read_next();
}
else if(accept("("))
@@ -471,6 +472,7 @@ ASTExpression * Parser::eq_expr(ASTNode * expression)
ASTExpression * Parser::expr(ASTNode * expression)
{
Assignment * assignment = new Assignment(expression, context);
+ auto name = tok_value;
ASTExpression * left = eq_expr(assignment);
if(accept("="))
@@ -479,7 +481,7 @@ ASTExpression * Parser::expr(ASTNode * expression)
assignment->add_lvalue(left);
assignment->add_rvalue(right);
-
+ assignment->set_name(name);
report(" :=\n");
return assignment;
}