slsjfl
parent
6296524bec
commit
4526de47cf
|
@ -29,7 +29,7 @@ SenchaObject BasicStatement::execute()
|
|||
{
|
||||
for(auto it = children.begin(); it!=children.end() -1; it++)
|
||||
(*it)->execute();
|
||||
return children[children.size() -1]->evaluate();
|
||||
return children[children.size() -1]->execute();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,12 @@ SenchaObject ContextManager::execute_function(std::string name, std::vector<ASTE
|
|||
{
|
||||
SenchaFunction * function = contexts["global"]->registered_sfunctions[name];
|
||||
std::string name_of_context = create_new_context()->name;
|
||||
if( arguments.size() != function->names_of_arguments.size())
|
||||
{
|
||||
result.type = SenchaObject::invalid;
|
||||
return result;
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < function->names_of_arguments.size(); i++)
|
||||
get_top()->add(function->names_of_arguments[i], arguments[i]->evaluate());
|
||||
result = (*function)();
|
||||
|
|
|
@ -54,14 +54,22 @@ std::string TestParser::test_adding_new_function()
|
|||
std::string TestParser::test_calling_funtion()
|
||||
{
|
||||
std::string test_report = "";
|
||||
register_some_functions();
|
||||
std::string call = "print_hello(77);";
|
||||
Lexer lexer;
|
||||
ContextManager context;
|
||||
Parser parser(&context);
|
||||
std::string call = " max(77, 12);";
|
||||
auto tokens = lexer.parse_line(prepare_some_function_declarations() + call);
|
||||
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;
|
||||
return test_report;
|
||||
}
|
||||
|
||||
void TestParser::register_some_functions()
|
||||
std::string TestParser::prepare_some_function_declarations()
|
||||
{
|
||||
std::string print_hello_declaration ="def print_hello(how_many_times) {";
|
||||
/*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) {";
|
||||
|
@ -79,8 +87,13 @@ void TestParser::register_some_functions()
|
|||
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 += " 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;
|
||||
}
|
||||
|
||||
std::vector<TestParser::SInputOutputPair> TestParser::prepare_function_call_input()
|
||||
|
@ -120,7 +133,7 @@ std::string TestParser::all_tests()
|
|||
std::string test_report = "";
|
||||
|
||||
mu_run_test(test_parsing_and_evaluating_logical_expressions);
|
||||
|
||||
mu_run_test(test_calling_funtion);
|
||||
|
||||
return test_report;
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ private:
|
|||
std::string test_parsing_and_evaluating_logical_expressions();
|
||||
std::string test_adding_new_function();
|
||||
std::string test_calling_funtion();
|
||||
void register_some_functions();
|
||||
std::string prepare_some_function_declarations();
|
||||
std::vector<InputOutputPair> prepare_logical_input();
|
||||
std::vector<TestParser::SInputOutputPair> prepare_function_call_input();
|
||||
|
||||
|
|
|
@ -53,6 +53,18 @@ SenchaObject print(vector<ASTExpression *> arguments)
|
|||
|
||||
std::cout << value.str();
|
||||
|
||||
}
|
||||
return SenchaObject();
|
||||
}
|
||||
|
||||
SenchaObject println(vector<ASTExpression *> arguments)
|
||||
{
|
||||
for (auto argument: arguments)
|
||||
{
|
||||
auto value = argument->evaluate();
|
||||
|
||||
std::cout << value.str();
|
||||
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return SenchaObject();
|
||||
|
@ -157,6 +169,7 @@ void interactive()
|
|||
|
||||
ContextManager context_manager;
|
||||
context_manager.context("global")->register_function("print", print);
|
||||
context_manager.context("global")->register_function("println", println);
|
||||
context_manager.context("global")->register_function("sin", s_sin);
|
||||
context_manager.context("global")->register_function("cos", s_cos);
|
||||
context_manager.context("global")->register_function("tan", s_tan);
|
||||
|
@ -212,6 +225,7 @@ int main(int argc, char *argv[])
|
|||
Lexer lexer;
|
||||
ContextManager context_manager;
|
||||
context_manager.context("global")->register_function("print", print);
|
||||
context_manager.context("global")->register_function("println", println);
|
||||
context_manager.context("global")->register_function("sin", s_sin);
|
||||
context_manager.context("global")->register_function("cos", s_cos);
|
||||
context_manager.context("global")->register_function("tan", s_tan);
|
||||
|
|
Loading…
Reference in New Issue