summaryrefslogtreecommitdiffstats
path: root/Sencha-lang
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2013-01-07 17:15:47 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2013-01-07 17:15:47 +0100
commit1088069b4ef3ed2a773629b20a633eddbfe0aa67 (patch)
treed5ac4238cfdbfae6e50e830c3facf2f546a9e9ab /Sencha-lang
parent22d6df9927970690136cb46c2728a21bb5830c41 (diff)
downloadsencha-lang-1088069b4ef3ed2a773629b20a633eddbfe0aa67.tar.gz
sencha-lang-1088069b4ef3ed2a773629b20a633eddbfe0aa67.tar.bz2
sencha-lang-1088069b4ef3ed2a773629b20a633eddbfe0aa67.zip
RepeatStatement runs okay in functions now.
Diffstat (limited to 'Sencha-lang')
-rw-r--r--Sencha-lang/AST/RepeatStatement.cpp35
-rw-r--r--Sencha-lang/AST/RepeatStatement.h7
-rw-r--r--Sencha-lang/ASTInspector.cpp2
-rw-r--r--Sencha-lang/Parser.cpp4
4 files changed, 17 insertions, 31 deletions
diff --git a/Sencha-lang/AST/RepeatStatement.cpp b/Sencha-lang/AST/RepeatStatement.cpp
index 69f75c8..f042a22 100644
--- a/Sencha-lang/AST/RepeatStatement.cpp
+++ b/Sencha-lang/AST/RepeatStatement.cpp
@@ -7,44 +7,33 @@
#include "RepeatStatement.h"
-RepeatStatement::RepeatStatement()
+RepeatStatement::RepeatStatement(ASTExpression * expr, ASTStatement * statement)
{
- how_many_times = 0;
- body = NULL;
+ body = statement;
+ how_many_times_expr = expr;
this->type = "RepeatStatement";
}
RepeatStatement::~RepeatStatement()
{
delete body;
+ delete how_many_times_expr;
}
-void RepeatStatement::add_body(ASTStatement * statement)
-{
- body = statement;
-}
-
SenchaObject RepeatStatement::execute()
{
- for(int i = 0; i < how_many_times; i++)
+ auto value = how_many_times_expr->execute();
+ if(value.type == SenchaObject::integer_number)
+ {
+ int how_many_times = value.integer;
+
+ for(int i = 0; i < how_many_times; i++)
{
if(i == how_many_times - 1) return body->execute();
else body->execute();
}
- return SenchaObject();
-}
-
-
-
-void RepeatStatement::add_iteration_number(SenchaObject so)
-{
- if(so.type == SenchaObject::integer_number)
- {
- how_many_times = so.integer;
- }
- else
- {
- how_many_times = 0;
}
+
+ return SenchaObject();
}
diff --git a/Sencha-lang/AST/RepeatStatement.h b/Sencha-lang/AST/RepeatStatement.h
index 3ed7cf8..14906c4 100644
--- a/Sencha-lang/AST/RepeatStatement.h
+++ b/Sencha-lang/AST/RepeatStatement.h
@@ -9,7 +9,7 @@
#define REPEATSTATEMENT_H_
#include "ASTStatement.h"
-
+#include "ASTExpression.h"
/**
* Repeat statement if very useful, when you need to do something n-times
* you just repeat(n) {block_of_code}
@@ -18,10 +18,9 @@
*/
class RepeatStatement: public ASTStatement {
public:
- RepeatStatement();
- int how_many_times;
+ RepeatStatement(ASTExpression * expr, ASTStatement * statement);
ASTStatement * body;
- void add_iteration_number(SenchaObject so);
+ ASTExpression * how_many_times_expr;
virtual SenchaObject execute();
void add_body(ASTStatement * statement);
virtual ~RepeatStatement();
diff --git a/Sencha-lang/ASTInspector.cpp b/Sencha-lang/ASTInspector.cpp
index ba1f0e7..6a24298 100644
--- a/Sencha-lang/ASTInspector.cpp
+++ b/Sencha-lang/ASTInspector.cpp
@@ -294,7 +294,7 @@ void ASTInspector::visit(RepeatStatement * repeat_statement)
std::string visit_notes = "";
visit_notes += "RepeatStatement:\n";
- visit_notes += "It executes " + to_string(repeat_statement->how_many_times) + "times\n";
+ visit_notes += "It executes " + to_string(repeat_statement->how_many_times_expr->evaluate().integer) + "times\n";
write_report(visit_notes);
repeat_statement->body->accept(this);
write_report("End of RepeatStatement\n");
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index 481e983..b392527 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -175,9 +175,7 @@ ASTStatement * Parser::statement()
}
else if(accept("repeat"))
{
- RepeatStatement * repeat = new RepeatStatement();
- repeat->add_iteration_number(expr()->evaluate());
- repeat->add_body(statement());
+ RepeatStatement * repeat = new RepeatStatement(expr(), statement());
return repeat;
}
else if(accept("while"))