fixed issue with not printing array instances in symbol table

This commit is contained in:
Partho
2025-04-14 02:01:51 -04:00
parent 3fc7a6371a
commit 3baa95288a
3 changed files with 108 additions and 88 deletions

View File

@ -21,7 +21,7 @@
void yyerror(const char *err); void yyerror(const char *err);
int token_tracker; int token_tracker;
TableNode * tn; TableNode * tn;
int counter; // int counter;
%} %}
%union { %union {
@ -159,29 +159,57 @@ definition:
CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo((TableNode*)$4 ,(TableNode*)$6)); CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo((TableNode*)$4 ,(TableNode*)$6));
} }
| ID { | ID {
TableNode *node = table_lookup(getAncestor(cur), $1); printdebug("see function def rule 1\n");
if (node == undefined) { TableNode *node = table_lookup(getAncestor(cur), $1);
if (node == undefined) {
printdebug(" [TYPE CHECK] undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column); printdebug(" [TYPE CHECK] undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column);
}else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){ }else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){
printdebug("[TYPE CHECK] not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column); printdebug("[TYPE CHECK] not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column);
} }
else { else {
printdebug("setting as keyword to true"); printdebug("setting as keyword to true");
setStartLine(node, @1.first_line); setStartLine(node, @1.first_line);
setAsKeyword(node, true); setAsKeyword(node, true);
} }
cur = CreateScope(cur, 0, 0); cur = CreateScope(cur, 0, 0);
printdebug("Created a new scope"); printdebug("Created a new scope");
} L_PAREN { } L_PAREN {
TableNode * parameter = getParameter(getTypeEntry(table_lookup(getAncestor(cur), $1))); TableNode * parameter = getParameter(getTypeEntry(table_lookup(getAncestor(cur), $1)));
//TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); //TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1))));
printdebug("type of parameter: %s", getName(parameter)); printdebug("type of parameter: %s", getName(parameter));
if (parameter == undefined) { if (parameter == undefined) {
printdebug("[TYPE CHECK] function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); printdebug("[TYPE CHECK] function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column);
}else if(getAdInfoType(parameter) != TYPE_RECORD_TYPE){ }else if(getAdInfoType(parameter) != TYPE_RECORD_TYPE){
int type_of_param_type = getAdInfoType(parameter);//this is an enum value defined in symbol_table.h int type_of_param_type = getAdInfoType(parameter);//this is an enum value defined in symbol_table.h
if( type_of_param_type == TYPE_UNDEFINED
|| type_of_param_type == TYPE_FUNCTION_DECLARATION
|| type_of_param_type == TYPE_ARRAY
|| type_of_param_type == TYPE_PRIMITIVE
|| type_of_param_type == TYPE_ALL_ELSE
|| type_of_param_type == TYPE_SYSTEM_DEFINED
|| type_of_param_type == TYPE_RECORD
|| type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused
printdebug("[TYPE CHECK] type of parameter being passed in to function definition is %s which is invalid", getAdInfo(parameter));
type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases
}
if(type_of_param_type == TYPE_UNDEFINED){
CreateEntry(cur,type_of_param_type, undefined, NULL, NULL);
} else {
if(type_of_param_type == TYPE_FUNCTION_TYPE){
CreateEntry(cur, TYPE_FUNCTION_DECLARATION, parameter,NULL, getAdInfo(parameter));
}
if(type_of_param_type == TYPE_ARRAY_TYPE){
CreateEntry(cur, TYPE_ARRAY, parameter,NULL, getAdInfo(parameter));
}
if(type_of_param_type == TYPE_PRIMITIVE_TYPE){
CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter));
}
}
} else {
for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){
int type_of_param_type = getAdInfoType(entry);
if( type_of_param_type == TYPE_UNDEFINED if( type_of_param_type == TYPE_UNDEFINED
|| type_of_param_type == TYPE_FUNCTION_DECLARATION || type_of_param_type == TYPE_FUNCTION_DECLARATION
|| type_of_param_type == TYPE_ARRAY || type_of_param_type == TYPE_ARRAY
@ -190,60 +218,31 @@ definition:
|| type_of_param_type == TYPE_SYSTEM_DEFINED || type_of_param_type == TYPE_SYSTEM_DEFINED
|| type_of_param_type == TYPE_RECORD || type_of_param_type == TYPE_RECORD
|| type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused || type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused
printdebug("[TYPE CHECK] type of parameter being passed in to function definition is %s which is invalid", getAdInfo(parameter)); printdebug("[TYPE CHECK] type of parameter (if record) inside record being passed in to function definition is %s which is invalid", getAdInfo(parameter));
type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases
}else{
printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry));
} }
if(type_of_param_type == TYPE_UNDEFINED){ if(type_of_param_type == TYPE_UNDEFINED){
CreateEntry(cur,type_of_param_type, undefined, NULL, NULL); CreateEntry(cur,type_of_param_type, undefined, NULL, NULL);
} else { } else {
if(type_of_param_type == TYPE_FUNCTION_TYPE){ if(type_of_param_type == TYPE_FUNCTION_TYPE){
CreateEntry(cur, TYPE_FUNCTION_DECLARATION, parameter,NULL, getAdInfo(parameter)); CreateEntry(cur, TYPE_FUNCTION_DECLARATION, entry,NULL, getAdInfo(entry));
} }
if(type_of_param_type == TYPE_ARRAY_TYPE){ if(type_of_param_type == TYPE_ARRAY_TYPE){
CreateEntry(cur, TYPE_ARRAY, parameter,NULL, getAdInfo(parameter)); CreateEntry(cur, TYPE_ARRAY, entry,NULL, getAdInfo(entry));
} }
if(type_of_param_type == TYPE_PRIMITIVE_TYPE){ if(type_of_param_type == TYPE_PRIMITIVE_TYPE){
CreateEntry(cur, TYPE_PRIMITIVE, parameter,NULL, getAdInfo(parameter)); CreateEntry(cur, TYPE_PRIMITIVE, entry,NULL, getAdInfo(entry));
} }
}
} else {
for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){
int type_of_param_type = getAdInfoType(entry);
if( type_of_param_type == TYPE_UNDEFINED
|| type_of_param_type == TYPE_FUNCTION_DECLARATION
|| type_of_param_type == TYPE_ARRAY
|| type_of_param_type == TYPE_PRIMITIVE
|| type_of_param_type == TYPE_ALL_ELSE
|| type_of_param_type == TYPE_SYSTEM_DEFINED
|| type_of_param_type == TYPE_RECORD
|| type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused
printdebug("[TYPE CHECK] type of parameter (if record) inside record being passed in to function definition is %s which is invalid", getAdInfo(parameter));
type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases
}else{
printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry));
}
if(type_of_param_type == TYPE_UNDEFINED){
CreateEntry(cur,type_of_param_type, undefined, NULL, NULL);
} else {
if(type_of_param_type == TYPE_FUNCTION_TYPE){
CreateEntry(cur, TYPE_FUNCTION_DECLARATION, entry,NULL, getAdInfo(entry));
}
if(type_of_param_type == TYPE_ARRAY_TYPE){
CreateEntry(cur, TYPE_ARRAY, entry,NULL, getAdInfo(entry));
}
if(type_of_param_type == TYPE_PRIMITIVE_TYPE){
CreateEntry(cur, TYPE_PRIMITIVE, entry,NULL, getAdInfo(entry));
}
/*printdebug("creating entry of type %s for function", getType(entry)); /*printdebug("creating entry of type %s for function", getType(entry));
CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/
} }
}
} }
counter = 0; }
//counter = 0;
printdebug("Created a new scope after seeing a function definition"); printdebug("Created a new scope after seeing a function definition");
} idlist { } idlist R_PAREN ASSIGN sblock //check sblock type
printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", $1,$5);
} R_PAREN ASSIGN sblock //check sblock type
; ;
@ -278,24 +277,31 @@ function_declaration:
idlist: idlist:
ID ID
{ {
counter ++; printdebug("idlist rule 1 ID: %s", $1);
TableNode *entry = getFirstEntry(cur); TableNode *entry = getFirstEntry(cur);
int count = 1; while((getName(entry) != getName(undefined))){
while(count<counter){
entry = getNextEntry(entry);
count++;
}
printdebug("RAAAAAAAAAAAAAAAAAAAAAAAAAAAAH");
printTableNode(entry);
while (strcmp(getName(entry),"undefined") != 0) {
entry = getNextEntry(entry); entry = getNextEntry(entry);
} }
if (getNextEntry(entry) == NULL) { if (entry == NULL){
printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column); printdebug("mismatch in number of parameters passed to function");
}
else if(entry->theName == NULL){
addName(entry, $1);
printdebug("name added to entry of type %s is %s in function parameter scope",getType(entry), $1);
} else {
printdebug("undefined types passed in to function scope. Improper.");
addName(entry, $1);
} }
addName(entry, $1);
printdebug("name added to entry is %s", $1);
printTableNode(entry); printTableNode(entry);
//while (strcmp(getName(entry),"undefined") != 0) {
// entry = getNextEntry(entry);
//}
//if (getNextEntry(entry) == NULL) {
// printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column);
//}
//addName(entry, $1);
//printdebug("name added to entry is %s", $1);
//printTableNode(entry);
} }
COMMA idlist COMMA idlist
{ {
@ -303,22 +309,25 @@ idlist:
} }
| ID | ID
{ { printdebug("idlist rule 2 ID: %s", $1);
counter ++;
TableNode *entry = getFirstEntry(cur); TableNode *entry = getFirstEntry(cur);
int count = 1; while((getName(entry) != getName(undefined))){
while(count<counter){
entry = getNextEntry(entry);
count++;
}
while (strcmp(getName(entry),"undefined") != 0) {
entry = getNextEntry(entry); entry = getNextEntry(entry);
} }
if (getNextEntry(entry) != NULL) { if (entry == NULL){
printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column); printdebug("mismatch in number of parameters passed to function");
} }
addName(entry, $<words>1); else if(entry->theName == NULL){
$$ = 1; addName(entry, $1);
printdebug("name added to entry of type %s is %s in function parameter scope",getType(entry), $1);
} else {
printdebug("undefined types passed in to function scope. Improper.");
addName(entry, $1);
}
printTableNode(entry);
printdebug("Name of entry is now %s", getName(entry));
printdebug("Type of entry is %s", getType(entry));
printdebug("tag is %d", getAdInfoType(entry));
} }
; ;
@ -375,7 +384,7 @@ dblock:
setColumnNumber(cur,@1.first_line); setColumnNumber(cur,@1.first_line);
printdebug("Did not create a new scope when saw dblock, set line number to %d instead", @1.first_line); printdebug("Did not create a new scope when saw dblock, set line number to %d instead", @1.first_line);
} else{ } else{
cur = CreateScope(cur,@1.first_line,@1.first_column); // <----- What is this? //cur = CreateScope(cur,@1.first_line,@1.first_column); // <----- What is this?
printdebug("Created a new scope when seeing a dblock"); printdebug("Created a new scope when seeing a dblock");
} }
} }

View File

@ -1128,6 +1128,17 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, arrayType, " Type of Array"); st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, arrayType, " Type of Array");
} }
} }
if (getAdInfoType(entry) == TYPE_ARRAY) {
char *arrayType = (char *)malloc(100);
sprintf(arrayType, " %d -> %s", getNumArrDim(entry),
getName(getArrType(entry)));
if (parentScopeNum == 0) {
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, getType(entry), " Array Instance");
} else {
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, getType(entry), " Array Instance");
}
}
if (getAdInfoType(entry) == TYPE_RECORD_TYPE) { if (getAdInfoType(entry) == TYPE_RECORD_TYPE) {
char *recordAdInfo = (char *)malloc(100); char *recordAdInfo = (char *)malloc(100);
sprintf(recordAdInfo, " elements-%d size-%d bytes", getRecLength(entry), getRecTotal(entry)); sprintf(recordAdInfo, " elements-%d size-%d bytes", getRecLength(entry), getRecTotal(entry));

View File

@ -47,11 +47,11 @@ foo (x) := {
return x * x; return x * x;
} }
bar1 (a) := { bar1(a,b) := {
return a.x * a.y; return a * b;
} }
bar2 as (r,s) := { bar2(r,s) := {
if (r < s) then { if (r < s) then {
while (!(r < s)) { while (!(r < s)) {
r := r + 1; r := r + 1;