sencha-lang/Sencha-lang/AST/DeclarationStatement.cpp

75 lines
1.5 KiB
C++

/*
* DeclarationStatement.cpp
*
* Created on: Dec 9, 2012
* Author: attero
*/
#include "DeclarationStatement.h"
DeclarationStatement::~DeclarationStatement() {
if(is_function)
{
delete body;
}
for(auto it = children.begin(); it != children.end(); )
{
delete *it;
it = children.erase(it);
}
}
void DeclarationStatement::add_right_value(ASTExpression * right)
{
right_value = right->evaluate();
children[0] = right;
}
//def print_d() { print("dupa"); }
DeclarationStatement::DeclarationStatement(ContextManager * context_manager)
{
this->context_manager = context_manager;
this->name_of_context = "global";//context_manager->create_new_context()->name;
is_function = false;
body = NULL;
right_value = SenchaObject();
children.push_back(new ConstantExpression(SenchaObject()));
this->type = "DeclarationStatement";
}
void DeclarationStatement::add_name(std::string name)
{
this->name = name;
}
void DeclarationStatement::add_argument(std::string name)
{
arguments.push_back(name);
}
SenchaObject DeclarationStatement::execute()
{
if(is_function)
{
SenchaFunction * sf = new SenchaFunction(name, arguments, body);
context_manager->context("global")->register_function(name, sf);
return SenchaObject();
}
else
{
auto context = context_manager->get_top();
context->add(name, right_value);
return children[0]->execute();
}
}
void DeclarationStatement::add_body(ASTStatement * statement)
{
is_function = true;
body = statement;
}