printdebug function with line and file names
This commit is contained in:
114
src/grammar.y
114
src/grammar.y
@ -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);
|
||||
}
|
||||
|
10
src/runner.c
10
src/runner.c
@ -10,18 +10,18 @@ extern TableNode *chara;
|
||||
extern TableNode *stri;
|
||||
extern TableNode *boo;
|
||||
extern bool DEBUG;
|
||||
extern char * COLOR_YELLOW;
|
||||
extern char * COLOR_BLUE;
|
||||
extern char * COLOR_WHITE;
|
||||
extern char *COLOR_YELLOW;
|
||||
extern char *COLOR_BLUE;
|
||||
extern char *COLOR_WHITE;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// if last argument is debug then set to true and ignore it for the rest of this file
|
||||
// if last argument is debug then set to true and ignore it for the rest
|
||||
// of this file
|
||||
if (argc > 1 && strcmp(argv[argc - 1], "-debug") == 0) {
|
||||
DEBUG = true;
|
||||
argc--;
|
||||
}
|
||||
|
||||
|
||||
if (argc == 1) {
|
||||
fprintf(stderr, INVALID);
|
||||
return -1;
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include "symbol_table.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -19,8 +20,43 @@ TableNode *boo;
|
||||
TableNode *recprime;
|
||||
TableNode *funtypeprime;
|
||||
TableNode *undefined;
|
||||
// AdInfo *Undefined_function_type_info;
|
||||
|
||||
char *COLOR_RED = "\033[0;31m";
|
||||
char *COLOR_GREEN = "\033[0;32m";
|
||||
char *COLOR_ORANGE = "\033[0;33m";
|
||||
char *COLOR_BLUE = "\033[0;34m";
|
||||
char *COLOR_PURPLE = "\033[0;35m";
|
||||
char *COLOR_CYAN = "\033[0;36m";
|
||||
char *COLOR_LIGHTGRAY = "\033[0;37m";
|
||||
char *COLOR_DARKGRAY = "\033[1;30m";
|
||||
char *COLOR_LIGHTRED = "\033[1;31m";
|
||||
char *COLOR_LIGHTGREEN = "\033[1;32m";
|
||||
char *COLOR_YELLOW = "\033[1;33m";
|
||||
char *COLOR_LIGHTBLUE = "\033[1;34m";
|
||||
char *COLOR_LIGHTPURPLE = "\033[1;35m";
|
||||
char *COLOR_LIGHTCYAN = "\033[1;36m";
|
||||
char *COLOR_WHITE = "\033[1;37m";
|
||||
|
||||
bool DEBUG = true;
|
||||
|
||||
void printdebug_impl(char *file, int line, const char *format, ...);
|
||||
|
||||
void printdebug_impl(char *file, int line, const char *format, ...) {
|
||||
if (DEBUG) {
|
||||
printf("%s<%s> [%d]%s ", COLOR_DARKGRAY, file, line,
|
||||
COLOR_WHITE);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vprintf(format, args);
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
#define printdebug(format, ...) \
|
||||
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
|
||||
// AdInfo *Undefined_function_type_info;
|
||||
typedef enum {
|
||||
// First 4 below are primitive types that are all encapsulated in
|
||||
// primitive type
|
||||
@ -107,16 +143,17 @@ AdInfo *CreatePrimitiveInfo(int size) {
|
||||
// only gets the size of a primitive type
|
||||
int getPrimSize(TableNode *definition) {
|
||||
if (definition == NULL || definition == undefined) {
|
||||
printf(
|
||||
"passed an NULL entry to getPrimSize function. Invalid.\n");
|
||||
printdebug(
|
||||
"passed an NULL entry to getPrimSize function. Invalid.");
|
||||
return -1;
|
||||
}
|
||||
if (definition->additionalinfo == NULL) {
|
||||
printf("node has NULL additionalinfo. Invalid.\n");
|
||||
printdebug("node has NULL additionalinfo. Invalid.");
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(getType(definition), "primitive") != 0) {
|
||||
printf("not checking the size of a primitive -- invalid op\n");
|
||||
printdebug(
|
||||
"not checking the size of a primitive -- invalid op");
|
||||
return 0;
|
||||
}
|
||||
return definition->additionalinfo->PrimAdInfo->size;
|
||||
@ -136,8 +173,8 @@ int getPrimSize(TableNode *definition) {
|
||||
// calculated at runtime so bounds checking only needs to be done then
|
||||
AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) {
|
||||
if (type == NULL || type == undefined) {
|
||||
printf("passed a NULL or undefined type reference to "
|
||||
"CreateArrayInfo. Invalid.\n");
|
||||
printdebug("passed a NULL or undefined type reference to "
|
||||
"CreateArrayInfo. Invalid.");
|
||||
return NULL;
|
||||
}
|
||||
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
|
||||
@ -151,12 +188,12 @@ AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) {
|
||||
// This gets the number of dimensions from array info
|
||||
int getNumArrDim(TableNode *definition) {
|
||||
if (definition == NULL || definition == undefined) {
|
||||
printf("passed an NULL or undefined entry to getNumArrDim "
|
||||
"function. Invalid.\n");
|
||||
printdebug("passed an NULL or undefined entry to getNumArrDim "
|
||||
"function. Invalid.");
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(getType(definition), "array") != 0) {
|
||||
printf("not checking the dim of an array -- invalid op\n");
|
||||
printdebug("not checking the dim of an array -- invalid op");
|
||||
return 0;
|
||||
}
|
||||
return definition->additionalinfo->ArrayAdInfo->numofdimensions;
|
||||
@ -165,12 +202,12 @@ int getNumArrDim(TableNode *definition) {
|
||||
// the entry of that type
|
||||
TableNode *getArrType(TableNode *definition) {
|
||||
if (definition == NULL || definition == undefined) {
|
||||
printf("passed an NULL or undefined entry to getArrType "
|
||||
"function. Invalid.\n");
|
||||
printdebug("passed an NULL or undefined entry to getArrType "
|
||||
"function. Invalid.");
|
||||
return NULL;
|
||||
}
|
||||
if (strcmp(getType(definition), "array") != 0) {
|
||||
printf("not checking the type of an array -- invalid op\n");
|
||||
printdebug("not checking the type of an array -- invalid op");
|
||||
return undefined;
|
||||
}
|
||||
return definition->additionalinfo->ArrayAdInfo->typeofarray;
|
||||
@ -193,12 +230,13 @@ AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) {
|
||||
// anyways.
|
||||
int getRecLength(TableNode *definition) {
|
||||
if (definition == NULL || definition == undefined) {
|
||||
printf("passed an NULL or undefined entry to getRecLength "
|
||||
"function. Invalid.\n");
|
||||
printdebug("passed an NULL or undefined entry to getRecLength "
|
||||
"function. Invalid.");
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(getType(definition), "record") != 0) {
|
||||
printf("not checking the length of an record -- invalid op\n");
|
||||
printdebug(
|
||||
"not checking the length of an record -- invalid op");
|
||||
return 0;
|
||||
}
|
||||
return definition->additionalinfo->RecAdInfo->numofelements;
|
||||
@ -206,13 +244,14 @@ int getRecLength(TableNode *definition) {
|
||||
// This gets the array. Needs to up be updated to get the scope instead
|
||||
SymbolTable *getRecList(TableNode *definition) {
|
||||
if (definition == NULL || definition == undefined) {
|
||||
printf("passed an NULL or undefined entry to getRecList "
|
||||
"function. Invalid.\n");
|
||||
printdebug("passed an NULL or undefined entry to getRecList "
|
||||
"function. Invalid.");
|
||||
return NULL;
|
||||
}
|
||||
if (strcmp(getType(definition), "record") != 0) {
|
||||
printf("not checking the list of types of a record -- invalid "
|
||||
"op\n");
|
||||
printdebug(
|
||||
"not checking the list of types of a record -- invalid "
|
||||
"op");
|
||||
return NULL;
|
||||
}
|
||||
return definition->additionalinfo->RecAdInfo->recordScope;
|
||||
@ -220,7 +259,7 @@ SymbolTable *getRecList(TableNode *definition) {
|
||||
|
||||
TableNode *setRecSize(TableNode *tn, int n) {
|
||||
if (tn == NULL || tn == undefined) {
|
||||
printf("passed in NULL entry for setRecSize. Invalid\n");
|
||||
printdebug("passed in NULL entry for setRecSize. Invalid");
|
||||
return undefined;
|
||||
}
|
||||
tn->additionalinfo->RecAdInfo->numofelements = n;
|
||||
@ -229,7 +268,8 @@ TableNode *setRecSize(TableNode *tn, int n) {
|
||||
|
||||
int getRecSize(SymbolTable *tn) {
|
||||
if (tn == NULL) {
|
||||
printf("passed in NULL SymbolTable for getRecSize. Invalid\n");
|
||||
printdebug(
|
||||
"passed in NULL SymbolTable for getRecSize. Invalid");
|
||||
return -1;
|
||||
}
|
||||
int s = 0;
|
||||
@ -261,13 +301,14 @@ AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular) {
|
||||
// out in table if needed)
|
||||
int getStartLine(TableNode *definition) {
|
||||
if (definition == NULL || definition == undefined) {
|
||||
printf("passed an NULL or undefined entry to getStartLine "
|
||||
"function. Invalid.\n");
|
||||
printdebug("passed an NULL or undefined entry to getStartLine "
|
||||
"function. Invalid.");
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(getType(definition), "primitive function") != 0) {
|
||||
printf("not checking the start line of a function -- invalid "
|
||||
"op\n");
|
||||
printdebug(
|
||||
"not checking the start line of a function -- invalid "
|
||||
"op");
|
||||
return 0;
|
||||
}
|
||||
return definition->additionalinfo->FunDecAdInfo->startlinenumber;
|
||||
@ -275,8 +316,9 @@ int getStartLine(TableNode *definition) {
|
||||
|
||||
TableNode *setStartLine(TableNode *tn, int start) {
|
||||
if (tn == NULL || tn == undefined) {
|
||||
printf("passing in a NULL or undefined entry to setStartLine. "
|
||||
"invalid\n");
|
||||
printdebug(
|
||||
"passing in a NULL or undefined entry to setStartLine. "
|
||||
"invalid");
|
||||
return undefined;
|
||||
}
|
||||
tn->additionalinfo->FunDecAdInfo->startlinenumber = start;
|
||||
@ -286,13 +328,14 @@ TableNode *setStartLine(TableNode *tn, int start) {
|
||||
// not used or used.
|
||||
bool getAsKeyword(TableNode *definition) {
|
||||
if (definition == NULL || definition == undefined) {
|
||||
printf("passed an NULL or undefined entry to getAsKeyword "
|
||||
"function. Invalid.\n");
|
||||
printdebug("passed an NULL or undefined entry to getAsKeyword "
|
||||
"function. Invalid.");
|
||||
return false;
|
||||
}
|
||||
if (strcmp(getType(definition), "primitive function") != 0) {
|
||||
printf("not checking if a function is called with as or not -- "
|
||||
"invalid op\n");
|
||||
printdebug(
|
||||
"not checking if a function is called with as or not -- "
|
||||
"invalid op");
|
||||
return 0;
|
||||
}
|
||||
return definition->additionalinfo->FunDecAdInfo->regularoras;
|
||||
@ -300,8 +343,9 @@ bool getAsKeyword(TableNode *definition) {
|
||||
|
||||
TableNode *setAsKeyword(TableNode *tn, bool as) {
|
||||
if (tn == NULL || tn == undefined) {
|
||||
printf("passing in a NULL or undefined entry to setAsKeyword. "
|
||||
"invalid\n");
|
||||
printdebug(
|
||||
"passing in a NULL or undefined entry to setAsKeyword. "
|
||||
"invalid");
|
||||
return undefined;
|
||||
}
|
||||
tn->additionalinfo->FunDecAdInfo->regularoras = as;
|
||||
@ -311,13 +355,13 @@ TableNode *setAsKeyword(TableNode *tn, bool as) {
|
||||
// stores the type of a function (parameter type and return type)
|
||||
AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) {
|
||||
if (parameter == NULL || parameter == undefined) {
|
||||
printf("passed a NULL or undefined parameter to "
|
||||
"CreateFunctionTypeInfo. Invalid.\n");
|
||||
printdebug("passed a NULL or undefined parameter to "
|
||||
"CreateFunctionTypeInfo. Invalid.");
|
||||
return NULL;
|
||||
}
|
||||
if (returntype == NULL || returntype == undefined) {
|
||||
printf("passed a NULL or undefined return type to "
|
||||
"CreateFunctionTypeInfo. Invalid.\n");
|
||||
printdebug("passed a NULL or undefined return type to "
|
||||
"CreateFunctionTypeInfo. Invalid.");
|
||||
return NULL;
|
||||
}
|
||||
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
|
||||
@ -330,13 +374,13 @@ AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) {
|
||||
// returns parameter type of a function
|
||||
TableNode *getParameter(TableNode *definition) {
|
||||
if (definition == NULL || definition == undefined) {
|
||||
printf("passed an NULL or undefined entry to getParameter "
|
||||
"function. Invalid.\n");
|
||||
printdebug("passed an NULL or undefined entry to getParameter "
|
||||
"function. Invalid.");
|
||||
return undefined;
|
||||
}
|
||||
if (strcmp(getType(definition), "primitive function type") != 0) {
|
||||
printf(
|
||||
"not checking the parameter of a function -- invalid op\n");
|
||||
printdebug(
|
||||
"not checking the parameter of a function -- invalid op");
|
||||
return undefined;
|
||||
}
|
||||
return definition->additionalinfo->FunTypeAdInfo->parameter;
|
||||
@ -344,12 +388,13 @@ TableNode *getParameter(TableNode *definition) {
|
||||
// returns return type of a function
|
||||
TableNode *getReturn(TableNode *definition) {
|
||||
if (definition == NULL || definition == undefined) {
|
||||
printf("passed an NULL or undefined entry to getReturn "
|
||||
"function. Invalid.\n");
|
||||
printdebug("passed an NULL or undefined entry to getReturn "
|
||||
"function. Invalid.");
|
||||
return NULL;
|
||||
}
|
||||
if (strcmp(getType(definition), "primitive function type") != 0) {
|
||||
printf("not checking the return of a function -- invalid op\n");
|
||||
printdebug(
|
||||
"not checking the return of a function -- invalid op");
|
||||
return undefined;
|
||||
}
|
||||
return definition->additionalinfo->FunTypeAdInfo->returntype;
|
||||
@ -389,8 +434,8 @@ SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column) {
|
||||
// types
|
||||
SymbolTable *init(SymbolTable *start) {
|
||||
if (start->Parent_Scope != NULL) {
|
||||
printf(
|
||||
"Cannot initialize a scope that is not the parent scope\n");
|
||||
printdebug(
|
||||
"Cannot initialize a scope that is not the parent scope");
|
||||
return NULL;
|
||||
}
|
||||
integ = (TableNode *)calloc(1, sizeof(TableNode));
|
||||
@ -504,18 +549,19 @@ TableNode* funtypeprime;
|
||||
*/
|
||||
TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) {
|
||||
if (tn == NULL || tn == undefined) {
|
||||
printf("passed in an invalid table node to modify (NULL or "
|
||||
"undefined).\n");
|
||||
printdebug("passed in an invalid table node to modify (NULL or "
|
||||
"undefined).");
|
||||
return undefined;
|
||||
}
|
||||
if (type == NULL || type == undefined) {
|
||||
printf("passed in a NULL or undefined type reference to "
|
||||
"populate a table node. Invalid.\n");
|
||||
printdebug("passed in a NULL or undefined type reference to "
|
||||
"populate a table node. Invalid.");
|
||||
return undefined;
|
||||
}
|
||||
if (info == NULL) {
|
||||
printf("passed in a NULL info reference to populate a table "
|
||||
"node. Invalid.\n");
|
||||
printdebug(
|
||||
"passed in a NULL info reference to populate a table "
|
||||
"node. Invalid.");
|
||||
return undefined;
|
||||
}
|
||||
tn->theType = type;
|
||||
@ -526,12 +572,12 @@ TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) {
|
||||
|
||||
int getAdInfoType(TableNode *tn) {
|
||||
if (tn == NULL || tn == undefined) {
|
||||
printf("passing in NULL or undefined table entry. Invalid\n");
|
||||
printdebug("passing in NULL or undefined table entry. Invalid");
|
||||
return -1;
|
||||
}
|
||||
if (tn->theType == NULL || tn->theType == undefined) {
|
||||
printf("Entry being passed in has a null or undefined "
|
||||
"reference for theType. Invalid.\n");
|
||||
printdebug("Entry being passed in has a null or undefined "
|
||||
"reference for theType. Invalid.");
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(getName(tn), getName(integ)) == 0) {
|
||||
@ -569,20 +615,21 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
|
||||
AdInfo *ad) {
|
||||
|
||||
if (table == NULL) {
|
||||
printf("Null reference to table");
|
||||
printdebug("Null reference to table");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/*
|
||||
TableNode* topDef = (table_lookup(getAncestor(table),typeOf));
|
||||
if(topDef == NULL){
|
||||
printf("This type is not defined at the top level\n");
|
||||
printdebug("This type is not defined at the top level");
|
||||
return NULL;
|
||||
}
|
||||
*/
|
||||
if (typeOf == NULL || typeOf == undefined) {
|
||||
printf("This is not pointing to a proper definition (either "
|
||||
"NULL or undefined)\n");
|
||||
printdebug(
|
||||
"This is not pointing to a proper definition (either "
|
||||
"NULL or undefined)");
|
||||
return undefined;
|
||||
}
|
||||
TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode));
|
||||
@ -602,11 +649,11 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
|
||||
|
||||
char *getType(TableNode *tn) {
|
||||
if (tn == NULL || tn == undefined) {
|
||||
printf("passed a NULL or undefined table entry to getType\n");
|
||||
printdebug("passed a NULL or undefined table entry to getType");
|
||||
return getName(undefined);
|
||||
}
|
||||
if (tn->theType == NULL || tn->theType == undefined) {
|
||||
printf("type of entry is currently NULL or undefined type \n");
|
||||
printdebug("type of entry is currently NULL or undefined type");
|
||||
return getName(undefined);
|
||||
}
|
||||
return tn->theType->theName;
|
||||
@ -614,12 +661,12 @@ char *getType(TableNode *tn) {
|
||||
|
||||
char *getName(TableNode *tn) {
|
||||
if (tn == NULL || tn == undefined) {
|
||||
// printf("passed a NULL or undefined table entry to
|
||||
// getName\n");
|
||||
// printdebug("passed a NULL or undefined table entry to
|
||||
// getName");
|
||||
return undefined->theName;
|
||||
}
|
||||
if (tn->theName == NULL) {
|
||||
// printf("name of entry is currently NULL, undefined \n");
|
||||
// printdebug("name of entry is currently NULL, undefined");
|
||||
return undefined->theName;
|
||||
}
|
||||
return tn->theName;
|
||||
@ -627,34 +674,36 @@ char *getName(TableNode *tn) {
|
||||
|
||||
int getLine(SymbolTable *st) {
|
||||
if (st == NULL) {
|
||||
printf("passed a NULL symbol table to getLine function. "
|
||||
"Invalid.\n");
|
||||
printdebug("passed a NULL symbol table to getLine function. "
|
||||
"Invalid.");
|
||||
return -1;
|
||||
}
|
||||
return st->Line_Number;
|
||||
}
|
||||
int getColumn(SymbolTable *st) {
|
||||
if (st == NULL) {
|
||||
printf("passed a NULL symbol table to getColumn function. "
|
||||
"Invalid.\n");
|
||||
printdebug("passed a NULL symbol table to getColumn function. "
|
||||
"Invalid.");
|
||||
return -1;
|
||||
}
|
||||
return st->Column_Number;
|
||||
}
|
||||
TableNode *addName(TableNode *tn, char *str) {
|
||||
if (tn == NULL || tn == undefined) {
|
||||
printf("passed a Null or undefined table node to the addName "
|
||||
"function. Invalid./n");
|
||||
printdebug(
|
||||
"passed a Null or undefined table node to the addName "
|
||||
"function. Invalid.");
|
||||
return undefined;
|
||||
}
|
||||
if (tn->theName != NULL) {
|
||||
printf("Name doesn't look like it is empty before you change. "
|
||||
"Are you sure you need to update name?/n");
|
||||
printdebug(
|
||||
"Name doesn't look like it is empty before you change. "
|
||||
"Are you sure you need to update name?");
|
||||
return undefined;
|
||||
}
|
||||
if (str == NULL) {
|
||||
printf(
|
||||
"passed a NULL string to the addName function. Invalid./n");
|
||||
printdebug(
|
||||
"passed a NULL string to the addName function. Invalid.");
|
||||
return undefined;
|
||||
}
|
||||
tn->theName = str;
|
||||
@ -663,8 +712,8 @@ TableNode *addName(TableNode *tn, char *str) {
|
||||
|
||||
SymbolTable *setLineNumber(SymbolTable *st, int line) {
|
||||
if (st == NULL) {
|
||||
printf("passed a Null Symbol Table to the setLineNumber "
|
||||
"function. Invalid./n");
|
||||
printdebug("passed a Null Symbol Table to the setLineNumber "
|
||||
"function. Invalid.");
|
||||
return st;
|
||||
}
|
||||
st->Line_Number = line;
|
||||
@ -673,8 +722,8 @@ SymbolTable *setLineNumber(SymbolTable *st, int line) {
|
||||
|
||||
SymbolTable *setColumnNumber(SymbolTable *st, int column) {
|
||||
if (st == NULL) {
|
||||
printf("passed a Null Symbol Table to the setColumnNumber "
|
||||
"function. Invalid./n");
|
||||
printdebug("passed a Null Symbol Table to the setColumnNumber "
|
||||
"function. Invalid.");
|
||||
return st;
|
||||
}
|
||||
st->Line_Number = column;
|
||||
@ -684,7 +733,7 @@ SymbolTable *setColumnNumber(SymbolTable *st, int column) {
|
||||
//we use false for type defs and true for functions for parameter of typeOf
|
||||
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
|
||||
if(table ==NULL || table->Parent_Scope != NULL){
|
||||
printf("No valid table given for header defs\n");
|
||||
printdebug("No valid table given for header defs");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -699,8 +748,8 @@ if (typeOf == 1){
|
||||
newEntry->theType = funy;
|
||||
}
|
||||
if(table_lookup(table,id) != NULL){
|
||||
printf("already defined at the top level, can't define duplicate
|
||||
names\n"); return NULL;
|
||||
printdebug("already defined at the top level, can't define duplicate
|
||||
names"); return NULL;
|
||||
}
|
||||
newEntry->theName = id;
|
||||
if (table->entries == NULL) {
|
||||
@ -718,7 +767,7 @@ names\n"); return NULL;
|
||||
// only check table that is given
|
||||
TableNode *table_lookup(SymbolTable *table, char *x) {
|
||||
if (table == NULL) {
|
||||
printf("passed in empty scope. error.\n");
|
||||
printdebug("passed in empty scope. error.");
|
||||
return undefined;
|
||||
}
|
||||
TableNode *entrie = table->entries;
|
||||
@ -732,16 +781,17 @@ TableNode *table_lookup(SymbolTable *table, char *x) {
|
||||
// check current table and all parents
|
||||
TableNode *look_up(SymbolTable *table, char *x) {
|
||||
if (table == NULL) {
|
||||
printf("passed in empty scope. error.\n");
|
||||
printdebug("passed in empty scope. error.");
|
||||
return undefined;
|
||||
}
|
||||
TableNode *ret = table_lookup(table, x);
|
||||
if (ret != NULL && ret != undefined) {
|
||||
return ret;
|
||||
}
|
||||
printf("could not find %s in scope that started at line %d and column "
|
||||
"%d so moving up a scope\n",
|
||||
x, getLine(table), getColumn(table));
|
||||
printdebug(
|
||||
"could not find %s in scope that started at line %d and column "
|
||||
"%d so moving up a scope",
|
||||
x, getLine(table), getColumn(table));
|
||||
return look_up(table->Parent_Scope, x);
|
||||
}
|
||||
/*
|
||||
@ -958,7 +1008,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
|
||||
// get top most symbol table
|
||||
SymbolTable *getAncestor(SymbolTable *table) {
|
||||
if (table == NULL) {
|
||||
printf("passing a NULL reference to getAncestor. Invalid.\n");
|
||||
printdebug("passing a NULL reference to getAncestor. Invalid.");
|
||||
return NULL;
|
||||
}
|
||||
if (table->Parent_Scope == NULL) {
|
||||
@ -1005,11 +1055,11 @@ bool typeCheck(char *firstID, char *secondID) {
|
||||
TableNode *entry1 = look_up(cur, firstID);
|
||||
TableNode *entry2 = look_up(cur, secondID);
|
||||
if (entry1 == NULL || entry1 == undefined) {
|
||||
printf("first type not defined\n");
|
||||
printdebug("first type not defined");
|
||||
return false;
|
||||
}
|
||||
if (entry2 == NULL || entry2 == undefined) {
|
||||
printf("second type not defined\n");
|
||||
printdebug("second type not defined");
|
||||
return false;
|
||||
}
|
||||
if (table_lookup(getAncestor(cur), getType(look_up(cur, firstID))) ==
|
||||
@ -1052,36 +1102,11 @@ TableNode *getNextEntry(TableNode *tn) { return tn->next; }
|
||||
char* String = "STRING";
|
||||
char* X = "X";
|
||||
SymbolTable* Second = CreateScope(NULL, 2,2);
|
||||
printf("Line number is %d, Column number of scope is
|
||||
%d\n",Second->Line_Number,Second->Column_Number); TableNode* First_Entry =
|
||||
printdebug("Line number is %d, Column number of scope is
|
||||
%d",Second->Line_Number,Second->Column_Number); TableNode* First_Entry =
|
||||
CreateEntry(Second,String,X);
|
||||
|
||||
printf("The type of the first entry is %s\n",First_Entry->theType);
|
||||
printdebug("The type of the first entry is %s",First_Entry->theType);
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
char *getLineStr();
|
||||
char *COLOR_RED = "\033[0;31m";
|
||||
char *COLOR_GREEN = "\033[0;32m";
|
||||
char *COLOR_ORANGE = "\033[0;33m";
|
||||
char *COLOR_BLUE = "\033[0;34m";
|
||||
char *COLOR_PURPLE = "\033[0;35m";
|
||||
char *COLOR_CYAN = "\033[0;36m";
|
||||
char *COLOR_LIGHTGRAY = "\033[0;37m";
|
||||
char *COLOR_DARKGRAY = "\033[1;30m";
|
||||
char *COLOR_LIGHTRED = "\033[1;31m";
|
||||
char *COLOR_LIGHTGREEN = "\033[1;32m";
|
||||
char *COLOR_YELLOW = "\033[1;33m";
|
||||
char *COLOR_LIGHTBLUE = "\033[1;34m";
|
||||
char *COLOR_LIGHTPURPLE = "\033[1;35m";
|
||||
char *COLOR_LIGHTCYAN = "\033[1;36m";
|
||||
char *COLOR_WHITE = "\033[1;37m";
|
||||
|
||||
bool DEBUG = false;
|
||||
|
||||
char *getLineStr(int d) {
|
||||
char *new_text = malloc(100);
|
||||
sprintf(new_text, "%s[%d]%s ", COLOR_DARKGRAY, d, COLOR_WHITE);
|
||||
return new_text;
|
||||
}
|
||||
*/
|
Reference in New Issue
Block a user