added more helper functions

This commit is contained in:
Partho Bhattacharya
2025-03-28 02:02:44 -04:00
parent d7f6272d37
commit 38a1d0b091
3 changed files with 43 additions and 24 deletions

View File

@ -145,7 +145,8 @@ idlist:
sblock:
L_BRACE {cur = CreateScope(cur,@1.first_line,@1.first_column);} statement_list {cur = getParent(cur);} R_BRACE
| L_BRACE {cur = CreateScope(cur,@1.first_line,@1.first_column);} dblock {printf("seen sblock with dblock\n");} statement_list {cur = getParent(cur);} R_BRACE
| L_BRACE {cur = CreateScope(cur,@1.first_line,@1.first_column);}
dblock {printf("seen sblock with dblock\n");} statement_list {cur = getParent(cur);} R_BRACE
;
dblock:
@ -157,9 +158,6 @@ declaration_list:
;
declaration:
id_or_types COLON ID {
TableNode* type = table_lookup(getAncestor(cur), $<words>1);
AdInfo* info = NULL;
id_or_types COLON ID {CreateEntry(cur,table_lookup(getAncestor(cur),$<words>1),$<words>3,NULL);}
;
@ -202,46 +200,49 @@ expression:
$$=strdup("undefined");}else{$$=$2;}}
| NOT expression {printf("not expression\n"); if(strcmp($2,"Boolean")==0){$$=$2;}else{$$=strdup("undefined");
printf("mismatch at line %d and column %d\n",@1.first_line,@1.first_column);}}
| expression ADD expression {printf("add expression\n");}
{printf("less than expression\n");
if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean");
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression SUB_OR_NEG expression {printf("subtract expression\n");}
{printf("less than expression\n");if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("Boolean");
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression MUL expression {printf("multiply expression\n");}
{printf("less than expression\n");if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("Boolean");
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression DIV expression {printf("division expression\n");}
{printf("less than expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean");
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression REM expression {printf("remainder expression\n");}
{printf("less than expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean");
| expression ADD expression
{printf("add expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression AND expression {printf("and expression\n");}
{printf("less than expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean");
| expression SUB_OR_NEG expression
{printf("less than expression\n");if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression MUL expression
{printf("multiply expression\n");
printf("less than expression\n");if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression DIV expression
{printf("less than expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression REM expression
{printf("less than expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression OR expression {printf("or expression\n");}
| expression AND expression
{printf("less than expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression OR expression
{printf("less than expression\n");if(strcmp($1,$3)==0 &&
strcmp($1,"integer")==0){$$=strdup("Boolean");
strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression LESS_THAN expression
{printf("less than expression\n");if(strcmp($1,$3)==0 &&
strcmp($1,"integer")==0){$$=strdup("Boolean");
strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
$$=strdup("Boolean");$$=strdup("undefined");}}
| expression EQUAL_TO expression {printf("equals check expression\n");
if(strcmp($1,$3)==0){$$=strdup("Boolean");}else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
if(strcmp($1,$3)==0){$$=strdup("Boolean");}
else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);$$=strdup("undefined");}}
| assignable {printf("assignable expression\n");$$=$1;}

View File

@ -72,7 +72,7 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
"," {if(DEBUG) {printf( "COMMA: %s (%d)\n", yytext, COMMA);} else {if(tok_flag != NULL){print_tok(COMMA);}incr(line_number,column_number,COMMA);return COMMA;}}
"->" {if(DEBUG) {printf( "ARROW: %s (%d)\n", yytext, ARROW);} else {if(tok_flag != NULL){print_tok(ARROW);}incr(line_number,column_number,ARROW);return ARROW;}}
{DIGIT}+ {if(DEBUG) {printf( "C_INTEGER: %s (%d)\n", yytext, C_INTEGER);} else {if(tok_flag != NULL){print_tok(C_INTEGER);}incr(line_number,column_number,C_INTEGER);yylval.integ = atoi(yytex)/*words = strdup("integer")*/;return C_INTEGER;}}
{DIGIT}+ {if(DEBUG) {printf( "C_INTEGER: %s (%d)\n", yytext, C_INTEGER);} else {if(tok_flag != NULL){print_tok(C_INTEGER);}incr(line_number,column_number,C_INTEGER);yylval.integ = atoi(yytext)/*words = strdup("integer")*/;return C_INTEGER;}}
'{CHAR}' {if(DEBUG) {printf( "C_CHARACTER: %s (%d)\n", yytext, C_CHARACTER);} else {if(tok_flag != NULL){print_tok(C_CHARACTER);}incr(line_number,column_number,C_CHARACTER);yylval.words = strdup("character");return C_CHARACTER;}}
\"{SCHAR}*\" {if(DEBUG) {printf( "C_STRING: %s (%d)\n", yytext, C_STRING);} else {if(tok_flag != NULL){print_tok(C_STRING);}incr(line_number,column_number,C_STRING);yylval.words = strdup("string");return C_STRING;}}
{COMMENT} {if(DEBUG) {printf( "COMMENT: %s (%d)\n", yytext, COMMENT);} else {if(tok_flag != NULL){print_tok(COMMENT);}incr(line_number,column_number,COMMENT);/*return COMMENT;*/}}

View File

@ -145,7 +145,6 @@ int getNumArrDim(TableNode *definition) {
printf("passed an NULL entry to getNumArrDim function. Invalid.\n");
return -1;
}
if(
if (strcmp(getType(definition), "array") != 0) {
printf("not checking the dim of an array -- invalid op\n");
return 0;
@ -236,6 +235,15 @@ int getStartLine(TableNode *definition) {
}
return definition->additionalinfo->FunDecAdInfo->startlinenumber;
}
TableNode* setStartLine(TableNode* tn, int start){
if(tn == NULL){
printf("passing in a NULL entry to setStartLine. invalid\n");
return NULL;
}
tn->additionalinfo->FunDecAdInfo->startlinenumber = start;
return tn;
}
// checks if "as" keyword was used for function definition. Either 0 or 1 for
// not used or used.
bool getAsKeyword(TableNode *definition) {
@ -246,6 +254,16 @@ bool getAsKeyword(TableNode *definition) {
}
return definition->additionalinfo->FunDecAdInfo->regularoras;
}
TableNode* setAsKeyword(TableNode* tn, bool as){
if(tn == NULL){
printf("passing in a NULL entry to setAsKeyword. invalid\n");
return NULL;
}
tn->additionalinfo->FunDecAdInfo->regularoras = as;
return tn;
}
// stores the type of a function (parameter type and return type)
AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) {
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));