summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2013-01-12 22:16:30 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2013-01-12 22:16:30 +0100
commit53f8ba2097355f7f488ed04b1d86d037e47216f2 (patch)
tree533fd39064f4148b61c943fdc25d0338a4d5b5d4
parent3a449cb09b812ef7017f584ff3968c40a4f6a83a (diff)
downloadsencha-lang-53f8ba2097355f7f488ed04b1d86d037e47216f2.tar.gz
sencha-lang-53f8ba2097355f7f488ed04b1d86d037e47216f2.tar.bz2
sencha-lang-53f8ba2097355f7f488ed04b1d86d037e47216f2.zip
Some new examples. New tests. Small improvements.imports
-rw-r--r--Sencha-lang/AST/PostfixExpression.cpp5
-rw-r--r--Sencha-lang/Elements/SenchaArray.cpp1
-rw-r--r--Sencha-lang/Elements/SenchaObject.cpp10
-rw-r--r--Sencha-lang/Examples/example10.se46
-rw-r--r--Sencha-lang/Examples/example11.se5
-rw-r--r--Sencha-lang/Examples/strings.se19
-rw-r--r--Sencha-lang/Lexer.cpp8
-rw-r--r--Sencha-lang/Tests/TestParser.cpp72
-rw-r--r--Sencha-lang/Tests/TestParser.h4
-rw-r--r--Sencha-lang/Tests/TestStdLib.cpp36
-rw-r--r--Sencha-lang/Tests/TestStdLib.h21
-rw-r--r--Sencha-lang/Tests/TestSuite.cpp4
-rw-r--r--Sencha-lang/Tests/tests.h9
-rw-r--r--Sencha-lang/Tests/tests_to_be_written2
14 files changed, 230 insertions, 12 deletions
diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp
index 7e3c658..0289d80 100644
--- a/Sencha-lang/AST/PostfixExpression.cpp
+++ b/Sencha-lang/AST/PostfixExpression.cpp
@@ -56,6 +56,11 @@ SenchaObject PostfixExpression::execute() {
SenchaArray array(name, context_manager, called_value.text);
return array.get(access_index.integer);
}
+ else
+ {
+ SenchaArray array(name, context_manager);
+ return array.get(access_index.integer);
+ }
}
}
result.type = SenchaObject::invalid;
diff --git a/Sencha-lang/Elements/SenchaArray.cpp b/Sencha-lang/Elements/SenchaArray.cpp
index be69825..a82e432 100644
--- a/Sencha-lang/Elements/SenchaArray.cpp
+++ b/Sencha-lang/Elements/SenchaArray.cpp
@@ -34,7 +34,6 @@ SenchaArray::SenchaArray(std::string name, ContextManager * context_manager, std
temp.text = letter_string;
temp.type = SenchaObject::string_literal;
context_manager->get_top()->add("_" + name+ "_" + to_string(i), temp);
- //objects.push_back(temp);
}
max_index = text.size();
//context->add(name, SenchaObject(text));
diff --git a/Sencha-lang/Elements/SenchaObject.cpp b/Sencha-lang/Elements/SenchaObject.cpp
index 56c6f3a..5a22bb1 100644
--- a/Sencha-lang/Elements/SenchaObject.cpp
+++ b/Sencha-lang/Elements/SenchaObject.cpp
@@ -33,6 +33,10 @@ void SenchaObject::set_value(std::string text)
this->text = text.substr(1, text.size()-2);
type = string_literal;
}
+ else {
+ this->text = text;
+ type = string_literal;
+ }
}
void SenchaObject::set_new_string(std::string text)
@@ -291,6 +295,12 @@ SenchaObject SenchaObject::operator==(const SenchaObject& right)const
value = true;
}
break;
+ case boolean:
+ if(this->truthy == right.truthy)
+ {
+ value = true;
+ }
+ break;
}
}
diff --git a/Sencha-lang/Examples/example10.se b/Sencha-lang/Examples/example10.se
new file mode 100644
index 0000000..5037051
--- /dev/null
+++ b/Sencha-lang/Examples/example10.se
@@ -0,0 +1,46 @@
+array data[4];
+data[0] = 122;
+data[1] = 3;
+data[2] = -6;
+data[4] = 55;
+
+#this array is a mess, let's sort it!
+def print_array(array)
+{
+ i = 0;
+ while(i < len(array))
+ {
+ print(array[i], " ");
+ i = i + 1;
+ }
+ print("\n");
+}
+
+print_array(data);
+
+#as for now - it won't work
+#I can't pass arrays to functions
+#heh
+
+def sort(data)
+{
+ i = 0;
+ while(i < len(data))
+ {
+ j = 0;
+ while(j < len(data) - 1)
+ {
+ if(data[j] > data[j + 1])
+ {
+ temp = data[j + 1];
+ data[j + 1] = data[j];
+ data[j] = data[j + 1];
+ }
+ j = j + 1;
+ }
+ i = i + 1;
+ }
+ return data;
+}
+
+sort(data);
diff --git a/Sencha-lang/Examples/example11.se b/Sencha-lang/Examples/example11.se
new file mode 100644
index 0000000..e01410b
--- /dev/null
+++ b/Sencha-lang/Examples/example11.se
@@ -0,0 +1,5 @@
+text = "AlfaBetaGamma";
+text[0] = "a";
+text[4] = "b";
+text[8] = "g";
+println(text); \ No newline at end of file
diff --git a/Sencha-lang/Examples/strings.se b/Sencha-lang/Examples/strings.se
new file mode 100644
index 0000000..a3a2bb3
--- /dev/null
+++ b/Sencha-lang/Examples/strings.se
@@ -0,0 +1,19 @@
+def slice(text, from, to)
+{
+ bit = "";
+ i = from;
+ while(i < to)
+ {
+ bit = bit + text[i];
+ i = i + 1;
+ }
+ return bit;
+}
+
+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;
+} \ No newline at end of file
diff --git a/Sencha-lang/Lexer.cpp b/Sencha-lang/Lexer.cpp
index 4c7327b..89f7262 100644
--- a/Sencha-lang/Lexer.cpp
+++ b/Sencha-lang/Lexer.cpp
@@ -106,8 +106,6 @@ pair<string, Token> Lexer::parse_token(string line)
{
string token_value = "";
unsigned int i;
-
-
for(i=0; i< line.size(); i++)
{
if(line[i] == '#')
@@ -115,10 +113,8 @@ pair<string, Token> Lexer::parse_token(string line)
i++;
while(line[i] != '\n' && i < line.size()) i++;
break;
- }
-
+ }
if(token_value == "" && isspace(line[i])) continue;
-
if(isdigit(line[i]) || line[i] == '-')
{
token_value += line[i++];
@@ -128,7 +124,6 @@ pair<string, Token> Lexer::parse_token(string line)
else break;
}
}
-
if(line[i] == '\"')
{
token_value += line[i++];
@@ -185,7 +180,6 @@ bool Lexer::is_keyword(string value)
bool Lexer::is_punctuation(char c)
{
-
for(unsigned int i=0; i< punctuation.size(); i++)
{
if(c == punctuation[i]) return true;
diff --git a/Sencha-lang/Tests/TestParser.cpp b/Sencha-lang/Tests/TestParser.cpp
index d656306..633fcc6 100644
--- a/Sencha-lang/Tests/TestParser.cpp
+++ b/Sencha-lang/Tests/TestParser.cpp
@@ -35,18 +35,85 @@ std::string TestParser::test_parsing_and_evaluating_logical_expressions()
return test_report;
}
+std::string TestParser::test_changing_letters_in_string()
+{
+ std::string test_report = "";
+ std::string preparations = "text = \"AlfaBetaGamma\";";
+ std::string changes = "text[0] = \"a\";";
+ changes += "text[4] = \"b\";";
+ changes += "text[8] = \"g\";";
+ changes += "text;";
+
+ ContextManager context_manager;
+ Parser parser(&context_manager);
+ Lexer lexer;
+ auto tokens = lexer.parse_line(preparations + changes);
+ parser.add_tokens(tokens);
+ parser.interpret();
+ SenchaObject value = parser.program->execute();
+ //std::cout << value.repr() << std::endl;
+ muu_assert("Changed text isn't alfabetagamma", value.text == "alfabetagamma");
+
+ return test_report;
+}
+
+
+
std::string TestParser::test_adding_new_function()
{
std::string test_report = "";
std::string function_declaration = "def print_hello(how_many_times) {";
function_declaration += " repeat(how_many_times) { print(\"Hello\");}";
+ ContextManager context_manager;
+ Parser parser(&context_manager);
+ Lexer lexer;
+ auto tokens = lexer.parse_line(function_declaration);
+ parser.add_tokens(tokens);
+ parser.interpret();
+
+ muu_assert("Function couldn't be added", context_manager.get_top()->contains_function("print_hello"));
+
+ return test_report;
+}
+
+std::string TestParser::test_writing_and_accessing_array_elements()
+{
+ std::string test_report = "";
+ std::string table_definition = "array data[4];";
+ table_definition += "data[0] = 3; data[1] = -88; data[2] = true; data[3] = 1.222;";
+
+ std::string data_access = "data[0];";
ContextManager context;
Parser parser(&context);
Lexer lexer;
- auto tokens = lexer.parse_line(function_declaration);
+ auto tokens = lexer.parse_line(table_definition + data_access);
parser.add_tokens(tokens);
parser.interpret();
+ SenchaObject value = parser.program->execute();
+
+ muu_assert("data[0] != 3", (value == SenchaObject(3)).truthy );
+ data_access = "data[1];";
+ tokens = lexer.parse_line(data_access);
+ parser.add_tokens(tokens);
+ parser.interpret();
+ value = parser.program->execute();
+ muu_assert("data[1] != -88", (value == SenchaObject(-88)).truthy );
+
+ data_access = "data[2];";
+ tokens = lexer.parse_line(data_access);
+ parser.add_tokens(tokens);
+ parser.interpret();
+ value = parser.program->execute();
+ muu_assert("data[2] != true", (value == SenchaObject(true)).truthy );
+
+ data_access = "data[3];";
+ tokens = lexer.parse_line(data_access);
+ parser.add_tokens(tokens);
+ parser.interpret();
+ value = parser.program->execute();
+ muu_assert("data[3] != 1.222", (value == SenchaObject(1.222)).truthy );
+
return test_report;
}
@@ -123,6 +190,9 @@ std::string TestParser::all_tests()
mu_run_test(test_parsing_and_evaluating_logical_expressions);
mu_run_test(test_calling_funtion);
+ mu_run_test(test_changing_letters_in_string);
+ //mu_run_test(test_adding_new_function);
+ mu_run_test(test_writing_and_accessing_array_elements);
return test_report;
}
diff --git a/Sencha-lang/Tests/TestParser.h b/Sencha-lang/Tests/TestParser.h
index acad6fe..a2a4843 100644
--- a/Sencha-lang/Tests/TestParser.h
+++ b/Sencha-lang/Tests/TestParser.h
@@ -28,6 +28,10 @@ private:
std::string test_adding_new_function();
std::string test_calling_funtion();
std::string prepare_some_function_declarations();
+ std::string test_writing_and_accessing_array_elements();
+ std::string test_changing_letters_in_string();
+
+
std::vector<InputOutputPair> prepare_logical_input();
virtual std::string all_tests();
diff --git a/Sencha-lang/Tests/TestStdLib.cpp b/Sencha-lang/Tests/TestStdLib.cpp
new file mode 100644
index 0000000..f3521e7
--- /dev/null
+++ b/Sencha-lang/Tests/TestStdLib.cpp
@@ -0,0 +1,36 @@
+/*
+ * TestStdLib.cpp
+ *
+ * Created on: Jan 12, 2013
+ * Author: att
+ */
+
+#include "TestStdLib.h"
+
+TestStdLib::TestStdLib() {
+ // TODO Auto-generated constructor stub
+
+}
+
+TestStdLib::~TestStdLib() {
+ // TODO Auto-generated destructor stub
+}
+
+std::string TestStdLib::test_string_lib()
+{
+ std::string test_report = "";
+
+
+ muu_assert("Write this test", false);
+
+ return test_report;
+}
+
+std::string TestStdLib::all_tests()
+{
+ std::string test_report = "";
+
+ mu_run_test(test_string_lib);
+
+ return test_report;
+}
diff --git a/Sencha-lang/Tests/TestStdLib.h b/Sencha-lang/Tests/TestStdLib.h
new file mode 100644
index 0000000..8f86f09
--- /dev/null
+++ b/Sencha-lang/Tests/TestStdLib.h
@@ -0,0 +1,21 @@
+/*
+ * TestStdLib.h
+ *
+ * Created on: Jan 12, 2013
+ * Author: att
+ */
+
+#ifndef TESTSTDLIB_H_
+#define TESTSTDLIB_H_
+
+#include "TestSuite.h"
+
+class TestStdLib: public TestSuite {
+public:
+ TestStdLib();
+ virtual ~TestStdLib();
+ std::string test_string_lib();
+ virtual std::string all_tests();
+};
+
+#endif /* TESTSTDLIB_H_ */
diff --git a/Sencha-lang/Tests/TestSuite.cpp b/Sencha-lang/Tests/TestSuite.cpp
index 3c37b25..f50531c 100644
--- a/Sencha-lang/Tests/TestSuite.cpp
+++ b/Sencha-lang/Tests/TestSuite.cpp
@@ -30,13 +30,13 @@ void TestSuite::run_tests()
std::string result = all_tests();
if(result != "")
{
- std::cout << "\x1b[00;31mSOME TESTS DIDN\'T PASS\x1b[00;00m" << std::endl; //Sets color to red
+ std::cout << "\n\x1b[00;31mSOME TESTS DIDN\'T PASS\x1b[00;00m" << std::endl; //Sets color to red
std::cout << result << std::endl;
}
else
{
- std::cout << "\x1b[00;32mALL TESTS PASSED\x1b[00;00m\n" << std::endl; //Sets color to green
+ std::cout << "\n\x1b[00;32mALL TESTS PASSED\x1b[00;00m" << std::endl; //Sets color to green
}
std::cout << tests_passed << " TESTS PASSED\n";
std::cout << tests_failed << " TESTS FAILED\n";
diff --git a/Sencha-lang/Tests/tests.h b/Sencha-lang/Tests/tests.h
index 49700d1..637da00 100644
--- a/Sencha-lang/Tests/tests.h
+++ b/Sencha-lang/Tests/tests.h
@@ -12,6 +12,7 @@
#include "TestLexer.h"
#include "TestASTInspector.h"
#include "TestParser.h"
+#include "TestStdLib.h"
#include "../Lexer.h"
#include "../Parser.h"
@@ -21,20 +22,26 @@ using namespace std;
void run_test_suites()
{
+ std::cout << "Testing lexer..." << std::endl;
TestLexer test_lexer;
test_lexer.run_tests();
+ std::cout << "Testing ASTInspector..." << std::endl;
TestASTInspector test_inspector;
test_inspector.run_tests();
+ std::cout << "Testing parser..." << std::endl;
TestParser test_parser;
test_parser.run_tests();
+ std::cout << "Testing standard library..." << std::endl;
+ TestStdLib test_standard_library;
+ test_standard_library.run_tests();
}
void run_tests()
{
- cout << "derp!" << endl;
+ cout << "daaarp!" << endl;
run_test_suites();
}
diff --git a/Sencha-lang/Tests/tests_to_be_written b/Sencha-lang/Tests/tests_to_be_written
index 1ab0d83..26a7370 100644
--- a/Sencha-lang/Tests/tests_to_be_written
+++ b/Sencha-lang/Tests/tests_to_be_written
@@ -24,3 +24,5 @@ My lexer doesn't have enough tests, i think. More tests! For example, given, acq
And em, It'd be nice if I could run test from console like ./Sencha-lang --run-tests or something. Done.
+Okay. Let's test some table manipulation.
+