Better assigning and usage in functions.
parent
fc7f656bc2
commit
2042c35d85
|
@ -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()
|
||||
|
|
|
@ -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 *);
|
||||
|
|
|
@ -27,5 +27,6 @@ std::string BasicStatement::debug()
|
|||
|
||||
void BasicStatement::execute()
|
||||
{
|
||||
children[0]->execute() ;
|
||||
for(auto child: children)
|
||||
child->execute() ;
|
||||
}
|
||||
|
|
|
@ -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"; }
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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_ */
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue