rebased made progress on type checking function definitions and calls

This commit is contained in:
Annie
2025-04-03 17:51:18 -04:00
parent 86d808f789
commit 1765878b85

View File

@ -199,15 +199,31 @@ definition:
} else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) { } else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) {
printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column);
} else { } 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), $<words>1)))), $<words>4, NULL);
}R_PAREN ASSIGN sblock
| ID {
TableNode *node = table_lookup(getAncestor(cur), $<words>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); setStartLine(node, @1.first_line);
setAsKeyword(node, true); setAsKeyword(node, true);
} }
cur = CreateScope(cur, 0, 0); cur = CreateScope(cur, 0, 0);
} }AS L_PAREN {
AS L_PAREN
{
TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $<words>1)))); TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $<words>1))));
printdebug("%s", getType(parameter)); printdebug("parameter type: %s", getType(parameter));
if (parameter == undefined) { if (parameter == undefined) {
printdebug("function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); 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){
@ -215,19 +231,15 @@ definition:
printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column); printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column);
}else { }else {
for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){ for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){
CreateEntry(cur, entry, NULL, NULL); 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",
idlist $1,$6);} R_PAREN ASSIGN sblock
{
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: function_declaration:
FUNCTION ID COLON ID FUNCTION ID COLON ID
{ {
@ -403,22 +415,22 @@ ablock:
argument_list: argument_list:
<<<<<<< HEAD
expression COMMA argument_list expression COMMA argument_list
{ {
CreateEntry(cur, look_up(cur, $1), "", NULL); CreateEntry(cur, look_up(cur, $1), $1, NULL);
$<integ>$ = $<integ>3 + 1; $<integ>$ = $<integ>3 + 1;
printdebug("[ARGUMENT_LIST] argument list is %d", $<integ>$); printdebug("[ARGUMENT_LIST] argument list is %d", $<integ>$);
} }
| expression | expression
{ {
CreateEntry(cur, look_up(cur, $1), "", NULL); CreateEntry(cur, look_up(cur, $1), $1, NULL);
$<integ>$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $<integ>$); $<integ>$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $<integ>$);
} }
; ;
// will ALWAYS be a TYPE // will ALWAYS be a TYPE
expression: expression:
constant constant
@ -575,6 +587,7 @@ expression:
assignable: assignable:
ID ID
{ {
//$$ = $1;
$$ = getType(look_up(cur,$1)); $$ = getType(look_up(cur,$1));
printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);
} }
@ -588,29 +601,40 @@ assignable:
{ {
int type = getAdInfoType(look_up(getParent(cur), $1)); int type = getAdInfoType(look_up(getParent(cur), $1));
printdebug("%stype is %d", COLOR_PURPLE, type); printdebug("%stype is %d", COLOR_PURPLE, type);
printdebug("%s", $1);
if (type == TYPE_FUNCTION_DECLARATION) { if (type == TYPE_FUNCTION_DECLARATION) {
printdebug("%sEntering function call", COLOR_LIGHTGREEN); printdebug("%sEntering function call", COLOR_LIGHTGREEN);
if (getAsKeyword(look_up(getParent(cur), $1))) { if (look_up(getParent(cur), $1)->additionalinfo->FunDecAdInfo->regularoras) {
TableNode *param = getParameter(look_up(getParent(cur), $1)); 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); SymbolTable *recList = getRecList(param);
TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedRef = getFirstEntry(recList);
TableNode *lastCheckedAct = getFirstEntry(cur); TableNode *lastCheckedAct = getFirstEntry(cur);
while (getNextEntry(lastCheckedRef) != NULL) { while (getNextEntry(lastCheckedRef) != NULL) {
lastCheckedRef = getNextEntry(lastCheckedRef); lastCheckedRef = getNextEntry(lastCheckedRef);
} }
//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), getType(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); 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); lastCheckedAct = getNextEntry(lastCheckedAct);
TableNode *tn = getFirstEntry(recList); TableNode *tn = getFirstEntry(recList);
if (tn != lastCheckedRef) {
while (getNextEntry(tn) != lastCheckedRef) { while (getNextEntry(tn) != lastCheckedRef) {
tn = getNextEntry(tn); tn = getNextEntry(tn);
} }
lastCheckedRef = tn; lastCheckedRef = tn;
} else {break;}
} }
} else { } else {
char *expected = getName(getParameter(look_up(getParent(cur), $1))); char *expected = getName(getParameter(look_up(getParent(cur), $1)));
char *actual = getType(getFirstEntry(cur)); 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); 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((look_up(cur, getType(look_up(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_TYPE) { } else if (type == TYPE_ARRAY_TYPE) {