diff options
Diffstat (limited to 'src/parser.y')
-rw-r--r-- | src/parser.y | 115 |
1 files changed, 45 insertions, 70 deletions
diff --git a/src/parser.y b/src/parser.y index 536f4ef..9b4f888 100644 --- a/src/parser.y +++ b/src/parser.y @@ -34,6 +34,7 @@ #include <unistd.h> #endif +#include "typedefs.h" #include "module.h" #include "expression.h" #include "value.h" @@ -58,16 +59,11 @@ int lexerlex(void); std::vector<Module*> module_stack; Module *currmodule; -class ArgContainer { -public: - std::string argname; - Expression *argexpr; -}; -class ArgsContainer { -public: - std::vector<std::string> argnames; - std::vector<Expression*> argexpr; -}; +extern void lexerdestroy(); +extern FILE *lexerin; +extern const char *parser_input_buffer; +const char *parser_input_buffer; +std::string parser_source_path; %} @@ -77,9 +73,10 @@ public: class Value *value; class Expression *expr; class ModuleInstantiation *inst; + std::vector<ModuleInstantiation*> *instvec; class IfElseModuleInstantiation *ifelse; - class ArgContainer *arg; - class ArgsContainer *args; + Assignment *arg; + AssignmentList *args; } %token TOK_MODULE @@ -117,8 +114,8 @@ public: %type <inst> module_instantiation %type <ifelse> if_statement %type <ifelse> ifelse_statement -%type <inst> children_instantiation -%type <inst> module_instantiation_list +%type <instvec> children_instantiation +%type <instvec> module_instantiation_list %type <inst> single_module_instantiation %type <args> arguments_call @@ -161,8 +158,7 @@ statement: module_stack.push_back(currmodule); currmodule = new Module(); p->modules[$2] = currmodule; - currmodule->argnames = $4->argnames; - currmodule->argexpr = $4->argexpr; + currmodule->definition_arguments = *$4; free($2); delete $4; } statement { @@ -171,8 +167,7 @@ statement: } | TOK_FUNCTION TOK_ID '(' arguments_decl optional_commas ')' '=' expr { Function *func = new Function(); - func->argnames = $4->argnames; - func->argexpr = $4->argexpr; + func->definition_arguments = *$4; func->expr = $8; currmodule->functions[$2] = func; free($2); @@ -182,9 +177,9 @@ statement: /* Will return a dummy parent node with zero or more children */ children_instantiation: module_instantiation { - $$ = new ModuleInstantiation(); + $$ = new std::vector<ModuleInstantiation*>; if ($1) { - $$->children.push_back($1); + $$->push_back($1); } } | '{' module_instantiation_list '}' { @@ -194,16 +189,15 @@ children_instantiation: if_statement: TOK_IF '(' expr ')' children_instantiation { $$ = new IfElseModuleInstantiation(); - $$->argnames.push_back(""); - $$->argexpr.push_back($3); + $$->arguments.push_back(Assignment("", $3)); + $$->setPath(parser_source_path); if ($$) { - $$->children = $5->children; + $$->children = *$5; } else { - for (size_t i = 0; i < $5->children.size(); i++) - delete $5->children[i]; + for (size_t i = 0; i < $5->size(); i++) + delete (*$5)[i]; } - $5->children.clear(); delete $5; } ; @@ -214,12 +208,11 @@ ifelse_statement: if_statement TOK_ELSE children_instantiation { $$ = $1; if ($$) { - $$->else_children = $3->children; + $$->else_children = *$3; } else { - for (size_t i = 0; i < $3->children.size(); i++) - delete $3->children[i]; + for (size_t i = 0; i < $3->size(); i++) + delete (*$3)[i]; } - $3->children.clear(); delete $3; } ; @@ -246,12 +239,11 @@ module_instantiation: single_module_instantiation children_instantiation { $$ = $1; if ($$) { - $$->children = $2->children; + $$->children = *$2; } else { - for (size_t i = 0; i < $2->children.size(); i++) - delete $2->children[i]; + for (size_t i = 0; i < $2->size(); i++) + delete (*$2)[i]; } - $2->children.clear(); delete $2; } | ifelse_statement { @@ -260,12 +252,12 @@ module_instantiation: module_instantiation_list: /* empty */ { - $$ = new ModuleInstantiation(); + $$ = new std::vector<ModuleInstantiation*>; } | module_instantiation_list module_instantiation { $$ = $1; if ($$) { - if ($2) $$->children.push_back($2); + if ($2) $$->push_back($2); } else { delete $2; } @@ -274,8 +266,8 @@ module_instantiation_list: single_module_instantiation: TOK_ID '(' arguments_call ')' { $$ = new ModuleInstantiation($1); - $$->argnames = $3->argnames; - $$->argexpr = $3->argexpr; + $$->arguments = *$3; + $$->setPath(parser_source_path); free($1); delete $3; } @@ -442,8 +434,7 @@ expr: $$ = new Expression(); $$->type = "F"; $$->call_funcname = $1; - $$->call_argnames = $3->argnames; - $$->children = $3->argexpr; + $$->call_arguments = *$3; free($1); delete $3; } ; @@ -464,61 +455,50 @@ vector_expr: arguments_decl: /* empty */ { - $$ = new ArgsContainer(); + $$ = new AssignmentList(); } | argument_decl { - $$ = new ArgsContainer(); - $$->argnames.push_back($1->argname); - $$->argexpr.push_back($1->argexpr); + $$ = new AssignmentList(); + $$->push_back(*$1); delete $1; } | arguments_decl ',' optional_commas argument_decl { $$ = $1; - $$->argnames.push_back($4->argname); - $$->argexpr.push_back($4->argexpr); + $$->push_back(*$4); delete $4; } ; argument_decl: TOK_ID { - $$ = new ArgContainer(); - $$->argname = $1; - $$->argexpr = NULL; + $$ = new Assignment($1, NULL); free($1); } | TOK_ID '=' expr { - $$ = new ArgContainer(); - $$->argname = $1; - $$->argexpr = $3; + $$ = new Assignment($1, $3); free($1); } ; arguments_call: /* empty */ { - $$ = new ArgsContainer(); + $$ = new AssignmentList(); } | argument_call { - $$ = new ArgsContainer(); - $$->argnames.push_back($1->argname); - $$->argexpr.push_back($1->argexpr); + $$ = new AssignmentList(); + $$->push_back(*$1); delete $1; } | arguments_call ',' optional_commas argument_call { $$ = $1; - $$->argnames.push_back($4->argname); - $$->argexpr.push_back($4->argexpr); + $$->push_back(*$4); delete $4; } ; argument_call: expr { - $$ = new ArgContainer(); - $$->argexpr = $1; + $$ = new Assignment("", $1); } | TOK_ID '=' expr { - $$ = new ArgContainer(); - $$->argname = $1; - $$->argexpr = $3; + $$ = new Assignment($1, $3); free($1); } ; @@ -536,21 +516,16 @@ void yyerror (char const *s) currmodule = NULL; } -extern void lexerdestroy(); -extern FILE *lexerin; -extern const char *parser_input_buffer; -const char *parser_input_buffer; -std::string parser_source_path; - Module *parse(const char *text, const char *path, int debug) { lexerin = NULL; parser_error_pos = -1; parser_input_buffer = text; - parser_source_path = std::string(path); + parser_source_path = boosty::absolute(std::string(path)).string(); module_stack.clear(); Module *rootmodule = currmodule = new Module(); + rootmodule->setModulePath(path); // PRINTB_NOCACHE("New module: %s %p", "root" % rootmodule); parserdebug = debug; |