diff --git a/src/grammar.y b/src/grammar.y index d2c4d8e..eb73cc0 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -28,7 +28,7 @@ %locations - +%token ACCESS 801 %type idlist %type assignable %type expression @@ -166,6 +166,8 @@ definition: } | ID { + //printf("ID: %s\n", $1); + //printf("Type: %s\n", getType(table_lookup(getAncestor(cur), $1))); printdebug("see function def rule 1\n"); TableNode *node = table_lookup(getAncestor(cur), $1); if (node == undefined) { @@ -251,6 +253,7 @@ definition: } } //counter = 0; + printdebug("Created a new scope after seeing a function definition"); } idlist R_PAREN ASSIGN sblock { TableNode *expected = getReturn(getTypeEntry(look_up(cur, $1))); @@ -265,6 +268,8 @@ definition: } else { printdebug("CORRECT RETURN TYPE!!!"); } + //printf("Ending ID: %s\n", $1); + //printf("Ending Type: %s\n", getType(table_lookup(getAncestor(cur), $1))); } ; @@ -272,8 +277,10 @@ definition: function_declaration: FUNCTION ID COLON ID { - if(getAdInfoType(look_up(cur, $4))==TYPE_FUNCTION_TYPE){ - CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); + if(getAdInfoType(table_lookup(cur, $4))==TYPE_FUNCTION_TYPE){ + //printf("%s\n",$2); + //printf("%s\n",getName(table_lookup(cur, $4))); + CreateEntry(cur,TYPE_FUNCTION_DECLARATION, table_lookup(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); } else{ throw_error(ERROR_TYPE, "Function declatation (%s) is not a valid function type", $2); @@ -565,8 +572,8 @@ WHILE L_PAREN expression R_PAREN sblock { $$ = $8; } else { printdebug("3 differing return types within same function at line %d, column %d", @1.first_line, @1.first_column); - printf("%s\n", getName((TableNode*)$6)); - printf("%s\n", getName((TableNode*)$8)); + //printf("%s\n", getName((TableNode*)$6)); + //printf("%s\n", getName((TableNode*)$8)); $$ = undefined; } } @@ -590,11 +597,11 @@ simple_statement: node = ((TableNode*)$1); } else { - printf("%d\n",getAdInfoType((getTypeEntry((TableNode*)$1)))); + //printf("%d\n",getAdInfoType((getTypeEntry((TableNode*)$1)))); throw_error(ERROR_TYPE, "Invalid type passed to assignable."); - printf("%d, %d\n", @1.first_line, @1.first_column); - printf("%s\n", getType(getTypeEntry((TableNode*)$1))); - printf("%s\n\n", getType(getTypeEntry((TableNode*)$3))); + //printf("%d, %d\n", @1.first_line, @1.first_column); + //printf("%s\n", getType(getTypeEntry((TableNode*)$1))); + //printf("%s\n\n", getType(getTypeEntry((TableNode*)$3))); node = undefined; } @@ -910,8 +917,8 @@ assignable: while(arg_given != NULL && getName(arg_given)[0]!='&'){ arg_given = getNextEntry(arg_given); } - 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)); + if(getTypeEntry(arg_given) != getTypeEntry(param_arg_type)){ + throw_error(ERROR_TYPE, "expected %s expression as first argument in function call but got %s", getType(param_arg_type), getType(arg_given)); } param_arg_type = getNextEntry(param_arg_type); arg_given = getNextEntry(arg_given); @@ -919,8 +926,8 @@ assignable: while(arg_given != NULL && getName(arg_given)[0]=='&'){ arg_given = getNextEntry(arg_given); } - 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)); + if(getTypeEntry(arg_given) != getTypeEntry(param_arg_type)){ + throw_error(ERROR_TYPE, "expected %s expression as argument in function call but got %s", getType(param_arg_type), getType(arg_given)); } arg_given = getNextEntry(arg_given); param_arg_type = getNextEntry(param_arg_type); @@ -937,7 +944,7 @@ assignable: } TableNode *actual = getTypeEntry(actual_instance); if (expected != actual) { - throw_error(ERROR_TYPE, "expected %s expression in function call but got %s", getName(expected), getName(actual)); + throw_error(ERROR_TYPE, "expected %s expression in function call but got %s", getType(expected), getName(actual)); } if ($3 != 1) { throw_error(ERROR_SYNTAX, "expected 1 argument but got %d", $3); } @@ -968,8 +975,8 @@ assignable: 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) { - throw_error(ERROR_SYNTAX, "expected %d arguments for this array but got %d", getNumArrDim(look_up(cur, getName((TableNode*)$1))), $2); + if (getNumArrDim(getTypeEntry((TableNode*)$1)) != $3) { + throw_error(ERROR_SYNTAX, "expected %d arguments for this array but got %d", getNumArrDim(getTypeEntry((TableNode*)$1)), $3); } char* temp = temp_var_gen(); @@ -998,15 +1005,32 @@ assignable: } cur = getParent(cur); } + | assignable rec_op ACCESS + { + if(getAdInfoType((TableNode*)$1) != TYPE_ARRAY){ + throw_error(ERROR_TYPE, "Invalid type passed to array access"); + $$ = undefined; + }else if($3>getNumArrDim(getTypeEntry((TableNode*)$1))){ + throw_error(ERROR_TYPE, "Invalid trying to access the size of dimension %d but this array only has %d dimensions", $3, getNumArrDim(getTypeEntry((TableNode*)$1))); + $$ = undefined; + } else{ + char* temp = temp_var_gen(); + int t = 6; + //emission + $$ = CreateEntry(cur,t, integ, temp, NULL); + } + } - | assignable rec_op ID + | assignable rec_op ID { + if(getAdInfoType((TableNode*)$1) != TYPE_RECORD){ throw_error(ERROR_TYPE, "Invalid type passed to record access"); + $$ = undefined; } - else if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), getName(getTypeEntry((TableNode*)$1)))), $3)) { + else if(undefined != table_lookup(getRecList(getTypeEntry((TableNode*)$1)), $3)) { - TableNode* type = getTypeEntry(table_lookup(getRecList(table_lookup(getAncestor(cur), getName(getTypeEntry((TableNode*)$1)))), $3)); + TableNode* type = getTypeEntry(table_lookup(getRecList(getTypeEntry((TableNode*)$1)), $3)); char* temp = temp_var_gen(); int t = -1; if(getAdInfoType(type) == TYPE_PRIMITIVE_TYPE){ diff --git a/src/lexicalStructure.lex b/src/lexicalStructure.lex index cd1b56e..7034997 100644 --- a/src/lexicalStructure.lex +++ b/src/lexicalStructure.lex @@ -59,7 +59,7 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\] '{CHAR}' {if(tok_flag != NULL){print_tok(C_CHARACTER);}incr(line_number,column_number,C_CHARACTER);char* token = strdup(yytext)/*yylval.tn = chara*/;yylval.letter = token[1];return C_CHARACTER;} \"{SCHAR}*\" {if(tok_flag != NULL){print_tok(C_STRING);}incr(line_number,column_number,C_STRING);int k = strlen(yytext);yytext[k-1] = '\0';yylval.words = strdup(&yytext[1]);return C_STRING;} {COMMENT} {if(tok_flag != NULL){print_tok(COMMENT);}incr(line_number,column_number,COMMENT);/*return COMMENT;*/} - +_{DIGIT}+ {if(tok_flag != NULL){print_tok(ACCESS);}incr(line_number,column_number,ACCESS);yylval.integ = atoi(&yytext[1])/*words = strdup("integer")*/;return ACCESS;} "(" { if(tok_flag != NULL) {print_tok(L_PAREN);} incr(line_number,column_number,L_PAREN); return L_PAREN; } ")" { if(tok_flag != NULL) {print_tok(R_PAREN);} incr(line_number,column_number,R_PAREN); return R_PAREN; } "[" { if(tok_flag != NULL) {print_tok(L_BRACKET);} incr(line_number,column_number,L_BRACKET); return L_BRACKET; } diff --git a/src/symbol_table.c b/src/symbol_table.c index cf6edd4..26faf11 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -1293,7 +1293,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) { char *functiontype = (char *)malloc(100); - sprintf(functiontype, " %s", getName(getReturn(entry))); + sprintf(functiontype, " %s", getName(getTypeEntry(entry))); if (parentScopeNum == 0) { st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Function Definition"); } else { diff --git a/src/typedefs.h b/src/typedefs.h index 5c476bf..3b75b3a 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -55,3 +55,5 @@ #define RELEASE 614 // comments #define COMMENT 700 +//Additional tokens +#define ACCESS 801 \ No newline at end of file