Some new examples. New tests. Small improvements.

imports
Justyna Ilczuk 2013-01-12 22:16:30 +01:00
parent 3a449cb09b
commit 53f8ba2097
14 changed files with 231 additions and 13 deletions

View File

@ -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;

View File

@ -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));

View File

@ -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;
}
}

View File

@ -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);

View File

@ -0,0 +1,5 @@
text = "AlfaBetaGamma";
text[0] = "a";
text[4] = "b";
text[8] = "g";
println(text);

View File

@ -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;
}

View File

@ -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;

View File

@ -35,19 +35,86 @@ 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;
Parser parser(&context);
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(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;
}

View File

@ -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();

View File

@ -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;
}

View File

@ -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_ */

View File

@ -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";

View File

@ -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();
}

View File

@ -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.