Imports works somehow. Another small improvements.

imports
Justyna Ilczuk 2013-01-07 20:05:53 +01:00
parent 7ff93163f4
commit 4754bc5562
8 changed files with 161 additions and 28 deletions

View File

@ -15,6 +15,7 @@
#include "BasicStatement.h"
#include "BasicExpression.h"
#include "ImportStatement.h"
#include "ConstantExpression.h"
#include "PostfixExpression.h"
#include "IncorrectExpression.h"

View File

@ -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();
}

View File

@ -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();
};

View File

@ -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,18 +291,33 @@ void ASTInspector::visit(IncorrectExpression * incorrect_expression)
depth_level--;
}
void ASTInspector::visit(ImportStatement * import)
{
this->occurences["ImportStatement"]++;
depth_level++;
std::string visit_notes = "";
visit_notes += "ImportStatement:\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);
depth_level--;
}
void ASTInspector::visit(RepeatStatement * repeat_statement)
{
this->occurences["RepeatStatement"]++;
depth_level++;
std::string visit_notes = "";
visit_notes += "RepeatStatement:\n";
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--;
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)

View File

@ -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);

View File

@ -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[] = {'.', ',', ';', '{', '}', '[', ']', '(', ')'};

View File

@ -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());

View File

@ -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;
}
}