printdebug function with line and file names

This commit is contained in:
Scarlett
2025-03-31 11:43:30 -04:00
parent d587ed9f3b
commit be311c7418
3 changed files with 206 additions and 181 deletions

View File

@ -27,7 +27,7 @@
extern TableNode* chara;
extern TableNode* stri;
extern TableNode* boo;
TableNode * tn;
TableNode * tn;
%}
//%define api.location.type {location_t}
%locations
@ -122,29 +122,29 @@ prototype:
definition:
TYPE ID COLON {
printf("Currently see a record definition for %s\n", $<words>2);
printdebug("Currently see a record definition for %s", $<words>2);
tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0)));
if (table_lookup(getAncestor(cur), $2) == NULL) {
printf("rec not found \n");
printdebug("rec not found ");
}
}dblock { setRecSize(table_lookup(getParent(cur), $2), getRecSize(cur));
cur = getParent(cur);}
| TYPE ID COLON C_INTEGER ARROW id_or_types
{printf("Currently see a array definition of name %s,storing type %s, of dimensions %d\n",
{printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d",
$2, $6, $4);
CreateEntry(cur, arrayprim, $2, CreateArrayInfo($4, look_up(cur, $6)));}
| function_declaration
| TYPE ID COLON id_or_types ARROW id_or_types {
printf("Currently see a function type definition of name %s,parameter type %s, of return type %s\n",
printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s",
$2, $4, $6);
CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo(table_lookup(cur,$4),table_lookup(cur,$6)));
}
| ID {
TableNode *node = table_lookup(getAncestor(cur), $<words>1);
if (node == NULL) {
printf("function not declared at line %d, column %d\n", @1.first_line, @1.first_column);
printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column);
}else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){
printf("function not declared at line %d, column %d\n", @1.first_line, @1.first_column);
printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column);
}
else {
setStartLine(node, @1.first_line);
@ -152,19 +152,19 @@ TYPE ID COLON {
}
cur = CreateScope(cur, 0, 0);
} L_PAREN ID {
printf("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s\n",
printdebug("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s",
$1,$4);
CreateEntry(cur, getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $<words>1)))), $<words>4, NULL);
}R_PAREN ASSIGN sblock
| ID {
TableNode *node = table_lookup(getAncestor(cur), $<words>1);
if (node == NULL) {
printf("null check\n");
printdebug("null check");
}
if (node == NULL) {
printf("function not declared at line %d, column %d\n", @1.first_line, @1.first_column);
printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column);
}else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){
printf("function not declared at line %d, column %d\n", @1.first_line, @1.first_column);
printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column);
}
else {
setStartLine(node, @1.first_line);
@ -174,15 +174,15 @@ TYPE ID COLON {
}AS L_PAREN {
TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $<words>1))));
if (parameter == NULL) {
printf("function defined with as, but parameter is not a record at line %d, column %d\n", @1.first_line, @1.first_column);
printdebug("function defined with as, but parameter is not a record at line %d, column %d", @1.first_line, @1.first_column);
}else if(getAdInfoType(parameter) != TYPE_RECORD){
printf("function defined with as, but parameter is not a record at line %d, column %d\n", @1.first_line, @1.first_column);
printdebug("function defined with as, but parameter is not a record at line %d, column %d", @1.first_line, @1.first_column);
}else {
for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){
CreateEntry(cur, entry, NULL, NULL);
}
}
} idlist {printf("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d\n",
} idlist {printdebug("Currently see a function definition taking one paramter (with as) of name %s and number of arguments %d",
$1,$6);} R_PAREN ASSIGN sblock
;
@ -200,7 +200,7 @@ idlist:
entry = getNextEntry(entry);
}
if (getNextEntry(entry) == NULL) {
printf("too many parameters at line %d column %d\n", @1.first_line, @1.first_column);
printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column);
}
addName(entry, $<words>1);
} COMMA idlist {$$ = $<integ>3 + 1;}
@ -211,7 +211,7 @@ idlist:
entry = getNextEntry(entry);
}
if (getNextEntry(entry) != NULL) {
printf("too many parameters at line %d column %d\n", @1.first_line, @1.first_column);
printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column);
}
addName(entry, $<words>1);
$$ = 1;
@ -222,7 +222,7 @@ idlist:
sblock:
L_BRACE {if (getLine(cur) != 0 && getColumn(cur))cur = CreateScope(cur,@1.first_line,@1.first_column);} statement_list {cur = getParent(cur);} R_BRACE
| L_BRACE {if (getLine(cur) != 0 && getColumn(cur))cur = CreateScope(cur,@1.first_line,@1.first_column);} dblock
{printf("seen sblock with dblock\n");} statement_list {cur = getParent(cur);} R_BRACE
{printdebug("seen sblock with dblock");} statement_list {cur = getParent(cur);} R_BRACE
;
dblock:
@ -234,14 +234,14 @@ declaration_list:
;
declaration:
id_or_types COLON ID {printf("ID/TYPE: %s, ID: %s\n", $<words>1, $<words>3) ; CreateEntry(cur,table_lookup(getAncestor(cur),$<words>1),$<words>3,NULL); }
id_or_types COLON ID {printdebug("ID/TYPE: %s, ID: %s", $<words>1, $<words>3) ; CreateEntry(cur,table_lookup(getAncestor(cur),$<words>1),$<words>3,NULL); }
;
id_or_types:
ID {printf("string of id is %s in ID pattern of id_or_type rule.\n"); $$ = $1;}
//{printf("string of id is %s in ID pattern of id_or_type rule. Type passed up the tree is %s.\n",$1,getType(look_up(cur,$1))); $$ = getType(look_up(cur,$1));}
| types {printf("string of type is %s in types pattern of id_or_type rule.\n",$1);} {$$ = $1;}
//{printf("string of type is %s in types pattern of id_or_type rule. That is passed up the tree.\n",$<words>1);} {$$ = $<words>1;}
ID {printdebug("string of id is %s in ID pattern of id_or_type rule."); $$ = $1;}
//{printdebug("string of id is %s in ID pattern of id_or_type rule. Type passed up the tree is %s.",$1,getType(look_up(cur,$1))); $$ = getType(look_up(cur,$1));}
| types {printdebug("string of type is %s in types pattern of id_or_type rule.",$1);} {$$ = $1;}
//{printdebug("string of type is %s in types pattern of id_or_type rule. That is passed up the tree.",$<words>1);} {$$ = $<words>1;}
;
;
@ -255,13 +255,13 @@ statement_list:
compound_statement:
WHILE L_PAREN expression R_PAREN sblock
| IF L_PAREN expression R_PAREN THEN sblock ELSE sblock
| sblock //{printf("seen a compound statement rule\n");}
| sblock //{printdebug("seen a compound statement rule");}
;
simple_statement:
assignable ASSIGN expression {if(strcmp($1, $3) == 0){
} else {
printf("Mismatch at line %d and column%d\n", @2.first_line, @2.first_column);
printdebug("Mismatch at line %d and column%d", @2.first_line, @2.first_column);
}}
| RETURN expression
@ -278,77 +278,77 @@ rec_op :
DOT
expression:
constant {printf("constant expression\n");} {$$ = $<words>1;}
constant {printdebug("constant expression");} {$$ = $<words>1;}
| SUB_OR_NEG expression %prec UMINUS {printf("negative expression\n");if(strcmp($2,"integer") != 0)
{printf("cant negate something not an integer at line %d and column %d\n",@2.first_line,@2.first_column);
| SUB_OR_NEG expression %prec UMINUS {printdebug("negative expression");if(strcmp($2,"integer") != 0)
{printdebug("cant negate something not an integer at line %d and column %d",@2.first_line,@2.first_column);
$$=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. Invalid type being negated is %s\n",
| NOT expression {printdebug("not expression"); if(strcmp($2,"Boolean")==0){$$=$2;}else{$$=strdup("undefined");
printdebug("mismatch at line %d and column %d. Invalid type being negated is %s",
@1.first_line,@1.first_column,$2);}}
| expression ADD expression
{printf("add expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
{printdebug("add expression");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s.",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression SUB_OR_NEG expression
{printf("sub or neg expression\n");if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
{printdebug("sub or neg expression");if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("integer");}
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s.",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression MUL expression
{printf("multiply expression\n");
{printdebug("multiply expression");
if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s.",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression DIV expression
{printf("divide expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
{printdebug("divide expression");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s.",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression REM expression
{printf("remainder expression\n");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
{printdebug("remainder expression");if(strcmp($1,$3)==0 && strcmp($1,"integer")==0){$$=strdup("integer");}
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s.",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression AND expression
{printf("AND expression\n");if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
{printdebug("AND expression");if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0){$$=strdup("Boolean");}
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s.",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression OR expression
{printf("OR\n");if(strcmp($1,$3)==0 &&
{printdebug("OR");if(strcmp($1,$3)==0 &&
strcmp($1,"Boolean")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s.",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression LESS_THAN expression
{printf("less than expression\n");if(strcmp($1,$3)==0 &&
{printdebug("less than expression");if(strcmp($1,$3)==0 &&
strcmp($1,"integer")==0){$$=strdup("Boolean");}
else{printf("mismatch at line %d and column %d. Invalid types %s and %s.\n",
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s.",
@2.first_line,@2.first_column,$1,$3);
$$=strdup("undefined");}}
| expression EQUAL_TO expression {printf("equals check expression\n");
| expression EQUAL_TO expression {printdebug("equals check expression");
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. Invalid types %s and %s\n",
else{printdebug("mismatch at line %d and column %d. Invalid types %s and %s",
@2.first_line,@2.first_column,$1,$3);$$=strdup("undefined");}}
| assignable {printf("assignable expression. current type is %s\n",$1);$$=$1;}
| assignable {printdebug("assignable expression. current type is %s",$1);$$=$1;}
| L_PAREN expression R_PAREN {printf("paren expression. current type is %s\n",$2);$$=$2;}
| L_PAREN expression R_PAREN {printdebug("paren expression. current type is %s",$2);$$=$2;}
| memOp assignable {$$ = strdup("address");}
;
@ -365,8 +365,8 @@ expression COMMA argument_list {$<integ>$ = $<integ>3 + 1;}
memOp:
RESERVE {printf("reserve expression\n");}
| RELEASE {printf("release expression\n");}
RESERVE {printdebug("reserve expression");}
| RELEASE {printdebug("release expression");}
;
@ -381,15 +381,15 @@ constant:
types:
// Commented out T_String below
// T_STRING {printf("string of T_STRING in types is %s\n",$<words>1);} {$$ = $<words>1;}
T_INTEGER {printf("string of T_INTEGER in types is %s\n",$<words>1);} {$$ = $1;}
| T_ADDRESS {printf("string of T_ADDRESS in types is %s\n",$<words>1);} {$$ = $1;}
| T_CHARACTER {printf("string of T_CHARACTER in types is %s\n",$<words>1);} {$$ = $1;}
| T_BOOLEAN {printf("string of T_BOOLEAN in types is %s\n",$<words>1);} {$$ = $1;}
// T_STRING {printdebug("string of T_STRING in types is %s",$<words>1);} {$$ = $<words>1;}
T_INTEGER {printdebug("string of T_INTEGER in types is %s",$<words>1);} {$$ = $1;}
| T_ADDRESS {printdebug("string of T_ADDRESS in types is %s",$<words>1);} {$$ = $1;}
| T_CHARACTER {printdebug("string of T_CHARACTER in types is %s",$<words>1);} {$$ = $1;}
| T_BOOLEAN {printdebug("string of T_BOOLEAN in types is %s",$<words>1);} {$$ = $1;}
;
%%
void yyerror(const char *err) {
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,yylloc.first_line,yylloc.first_column);
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d", err,yytext,yylloc.first_line,yylloc.first_column);
}