diff --git a/src/grammar.y b/src/grammar.y index 36741e3..4a3df43 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -650,7 +650,7 @@ argument_list: //NEED TO EMIT PARAMETERS HERE. MAYBE USE STACK STRUCTURE 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; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } @@ -880,6 +880,8 @@ assignable: | assignable { 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); } //we have to consider emmissions in ablocks @@ -892,61 +894,45 @@ assignable: if (type == TYPE_FUNCTION_TYPE) { printdebug("%sEntering function call", COLOR_LIGHTGREEN); - if (look_up(getParent(cur), getType((TableNode*)$1))->additionalinfo->FunDecAdInfo->regularoras) { - printdebug("as function"); - //char *funtype = getType(look_up(cur, $1)); - //printdebug("%s", getType(look_up(cur, getName((TableNode*)$1)))); - - - - TableNode * typeNode = getTypeEntry((TableNode*)$1); - TableNode *param = getParameter(typeNode); - printTableNode(param); - - if (getAdInfoType(param) == TYPE_RECORD_TYPE) { - SymbolTable *recList = getRecList(param); - TableNode *lastCheckedRef = getFirstEntry(recList); - TableNode *lastCheckedAct = getFirstEntry(cur); - while (getNextEntry(lastCheckedRef) != NULL) { - lastCheckedRef = getNextEntry(lastCheckedRef); + //getting the parameter. The type of assignable is a function type so we need to access the paramater of the type + TableNode *expected = getParameter(getTypeEntry((TableNode*)$1)); + //Jump into case where the parameter is a record type + if(getAdInfoType(expected) == TYPE_RECORD_TYPE){ + //int argument_size = getRecSize(cur); + int parameter_size = getRecSize(getRecList(expected)); + printdebug("argument size is %d\n", $3); + printdebug("parameter size is %d\n", parameter_size); + if ($3 != parameter_size) { + throw_error(ERROR_SYNTAX, "expected %d arguments for this function but got %d", parameter_size, $3); + }else{ + TableNode* param_arg_type = getFirstEntry(getRecList(expected)); + TableNode* arg_given = getFirstEntry(cur); + while(arg_given != NULL && getName(arg_given)[0]=='&'){ + arg_given = getNextEntry(arg_given); } - - if ($3 != getRecLength(param)) { - throw_error(ERROR_SYNTAX, "expected %d arguments but got %d", getRecLength(param), $3); + if(getTypeEntry(arg_given) != param_arg_type){ + throw_error(ERROR_TYPE, "expected %s expression as first argument in function call but got %s", getName(param_arg_type), getType(arg_given)); } - //this isn't very efficient, but will hopefully work - while (lastCheckedAct != NULL && lastCheckedRef != NULL) { - if (getTypeEntry(lastCheckedRef) != getTypeEntry(lastCheckedAct)) { - throw_error(ERROR_TYPE, "expected %s. expression in function call got %s",getType(lastCheckedRef), getName(lastCheckedAct)); + param_arg_type = getNextEntry(param_arg_type); + arg_given = getNextEntry(arg_given); + while(arg_given != NULL && param_arg_type != NULL){ + while(arg_given != NULL && getName(arg_given)[0]=='&'){ + arg_given = getNextEntry(arg_given); } - lastCheckedAct = getNextEntry(lastCheckedAct); - TableNode *tn = getFirstEntry(recList); - - if (tn != lastCheckedRef) { - while (getNextEntry(tn) != lastCheckedRef) { - 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"); + if(getTypeEntry(arg_given) != param_arg_type){ + throw_error(ERROR_TYPE, "expected %s expression as argument in function call but got %s", getName(param_arg_type), getType(arg_given)); + } + arg_given = getNextEntry(arg_given); + param_arg_type = getNextEntry(param_arg_type); } } - } else { - char *expected = getName(getParameter(look_up(getParent(cur), getType((TableNode*)$1)))); - char *actual = getType(getFirstEntry(cur)); - if (strcmp(expected, actual) != 0) { - 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); } + }else{ + TableNode *actual = getTypeEntry(getFirstEntry(cur)); + if (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); } printTableNode(getReturn(getTypeEntry((TableNode*)$1))); char* temp = temp_var_gen(); @@ -971,7 +957,7 @@ assignable: $$ = node; //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)); - + } } else if (type == TYPE_ARRAY_TYPE) { printdebug("%sEntering array call", COLOR_LIGHTGREEN); if (getNumArrDim(look_up(getParent(cur), getType((TableNode*)$1))) != $2) { diff --git a/src/symbol_table.c b/src/symbol_table.c index 2fc926c..7b6771e 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -5,6 +5,7 @@ Constant_Stack *head = NULL; int temp2_count = 0; +int temp3_count = 0; void printdebug_impl(char *file, int line, const char *format, ...) { if (DEBUG) { @@ -24,6 +25,12 @@ char *temp_var_gen() { temp2_count++; 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) { if (type == NULL || type == undefined) { diff --git a/src/symbol_table.h b/src/symbol_table.h index 391f05a..c64f036 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -101,6 +101,7 @@ void printdebug_impl(char *file, int line, const char *format, ...); printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) char *temp_var_gen(); +char *arg_var_gen(); Constant_Stack *Push(TableNode *type, void *value, bool isConst); Constant_Stack *Pop(); Constant_Stack *Print_Stack(); @@ -165,6 +166,7 @@ extern int column_number; extern FILE *yyin; extern bool DEBUG; extern int temp2_count; +extern int temp3_count; extern TableNode *funprime; extern TableNode *arrayprim; extern TableNode *integ; diff --git a/tests/carl/NoErrors/functionValue.alpha b/tests/carl/NoErrors/functionValue.alpha index f521a42..8ad0e6c 100644 --- a/tests/carl/NoErrors/functionValue.alpha +++ b/tests/carl/NoErrors/functionValue.alpha @@ -56,11 +56,11 @@ c(x) := { entry is the first function called *) entry(arg) := { - [integer: result; string2int: f; integer: temp] + [integer: result; string2int: f; integer: temp; character: char] temp := a("Hello"); f := b(temp); result := c(f); - if (d(1,2,'c')) + if (d(1,2,char)) then { result := 0; }