From 1765878b85c9846e0b19134cdad9378fb6f1ace9 Mon Sep 17 00:00:00 2001 From: Annie Date: Thu, 3 Apr 2025 17:51:18 -0400 Subject: [PATCH] rebased made progress on type checking function definitions and calls --- src/grammar.y | 85 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 31 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index b3f2334..cbb9ec1 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -199,33 +199,45 @@ definition: } else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) { printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); } else { + setStartLine(node, @1.first_line); + setAsKeyword(node, false); + } + cur = CreateScope(cur, 0, 0); + } L_PAREN ID { + printdebug("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s", + $1,$4); + CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))), $4, NULL); + }R_PAREN ASSIGN sblock + | ID { + TableNode *node = table_lookup(getAncestor(cur), $1); + if (node == undefined) { + printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); + }else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){ + printdebug("function not declared 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); - } - AS L_PAREN - { + }AS L_PAREN { TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); - printdebug("%s", getType(parameter)); + printdebug("parameter type: %s", getType(parameter)); if (parameter == undefined) { printdebug("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) { + }else if(getAdInfoType(parameter) != TYPE_RECORD){ printdebug("record: %s., primitive: %s.", getType(parameter), getName(recprime)); printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column); - } else { - for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)) { - CreateEntry(cur, entry, NULL, NULL); + }else { + for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ + printdebug("creating entry of type %s for function", getType(entry)); + CreateEntry(cur, entry, "undefined", NULL); } } - } - idlist - { - printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", $1,$6); - } - R_PAREN ASSIGN sblock - ; - + } idlist {printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d", + $1,$6);} R_PAREN ASSIGN sblock + ; function_declaration: @@ -403,22 +415,22 @@ ablock: argument_list: +<<<<<<< HEAD expression COMMA argument_list { - CreateEntry(cur, look_up(cur, $1), "", NULL); + CreateEntry(cur, look_up(cur, $1), $1, NULL); $$ = $3 + 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } | expression { - CreateEntry(cur, look_up(cur, $1), "", NULL); + CreateEntry(cur, look_up(cur, $1), $1, NULL); $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } ; - // will ALWAYS be a TYPE expression: constant @@ -575,6 +587,7 @@ expression: assignable: ID { + //$$ = $1; $$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1); } @@ -588,29 +601,40 @@ assignable: { int type = getAdInfoType(look_up(getParent(cur), $1)); printdebug("%stype is %d", COLOR_PURPLE, type); + printdebug("%s", $1); if (type == TYPE_FUNCTION_DECLARATION) { printdebug("%sEntering function call", COLOR_LIGHTGREEN); - if (getAsKeyword(look_up(getParent(cur), $1))) { - TableNode *param = getParameter(look_up(getParent(cur), $1)); + if (look_up(getParent(cur), $1)->additionalinfo->FunDecAdInfo->regularoras) { + printdebug("as function"); + //char *funtype = getType(look_up(cur, $1)); + printdebug("%s", getType(look_up(cur, $1))); + TableNode *param = getParameter(look_up(cur, getType(look_up(cur, $1)))); SymbolTable *recList = getRecList(param); TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedAct = getFirstEntry(cur); while (getNextEntry(lastCheckedRef) != NULL) { - lastCheckedRef = getNextEntry(lastCheckedRef); + lastCheckedRef = getNextEntry(lastCheckedRef); } + + //this isn't very efficient, but will hopefully work while (lastCheckedAct != NULL && lastCheckedRef != NULL) { - if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) { - 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); + if (strcmp(getName(lastCheckedAct), getType(lastCheckedRef)) != 0) { + printdebug("expected %s. expression in function call got %s. at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); + printdebug("%d", strcmp(getName(lastCheckedAct), getName(lastCheckedRef))); } lastCheckedAct = getNextEntry(lastCheckedAct); TableNode *tn = getFirstEntry(recList); - while (getNextEntry(tn) != lastCheckedRef) { - tn = getNextEntry(tn); - } - lastCheckedRef = tn; - } + + if (tn != lastCheckedRef) { + while (getNextEntry(tn) != lastCheckedRef) { + tn = getNextEntry(tn); + } + lastCheckedRef = tn; + } else {break;} + } + } else { char *expected = getName(getParameter(look_up(getParent(cur), $1))); char *actual = getType(getFirstEntry(cur)); @@ -618,8 +642,7 @@ 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); } } - - $$ = getName(getReturn(table_lookup(getAncestor(cur), $1))); + $$ = getName(getReturn((look_up(cur, getType(look_up(cur, $1)))))); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); } else if (type == TYPE_ARRAY_TYPE) {