diff --git a/src/grammar.y b/src/grammar.y index dfc8db3..79ff3db 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -21,7 +21,7 @@ void yyerror(const char *err); int token_tracker; TableNode * tn; - int counter; +// int counter; %} %union { @@ -159,29 +159,57 @@ definition: CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo((TableNode*)$4 ,(TableNode*)$6)); } - | ID { - TableNode *node = table_lookup(getAncestor(cur), $1); - if (node == undefined) { + | ID { + printdebug("see function def rule 1\n"); + 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); - }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); - } - else { - printdebug("setting as keyword to true"); - setStartLine(node, @1.first_line); - setAsKeyword(node, true); - } - cur = CreateScope(cur, 0, 0); - printdebug("Created a new scope"); + printdebug(" [TYPE CHECK] undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column); + }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); + } + else { + printdebug("setting as keyword to true"); + setStartLine(node, @1.first_line); + setAsKeyword(node, true); + } + cur = CreateScope(cur, 0, 0); + printdebug("Created a new scope"); } L_PAREN { TableNode * parameter = getParameter(getTypeEntry(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)); - 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); - }else if(getAdInfoType(parameter) != TYPE_RECORD_TYPE){ - int type_of_param_type = getAdInfoType(parameter);//this is an enum value defined in symbol_table.h + 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); + }else if(getAdInfoType(parameter) != TYPE_RECORD_TYPE){ + 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 || type_of_param_type == TYPE_FUNCTION_DECLARATION || type_of_param_type == TYPE_ARRAY @@ -190,60 +218,31 @@ definition: || 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)); + 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, parameter,NULL, getAdInfo(parameter)); + CreateEntry(cur, TYPE_FUNCTION_DECLARATION, entry,NULL, getAdInfo(entry)); } 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){ - 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 - || 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)); - } + CreateEntry(cur, TYPE_PRIMITIVE, entry,NULL, getAdInfo(entry)); + } /*printdebug("creating entry of type %s for function", getType(entry)); CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/ - } - } + } } - counter = 0; + } + //counter = 0; printdebug("Created a new scope after seeing a function definition"); - } idlist { - 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 + } idlist R_PAREN ASSIGN sblock //check sblock type ; @@ -278,24 +277,31 @@ function_declaration: idlist: ID { - counter ++; + printdebug("idlist rule 1 ID: %s", $1); TableNode *entry = getFirstEntry(cur); - int count = 1; - while(counttheName == 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); + //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 { @@ -303,22 +309,25 @@ idlist: } | ID - { - counter ++; + { printdebug("idlist rule 2 ID: %s", $1); TableNode *entry = getFirstEntry(cur); - int count = 1; - while(count1); - $$ = 1; + 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); + } + 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); printdebug("Did not create a new scope when saw dblock, set line number to %d instead", @1.first_line); } 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"); } } diff --git a/src/symbol_table.c b/src/symbol_table.c index 7cd8b01..5370912 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -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"); } } + 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) { char *recordAdInfo = (char *)malloc(100); sprintf(recordAdInfo, " elements-%d size-%d bytes", getRecLength(entry), getRecTotal(entry)); diff --git a/tests/sprint2/test/sp2_llnode.alpha b/tests/sprint2/test/sp2_llnode.alpha index 08048a7..b102ba8 100644 --- a/tests/sprint2/test/sp2_llnode.alpha +++ b/tests/sprint2/test/sp2_llnode.alpha @@ -47,11 +47,11 @@ foo (x) := { return x * x; } -bar1 (a) := { - return a.x * a.y; +bar1(a,b) := { + return a * b; } -bar2 as (r,s) := { +bar2(r,s) := { if (r < s) then { while (!(r < s)) { r := r + 1;