I repaired broken tests.

imports
Justyna Ilczuk 2013-01-10 11:46:41 +01:00
parent d959b701bf
commit 3ad07986ff
12 changed files with 36 additions and 35 deletions

View File

@ -13,7 +13,7 @@ class ASTStatement : public ASTNode {
public:
ASTStatement();
virtual SenchaObject execute() = 0;
virtual SenchaObject evaluate() { return SenchaObject(); }
virtual SenchaObject evaluate() = 0;
virtual ~ASTStatement();
};

View File

@ -32,7 +32,7 @@ void DeclarationStatement::add_right_value(ASTExpression * right)
DeclarationStatement::DeclarationStatement(ContextManager * context_manager)
{
this->context_manager = context_manager;
this->name_of_context = "global";//context_manager->create_new_context()->name;
this->name_of_context = "global";
is_function = false;
body = NULL;
right_value = SenchaObject();

View File

@ -37,6 +37,9 @@ public:
void add_body(ASTStatement * statement);
virtual SenchaObject execute();
virtual SenchaObject evaluate() {
return right_value;
}
virtual ~DeclarationStatement();
};

View File

@ -26,6 +26,7 @@ public:
void add_else_block(ASTStatement * statement);
bool is_else;
virtual SenchaObject execute();
virtual SenchaObject evaluate() { return execute(); }
bool evaluate_condition();
ASTNode * condition() { return children[0]; }
ASTNode * then_block() { return children[1]; }

View File

@ -23,12 +23,16 @@ public:
std::vector<std::string> DEFAULT_LIB_PATHS;
ContextManager * context_manager;
bool correctly_imported;
ImportStatement(std::string name_of_module, ContextManager * context_manager);
ASTNode * import_body;
std::string strip_string(std::string text);
void prepare_defaults();
std::vector<std::string> prepare_paths(std::string name_of_module);
SenchaObject execute();
SenchaObject evaluate() {
return SenchaObject(correctly_imported);
}
virtual ~ImportStatement();
};

View File

@ -22,6 +22,7 @@ public:
ASTStatement * body;
ASTExpression * how_many_times_expr;
virtual SenchaObject execute();
virtual SenchaObject evaluate() { return execute(); }
void add_body(ASTStatement * statement);
virtual ~RepeatStatement();
};

View File

@ -7,7 +7,7 @@
#include "UnaryExpression.h"
UnaryExpression::UnaryExpression(ASTNode * argument, std::string oper) {
UnaryExpression::UnaryExpression(ASTExpression * argument, std::string oper) {
type = "UnaryExpression";
children.push_back(argument);
this->oper = oper;

View File

@ -9,7 +9,10 @@
#define UNARYEXPRESSION_H_
#include "ASTExpression.h"
/**
* UnaryExpression is abstraction of unary operator and it's operand (right binding);
* It can be build like that: UnaryExpression(some_expression, "-")
*/
class UnaryExpression: public ASTExpression {
public:
@ -20,7 +23,7 @@ public:
virtual SenchaObject execute();
virtual SenchaObject evaluate();
UnaryExpression(ASTNode * argument, std::string oper);
UnaryExpression(ASTExpression * argument, std::string oper);
UnaryExpression();
virtual ~UnaryExpression();
};

View File

@ -21,6 +21,7 @@ public:
virtual ~WhileNode();
ASTStatement * body;
virtual SenchaObject execute();
virtual SenchaObject evaluate() { return execute(); }
bool evaluate_condition();
};

View File

@ -57,28 +57,33 @@ std::string TestParser::test_calling_funtion()
Lexer lexer;
ContextManager context;
Parser parser(&context);
std::string call = " max(77, 12);";
auto tokens = lexer.parse_line(prepare_some_function_declarations() + call);
std::string call_max = " max(77, 12);";
std::string declarations = prepare_some_function_declarations();
auto tokens = lexer.parse_line( declarations + call_max);
parser.add_tokens(tokens);
parser.interpret();
auto value = parser.program->execute_last();
muu_assert("function max doesn't work", value.integer == 77);
std::cout << "function call returned " << value.repr() << std::endl;
auto value1 = parser.program->execute();
std::string call_multiply = " multiply_string(\"banana\", 3);";
muu_assert("function max doesn't work", value1.integer == 77);
parser.erase_all();
parser.add_tokens(lexer.parse_line(declarations + call_multiply));
parser.interpret();
auto value2 = parser.program->execute();
muu_assert("function multiply_string doesn't work", value2.text == "bananabananabanana");
return test_report;
}
std::string TestParser::prepare_some_function_declarations()
{
/*std::string print_hello_declaration ="def print_hello(how_many_times) {";
print_hello_declaration += " repeat(how_many_times) { print(\"Hello\");}";
std::string how_long_declaration = "def how_long(some_text) {";
how_long_declaration += " return len(some_text);}";
std::string max_declaration = "def max(a, b) { ";
max_declaration += "if(a > b) return a;";
max_declaration += "else return b; }";
max_declaration += "if(a > b) { return a; }";
max_declaration += "else { return b;} }";
std::string min_declaration = "def min(a, b) { ";
min_declaration += "if(a < b) return a;";
@ -86,29 +91,13 @@ std::string TestParser::prepare_some_function_declarations()
std::string multiply_string_declaration = "def multiply_string(text, i) {";
multiply_string_declaration += "result = \"\"; ";
multiply_string_declaration += "repeats(i) { result = result + text }";
multiply_string_declaration += " return result; ";*/
multiply_string_declaration += "repeat(i) { result = result + text; }";
multiply_string_declaration += " return result;} ";
std::string max_declaration = "def max(a, b) { ";
max_declaration += "if(a > b) return a; ";
max_declaration += "else return b; }";
return max_declaration;
return max_declaration + min_declaration + multiply_string_declaration;
}
std::vector<TestParser::SInputOutputPair> TestParser::prepare_function_call_input()
{
std::vector<SInputOutputPair> function_calls;
function_calls.push_back(SInputOutputPair("print_hello(77);", SenchaObject()));
function_calls.push_back(SInputOutputPair("how_long(\"elephant\");", SenchaObject(8)));
function_calls.push_back(SInputOutputPair("max(77, 12);", SenchaObject(77)));
function_calls.push_back(SInputOutputPair("min(12, 1);", SenchaObject(1)));
function_calls.push_back(SInputOutputPair("multiply_string(\"alfa\", 3);", SenchaObject("alfaalfaalfa")));
return function_calls;
}
std::vector<TestParser::InputOutputPair> TestParser::prepare_logical_input()
{

View File

@ -29,7 +29,6 @@ private:
std::string test_calling_funtion();
std::string prepare_some_function_declarations();
std::vector<InputOutputPair> prepare_logical_input();
std::vector<TestParser::SInputOutputPair> prepare_function_call_input();
virtual std::string all_tests();

View File

@ -22,5 +22,5 @@ What's more?
My lexer doesn't have enough tests, i think. More tests! For example, given, acquired, desired.
And em, It'd be nice if I could run test from console like ./Sencha-lang --run-tests or something.
And em, It'd be nice if I could run test from console like ./Sencha-lang --run-tests or something. Done.