diff --git a/src/grammar.y b/src/grammar.y index 352c2ea..4b36a61 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -267,16 +267,30 @@ simple_statement: | RETURN expression ; -assignable: - ID {$$ = getType(look_up(cur,$1));} - | assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));} //add array case here - | assignable rec_op ID {if(undefined != table_lookup(getRecList(look_up(cur, $1)), $3)){ - {$$ = getName(table_lookup(getRecList(look_up(cur, $1)), $3));}};} - ; - rec_op : DOT + + + +/////////// VERIFIED UP UNTIL THIS POINT + +// assignable needs more code- specifically for arrays, records and ablock checks + + + + + +ablock: + L_PAREN argument_list {$$ = $2;} R_PAREN + ; + +argument_list: + expression COMMA argument_list {$$ = $3 + 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$);} + | expression {$$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$);} + ; + +// will ALWAYS be a TYPE expression: constant {printdebug("constant expression");} {$$ = $1;} @@ -341,8 +355,8 @@ expression: | expression EQUAL_TO expression {printdebug("equals check expression"); if(strcmp($1,$3)==0){$$=strdup("Boolean");} - else if((strcmp($1,"array")==0||strcmp($1,"record")==0|| - strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");} + //else if((strcmp($1,"array")==0||strcmp($1,"record")==0|| + // strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");} else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s", @2.first_line,@2.first_column,$1,$3);$$=strdup("undefined");}} @@ -353,17 +367,16 @@ expression: | memOp assignable {$$ = strdup("address");} ; - -ablock: -L_PAREN argument_list {$$ = $2;} R_PAREN +// prolly right, check back with me later +// add array case +// include type check for ablock in arrays - ablock is always the int of the elements in array/rec +assignable: + ID {$$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);} + | assignable ablock {$$ = getName(getReturn(table_lookup(getAncestor(cur), $1))); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1);} // add array case + | assignable rec_op ID {if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3)){ + {$$ = getName(table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3));}}; printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", $$, $1);} ; -argument_list: -expression COMMA argument_list {$$ = $3 + 1;} -| expression {$$ = 1;} - ; - - memOp: RESERVE {printdebug("reserve expression");} | RELEASE {printdebug("release expression");} @@ -371,12 +384,12 @@ memOp: constant: - C_STRING {$$ = $1;} - | C_INTEGER {$$ = "integer";} - | C_NULL {$$ = $1;} - | C_CHARACTER {$$ = $1;} - | C_TRUE {$$ = $1;} - | C_FALSE {$$ = $1;} + C_STRING {$$ = $1; printdebug("string of C_STRING in constant is %s",$1);} + | C_INTEGER {$$ = "integer"; printdebug("string of C_INTEGER in constant is integer");} + | C_NULL {$$ = $1; printdebug("string of C_NULL in constant is %s",$1);} + | C_CHARACTER {$$ = $1; printdebug("string of C_CHARACTER in constant is %s",$1);} + | C_TRUE {$$ = $1; printdebug("string of C_TRUE in constant is %s",$1);} + | C_FALSE {$$ = $1; printdebug("string of C_FALSE in constant is %s",$1);} ; types: diff --git a/src/runner.c b/src/runner.c index 7c053a4..c6f94a3 100644 --- a/src/runner.c +++ b/src/runner.c @@ -223,13 +223,4 @@ int is_alpha_file(char *alpha, int file_len) { return -1; // not alpha file } return 0; // is alpha file -} - -void enter_scope(int line, int column) { cur = CreateScope(cur, line, column); } -void exit_scope() { - if (cur->Parent_Scope == NULL) { - printf("Can't close top"); - return; - } - cur = cur->Parent_Scope; } \ No newline at end of file diff --git a/src/runner.h b/src/runner.h index d378be5..9678609 100644 --- a/src/runner.h +++ b/src/runner.h @@ -40,8 +40,6 @@ SymbolTable *cur; // int main(int argc, char* argv[]); char *is_tok(int argc, char *argv[]); // int is_alpha_file(char *file, int file_len); -void enter_scope(int, int); -void exit_scope(void); FILE *alpha_file; FILE *tok_flag = NULL; diff --git a/src/symbol_table.c b/src/symbol_table.c index e68c886..29d34a1 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -512,7 +512,7 @@ SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column) { SymbolTable *init(SymbolTable *start) { if (start->Parent_Scope != NULL) { printdebug( - "Cannot initialize a scope that is not the parent scope"); + "%s[FATAL] Cannot initialize a scope that is not the parent scope", COLOR_RED); return NULL; } integ = (TableNode *)calloc(1, sizeof(TableNode)); @@ -979,6 +979,9 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { printdebug("%s[WARNING] passed in a non-top level scope to " "print_symbol_table", COLOR_ORANGE); + printdebug("%sParent of's: line %d, column %d", + COLOR_ORANGE, table->Parent_Scope->Line_Number, table->Parent_Scope->Column_Number); + return; } if (table->Parent_Scope == NULL) { @@ -1100,13 +1103,13 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { fprintf(file_ptr, "%-17s: %06d : :%-21s: %-28s\n", entrie->theName, current_scope, - getType(entrie), "Function"); + getType(entrie), "User Defined"); } else { fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName, current_scope, parant_scope, getType(entrie), - "Function"); + "User Defined"); } } if (getAdInfoType(entrie) == TYPE_UNDEFINED) { @@ -1145,6 +1148,12 @@ SymbolTable *getAncestor(SymbolTable *table) { } if (table->Parent_Scope == NULL) { // if table has no parent, return itself + printdebug("already at top scope!"); + if (table == cur) { + printdebug("passed in the current scope"); + } else { + printdebug("passed in a different scope"); + } return table; } else { // call function recursively to grab ancestor diff --git a/tests/sprint2/test/sp2_library.alpha b/tests/sprint2/test/sp2_library.alpha index 9bacd6c..866857c 100644 --- a/tests/sprint2/test/sp2_library.alpha +++ b/tests/sprint2/test/sp2_library.alpha @@ -7,9 +7,9 @@ You should #include this file at the start of your alpha file. Some useful types are defined below. *) type string: 1 -> character -type BooleanXBoolean: [Boolean: x, y] -type characterXcharacter: [character: x, y] -type integerXinteger: [integer: x, y] +type BooleanXBoolean: [Boolean: x; Boolean: y] +type characterXcharacter: [character: x; character: y] +type integerXinteger: [integer: x; integer: y] type Boolean2Boolean: Boolean -> Boolean type integer2integer: integer -> integer @@ -25,6 +25,4 @@ type address2integer: address -> integer external function printInteger: integer2integer external function printCharacter: character2integer external function printBoolean: Boolean2integer -external function reserve: integer2address -external function release: address2integer function entry: string2integer