summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/AST/ImportStatement.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Sencha-lang/AST/ImportStatement.cpp')
-rw-r--r--Sencha-lang/AST/ImportStatement.cpp96
1 files changed, 80 insertions, 16 deletions
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();
}