blob: 9539221aa8156fda95893ae7f5857fa83c83ccc2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*
* 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";
is_function = false;
body = nullptr;
right_value = SenchaObject();
children.push_back(new ConstantExpression(SenchaObject()));
this->type = "DeclarationStatement";
this->is_array = false;
array_size_expression = nullptr;
}
void DeclarationStatement::add_name(std::string name)
{
this->name = name;
}
void DeclarationStatement::add_array_size(ASTExpression * expression)
{
this->is_array = true;
array_size_expression = expression;
}
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 if(is_array)
{
auto expr_value = array_size_expression->evaluate();
if(expr_value.type == SenchaObject::integer_number)
{
int array_size = expr_value.integer;
Context * context = context_manager->get_top();
for(int i = 0; i< array_size; i++)
{
context->add("_" + name + "_" + to_string(i), SenchaObject());
}
}
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;
}
|