RepeatStatement runs okay in functions now.

imports
Justyna Ilczuk 2013-01-07 17:15:47 +01:00
parent 22d6df9927
commit 1088069b4e
4 changed files with 17 additions and 31 deletions

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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");

View File

@ -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"))