From 38a1d0b0911ffa99809191616e99a94bac783a23 Mon Sep 17 00:00:00 2001 From: Partho Bhattacharya Date: Fri, 28 Mar 2025 02:02:44 -0400 Subject: [PATCH] added more helper functions --- src/grammar.y | 45 ++++++++++++++++++++-------------------- src/lexicalStructure.lex | 2 +- src/symbol_table.c | 20 +++++++++++++++++- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 05fee0c..faaabae 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -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,10 +158,7 @@ declaration_list: ; declaration: - id_or_types COLON ID { - TableNode* type = table_lookup(getAncestor(cur), $1); - AdInfo* info = NULL; - id_or_types COLON ID {CreateEntry(cur,table_lookup(getAncestor(cur),$1),$3,NULL); } + id_or_types COLON ID {CreateEntry(cur,table_lookup(getAncestor(cur),$1),$3,NULL);} ; id_or_types: @@ -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"); + | 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 SUB_OR_NEG expression {printf("subtract 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"); + | 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"); + | 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("remainder expression\n");} - {printf("less than expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean"); + | 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 AND expression {printf("and expression\n");} - {printf("less than expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("Boolean"); + | 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("or expression\n");} + | 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;} diff --git a/src/lexicalStructure.lex b/src/lexicalStructure.lex index 2fab3b4..b43c979 100644 --- a/src/lexicalStructure.lex +++ b/src/lexicalStructure.lex @@ -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;*/}} diff --git a/src/symbol_table.c b/src/symbol_table.c index 8b0b99c..59a27c9 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -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));