New class for variables. It's much more appropriate than storing

variables in constant expression class isn't it?
functions
Justyna Ilczuk 2012-12-17 10:41:48 +01:00
parent 46ac5b352b
commit b9d9788da2
10 changed files with 96 additions and 47 deletions

View File

@ -23,6 +23,7 @@
#include "IfNode.h"
#include "WhileNode.h"
#include "RepeatStatement.h"
#include "VariableExpression.h"
//And probably more
//TODO actualize it

View File

@ -25,7 +25,6 @@ void Assignment::execute()
if(name != "")
{
right_value.name = left_value.name;
context->set(name, right_value);
}
}
@ -37,7 +36,6 @@ void Assignment::execute_quietly()
static_cast<ASTExpression *>(children[1])->execute_quietly();
if(left_value.name != "")
{
right_value.name = left_value.name;
context->set(left_value.name, right_value);
}

View File

@ -99,7 +99,7 @@ BasicExpression::BasicExpression(ASTNode * parent) {
}
BasicExpression::~BasicExpression() {
//Do nothing
}

View File

@ -10,47 +10,26 @@
ConstantExpression::ConstantExpression(ASTNode * parent) {
this->parent = parent;
value = SenchaObject();
this->context = NULL;
}
ConstantExpression::~ConstantExpression() {
// Do nothing
}
ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value, std::string name)
{
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)
{
return context->get(value.name);
}
return value;
}
void ConstantExpression::execute()
{
std::cout << evaluate().repr() << std::endl;//Do nothing
std::cout << evaluate().repr() << std::endl;
}
void ConstantExpression::execute_quietly()
@ -62,20 +41,17 @@ 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"; }

View File

@ -17,12 +17,9 @@ public:
ConstantExpression(ASTNode * parent, int number) ;
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();

View File

@ -63,7 +63,7 @@ void DeclarationStatement::execute()
else
{
context->add(name, right_value);
children[0]->execute() ;
children[0]->execute();
}
}

View File

@ -0,0 +1,41 @@
/*
* VariableExpression.cpp
*
* Created on: Dec 17, 2012
* Author: att
*/
#include "VariableExpression.h"
VariableExpression::VariableExpression(ASTNode * parent, Context * context, std::string name) {
this->name = name;
this->parent = parent;
this->context = context;
}
std::string VariableExpression::debug()
{
std::string debug_note = "variable: " + name;
return debug_note;
}
VariableExpression::~VariableExpression() {
// Do nothing
}
SenchaObject VariableExpression::evaluate()
{
SenchaObject result = context->get(name);
result.name = name;
return result;
}
void VariableExpression::execute()
{
std::cout << evaluate().repr() << std::endl;
}
void VariableExpression::execute_quietly()
{
evaluate();
}

View File

@ -0,0 +1,30 @@
/*
* VariableExpression.h
*
* Created on: Dec 17, 2012
* Author: att
*/
#ifndef VARIABLEEXPRESSION_H_
#define VARIABLEEXPRESSION_H_
#include <iostream>
#include <string>
#include "../Context.h"
#include "ASTExpression.h"
class VariableExpression: public ASTExpression {
public:
VariableExpression();
VariableExpression(ASTNode * parent, Context * context, std::string name);
Context * context;
std::string debug();
std::string name;
void execute();
void execute_quietly();
SenchaObject evaluate();
virtual ~VariableExpression();
};
#endif /* VARIABLEEXPRESSION_H_ */

View File

@ -273,31 +273,37 @@ ASTStatement * Parser::statement(ASTNode * parent)
ASTExpression * Parser::prim_expr(ASTNode * expression)
{
ConstantExpression * ce;
BasicExpression * be;
//TODO add reference type to prims, syblol is now just a literal... and floats
if(current_token.get_type() == t_integer)
{
report("Number: " + tok_value + "\n");
ConstantExpression * ce;
ce = new ConstantExpression(expression, std::atoi(tok_value.c_str()));
read_next();
read_next();
return ce;
}
else if(current_token.get_type() == t_float)
{
report("Number: " + tok_value + "\n");
ConstantExpression * ce;
ce = new ConstantExpression(expression, std::atof(tok_value.c_str()));
read_next();
return ce;
}
else if(current_token.get_type() == t_literal)
{
report("Character literal: " + tok_value + "\n");
ConstantExpression * ce;
ce = new ConstantExpression(expression, tok_value);
read_next();
read_next();
return ce;
}
else if(current_token.get_type() == t_keyword)
{
ConstantExpression * ce;
if(tok_value == "true")
{
report("Boolean: " + tok_value + "\n");
@ -310,23 +316,22 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
ce = new ConstantExpression(expression, SenchaObject(false));
read_next();
}
return ce;
}
else if(current_token.get_type() == t_symbol)
{
report("variable: " + tok_value + "\n");
SenchaObject variable;
//TODO is it right?
string name = current_token.value;
//variable = context->get(name);
variable.name = name;
ce = new ConstantExpression(expression, variable, context);
VariableExpression * ve;
ve = new VariableExpression(expression, context, name);
read_next();
return ve;
}
else if(accept("("))
{
report("( ");
report("( ");
BasicExpression * be;
be = static_cast<BasicExpression *>(expr(expression));
report(" ) ");
expect(")");
@ -340,7 +345,6 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
return new IncorrectExpression(expression, error_message);
}
return ce;
}
ASTExpression * Parser::postfix_expr(ASTNode * expression)

View File

@ -19,7 +19,9 @@ I like bananasnull
Context: 0: dupa"); type: null
null
dupa"); declaration? hello?
dupa"); declaration? hello? It's probably temporary effect of searching for this name in variable tables
>>
*/
SenchaObject print(vector<ASTExpression *> arguments)