This commit is contained in:
Scarlett
2025-04-02 15:03:29 -04:00
parent 20c372f134
commit 77c4106b1b
3 changed files with 63 additions and 45 deletions

View File

@ -132,7 +132,9 @@ definition:
| TYPE ID COLON C_INTEGER ARROW id_or_types | TYPE ID COLON C_INTEGER ARROW id_or_types
{printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d", {printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d",
$2, $6, $4); $2, $6, $4);
CreateEntry(cur, arrayprim, $2, CreateArrayInfo($4, look_up(cur, $6)));} CreateEntry(cur, arrayprim, $2, CreateArrayInfo($4, look_up(cur, $6)));
printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, $6);
}
| function_declaration | function_declaration
| TYPE ID COLON id_or_types ARROW id_or_types { | TYPE ID COLON id_or_types ARROW id_or_types {
printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s",
@ -237,7 +239,11 @@ declaration_list:
; ;
declaration: declaration:
id_or_types COLON ID {printdebug("ID/TYPE: %s, ID: %s", $<words>1, $<words>3) ; CreateEntry(cur,table_lookup(getAncestor(cur),$<words>1),$<words>3,NULL); } id_or_types COLON ID
{
printdebug("ID/TYPE: %s, ID: %s", $<words>1, $<words>3) ;
CreateEntry(cur,table_lookup(getAncestor(cur),$<words>1),$<words>3,NULL);
}
; ;
id_or_types: id_or_types:
@ -394,15 +400,21 @@ expression:
// add array case // add array case
// include type check for ablock in arrays - ablock is always the int of the elements in array/rec // include type check for ablock in arrays - ablock is always the int of the elements in array/rec
assignable: assignable:
ID {$$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);} ID
{
$$ = getType(look_up(cur,$1));
printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);}
| assignable | assignable
{ {
printdebug("%sBeginning rule 2 of assignable.", COLOR_CYAN);
cur = CreateScope(cur, -1,-1); cur = CreateScope(cur, -1,-1);
} }
ablock ablock {
{
int type = getAdInfoType(look_up(getParent(cur), $1)); int type = getAdInfoType(look_up(getParent(cur), $1));
printdebug("%stype is %d", COLOR_PURPLE, type);
if (type == TYPE_FUNCTION_DECLARATION) { if (type == TYPE_FUNCTION_DECLARATION) {
printdebug("%sEntering function call", COLOR_LIGHTGREEN);
if (getAsKeyword(look_up(getParent(cur), $1))) { if (getAsKeyword(look_up(getParent(cur), $1))) {
TableNode *param = getParameter(look_up(getParent(cur), $1)); TableNode *param = getParameter(look_up(getParent(cur), $1));
SymbolTable *recList = getRecList(param); SymbolTable *recList = getRecList(param);
@ -414,7 +426,7 @@ assignable:
//this isn't very efficient, but will hopefully work //this isn't very efficient, but will hopefully work
while (lastCheckedAct != NULL && lastCheckedRef != NULL) { while (lastCheckedAct != NULL && lastCheckedRef != NULL) {
if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) { if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) {
printdebug("expected %s expression in function call but got %s at line %d and column %d",getName(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); printdebug("expected %s expression in function call but got %s at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column);
} }
lastCheckedAct = getNextEntry(lastCheckedAct); lastCheckedAct = getNextEntry(lastCheckedAct);
TableNode *tn = getFirstEntry(recList); TableNode *tn = getFirstEntry(recList);
@ -431,25 +443,31 @@ assignable:
printdebug("expected %s expression in function call but got %s at line %d and column %d",expected, actual, @3.first_line, @3.first_column); printdebug("expected %s expression in function call but got %s at line %d and column %d",expected, actual, @3.first_line, @3.first_column);
} }
} }
$$ = getName(getReturn(table_lookup(getAncestor(cur), $1))); $$ = getName(getReturn(table_lookup(getAncestor(cur), $1)));
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1);
} else if (type == TYPE_ARRAY) {
} else if (type == TYPE_ARRAY_TYPE) {
printdebug("%sEntering array call", COLOR_LIGHTGREEN);
if (getNumArrDim(look_up(getParent(cur), $1)) != $<integ>2) { if (getNumArrDim(look_up(getParent(cur), $1)) != $<integ>2) {
printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, $1)), $<integ>2, @2.first_line, @2.first_column); printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, $1)), $<integ>2, @2.first_line, @2.first_column);
} }
for (TableNode *tn = getFirstEntry(cur); tn != NULL; tn = getNextEntry(tn)) { // for (TableNode *tn = getFirstEntry(cur); tn != NULL; tn = getNextEntry(tn)) {
if (strcmp(getName(tn), "integer") != 0) { // if (strcmp(getName(tn), "integer") != 0) {
printdebug("expected only integer expressions in array ablock at line %d column %d", @3.first_line, @3.first_column); // printdebug("expected only integer expressions in array ablock at line %d column %d", @3.first_line, @3.first_column);
} // }
} // }
$$ = getName(getArrType(look_up(getParent(cur), $1))); $$ = getName(getArrType(look_up(getParent(cur), $1)));
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1);
} }
cur = getParent(cur); cur = getParent(cur);
} }
| assignable rec_op ID {if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3)){ | assignable rec_op ID
{$$ = getName(table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3));}}; printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", $$, $1);} {
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);
}
; ;
memOp: memOp:

View File

@ -702,7 +702,7 @@ int getAdInfoType(TableNode *tn) {
return TYPE_FUNCTION_TYPE; return TYPE_FUNCTION_TYPE;
} }
if (strcmp(getName(tn), getName(arrayprim)) == 0) { if (strcmp(getName(tn), getName(arrayprim)) == 0) {
return TYPE_ARRAY; return TYPE_ARRAY_TYPE; // changed from TYPE_ARRAY cuz
} }
if (strcmp(getName(tn), getName(undefined)) == 0) { if (strcmp(getName(tn), getName(undefined)) == 0) {
return TYPE_UNDEFINED; return TYPE_UNDEFINED;
@ -713,7 +713,7 @@ int getAdInfoType(TableNode *tn) {
} }
if(strcmp(getType(tn), getName(arrayprim))==0){ if(strcmp(getType(tn), getName(arrayprim))==0){
printdebug("passed in an array to getAdInfoType"); printdebug("passed in an array to getAdInfoType");
return TYPE_ARRAY; return TYPE_ARRAY_TYPE;
} }
if(strcmp(getType(tn), getName(recprime))==0){ if(strcmp(getType(tn), getName(recprime))==0){
printdebug("passed in a record to getAdInfoType"); printdebug("passed in a record to getAdInfoType");
@ -1004,8 +1004,8 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
int parant_scope = 0; int parant_scope = 0;
int current_scope = 0; int current_scope = 0;
if (table->Parent_Scope != NULL) { if (table->Parent_Scope != NULL) {
parant_scope = table->Parent_Scope->Line_Number * 1000 + parant_scope = getParent(table)->Line_Number * 1000 +
table->Parent_Scope->Column_Number; getParent(table)->Column_Number;
current_scope = current_scope =
table->Line_Number * 1000 + table->Column_Number; table->Line_Number * 1000 + table->Column_Number;
} else { } else {
@ -1015,8 +1015,8 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "", fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "",
current_scope, parant_scope, "", "Empty Scope"); current_scope, parant_scope, "", "Empty Scope");
} }
for (; entrie != NULL; entrie = entrie->next) { for (; entrie != NULL; entrie = getNextEntry(entrie)) {
if (getAdInfoType(entrie) == TYPE_ARRAY) { if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) {
if (parant_scope == 0) { if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
@ -1136,13 +1136,13 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
} }
} }
} }
if (table->Children_Scope != NULL) { if (getChildren(table) != NULL) {
ListOfTable *node = table->Children_Scope; ListOfTable *node = getChildren(table);
for (; node != NULL; node = node->next) { for (; node != NULL; node = node->next) {
print_symbol_table(node->table, file_ptr); print_symbol_table(node->table, file_ptr);
} }
} }
if (table->Parent_Scope == NULL) { if (getParent(table) == NULL) {
fprintf(file_ptr, "-----------------:--------:--------:--------" fprintf(file_ptr, "-----------------:--------:--------:--------"
"--------------:-------" "--------------:-------"
"----------------------\n"); "----------------------\n");

View File

@ -20,6 +20,6 @@ entry(arg) := {
many_names(2)(2) := 'r'; many_names(2)(2) := 'r';
many_names(2)(3) := 't'; many_names(2)(3) := 't';
many_names(2)(4) := 'h'; many_names(2)(4) := 'h';
many_names(2)(5) := 'o'; 0(2)(5) := 'o';
return 0; return 0;
} }