almost finished type checks
This commit is contained in:
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
%locations
|
%locations
|
||||||
|
|
||||||
|
%token <integ> ACCESS 801
|
||||||
%type <integ> idlist
|
%type <integ> idlist
|
||||||
%type <tn> assignable
|
%type <tn> assignable
|
||||||
%type <tn> expression
|
%type <tn> expression
|
||||||
@ -166,6 +166,8 @@ definition:
|
|||||||
}
|
}
|
||||||
|
|
||||||
| ID {
|
| ID {
|
||||||
|
//printf("ID: %s\n", $1);
|
||||||
|
//printf("Type: %s\n", getType(table_lookup(getAncestor(cur), $1)));
|
||||||
printdebug("see function def rule 1\n");
|
printdebug("see function def rule 1\n");
|
||||||
TableNode *node = table_lookup(getAncestor(cur), $1);
|
TableNode *node = table_lookup(getAncestor(cur), $1);
|
||||||
if (node == undefined) {
|
if (node == undefined) {
|
||||||
@ -251,6 +253,7 @@ definition:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//counter = 0;
|
//counter = 0;
|
||||||
|
|
||||||
printdebug("Created a new scope after seeing a function definition");
|
printdebug("Created a new scope after seeing a function definition");
|
||||||
} idlist R_PAREN ASSIGN sblock {
|
} idlist R_PAREN ASSIGN sblock {
|
||||||
TableNode *expected = getReturn(getTypeEntry(look_up(cur, $1)));
|
TableNode *expected = getReturn(getTypeEntry(look_up(cur, $1)));
|
||||||
@ -265,6 +268,8 @@ definition:
|
|||||||
} else {
|
} else {
|
||||||
printdebug("CORRECT RETURN TYPE!!!");
|
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_declaration:
|
||||||
FUNCTION ID COLON ID
|
FUNCTION ID COLON ID
|
||||||
{
|
{
|
||||||
if(getAdInfoType(look_up(cur, $4))==TYPE_FUNCTION_TYPE){
|
if(getAdInfoType(table_lookup(cur, $4))==TYPE_FUNCTION_TYPE){
|
||||||
CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false));
|
//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{
|
else{
|
||||||
throw_error(ERROR_TYPE, "Function declatation (%s) is not a valid function type", $2);
|
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;
|
$$ = $8;
|
||||||
} else {
|
} else {
|
||||||
printdebug("3 differing return types within same function at line %d, column %d", @1.first_line, @1.first_column);
|
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*)$6));
|
||||||
printf("%s\n", getName((TableNode*)$8));
|
//printf("%s\n", getName((TableNode*)$8));
|
||||||
$$ = undefined;
|
$$ = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -590,11 +597,11 @@ simple_statement:
|
|||||||
|
|
||||||
node = ((TableNode*)$1);
|
node = ((TableNode*)$1);
|
||||||
} else {
|
} else {
|
||||||
printf("%d\n",getAdInfoType((getTypeEntry((TableNode*)$1))));
|
//printf("%d\n",getAdInfoType((getTypeEntry((TableNode*)$1))));
|
||||||
throw_error(ERROR_TYPE, "Invalid type passed to assignable.");
|
throw_error(ERROR_TYPE, "Invalid type passed to assignable.");
|
||||||
printf("%d, %d\n", @1.first_line, @1.first_column);
|
//printf("%d, %d\n", @1.first_line, @1.first_column);
|
||||||
printf("%s\n", getType(getTypeEntry((TableNode*)$1)));
|
//printf("%s\n", getType(getTypeEntry((TableNode*)$1)));
|
||||||
printf("%s\n\n", getType(getTypeEntry((TableNode*)$3)));
|
//printf("%s\n\n", getType(getTypeEntry((TableNode*)$3)));
|
||||||
node = undefined;
|
node = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -910,8 +917,8 @@ assignable:
|
|||||||
while(arg_given != NULL && getName(arg_given)[0]!='&'){
|
while(arg_given != NULL && getName(arg_given)[0]!='&'){
|
||||||
arg_given = getNextEntry(arg_given);
|
arg_given = getNextEntry(arg_given);
|
||||||
}
|
}
|
||||||
if(getTypeEntry(arg_given) != param_arg_type){
|
if(getTypeEntry(arg_given) != getTypeEntry(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));
|
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);
|
param_arg_type = getNextEntry(param_arg_type);
|
||||||
arg_given = getNextEntry(arg_given);
|
arg_given = getNextEntry(arg_given);
|
||||||
@ -919,8 +926,8 @@ assignable:
|
|||||||
while(arg_given != NULL && getName(arg_given)[0]=='&'){
|
while(arg_given != NULL && getName(arg_given)[0]=='&'){
|
||||||
arg_given = getNextEntry(arg_given);
|
arg_given = getNextEntry(arg_given);
|
||||||
}
|
}
|
||||||
if(getTypeEntry(arg_given) != param_arg_type){
|
if(getTypeEntry(arg_given) != getTypeEntry(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));
|
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);
|
arg_given = getNextEntry(arg_given);
|
||||||
param_arg_type = getNextEntry(param_arg_type);
|
param_arg_type = getNextEntry(param_arg_type);
|
||||||
@ -937,7 +944,7 @@ assignable:
|
|||||||
}
|
}
|
||||||
TableNode *actual = getTypeEntry(actual_instance);
|
TableNode *actual = getTypeEntry(actual_instance);
|
||||||
if (expected != actual) {
|
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) {
|
if ($3 != 1) {
|
||||||
throw_error(ERROR_SYNTAX, "expected 1 argument but got %d", $3); }
|
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));
|
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(getTypeEntry((TableNode*)$1)) != $3) {
|
||||||
throw_error(ERROR_SYNTAX, "expected %d arguments for this array but got %d", getNumArrDim(look_up(cur, getName((TableNode*)$1))), $<integ>2);
|
throw_error(ERROR_SYNTAX, "expected %d arguments for this array but got %d", getNumArrDim(getTypeEntry((TableNode*)$1)), $3);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
@ -998,15 +1005,32 @@ assignable:
|
|||||||
}
|
}
|
||||||
cur = getParent(cur);
|
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){
|
if(getAdInfoType((TableNode*)$1) != TYPE_RECORD){
|
||||||
throw_error(ERROR_TYPE, "Invalid type passed to record access");
|
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();
|
char* temp = temp_var_gen();
|
||||||
int t = -1;
|
int t = -1;
|
||||||
if(getAdInfoType(type) == TYPE_PRIMITIVE_TYPE){
|
if(getAdInfoType(type) == TYPE_PRIMITIVE_TYPE){
|
||||||
|
@ -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;}
|
'{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;}
|
\"{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;*/}
|
{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(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(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; }
|
"[" { if(tok_flag != NULL) {print_tok(L_BRACKET);} incr(line_number,column_number,L_BRACKET); return L_BRACKET; }
|
||||||
|
@ -1293,7 +1293,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
|
|||||||
|
|
||||||
if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) {
|
if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) {
|
||||||
char *functiontype = (char *)malloc(100);
|
char *functiontype = (char *)malloc(100);
|
||||||
sprintf(functiontype, " %s", getName(getReturn(entry)));
|
sprintf(functiontype, " %s", getName(getTypeEntry(entry)));
|
||||||
if (parentScopeNum == 0) {
|
if (parentScopeNum == 0) {
|
||||||
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Function Definition");
|
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Function Definition");
|
||||||
} else {
|
} else {
|
||||||
|
@ -55,3 +55,5 @@
|
|||||||
#define RELEASE 614
|
#define RELEASE 614
|
||||||
// comments
|
// comments
|
||||||
#define COMMENT 700
|
#define COMMENT 700
|
||||||
|
//Additional tokens
|
||||||
|
#define ACCESS 801
|
Reference in New Issue
Block a user