diff --git a/src/grammar.y b/src/grammar.y index e4185c8..ba66d6b 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -141,7 +141,7 @@ definition: } | ID { TableNode *node = table_lookup(getAncestor(cur), $1); - if (node == NULL) { + 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); @@ -158,25 +158,26 @@ definition: }R_PAREN ASSIGN sblock | ID { TableNode *node = table_lookup(getAncestor(cur), $1); - if (node == NULL) { + if (node == undefined) { printdebug("null check"); } - if (node == NULL) { + 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 { setStartLine(node, @1.first_line); - setAsKeyword(node, false); + setAsKeyword(node, true); } cur = CreateScope(cur, 0, 0); }AS L_PAREN { TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1)))); - if (parameter == NULL) { - printdebug("function defined with as, but parameter is not a record at line %d, column %d", @1.first_line, @1.first_column); + 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){ - printdebug("function defined with as, but parameter is not a record at line %d, column %d", @1.first_line, @1.first_column); + printdebug("record: %s., primitive: %s.", getName(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); @@ -187,6 +188,7 @@ definition: ; + function_declaration: FUNCTION ID COLON ID {CreateEntry(cur, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false));} | EXTERNAL FUNCTION ID COLON ID {CreateEntry(cur, look_up(cur, $5), $3, NULL);} @@ -203,7 +205,7 @@ idlist: printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column); } addName(entry, $1); - } COMMA idlist {$$ = $3 + 1;} + } COMMA idlist {$$ = $4 + 1;} | ID { TableNode *entry = getFirstEntry(cur); @@ -259,19 +261,10 @@ compound_statement: ; simple_statement: - assignable ASSIGN expression - { - if(strcmp($1, $3) == 0) { - printdebug("Passed standard type check; assignable = expression"); - } else if((strcmp($1, "rec") == 0) && (strcmp($3, "address") == 0)) { - printdebug("Passed rec type check; rec = address"); - } else if((strcmp($1, "array") == 0) && (strcmp($3, "address") == 0)) { - printdebug("Passed array type check; array = address"); - } else { - printdebug("%s[TYPE ERROR] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE); - printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, $1, $3, COLOR_WHITE); - } - } + assignable ASSIGN expression {if(strcmp($1, $3) == 0){ + } else { + printdebug("Mismatch at line %d and column%d", @2.first_line, @2.first_column); + }} | RETURN expression ; @@ -295,8 +288,13 @@ L_PAREN argument_list R_PAREN {$$ = $2; printdebug("ablock is %d", ; argument_list: - expression COMMA argument_list {$$ = $3 + 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$);} - | expression {$$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$);} + expression COMMA argument_list { + CreateEntry(cur, NULL, $1, NULL); + $$ = $3 + 1; + printdebug("[ARGUMENT_LIST] argument list is %d", $$);} + | expression { + CreateEntry(cur, NULL, $1, NULL); + $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$);} ; // will ALWAYS be a TYPE @@ -381,19 +379,56 @@ expression: // include type check for ablock in arrays - ablock is always the int of the elements in array/rec assignable: ID {$$ = getType(look_up(cur,$1)); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);} - | assignable ablock { - int type = getAdInfoType(look_up(cur, $1)); - if (type == TYPE_FUNCTION_TYPE) { + | assignable { + cur = CreateScope(cur, -1,-1); + }ablock { + int type = getAdInfoType(look_up(getParent(cur), $1)); + if (type == TYPE_FUNCTION_DECLARATION) { + if (getAsKeyword(look_up(getParent(cur), $1))) { + TableNode *param = getParameter(look_up(getParent(cur), $1)); + SymbolTable *recList = getRecList(param); + TableNode *lastCheckedRef = getFirstEntry(recList); + TableNode *lastCheckedAct = getFirstEntry(cur); + while (getNextEntry(lastCheckedRef) != NULL) { + 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",getName(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); + } + lastCheckedAct = getNextEntry(lastCheckedAct); + TableNode *tn = getFirstEntry(recList); + while (getNextEntry(tn) != lastCheckedRef) { + tn = getNextEntry(tn); + } + lastCheckedRef = tn; + } + + } else { + char *expected = getName(getParameter(look_up(getParent(cur), $1))); + char *actual = getName(getFirstEntry(cur)); + if (strcmp(expected, actual) != 0) { + 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))); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); } else if (type == TYPE_ARRAY) { - if (getNumArrDim(look_up(cur, $1)) != $2) { - printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, $1)), $2, @2.first_line, @2.first_column); - } + if (getNumArrDim(look_up(getParent(cur), $1)) != $2) { + printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, $1)), $2, @2.first_line, @2.first_column); + } + for (TableNode *tn = getFirstEntry(cur); tn != NULL; tn = getNextEntry(tn)) { + if (strcmp(getName(tn), "integer") != 0) { + printdebug("expected only integer expressions in array ablock at line %d column %d", @3.first_line, @3.first_column); + } + } $$ = $1; printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); } - } + cur = getParent(cur); + } | assignable rec_op ID {if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3)){ {$$ = getName(table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3));}}; printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", $$, $1);} ;