summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Sencha-lang/AST/BasicStatement.cpp2
-rw-r--r--Sencha-lang/ContextManager.cpp6
-rw-r--r--Sencha-lang/Tests/TestParser.cpp25
-rw-r--r--Sencha-lang/Tests/TestParser.h2
-rw-r--r--Sencha-lang/main.cpp14
5 files changed, 41 insertions, 8 deletions
diff --git a/Sencha-lang/AST/BasicStatement.cpp b/Sencha-lang/AST/BasicStatement.cpp
index 14fe4b2..abd010c 100644
--- a/Sencha-lang/AST/BasicStatement.cpp
+++ b/Sencha-lang/AST/BasicStatement.cpp
@@ -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();
}
diff --git a/Sencha-lang/ContextManager.cpp b/Sencha-lang/ContextManager.cpp
index 4378b12..51993de 100644
--- a/Sencha-lang/ContextManager.cpp
+++ b/Sencha-lang/ContextManager.cpp
@@ -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)();
diff --git a/Sencha-lang/Tests/TestParser.cpp b/Sencha-lang/Tests/TestParser.cpp
index 3f58cda..1de0a91 100644
--- a/Sencha-lang/Tests/TestParser.cpp
+++ b/Sencha-lang/Tests/TestParser.cpp
@@ -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;
}
diff --git a/Sencha-lang/Tests/TestParser.h b/Sencha-lang/Tests/TestParser.h
index 10157ce..e5da753 100644
--- a/Sencha-lang/Tests/TestParser.h
+++ b/Sencha-lang/Tests/TestParser.h
@@ -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();
diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp
index c7b9780..22d7b71 100644
--- a/Sencha-lang/main.cpp
+++ b/Sencha-lang/main.cpp
@@ -54,6 +54,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);