summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Sencha-lang/main.cpp')
-rw-r--r--Sencha-lang/main.cpp229
1 files changed, 56 insertions, 173 deletions
diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp
index 8d58e77..01fea68 100644
--- a/Sencha-lang/main.cpp
+++ b/Sencha-lang/main.cpp
@@ -9,139 +9,33 @@
#include "ContextManager.h"
#include "ASTInspector.h"
#include "Tests/tests.h"
+#include "basic_native_functions.h"
#include <unistd.h>
using namespace std;
-
-/*TODO
- * Known bugs:
- *
- * >> print("I like bananas", dupa");
-I like bananasnull
-Context: 0: dupa"); type: null
-null
-
-dupa"); declaration? hello? It's probably temporary effect of searching for this name in variable tables
-
->>
- */
-
-
-SenchaObject sleep(vector<ASTExpression *> arguments)
-{
- SenchaObject argument = arguments[0]->evaluate();
- if(argument.type == SenchaObject::integer_number && argument.integer >= 0)
- {
- cout << "valid argument for sleep: " << argument.integer << endl;
- usleep(1000 * argument.integer);
- return SenchaObject();
- }
- else
- {
- cout << "invalid argument for sleep";
- argument.type = SenchaObject::invalid;
- return argument;
- }
-
-}
-SenchaObject print(vector<ASTExpression *> arguments)
-{
- for (auto argument: arguments)
- {
- auto value = argument->evaluate();
-
- 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();
-}
-
-SenchaObject s_sin(vector<ASTExpression *> arguments)
+//Helper functions
+void register_functions(ContextManager & context_manager)
{
- SenchaObject result;
- SenchaObject sin_argument = arguments[0]->evaluate();
- if(sin_argument.type == SenchaObject::integer_number)
- {
- result = SenchaObject(sin(sin_argument.integer));
- }
- else if(sin_argument.type == SenchaObject::float_number)
- {
- result = SenchaObject(sin(sin_argument.number));
- }
- else
- {
- result.type = SenchaObject::invalid;
- }
-
- return result;
-}
-
-SenchaObject s_cos(vector<ASTExpression *> arguments)
-{
- SenchaObject result;
- SenchaObject cos_argument = arguments[0]->evaluate();
- if(cos_argument.type == SenchaObject::integer_number)
- {
- result = SenchaObject(cos(cos_argument.integer));
- }
- else if(cos_argument.type == SenchaObject::float_number)
- {
- result = SenchaObject(cos(cos_argument.number));
- }
- else
- {
- result.type = SenchaObject::invalid;
- }
-
- return result;
-}
-
-SenchaObject s_tan(vector<ASTExpression *> arguments)
-{
- SenchaObject result;
- SenchaObject tan_argument = arguments[0]->evaluate();
- if(tan_argument.type == SenchaObject::integer_number)
- {
- result = SenchaObject(tan(tan_argument.integer));
- }
- else if(tan_argument.type == SenchaObject::float_number)
- {
- result = SenchaObject(tan(tan_argument.number));
- }
- else
- {
- result.type = SenchaObject::invalid;
- }
-
- return result;
+ 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);
+ context_manager.context("global")->register_function("sleep", sleep);
+ context_manager.context("global")->register_function("len", len);
}
-SenchaObject len(vector<ASTExpression *> arguments)
+string compute_indentation(int depth_level)
{
- SenchaObject result;
- SenchaObject argument = arguments[0]->evaluate();
- if(argument.type == SenchaObject::string_literal)
+ string indent = "";
+ for(int i=0; i< depth_level; i++)
{
- result = SenchaObject(int(argument.text.size()));
+ indent += " ";
}
- return result;
+ return indent;
}
-
int how_depth_change(vector<Token> tokens)
{
int change = 0;
@@ -153,28 +47,13 @@ int how_depth_change(vector<Token> tokens)
return change;
}
-string compute_indentation(int depth_level)
-{
- string indent = "";
- for(int i=0; i< depth_level; i++)
- {
- indent += " ";
- }
- return indent;
-}
-
void interactive()
{
+ //Initialization
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);
- context_manager.context("global")->register_function("sleep", sleep);
- context_manager.context("global")->register_function("len", len);
+ register_functions(context_manager);
Parser parser(&context_manager);
ASTInspector inspector;
@@ -183,6 +62,7 @@ void interactive()
string input;
int level_of_depth = 0;
string indentation = "";
+ //Main loop of interactive mode
while(true)
{
cout << ">> " + indentation;
@@ -195,6 +75,7 @@ void interactive()
parser.add_tokens(tokens);
+ //It executes when statements and braces are closed
if(level_of_depth == 0) {
parser.interpret();
parser.program->execute_last();
@@ -206,55 +87,57 @@ void interactive()
}
}
}
+
+void execute_script(std::string name )
+{
+ Lexer lexer;
+ ContextManager context_manager;
+ register_functions(context_manager);
+ Parser parser(&context_manager);
+
+ vector<Token> tokens;
+ string line;
+ ifstream input_file (name);
+
+ if (input_file.is_open())
+ {
+ while ( input_file.good() )
+ {
+ getline (input_file,line);
+ tokens = lexer.parse_line(line);
+ parser.add_tokens(tokens);
+ }
+ input_file.close();
+ }
+ parser.interpret();
+ parser.program->execute();
+}
+
+/**
+ * Main function resolves in which mode user want to run program
+ * Modes are:
+ * Interactive - it is run when no arguments are provided
+ * Tests - "SenchaLang --test"
+ * Script execution - if path provided
+ */
int main(int argc, char *argv[])
{
if(argc <= 1)
{
- cout << "Sencha-lang interpreter, version 0.12" << endl;
+ cout << "Sencha-lang interpreter, version 0.44" << endl;
interactive();
}
else
{
std::string argument1 = argv[1];
- if(argument1 == "--test") {
-
+ if(argument1 == "--test")
+ {
cout << "I'm running tests" << endl;
run_tests();
}
else {
- auto name = argument1;
- 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);
- context_manager.context("global")->register_function("sleep", sleep);
- context_manager.context("global")->register_function("len", len);
-
- Parser parser(&context_manager);
-
- vector<Token> tokens;
- string line;
- ifstream input_file (name);
-
- if (input_file.is_open())
- {
- while ( input_file.good() )
- {
- getline (input_file,line);
- tokens = lexer.parse_line(line);
- parser.add_tokens(tokens);
- }
- input_file.close();
- }
- ASTInspector inspector;
- parser.interpret();
- inspector.visit(parser.tree.root);
- parser.program->execute();
- //cout << inspector.get_report()<< endl;
+ execute_script(argument1);
}
}