summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2013-01-07 20:05:53 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2013-01-07 20:05:53 +0100
commit4754bc5562abfcfc8aec5779d20f0a7a8184226c (patch)
tree1007bcc4d9fa71c667f5b52f4e17e03179e9d2e0
parent7ff93163f4dbde6c2c7c54e30bbc70b5514a66f7 (diff)
downloadsencha-lang-4754bc5562abfcfc8aec5779d20f0a7a8184226c.tar.gz
sencha-lang-4754bc5562abfcfc8aec5779d20f0a7a8184226c.tar.bz2
sencha-lang-4754bc5562abfcfc8aec5779d20f0a7a8184226c.zip
Imports works somehow. Another small improvements.
-rw-r--r--Sencha-lang/AST/AllTypesOfASTNodes.h1
-rw-r--r--Sencha-lang/AST/ImportStatement.cpp96
-rw-r--r--Sencha-lang/AST/ImportStatement.h9
-rw-r--r--Sencha-lang/ASTInspector.cpp33
-rw-r--r--Sencha-lang/ASTInspector.h1
-rw-r--r--Sencha-lang/Lexer.cpp2
-rw-r--r--Sencha-lang/Parser.cpp21
-rw-r--r--Sencha-lang/libs/my_functions.se22
8 files changed, 159 insertions, 26 deletions
diff --git a/Sencha-lang/AST/AllTypesOfASTNodes.h b/Sencha-lang/AST/AllTypesOfASTNodes.h
index e926cd2..65649bc 100644
--- a/Sencha-lang/AST/AllTypesOfASTNodes.h
+++ b/Sencha-lang/AST/AllTypesOfASTNodes.h
@@ -15,6 +15,7 @@
#include "BasicStatement.h"
#include "BasicExpression.h"
+#include "ImportStatement.h"
#include "ConstantExpression.h"
#include "PostfixExpression.h"
#include "IncorrectExpression.h"
diff --git a/Sencha-lang/AST/ImportStatement.cpp b/Sencha-lang/AST/ImportStatement.cpp
index 9f05da0..3e5740d 100644
--- a/Sencha-lang/AST/ImportStatement.cpp
+++ b/Sencha-lang/AST/ImportStatement.cpp
@@ -10,26 +10,87 @@
ImportStatement::ImportStatement(std::string name_of_module, ContextManager * context_manager) :
name_of_module(name_of_module), context_manager(context_manager){
//Hrum hrum magic
-
- std::ifstream module_file(name_of_module);
+ this->type = "ImportStatement";
+ prepare_defaults();
+ std::ifstream module_file;
std::string module_source_text = "";
std::string line;
Lexer lexer;
vector<Token> tokens;
Parser parser(context_manager);
- if (module_file.is_open())
- {
- while ( module_file.good() )
- {
- getline (module_file, line);
- module_source_text += line;
- tokens = lexer.parse_line(line);
- parser.add_tokens(tokens);
- }
- module_file.close();
- }
- import_body = parser.interpret();
- children.push_back(import_body);
+ correctly_imported = false;
+ for(auto path : prepare_paths(name_of_module))
+ {
+ if(correctly_imported) break;
+
+ std::cout << "Path was: " + path << std::endl;
+ module_file.open(path.c_str());
+ if (module_file.is_open())
+ {
+ while ( module_file.good() )
+ {
+ getline (module_file, line);
+ module_source_text += line;
+ tokens = lexer.parse_line(line);
+ parser.add_tokens(tokens);
+ }
+ module_file.close();
+ parser.interpret();
+ import_body = parser.tree.root;
+
+ children.push_back(import_body);
+ correctly_imported = true;
+ }
+ }
+
+ if(correctly_imported)
+ std::cout << "Import complete!" << std::endl;
+ else
+ std::cout << "Invalid import!" << std::endl;
+
+
+ //std::cout << "Importing file: " << module_source_text << std::endl;
+
+ //std::cout << "Tokens from file: " << parser.show_tokens() << std::endl;
+ //std::cout << "Errors from file: " << parser.error_message << std::endl;
+
+}
+
+void ImportStatement::prepare_defaults()
+{
+ DEFAULT_LIB_PATHS.push_back("/home/att/old/development/sencha-lang/Sencha-lang/libs/");
+}
+
+std::vector<std::string> ImportStatement::prepare_paths(std::string name_of_module)
+{
+ std::vector<std::string> paths;
+ if(name_of_module[0] == '\"' && name_of_module[name_of_module.size() -1] == '\"')
+ {
+ paths.push_back(strip_string(name_of_module));
+ }
+ else
+ {
+ for(auto path : DEFAULT_LIB_PATHS)
+ {
+ paths.push_back(path + name_of_module);
+ }
+ }
+ return paths;
+}
+
+std::string ImportStatement::strip_string(std::string text)
+{
+ if(text.size() > 2)
+ {
+ std::string result;
+ for(unsigned int i = 1; i < text.size() - 1; i++)
+ {
+ result += text[i];
+ }
+ return result;
+ }
+ else
+ return string("");
}
ImportStatement::~ImportStatement() {
@@ -38,5 +99,8 @@ ImportStatement::~ImportStatement() {
SenchaObject ImportStatement::execute()
{
- return import_body->execute();
+ if(correctly_imported)
+ return import_body->execute();
+ else
+ return SenchaObject();
}
diff --git a/Sencha-lang/AST/ImportStatement.h b/Sencha-lang/AST/ImportStatement.h
index 8472d1a..f45d92e 100644
--- a/Sencha-lang/AST/ImportStatement.h
+++ b/Sencha-lang/AST/ImportStatement.h
@@ -10,6 +10,7 @@
#include <fstream>
#include <iostream>
+#include <vector>
#include "ASTStatement.h"
#include "../Parser.h"
@@ -19,10 +20,14 @@
class ImportStatement: public ASTStatement {
public:
std::string name_of_module;
+ std::vector<std::string> DEFAULT_LIB_PATHS;
ContextManager * context_manager;
-
+ bool correctly_imported;
ImportStatement(std::string name_of_module, ContextManager * context_manager);
- ProgramNode * import_body;
+ 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();
virtual ~ImportStatement();
};
diff --git a/Sencha-lang/ASTInspector.cpp b/Sencha-lang/ASTInspector.cpp
index 6a24298..ba69aeb 100644
--- a/Sencha-lang/ASTInspector.cpp
+++ b/Sencha-lang/ASTInspector.cpp
@@ -58,6 +58,10 @@ void ASTInspector::visit(Visitable * node)
{
visit(static_cast<DeclarationStatement *>(node));
}
+ else if (node->type == "ImportStatement")
+ {
+ visit(static_cast<ImportStatement *>(node));
+ }
else if (node->type == "VariableExpression")
{
visit(static_cast<VariableExpression *>(node));
@@ -216,7 +220,7 @@ void ASTInspector::visit(DeclarationStatement * declaration_statement)
depth_level++;
std::string visit_notes = "";
visit_notes += "DeclarationStatement:\n";
- visit_notes += "It declares: " + declaration_statement->name + "to be:\n";
+ visit_notes += "It declares: " + declaration_statement->name + " to be:\n";
if(!declaration_statement->is_function )
{
visit_notes += declaration_statement->right_value.repr();
@@ -287,20 +291,35 @@ void ASTInspector::visit(IncorrectExpression * incorrect_expression)
depth_level--;
}
-void ASTInspector::visit(RepeatStatement * repeat_statement)
+void ASTInspector::visit(ImportStatement * import)
{
- this->occurences["RepeatStatement"]++;
+ this->occurences["ImportStatement"]++;
depth_level++;
std::string visit_notes = "";
- visit_notes += "RepeatStatement:\n";
+ visit_notes += "ImportStatement:\n";
- visit_notes += "It executes " + to_string(repeat_statement->how_many_times_expr->evaluate().integer) + "times\n";
+ visit_notes += "It imports " + import->name_of_module + " and import is:\n";
+ if(import->correctly_imported) visit_notes += "success\n";
+ else visit_notes += "failure\n";
+ visit_notes += "End of ImportStatement";
write_report(visit_notes);
- repeat_statement->body->accept(this);
- write_report("End of RepeatStatement\n");
depth_level--;
}
+void ASTInspector::visit(RepeatStatement * repeat_statement)
+{
+ this->occurences["RepeatStatement"]++;
+ depth_level++;
+ std::string visit_notes = "";
+ visit_notes += "RepeatStatement:\n";
+
+ visit_notes += "It executes " + to_string(repeat_statement->how_many_times_expr->evaluate().integer) + "times\n";
+ write_report(visit_notes);
+ repeat_statement->body->accept(this);
+ write_report("End of RepeatStatement\n");
+ depth_level--;
+}
+
void ASTInspector::visit(VariableExpression * variable)
{
this->occurences["VariableExpression"]++;
diff --git a/Sencha-lang/ASTInspector.h b/Sencha-lang/ASTInspector.h
index 6e399dd..209b745 100644
--- a/Sencha-lang/ASTInspector.h
+++ b/Sencha-lang/ASTInspector.h
@@ -63,6 +63,7 @@ private:
void write_report(std::string visit_notes);
void visit(ProgramNode * node);
+ void visit(ImportStatement * import);
void visit(ConstantExpression * node);
void visit(BasicExpression * node);
void visit(UnaryExpression * node);
diff --git a/Sencha-lang/Lexer.cpp b/Sencha-lang/Lexer.cpp
index 52c5af6..5c8f42c 100644
--- a/Sencha-lang/Lexer.cpp
+++ b/Sencha-lang/Lexer.cpp
@@ -2,7 +2,7 @@
Lexer::Lexer()
{
- string keys[] = {"function", "class", "for", "while", "if", "else", "true", "false", "and", "or"};
+ string keys[] = {"import", "class", "for", "while", "if", "else", "true", "false", "and", "or"};
keywords.assign(keys, keys+10);
char punct[] = {'.', ',', ';', '{', '}', '[', ']', '(', ')'};
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index b392527..bdb4c63 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -178,6 +178,27 @@ ASTStatement * Parser::statement()
RepeatStatement * repeat = new RepeatStatement(expr(), statement());
return repeat;
}
+ else if(accept("import"))
+ {
+ auto name_expression = expr()->execute();
+ cout << name_expression.repr() << endl;
+ if(name_expression.type == SenchaObject::string_literal)
+ {
+ std::string name_of_import = name_expression.text;
+ ImportStatement * import = new ImportStatement(name_of_import, context_manager);
+ accept(";");
+ return import;
+ }
+ else
+ {
+ string error_message = "ERROR: invalid import\n";
+ error(error_message);
+ BasicStatement * stat = new BasicStatement();
+ stat->add_expression(new IncorrectExpression(error_message));
+ accept(";");
+ return stat;
+ }
+ }
else if(accept("while"))
{
WhileNode * while_node = new WhileNode(expr(), statement());
diff --git a/Sencha-lang/libs/my_functions.se b/Sencha-lang/libs/my_functions.se
new file mode 100644
index 0000000..a785111
--- /dev/null
+++ b/Sencha-lang/libs/my_functions.se
@@ -0,0 +1,22 @@
+def min(a, b)
+{
+ if( a > b)
+ {
+ return b;
+ }
+ else {
+ return a;
+ }
+}
+
+def max(a, b)
+{
+ if(a > b)
+ {
+ return a;
+ }
+ else
+ {
+ return b;
+ }
+}