summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2013-01-12 19:20:47 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2013-01-12 19:20:47 +0100
commit3a449cb09b812ef7017f584ff3968c40a4f6a83a (patch)
treea46d2dbf8af84042f2410c86aad4a1a493faf062
parent7d60b159cc5a63f3835d253a109ae5999bf29fc5 (diff)
downloadsencha-lang-3a449cb09b812ef7017f584ff3968c40a4f6a83a.tar.gz
sencha-lang-3a449cb09b812ef7017f584ff3968c40a4f6a83a.tar.bz2
sencha-lang-3a449cb09b812ef7017f584ff3968c40a4f6a83a.zip
Small changes in Array. New programming examples. Comments enabled.
-rw-r--r--Sencha-lang/AST/PostfixExpression.cpp29
-rw-r--r--Sencha-lang/Elements/SenchaArray.cpp21
-rw-r--r--Sencha-lang/Elements/SenchaArray.h8
-rw-r--r--Sencha-lang/Examples/example7.se14
-rw-r--r--Sencha-lang/Examples/example8.se60
-rw-r--r--Sencha-lang/Examples/example9.se9
-rw-r--r--Sencha-lang/Lexer.cpp15
7 files changed, 114 insertions, 42 deletions
diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp
index 5fac1dc..7e3c658 100644
--- a/Sencha-lang/AST/PostfixExpression.cpp
+++ b/Sencha-lang/AST/PostfixExpression.cpp
@@ -37,31 +37,7 @@ void PostfixExpression::add_argument(ASTExpression * expression)
SenchaObject PostfixExpression::evaluate()
{
- 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;
+ return execute();
}
SenchaObject PostfixExpression::execute() {
@@ -76,7 +52,8 @@ SenchaObject PostfixExpression::execute() {
{
if(called_value.type == SenchaObject::string_literal)
{
- SenchaArray array(name, context_manager->get_top(), called_value.text);
+ called_value = context_manager->get_top()->get_updated_string(name);
+ SenchaArray array(name, context_manager, called_value.text);
return array.get(access_index.integer);
}
}
diff --git a/Sencha-lang/Elements/SenchaArray.cpp b/Sencha-lang/Elements/SenchaArray.cpp
index 1bd004d..be69825 100644
--- a/Sencha-lang/Elements/SenchaArray.cpp
+++ b/Sencha-lang/Elements/SenchaArray.cpp
@@ -9,12 +9,12 @@
-SenchaArray::SenchaArray(std::string name, Context * context) {
+SenchaArray::SenchaArray(std::string name, ContextManager * context_manager) {
// TODO Auto-generated constructor stub
max_index = 0;
- this->context = context;
+ this->context_manager = context_manager;
this->name = name;
- context->add(name, SenchaObject());
+ context_manager->get_top()->add(name, SenchaObject());
}
SenchaArray::~SenchaArray() {
@@ -22,10 +22,10 @@ SenchaArray::~SenchaArray() {
}
-SenchaArray::SenchaArray(std::string name, Context * context, std::string text)
+SenchaArray::SenchaArray(std::string name, ContextManager * context_manager, std::string text)
{
max_index = 0;
- this->context = context;
+ this->context_manager = context_manager;
this->name = name;
for(unsigned int i = 0; i < text.size(); i++)
{
@@ -33,7 +33,7 @@ SenchaArray::SenchaArray(std::string name, Context * context, std::string text)
std::string letter_string = text.substr(i, 1) ;
temp.text = letter_string;
temp.type = SenchaObject::string_literal;
- context->add("_" + name+ "_" + to_string(i), temp);
+ context_manager->get_top()->add("_" + name+ "_" + to_string(i), temp);
//objects.push_back(temp);
}
max_index = text.size();
@@ -42,7 +42,7 @@ SenchaArray::SenchaArray(std::string name, Context * context, std::string text)
SenchaObject SenchaArray::get(IndexNumber index)
{
- SenchaObject result = context->get("_" + name +"_" +to_string(index));
+ SenchaObject result = context_manager->get_top()->get("_" + name +"_" +to_string(index));
result.name = "_" + name + "_" + to_string(index);
return result;
//return objects[index];
@@ -50,15 +50,14 @@ SenchaObject SenchaArray::get(IndexNumber index)
SenchaArray::IndexNumber SenchaArray::add(SenchaObject value)
{
- context->add("_" + name+ "_" + to_string(max_index), value);
+ context_manager->get_top()->add("_" + name+ "_" + to_string(max_index), value);
max_index++;
- //objects.push_back(value);
- max_index = objects.size();
+
return max_index;
}
void SenchaArray::set(IndexNumber index, SenchaObject value)
{
//objects[index] = value;
- context->add("_" + name +"_" +to_string(index), value);
+ context_manager->get_top()->add("_" + name +"_" +to_string(index), value);
}
diff --git a/Sencha-lang/Elements/SenchaArray.h b/Sencha-lang/Elements/SenchaArray.h
index 43c97a2..d100553 100644
--- a/Sencha-lang/Elements/SenchaArray.h
+++ b/Sencha-lang/Elements/SenchaArray.h
@@ -9,13 +9,13 @@
#define SENCHAARRAY_H_
#include "SenchaObject.h"
#include "Element.h"
-#include "../Context.h"
+#include "../ContextManager.h"
class SenchaArray: public Element {
public:
- SenchaArray(std::string name, Context * context);
- SenchaArray(std::string name, Context * context, std::string);
- Context * context;
+ SenchaArray(std::string name, ContextManager * context_manager);
+ SenchaArray(std::string name, ContextManager * context_manager, std::string);
+ ContextManager * context_manager;
std::string name;
typedef int IndexNumber;
diff --git a/Sencha-lang/Examples/example7.se b/Sencha-lang/Examples/example7.se
new file mode 100644
index 0000000..f3a46f3
--- /dev/null
+++ b/Sencha-lang/Examples/example7.se
@@ -0,0 +1,14 @@
+println("I start program by assigning 0 to x");
+x = 0;
+println("Then I declare a table, which is not obligatory");
+array t[100];
+println("After that I fill the table like that:");
+while (x < 100)
+{
+ print("Index: ", x);
+ t[x] = 3*x;
+ println(", value: ", t[x]);
+ x = x + 1;
+}
+
+println("Pretty simple isn't it?"); \ No newline at end of file
diff --git a/Sencha-lang/Examples/example8.se b/Sencha-lang/Examples/example8.se
new file mode 100644
index 0000000..9274d14
--- /dev/null
+++ b/Sencha-lang/Examples/example8.se
@@ -0,0 +1,60 @@
+println("Some string manipulation");
+
+text = "When I was young, everything was simpler.";
+
+println("Our text looks like that:");
+println(text);
+
+x = 0;
+while(x < 4)
+{
+ print(text[x]);
+ x = x +1;
+}
+
+print("\n");
+
+println("We can replace some content of the text, for example \" whatever \" ");
+
+println("But first, let's try some simple replacement!");
+
+text[5] = "M";
+text[3] = "K";
+
+println("Now text is: ", text);
+
+println("Let's define some useful functions for us:");
+
+def slice(text, from, to)
+{
+ bit = "";
+ i = from;
+ while(i < to)
+ {
+ bit = bit + text[i];
+ i = i + 1;
+ }
+ return bit;
+}
+
+println("We defined slice, let\'s try it now!");
+
+println(slice(text, 0, 8));
+
+println("And some more: ", slice(text, 9, 14));
+
+println("Sweet!");
+println("What about replace function?");
+
+def replace(text, replacement, start_position, end_position)
+{
+ prefix = slice(text, 0, start_position);
+ suffix = slice(text, end_position, len(text));
+ result = prefix + replacement + suffix;
+ return result;
+}
+
+println("It looks pretty simple. Lets replace some text!");
+
+println("Original: ", text);
+println("After replacement: ", replace(text, "nice", 11, 16)); \ No newline at end of file
diff --git a/Sencha-lang/Examples/example9.se b/Sencha-lang/Examples/example9.se
new file mode 100644
index 0000000..396871e
--- /dev/null
+++ b/Sencha-lang/Examples/example9.se
@@ -0,0 +1,9 @@
+#some comment
+println("x = ");
+x = 99;
+println(x);
+#another comment
+println(" - Shouldn't comments be useful?"); #why?
+
+#If not, what's the point in writing them?
+println(" - Aren't they?"); \ No newline at end of file
diff --git a/Sencha-lang/Lexer.cpp b/Sencha-lang/Lexer.cpp
index 5c8f42c..4c7327b 100644
--- a/Sencha-lang/Lexer.cpp
+++ b/Sencha-lang/Lexer.cpp
@@ -37,6 +37,12 @@ std::string Lexer::unescape_string(string text)
case 't':
replacement = '\t';
break;
+ case '\"':
+ replacement = '\"';
+ break;
+ case 39:
+ replacement = '\'';
+ break;
case '[':
replacement = 27;
break;
@@ -103,7 +109,14 @@ pair<string, Token> Lexer::parse_token(string line)
for(i=0; i< line.size(); i++)
- {
+ {
+ if(line[i] == '#')
+ {
+ i++;
+ while(line[i] != '\n' && i < line.size()) i++;
+ break;
+ }
+
if(token_value == "" && isspace(line[i])) continue;
if(isdigit(line[i]) || line[i] == '-')