summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/AST
diff options
context:
space:
mode:
Diffstat (limited to 'Sencha-lang/AST')
-rw-r--r--Sencha-lang/AST/Assignment.cpp10
-rw-r--r--Sencha-lang/AST/DeclarationStatement.cpp24
-rw-r--r--Sencha-lang/AST/DeclarationStatement.h4
-rw-r--r--Sencha-lang/AST/PostfixExpression.cpp54
-rw-r--r--Sencha-lang/AST/PostfixExpression.h5
-rw-r--r--Sencha-lang/AST/VariableExpression.cpp4
6 files changed, 94 insertions, 7 deletions
diff --git a/Sencha-lang/AST/Assignment.cpp b/Sencha-lang/AST/Assignment.cpp
index fe4c79a..19c0cff 100644
--- a/Sencha-lang/AST/Assignment.cpp
+++ b/Sencha-lang/AST/Assignment.cpp
@@ -9,11 +9,19 @@
SenchaObject Assignment::evaluate()
{
+ //TODO do something different with name evaluation
+ auto left_value = static_cast<ASTExpression *>(children[0])->evaluate();
+ if(left_value.name != "")
+ {
+ name = left_value.name;
+ }
auto right_value = static_cast<ASTExpression *>(children[1])->evaluate();
auto context = context_manager->get_top();
context->add(name, right_value);
- return children[0]->execute();
+ left_value = right_value;
+ left_value.name = name;
+ return left_value;
}
diff --git a/Sencha-lang/AST/DeclarationStatement.cpp b/Sencha-lang/AST/DeclarationStatement.cpp
index fdc02d9..9539221 100644
--- a/Sencha-lang/AST/DeclarationStatement.cpp
+++ b/Sencha-lang/AST/DeclarationStatement.cpp
@@ -34,10 +34,12 @@ DeclarationStatement::DeclarationStatement(ContextManager * context_manager)
this->context_manager = context_manager;
this->name_of_context = "global";
is_function = false;
- body = NULL;
+ 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)
@@ -45,6 +47,11 @@ 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)
{
@@ -59,6 +66,21 @@ SenchaObject DeclarationStatement::execute()
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();
diff --git a/Sencha-lang/AST/DeclarationStatement.h b/Sencha-lang/AST/DeclarationStatement.h
index a8ed045..e7c285d 100644
--- a/Sencha-lang/AST/DeclarationStatement.h
+++ b/Sencha-lang/AST/DeclarationStatement.h
@@ -25,6 +25,8 @@ public:
std::string name_of_context;
SenchaObject right_value;
ASTStatement * body;
+ bool is_array;
+ ASTExpression * array_size_expression;
std::vector<std::string> arguments;
bool is_function;
@@ -32,7 +34,7 @@ public:
DeclarationStatement(ContextManager * context);
void add_name(std::string);
-
+ void add_array_size(ASTExpression * size_expression);
void add_argument(std::string name);
void add_body(ASTStatement * statement);
diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp
index 69fd174..5fac1dc 100644
--- a/Sencha-lang/AST/PostfixExpression.cpp
+++ b/Sencha-lang/AST/PostfixExpression.cpp
@@ -8,7 +8,14 @@
#include "PostfixExpression.h"
PostfixExpression::PostfixExpression(std::string name, ContextManager * context_manager):
- name(name), context_manager(context_manager), native(false)
+ name(name), operation("call"), context_manager(context_manager)
+{
+ type= "PostfixExpression";
+ access_index_expr = nullptr;
+}
+
+PostfixExpression::PostfixExpression(std::string name, ContextManager * context_manager, ASTExpression * access_index_expr):
+ name(name), operation("access"), context_manager(context_manager), access_index_expr(access_index_expr)
{
type= "PostfixExpression";
}
@@ -30,11 +37,52 @@ void PostfixExpression::add_argument(ASTExpression * expression)
SenchaObject PostfixExpression::evaluate()
{
- return context_manager->execute_function(name, arguments);
+ SenchaObject result = SenchaObject();
+ if(operation == "call")
+ return context_manager->execute_function(name, arguments);
+ else if(operation == "access")
+ {
+ SenchaObject called_value = context_manager->get_top()->get(name);
+ SenchaObject access_index = access_index_expr->execute();
+ //std::cout << "index is: " << access_index.repr() << std::endl;
+ if(access_index.type == SenchaObject::integer_number)
+ {
+ if(called_value.type == SenchaObject::string_literal)
+ {
+ SenchaArray array(name, context_manager->get_top(), called_value.text);
+ return array.get(access_index.integer);
+ }
+ else
+ {
+ SenchaArray array(name, context_manager->get_top());
+ return array.get(access_index.integer);
+ }
+ }
+
+ }
+ result.type = SenchaObject::invalid;
+ return result;
}
SenchaObject PostfixExpression::execute() {
- return context_manager->execute_function(name, arguments);
+ SenchaObject result = SenchaObject();
+ if(operation == "call")
+ return context_manager->execute_function(name, arguments);
+ else if(operation == "access")
+ {
+ SenchaObject called_value = context_manager->get_top()->get(name);
+ SenchaObject access_index = access_index_expr->execute();
+ if(access_index.type == SenchaObject::integer_number)
+ {
+ if(called_value.type == SenchaObject::string_literal)
+ {
+ SenchaArray array(name, context_manager->get_top(), called_value.text);
+ return array.get(access_index.integer);
+ }
+ }
+ }
+ result.type = SenchaObject::invalid;
+ return result;
}
diff --git a/Sencha-lang/AST/PostfixExpression.h b/Sencha-lang/AST/PostfixExpression.h
index 806a353..f4dfe7e 100644
--- a/Sencha-lang/AST/PostfixExpression.h
+++ b/Sencha-lang/AST/PostfixExpression.h
@@ -10,6 +10,7 @@
#include "ASTExpression.h"
#include "ASTStatement.h"
#include "../ContextManager.h"
+#include "../Elements/SenchaArray.h"
/**
* PostfixExpression is in current implementation an abstraction
@@ -18,9 +19,10 @@
class PostfixExpression : public ASTExpression {
public:
std::string name;
+ std::string operation;
ContextManager * context_manager;
- bool native;
std::vector<ASTExpression *> arguments;
+ ASTExpression * access_index_expr;
void add_argument(ASTExpression * expression);
virtual SenchaObject evaluate();
@@ -28,6 +30,7 @@ public:
PostfixExpression( std::string name, ContextManager * context);
+ PostfixExpression(std::string name, ContextManager * context_manager, ASTExpression * access_index_expr);
virtual ~PostfixExpression();
};
diff --git a/Sencha-lang/AST/VariableExpression.cpp b/Sencha-lang/AST/VariableExpression.cpp
index 22f4465..8adfaaf 100644
--- a/Sencha-lang/AST/VariableExpression.cpp
+++ b/Sencha-lang/AST/VariableExpression.cpp
@@ -24,6 +24,10 @@ SenchaObject VariableExpression::evaluate()
auto context = context_manager->get_top();
//std::cout << "I'm retrieving variable " + name + " from " + context->name << std::endl;
SenchaObject result = context->get(name);
+ if(result.type == SenchaObject::string_literal)
+ {
+ result = context->get_updated_string(name);
+ }
//std::cout << "And it's: " + result.repr() << std::endl;
result.name = name;
return result;