sencha-lang/Sencha-lang/AST/ImportStatement.cpp

108 lines
2.4 KiB
C++

/*
* ImportStatement.cpp
*
* Created on: Jan 7, 2013
* Author: att
*/
#include "ImportStatement.h"
ImportStatement::ImportStatement(std::string name_of_module, ContextManager * context_manager) :
name_of_module(name_of_module), context_manager(context_manager){
//Hrum hrum magic
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);
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.substr(name_of_module.size() -3,name_of_module.size()) == ".se")
{
std::cout << name_of_module.substr(name_of_module.size() -3,name_of_module.size() ) << std::endl;
paths.push_back(name_of_module);
}
else
{
for(auto path : DEFAULT_LIB_PATHS)
{
paths.push_back(path + name_of_module + ".se");
}
}
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() {
// TODO Auto-generated destructor stub
}
SenchaObject ImportStatement::execute()
{
if(correctly_imported)
return import_body->execute();
else
return SenchaObject();
}