still fixing assignable type checking

This commit is contained in:
Partho
2025-04-29 14:42:19 -04:00
parent 49b83cd8ad
commit dcdcf31068
4 changed files with 48 additions and 53 deletions

View File

@ -650,7 +650,7 @@ argument_list:
//NEED TO EMIT PARAMETERS HERE. MAYBE USE STACK STRUCTURE //NEED TO EMIT PARAMETERS HERE. MAYBE USE STACK STRUCTURE
expression COMMA argument_list expression COMMA argument_list
{ {
CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), getName((TableNode*)$1), NULL); CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
$$ = $3 + 1; $$ = $3 + 1;
printdebug("[ARGUMENT_LIST] argument list is %d", $$); printdebug("[ARGUMENT_LIST] argument list is %d", $$);
} }
@ -880,6 +880,8 @@ assignable:
| assignable | assignable
{ {
printdebug("%sBeginning rule 2 of assignable.", COLOR_CYAN); printdebug("%sBeginning rule 2 of assignable.", COLOR_CYAN);
//Creating a dummy scope where we create entries for all the arguments of a function call
//Must also consider that we might be in an array access
cur = CreateScope(cur, -1,-1); cur = CreateScope(cur, -1,-1);
} }
//we have to consider emmissions in ablocks //we have to consider emmissions in ablocks
@ -892,61 +894,45 @@ assignable:
if (type == TYPE_FUNCTION_TYPE) { if (type == TYPE_FUNCTION_TYPE) {
printdebug("%sEntering function call", COLOR_LIGHTGREEN); printdebug("%sEntering function call", COLOR_LIGHTGREEN);
if (look_up(getParent(cur), getType((TableNode*)$1))->additionalinfo->FunDecAdInfo->regularoras) { //getting the parameter. The type of assignable is a function type so we need to access the paramater of the type
printdebug("as function"); TableNode *expected = getParameter(getTypeEntry((TableNode*)$1));
//char *funtype = getType(look_up(cur, $1)); //Jump into case where the parameter is a record type
//printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); if(getAdInfoType(expected) == TYPE_RECORD_TYPE){
//int argument_size = getRecSize(cur);
int parameter_size = getRecSize(getRecList(expected));
printdebug("argument size is %d\n", $3);
TableNode * typeNode = getTypeEntry((TableNode*)$1); printdebug("parameter size is %d\n", parameter_size);
TableNode *param = getParameter(typeNode); if ($3 != parameter_size) {
printTableNode(param); throw_error(ERROR_SYNTAX, "expected %d arguments for this function but got %d", parameter_size, $3);
}else{
if (getAdInfoType(param) == TYPE_RECORD_TYPE) { TableNode* param_arg_type = getFirstEntry(getRecList(expected));
SymbolTable *recList = getRecList(param); TableNode* arg_given = getFirstEntry(cur);
TableNode *lastCheckedRef = getFirstEntry(recList); while(arg_given != NULL && getName(arg_given)[0]=='&'){
TableNode *lastCheckedAct = getFirstEntry(cur); arg_given = getNextEntry(arg_given);
while (getNextEntry(lastCheckedRef) != NULL) {
lastCheckedRef = getNextEntry(lastCheckedRef);
} }
if(getTypeEntry(arg_given) != param_arg_type){
if ($3 != getRecLength(param)) { throw_error(ERROR_TYPE, "expected %s expression as first argument in function call but got %s", getName(param_arg_type), getType(arg_given));
throw_error(ERROR_SYNTAX, "expected %d arguments but got %d", getRecLength(param), $3);
} }
//this isn't very efficient, but will hopefully work param_arg_type = getNextEntry(param_arg_type);
while (lastCheckedAct != NULL && lastCheckedRef != NULL) { arg_given = getNextEntry(arg_given);
if (getTypeEntry(lastCheckedRef) != getTypeEntry(lastCheckedAct)) { while(arg_given != NULL && param_arg_type != NULL){
throw_error(ERROR_TYPE, "expected %s. expression in function call got %s",getType(lastCheckedRef), getName(lastCheckedAct)); while(arg_given != NULL && getName(arg_given)[0]=='&'){
arg_given = getNextEntry(arg_given);
} }
lastCheckedAct = getNextEntry(lastCheckedAct); if(getTypeEntry(arg_given) != param_arg_type){
TableNode *tn = getFirstEntry(recList); throw_error(ERROR_TYPE, "expected %s expression as argument in function call but got %s", getName(param_arg_type), getType(arg_given));
}
if (tn != lastCheckedRef) { arg_given = getNextEntry(arg_given);
while (getNextEntry(tn) != lastCheckedRef) { param_arg_type = getNextEntry(param_arg_type);
tn = getNextEntry(tn);
}
lastCheckedRef = tn;
} else {break;}
}
} else {
if (strcmp(getName(param), getType(getFirstEntry(cur))) != 0) {
throw_error(ERROR_TYPE, "expected %s expression in function call but got %s", getName(param), getName(getFirstEntry(cur)));
}
if (getNextEntry(getFirstEntry(cur)) != NULL) {
throw_error(ERROR_SYNTAX, "expected 1 parameter, but got multiple in function call");
} }
} }
} else { }else{
char *expected = getName(getParameter(look_up(getParent(cur), getType((TableNode*)$1)))); TableNode *actual = getTypeEntry(getFirstEntry(cur));
char *actual = getType(getFirstEntry(cur)); if (expected == actual) {
if (strcmp(expected, actual) != 0) { throw_error(ERROR_TYPE, "expected %s expression in function call but got %s", expected, actual);
throw_error(ERROR_TYPE, "expected %s expression in function call but got %s", expected, actual);
}
if ($3 != 1) {
throw_error(ERROR_SYNTAX, "expected 1 argument but got %d", $3); }
} }
if ($3 != 1) {
throw_error(ERROR_SYNTAX, "expected 1 argument but got %d", $3); }
printTableNode(getReturn(getTypeEntry((TableNode*)$1))); printTableNode(getReturn(getTypeEntry((TableNode*)$1)));
char* temp = temp_var_gen(); char* temp = temp_var_gen();
@ -971,7 +957,7 @@ assignable:
$$ = node; $$ = node;
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK for function call) //NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK for function call)
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName(typeNode2), getName((TableNode*)$1)); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName(typeNode2), getName((TableNode*)$1));
}
} else if (type == TYPE_ARRAY_TYPE) { } else if (type == TYPE_ARRAY_TYPE) {
printdebug("%sEntering array call", COLOR_LIGHTGREEN); printdebug("%sEntering array call", COLOR_LIGHTGREEN);
if (getNumArrDim(look_up(getParent(cur), getType((TableNode*)$1))) != $<integ>2) { if (getNumArrDim(look_up(getParent(cur), getType((TableNode*)$1))) != $<integ>2) {

View File

@ -5,6 +5,7 @@
Constant_Stack *head = NULL; Constant_Stack *head = NULL;
int temp2_count = 0; int temp2_count = 0;
int temp3_count = 0;
void printdebug_impl(char *file, int line, const char *format, ...) { void printdebug_impl(char *file, int line, const char *format, ...) {
if (DEBUG) { if (DEBUG) {
@ -24,6 +25,12 @@ char *temp_var_gen() {
temp2_count++; temp2_count++;
return ret; return ret;
} }
char *arg_var_gen() {
char *ret = calloc(9, sizeof(*ret));
sprintf(ret, "&t%d", temp3_count);
temp3_count++;
return ret;
}
Constant_Stack *Push(TableNode *type, void *value, bool isConst) { Constant_Stack *Push(TableNode *type, void *value, bool isConst) {
if (type == NULL || type == undefined) { if (type == NULL || type == undefined) {

View File

@ -101,6 +101,7 @@ void printdebug_impl(char *file, int line, const char *format, ...);
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)
char *temp_var_gen(); char *temp_var_gen();
char *arg_var_gen();
Constant_Stack *Push(TableNode *type, void *value, bool isConst); Constant_Stack *Push(TableNode *type, void *value, bool isConst);
Constant_Stack *Pop(); Constant_Stack *Pop();
Constant_Stack *Print_Stack(); Constant_Stack *Print_Stack();
@ -165,6 +166,7 @@ extern int column_number;
extern FILE *yyin; extern FILE *yyin;
extern bool DEBUG; extern bool DEBUG;
extern int temp2_count; extern int temp2_count;
extern int temp3_count;
extern TableNode *funprime; extern TableNode *funprime;
extern TableNode *arrayprim; extern TableNode *arrayprim;
extern TableNode *integ; extern TableNode *integ;

View File

@ -56,11 +56,11 @@ c(x) := {
entry is the first function called entry is the first function called
*) *)
entry(arg) := { entry(arg) := {
[integer: result; string2int: f; integer: temp] [integer: result; string2int: f; integer: temp; character: char]
temp := a("Hello"); temp := a("Hello");
f := b(temp); f := b(temp);
result := c(f); result := c(f);
if (d(1,2,'c')) if (d(1,2,char))
then { then {
result := 0; result := 0;
} }