Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

reverse variable declaration lists for great justice

  • Loading branch information...
commit 9ebda87f006337d648a14e87ef0ef9a77ade045f 1 parent fe68be6
@andrewrk authored
Showing with 29 additions and 2 deletions.
  1. +2 −2 pascal.y
  2. +26 −0 symbol_table.cpp
  3. +1 −0  symbol_table.h
View
4 pascal.y
@@ -184,8 +184,8 @@ variable_declaration_list : variable_declaration_list KEYWORD_SEMICOLON variable
$$ = new VariableDeclarationList($1, NULL);
};
-identifier_list : identifier_list KEYWORD_COMMA TOKEN_IDENTIFIER {
- $$ = new IdentifierList($3, $1);
+identifier_list : TOKEN_IDENTIFIER KEYWORD_COMMA identifier_list {
+ $$ = new IdentifierList($1, $3);
} | TOKEN_IDENTIFIER {
$$ = new IdentifierList($1, NULL);
};
View
26 symbol_table.cpp
@@ -9,6 +9,19 @@ SymbolTable * build_symbol_table(Program * program) {
SymbolTable * symbol_table = new SymbolTable();
bool success = true;
+ // reverse all VariableDeclarationLists
+ for (ClassList * class_list = program->class_list; class_list != NULL; class_list = class_list->next) {
+ ClassDeclaration * class_declaration = class_list->item;
+ class_declaration->class_block->variable_list = reverse_variable_declaration_list(class_declaration->class_block->variable_list);
+
+ for (FunctionDeclarationList * function_list = class_declaration->class_block->function_list; function_list != NULL; function_list = function_list->next) {
+ FunctionDeclaration * function_declaration = function_list->item;
+
+ function_declaration->parameter_list = reverse_variable_declaration_list(function_declaration->parameter_list);
+ function_declaration->block->variable_list = reverse_variable_declaration_list(function_declaration->block->variable_list);
+ }
+ }
+
// collect all the classes that are declared
for (ClassList * class_list = program->class_list; class_list != NULL; class_list = class_list->next) {
ClassDeclaration * class_declaration = class_list->item;
@@ -137,3 +150,16 @@ bool add_variables(InsensitiveMap<VariableData *> * function_variables, Variable
return success;
}
+VariableDeclarationList * reverse_variable_declaration_list(VariableDeclarationList * variable_declaration_list, VariableDeclarationList * prev)
+{
+ if (variable_declaration_list == NULL)
+ return NULL;
+ VariableDeclarationList * ret;
+ if (variable_declaration_list->next != NULL)
+ ret = reverse_variable_declaration_list(variable_declaration_list->next, variable_declaration_list);
+ else
+ ret = variable_declaration_list;
+ variable_declaration_list->next = prev;
+ return ret;
+}
+
View
1  symbol_table.h
@@ -46,5 +46,6 @@ SymbolTable * build_symbol_table(Program * program);
bool add_variables(InsensitiveMap<VariableData *> * function_variables, VariableDeclaration * variable_declaration, std::string function_name);
VariableData * get_field(SymbolTable * symbol_table, std::string class_name, std::string field_name);
+VariableDeclarationList * reverse_variable_declaration_list(VariableDeclarationList * variable_declaration_list, VariableDeclarationList * prev = NULL);
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.