Merge branch 'Sprint3-Symbol_Table_Restructure-FE-t#NoTask' into Dev

This commit is contained in:
Moroseui
2025-04-04 21:34:52 -04:00
committed by GitHub
9 changed files with 544 additions and 364 deletions

View File

@ -24,31 +24,34 @@
TableNode * tn; TableNode * tn;
%} %}
%locations
%union { %union {
int integ; int integ;
char * words; char* words;
void* tn;
} }
%locations
%type <integ> idlist %type <integ> idlist
%type <words> assignable %type <tn> assignable
%type <words> expression %type <tn> expression
%type <words> constant %type <tn> constant
%type <words> id_or_types %type <tn> id_or_types
%type <words> types %type <tn> types
%type <integ> argument_list
%type <integ> ablock
%token <words> ID 101 %token <words> ID 101
%token <words> T_INTEGER 201 %token <tn> T_INTEGER 201
%token <words> T_ADDRESS 202 %token <tn> T_ADDRESS 202
%token <words> T_BOOLEAN 203 %token <tn> T_BOOLEAN 203
%token <words> T_CHARACTER 204 %token <tn> T_CHARACTER 204
%token <words> T_STRING 205 %token <tn> T_STRING 205
%token <integ> C_INTEGER 301 %token <integ> C_INTEGER 301
%token <words> C_NULL 302 %token <tn> C_NULL 302
%token <words> C_CHARACTER 303 %token <tn> C_CHARACTER 303
%token <words> C_STRING 304 %token <tn> C_STRING 304
%token <words> C_TRUE 305 %token <tn> C_TRUE 305
%token <words> C_FALSE 306 %token <tn> C_FALSE 306
%token WHILE 401 %token WHILE 401
%token IF 402 %token IF 402
%token THEN 403 %token THEN 403
@ -122,36 +125,39 @@ prototype:
definition: definition:
TYPE ID COLON TYPE ID COLON
{ {
printdebug("Currently see a record definition for %s", $<words>2);
tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); printdebug("Currently see a record definition for %s", $2);
if (table_lookup(getAncestor(cur), $2) == undefined) { tn = CreateEntry(getAncestor(cur),TYPE_RECORD_TYPE, recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0)));
if (look_up(cur, $2) == undefined) {
printdebug("rec not found"); printdebug("rec not found");
} }
} }
dblock dblock
{ {
setRecSize(table_lookup(getParent(cur), $2), getRecSize(cur)); //We are scanning through the dblock scope to get the length of the dblock (num of elements) from getRecSize
//and then putting it in the entry that we created above.
setRecSize(look_up(getParent(cur), $2), getRecSize(cur));
cur = getParent(cur); cur = getParent(cur);
} }
| TYPE ID COLON C_INTEGER ARROW id_or_types | TYPE ID COLON C_INTEGER ARROW id_or_types
{ {
printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d", $2, $6, $4); printdebug("Currently see a array definition of name %s,storing type %s, of dimensions %d", $2, getName($6), $4);
CreateEntry(cur, arrayprim, $2, CreateArrayInfo($4, look_up(cur, $6))); CreateEntry(cur,TYPE_ARRAY_TYPE, arrayprim, $2, CreateArrayInfo($4, $6));
printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, $6); printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, getName($6));
} }
| function_declaration | function_declaration
| TYPE ID COLON id_or_types ARROW id_or_types | TYPE ID COLON id_or_types ARROW id_or_types
{ {
printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", $2, $4, $6); printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", $2, getName($4), getName($6));
CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo(table_lookup(cur,$4),table_lookup(cur,$6))); CreateEntry(cur,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo($4 ,$6));
} }
| ID | ID
{ {
TableNode *node = table_lookup(getAncestor(cur), $<words>1); TableNode *node = table_lookup(getAncestor(cur), $1);
if (node == undefined) { if (node == undefined) {
printdebug("function not declared at line %d, column %d", @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) { } else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) {
@ -165,59 +171,84 @@ definition:
L_PAREN ID L_PAREN ID
{ {
printdebug("Currently see a function definition taking only one parameter (no as) of name %s and argument name %s", $1,$4); 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); TableNode* type_of_param = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1))));
} int type_of_param_type = getAdInfoType(type_of_param);
R_PAREN ASSIGN sblock if( type_of_param_type == TYPE_UNDEFINED
|| type_of_param_type == TYPE_FUNCTION_DECLARATION
|| type_of_param_type == TYPE_ARRAY
|| type_of_param_type == TYPE_ALL_ELSE
|| type_of_param_type == TYPE_SYSTEM_DEFINED
|| type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused
printdebug("type of parameter is undefined or invalid at line %d, column %d", @4.first_line, @4.first_column);
type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases
}else{
printdebug("type of parameter is %s at line %d, column %d", getName(type_of_param), @4.first_line, @4.first_column);
}
| ID CreateEntry(cur,type_of_param_type, type_of_param, $4, getAdInfo(type_of_param));
{ }
TableNode *node = table_lookup(getAncestor(cur), $<words>1); R_PAREN ASSIGN sblock //leaving scope is being taken care of in sblock
| ID {
TableNode *node = table_lookup(getAncestor(cur), $1);
if (node == undefined) { if (node == undefined) {
printdebug("null check");
printdebug(" undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column);
}else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){
printdebug("not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column);
} }
if (node == undefined) { else {
printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column); printdebug("setting as keyword to true");
} else if (getAdInfoType(node) != TYPE_FUNCTION_DECLARATION) {
printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column);
} else {
setStartLine(node, @1.first_line); setStartLine(node, @1.first_line);
setAsKeyword(node, true); setAsKeyword(node, true);
} }
cur = CreateScope(cur, 0, 0); cur = CreateScope(cur, 0, 0);
} }AS L_PAREN {
AS L_PAREN TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $1))));
{ printdebug("parameter type: %s", getType(parameter));
TableNode *parameter = getParameter(table_lookup(getAncestor(cur), getType(table_lookup(getAncestor(cur), $<words>1))));
printdebug("%s", getType(parameter));
if (parameter == undefined) { if (parameter == undefined) {
printdebug("function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column); printdebug("function defined with as, but parameter is undefined at line %d, column %d", @1.first_line, @1.first_column);
} else if(getAdInfoType(parameter) != TYPE_RECORD) { }else if(getAdInfoType(parameter) != TYPE_RECORD){
printdebug("record: %s., primitive: %s.", getType(parameter), getName(recprime)); printdebug("record: %s., primitive: %s.", getType(parameter), getName(recprime));
printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column); printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column);
} else {
for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)) {
CreateEntry(cur, entry->theType, NULL, NULL);
}
}
}
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
;
}else {
for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){
int type_of_param_type = getAdInfoType(entry);
if( type_of_param_type == TYPE_UNDEFINED
|| type_of_param_type == TYPE_FUNCTION_DECLARATION
|| type_of_param_type == TYPE_ARRAY
|| type_of_param_type == TYPE_ALL_ELSE
|| type_of_param_type == TYPE_SYSTEM_DEFINED
|| type_of_param_type == TYPE_STRING){ // note that strings are actually arrays so this is unused
printdebug("type of parameter being passed in to AS function definition is %s which is invalid", getName(entry));
type_of_param_type = TYPE_UNDEFINED; // setting tag as undefined in these cases
}else{
printdebug("type of parameter correctly being passed in to AS function definition is %s which is valid", getName(entry));
}
if(type_of_param_type == TYPE_UNDEFINED){
CreateEntry(cur,type_of_param_type, undefined, NULL, NULL);
} else {
CreateEntry(cur,type_of_param_type, entry, NULL, getAdInfo(entry));
/*printdebug("creating entry of type %s for function", getType(entry));
CreateEntry(cur, getTypeEntry(entry), "undefined", NULL);*/
}
}
}
} 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 //check sblock type
;
function_declaration: function_declaration:
FUNCTION ID COLON ID FUNCTION ID COLON ID
{ {
CreateEntry(cur, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false)); CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $4), $2, CreateFunctionDeclarationInfo(-1, false));
} }
| EXTERNAL FUNCTION ID COLON ID | EXTERNAL FUNCTION ID COLON ID
{ {
CreateEntry(cur, look_up(cur, $5), $3, NULL); CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, NULL);
} }
; ;
@ -233,11 +264,11 @@ idlist:
if (getNextEntry(entry) == NULL) { if (getNextEntry(entry) == NULL) {
printdebug("too many parameters at line %d column %d", @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); addName(entry, $1);
} }
COMMA idlist COMMA idlist
{ {
$$ = $<integ>4 + 1; $$ = $4 + 1;
} }
| ID | ID
@ -273,8 +304,11 @@ sblock:
| L_BRACE | L_BRACE
{ {
if (getLine(cur) != 0 && getColumn(cur)) { if (getLine(cur) != 0) {
cur = CreateScope(cur,@1.first_line,@1.first_column); cur = CreateScope(cur,@1.first_line,@1.first_column);
} else {
setLineNumber(cur, @1.first_line);
setColumnNumber(cur,@1.first_line);
} }
} }
dblock dblock
@ -291,15 +325,16 @@ sblock:
dblock: dblock:
L_BRACKET L_BRACKET
{ {if(getLine(cur)==0){
if(getLine(cur)==0) { setLineNumber(cur, @1.first_line);
setLineNumber(cur, @1.first_line); setColumnNumber(cur,@1.first_line);}
setColumnNumber(cur,@1.first_line); else{
} else {
cur = CreateScope(cur,@1.first_line,@1.first_column); cur = CreateScope(cur,@1.first_line,@1.first_column);
} }
} }
declaration_list R_BRACKET; declaration_list R_BRACKET;
@ -314,8 +349,8 @@ declaration_list:
declaration: declaration:
id_or_types COLON ID id_or_types COLON ID
{ {
printdebug("ID/TYPE: %s, ID: %s", $<words>1, $<words>3) ; printdebug("ID/TYPE: %s, ID: %s", getName($1), $3) ;
CreateEntry(cur,table_lookup(getAncestor(cur),$<words>1),$<words>3,NULL); CreateEntry(cur,getAdInfoType($1),$1,$3,getAdInfo($1));
} }
; ;
@ -324,12 +359,13 @@ declaration:
id_or_types: id_or_types:
ID ID
{ {
printdebug("string of id is %s in ID pattern of id_or_type rule.", $1); $$ = $1; printdebug("string of id is %s in ID pattern of id_or_type rule.", $1);
$$ = table_lookup(getAncestor(cur), $1);
} }
| types | types
{ {
printdebug("string of type is %s in types pattern of id_or_type rule.",$1); printdebug("string of type is %s in types pattern of id_or_type rule.",getName($1));
$$ = $1; $$ = $1;
} }
; ;
@ -356,22 +392,22 @@ compound_statement:
simple_statement: simple_statement:
assignable ASSIGN expression assignable ASSIGN expression
{ {
if(strcmp($1, $3) == 0) { if(strcmp(getName($1), getName($3)) == 0) {
printdebug("Passed standard type check; assignable = expression"); printdebug("Passed standard type check; assignable = expression");
} else if((strcmp(getType(look_up(cur, $1)), "array") == 0) && (strcmp($3, "address") == 0)) { } else if((strcmp(getType($1), "array") == 0) && (strcmp(getName($3), "address") == 0)) {
printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, $1, $3); printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName($1), getName($3));
} else if((strcmp(getType(look_up(cur, $1)), "record") == 0) && (strcmp($3, "address") == 0)) { } else if((strcmp(getType($1), "record") == 0) && (strcmp(getName($3), "address") == 0)) {
printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, $1, $3); printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName($1), getName($3));
} else if((strcmp(getType(look_up(cur, $1)), "function type primitive") == 0) && (strcmp($3, "address") == 0)) { } else if((strcmp(getType($1), "function type primitive") == 0) && (strcmp(getName($3), "address") == 0)) {
printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, $1, $3); printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName($1), getName($3));
// } else if () { // } else if () {
// } else if(strcmp(getType(table_lookup(cur, $1)), getType(table_lookup(cur, $3))) == 0) { // } else if(strcmp(getType(table_lookup(cur, $1)), getType(table_lookup(cur, $3))) == 0) {
// printdebug("%s[] Passed double lookup type check; %s = %s", COLOR_GREEN, $1, $3); // printdebug("%s[] Passed double lookup type check; %s = %s", COLOR_GREEN, $1, $3);
} else { } else {
printdebug("%s[TYPE ERROR] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE); printdebug("%s[TYPE ERROR] %sMismatch at %sline %d and column %d%s", COLOR_ORANGE, COLOR_WHITE, COLOR_YELLOW, @2.first_line, @2.first_column, COLOR_WHITE);
printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, $1, $3, COLOR_WHITE); printdebug(" - Invalid types %s$1: %s and $3: %s%s", COLOR_YELLOW, getType($1), getType($3), COLOR_WHITE);
printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType(look_up(cur, $1))); printdebug(" - %sgetType for address: %s", COLOR_YELLOW, getType($1));
} }
} }
@ -388,8 +424,8 @@ rec_op:
ablock: ablock:
L_PAREN argument_list R_PAREN L_PAREN argument_list R_PAREN
{ {
$<integ>$ = $<integ>2; $$ = $2;
printdebug("ablock is %d", $<integ>$); printdebug("ablock is %d", $$);
} }
; ;
@ -398,34 +434,33 @@ ablock:
argument_list: argument_list:
expression COMMA argument_list expression COMMA argument_list
{ {
CreateEntry(cur, look_up(cur, $1), "", NULL); CreateEntry(cur,getAdInfoType($1), $1, getName($1), NULL);
$<integ>$ = $<integ>3 + 1; $$ = $3 + 1;
printdebug("[ARGUMENT_LIST] argument list is %d", $<integ>$); printdebug("[ARGUMENT_LIST] argument list is %d", $$);
} }
| expression | expression
{ {
CreateEntry(cur, look_up(cur, $1), "", NULL); CreateEntry(cur,getAdInfoType($1),$1, getName($1), NULL);
$<integ>$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $<integ>$); $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$);
} }
; ;
// will ALWAYS be a TYPE // will ALWAYS be a TYPE
expression: expression:
constant constant
{ {
printdebug("constant expression"); printdebug("constant expression");
$$ = $<words>1; $$ = $1;
} }
| SUB_OR_NEG expression %prec UMINUS | SUB_OR_NEG expression %prec UMINUS
{ {
printdebug("negative expression"); printdebug("negative expression");
if(strcmp($2,"integer") != 0) { if($2 != integ) {
printdebug("cant negate something not an integer at line %d and column %d",@2.first_line,@2.first_column); printdebug("cant negate something not an integer at line %d and column %d",@2.first_line,@2.first_column);
$$=strdup("undefined"); $$=undefined;
} else { } else {
$$=$2; $$=$2;
} }
@ -434,133 +469,133 @@ expression:
| NOT expression | NOT expression
{ {
printdebug("not expression"); printdebug("not expression");
if(strcmp($2,"Boolean")==0) { if($2 == boo) {
$$=$2; $$=$2;
} else { } else {
$$=strdup("undefined"); $$=undefined;
printdebug("mismatch at line %d and column %d. Invalid type being negated is %s", @1.first_line,@1.first_column,$2); printdebug("mismatch at line %d and column %d. Invalid type being negated is %s", @1.first_line,@1.first_column,getName($2));
} }
} }
| expression ADD expression | expression ADD expression
{ {
printdebug("add expression"); printdebug("add expression");
if(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { if($1 == $3 && $1 == integ) {
$$=strdup("integer"); $$=$1;
} else { } else {
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
$$=strdup("undefined"); $$=undefined;
} }
} }
| expression SUB_OR_NEG expression | expression SUB_OR_NEG expression
{ {
printdebug("sub or neg expression"); printdebug("sub or neg expression");
if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0) { if($1 == $3 && $1 == integ) {
$$=strdup("integer"); $$=$1;
} else { } else {
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
$$=strdup("undefined"); $$=undefined;
} }
} }
| expression MUL expression | expression MUL expression
{ {
printdebug("multiply expression"); printdebug("multiply expression");
if(strcmp($1,$3)==0 &&strcmp($1,"integer")==0) { if($1 == $3 && $1 == integ) {
$$=strdup("integer"); $$=$1;
} else{ } else{
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
$$=strdup("undefined"); $$=undefined;
} }
} }
| expression DIV expression | expression DIV expression
{ {
printdebug("divide expression"); printdebug("divide expression");
if(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { if((strcmp(getName($1),getName($3))==0) && ($1 == integ)) {
$$=strdup("integer"); $$=$1;
} else { } else {
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
$$=strdup("undefined"); $$=undefined;
} }
} }
| expression REM expression | expression REM expression
{ {
printdebug("remainder expression"); printdebug("remainder expression");
if(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { if($1 == $3 && $1 == integ) {
$$=strdup("integer"); $$=$1;
} else { } else {
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
$$=strdup("undefined"); $$=undefined;
} }
} }
| expression AND expression | expression AND expression
{ {
printdebug("AND expression"); printdebug("AND expression");
if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0) { if($1 == $3 && $1 == boo){
$$=strdup("Boolean"); $$=$1;
} else{ } else{
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
$$=strdup("undefined"); $$=undefined;
} }
} }
| expression OR expression | expression OR expression
{ {
printdebug("OR"); printdebug("OR");
if(strcmp($1,$3)==0 && strcmp($1,"Boolean")==0) { if((strcmp(getName($1),getName($3))==0) && $1 == boo) {
$$=strdup("Boolean"); $$=$1;
} else { } else {
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
$$=strdup("undefined"); $$=undefined;
} }
} }
| expression LESS_THAN expression | expression LESS_THAN expression
{ {
printdebug("less than expression"); printdebug("less than expression");
if(strcmp($1,$3)==0 && strcmp($1,"integer")==0) { if($1 == $3 && $1==integ) {
$$=strdup("Boolean"); $$=boo;
} else { } else {
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,$1,$3); printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
$$=strdup("undefined"); $$=undefined;
} }
} }
| expression EQUAL_TO expression | expression EQUAL_TO expression
{ {
printdebug("equals check expression"); printdebug("equals check expression");
if(strcmp($1,$3)==0) { if($1 == $3 && $1 != undefined) {
$$=strdup("Boolean"); $$=boo;
} else { } else {
printdebug("mismatch at line %d and column %d. Invalid types %s and %s", @2.first_line,@2.first_column,$1,$3); printdebug("mismatch at line %d and column %d. Invalid types %s and %s", @2.first_line,@2.first_column,getName($1),getName($3));
$$=strdup("undefined"); $$ = undefined;
} }
} }
| assignable | assignable
{ {
printdebug("assignable expression. current type is %s",$1); printdebug("assignable expression. current type is %s",$1);
$$=$1; $$= getTypeEntry($1);
} }
| L_PAREN expression R_PAREN | L_PAREN expression R_PAREN
{ {
printdebug("paren expression. current type is %s",$2); printdebug("paren expression. current type is %s",getName($2));
$$=$2; $$=$2;
} }
| memOp assignable | memOp assignable
{ {
$$ = strdup("address"); $$ = addr;
} }
; ;
//UPDATED $$ for tablenodes to this point
// prolly right, check back with me later // prolly right, check back with me later
// add array case // add array case
@ -568,7 +603,7 @@ expression:
assignable: assignable:
ID ID
{ {
$$ = getType(look_up(cur,$1)); $$ = look_up(cur,$1);
printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1); printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", $$, $1);
} }
@ -579,59 +614,69 @@ assignable:
} }
ablock ablock
{ {
int type = getAdInfoType(look_up(getParent(cur), $1)); int type = getAdInfoType(look_up(getParent(cur), getName($1)));
printdebug("%stype is %d", COLOR_PURPLE, type); printdebug("%stype is %d", COLOR_PURPLE, type);
printdebug("%s", $1);
if (type == TYPE_FUNCTION_DECLARATION) { if (type == TYPE_FUNCTION_DECLARATION) {
printdebug("%sEntering function call", COLOR_LIGHTGREEN); printdebug("%sEntering function call", COLOR_LIGHTGREEN);
if (getAsKeyword(look_up(getParent(cur), $1))) { if (look_up(getParent(cur), getName($1))->additionalinfo->FunDecAdInfo->regularoras) {
TableNode *param = getParameter(look_up(getParent(cur), $1)); printdebug("as function");
//char *funtype = getType(look_up(cur, $1));
printdebug("%s", getType(look_up(cur, getName($1))));
TableNode *param = getParameter(look_up(getParent(cur), getName($1)));
SymbolTable *recList = getRecList(param); SymbolTable *recList = getRecList(param);
TableNode *lastCheckedRef = getFirstEntry(recList); TableNode *lastCheckedRef = getFirstEntry(recList);
TableNode *lastCheckedAct = getFirstEntry(cur); TableNode *lastCheckedAct = getFirstEntry(cur);
while (getNextEntry(lastCheckedRef) != NULL) { while (getNextEntry(lastCheckedRef) != NULL) {
lastCheckedRef = getNextEntry(lastCheckedRef); lastCheckedRef = getNextEntry(lastCheckedRef);
} }
//this isn't very efficient, but will hopefully work //this isn't very efficient, but will hopefully work
while (lastCheckedAct != NULL && lastCheckedRef != NULL) { while (lastCheckedAct != NULL && lastCheckedRef != NULL) {
if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) { if (strcmp(getName(lastCheckedAct), getName(lastCheckedRef)) != 0) {
printdebug("expected %s expression in function call but got %s at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column); printdebug("expected %s. expression in function call got %s. at line %d and column %d",getType(lastCheckedRef), getName(lastCheckedAct), @3.first_line, @3.first_column);
printdebug("%d", strcmp(getName(lastCheckedAct), getName(lastCheckedRef)));
} }
lastCheckedAct = getNextEntry(lastCheckedAct); lastCheckedAct = getNextEntry(lastCheckedAct);
TableNode *tn = getFirstEntry(recList); TableNode *tn = getFirstEntry(recList);
while (getNextEntry(tn) != lastCheckedRef) {
tn = getNextEntry(tn); if (tn != lastCheckedRef) {
} while (getNextEntry(tn) != lastCheckedRef) {
lastCheckedRef = tn; tn = getNextEntry(tn);
}
lastCheckedRef = tn;
} else {break;}
} }
} else { } else {
char *expected = getName(getParameter(look_up(getParent(cur), $1))); char *expected = getName(getParameter(look_up(getParent(cur), getName($1))));
char *actual = getType(getFirstEntry(cur)); char *actual = getType(getFirstEntry(cur));
if (strcmp(expected, actual) != 0) { if (strcmp(expected, actual) != 0) {
printdebug("expected %s expression in function call but got %s at line %d and column %d",expected, actual, @3.first_line, @3.first_column); printdebug("expected %s expression in function call but got %s at line %d and column %d",expected, actual, @3.first_line, @3.first_column);
} }
} }
$$ = getReturn((table_lookup(getAncestor(cur), getType($1))));
$$ = getName(getReturn(table_lookup(getAncestor(cur), $1))); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName($1));
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $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), $1)) != $<integ>2) { if (getNumArrDim(look_up(getParent(cur), getName($1))) != $<integ>2) {
printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, $1)), $<integ>2, @2.first_line, @2.first_column); printdebug("expected %d arguments but had %d at line %d and column %d\n", getNumArrDim(look_up(cur, getName($1))), $<integ>2, @2.first_line, @2.first_column);
} }
$$ = getName(getArrType(look_up(getParent(cur), $1))); $$ = getArrType(look_up(getParent(cur), getName($1)));
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1); printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName($1));
} }
cur = getParent(cur); cur = getParent(cur);
} }
| assignable rec_op ID | assignable rec_op ID
{ {
if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3)) { if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), getName($1))), $3)) {
$$ = getName(table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3)); $$ = table_lookup(getRecList(table_lookup(getAncestor(cur), getName($1))), $3);
} }
printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", $$, $1); printdebug("[ASSIGNABLE - RULE 3] assignable = type: %s | ID = %s", getName($$), $1);
} }
; ;
@ -655,37 +700,37 @@ memOp:
constant: constant:
C_STRING C_STRING
{ {
$$ = $<words>1; $$ = $1;
printdebug("string of C_STRING in constant is %s",$<words>1); printdebug("string of C_STRING in constant is %s",$<words>1);
} }
| C_INTEGER | C_INTEGER
{ {
$$ = "integer"; $$ = integ;
printdebug("string of C_INTEGER in constant is integer"); printdebug("string of C_INTEGER in constant is integer");
} }
| C_NULL | C_NULL
{ {
$$ = $<words>1; $$ = $1;
printdebug("string of C_NULL in constant is %s",$<words>1); printdebug("string of C_NULL in constant is %s",$<words>1);
} }
| C_CHARACTER | C_CHARACTER
{ {
$$ = $<words>1; $$ = $1;
printdebug("string of C_CHARACTER in constant is %s",$<words>1); printdebug("string of C_CHARACTER in constant is %s",$<words>1);
} }
| C_TRUE | C_TRUE
{ {
$$ = $<words>1; $$ = $1;
printdebug("string of C_TRUE in constant is %s",$<words>1); printdebug("string of C_TRUE in constant is %s",$<words>1);
} }
| C_FALSE | C_FALSE
{ {
$$ = $<words>1; $$ = $1;
printdebug("string of C_FALSE in constant is %s",$<words>1); printdebug("string of C_FALSE in constant is %s",$<words>1);
} }

View File

@ -13,6 +13,16 @@
#endif #endif
extern SymbolTable * cur; extern SymbolTable * cur;
extern FILE* tok_flag; extern FILE* tok_flag;
extern TableNode *funprime;
extern TableNode *funtypeprime;
extern TableNode *arrayprim;
extern TableNode *recprime;
extern TableNode *integ;
extern TableNode *addr;
extern TableNode *chara;
extern TableNode *stri;
extern TableNode *boo;
extern TableNode *undefined;
extern void incr(int lnum,int cnum, int tok); extern void incr(int lnum,int cnum, int tok);
extern void print_tok(int tok); extern void print_tok(int tok);
@ -36,10 +46,10 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
%% %%
"integer" {if(DEBUG) {printf( "T_INTEGER: %s (%d)\n", yytext, T_INTEGER);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.words = strdup(yytext);return T_INTEGER;}} "integer" {if(DEBUG) {printf( "T_INTEGER: %s (%d)\n", yytext, T_INTEGER);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.tn = integ;return T_INTEGER;}}
"address" {if(DEBUG) {printf( "T_ADDRESS: %s (%d)\n", yytext, T_ADDRESS);} else {if(tok_flag != NULL){print_tok(T_ADDRESS);}incr(line_number,column_number,T_ADDRESS);yylval.words = strdup(yytext);return T_ADDRESS;}} "address" {if(DEBUG) {printf( "T_ADDRESS: %s (%d)\n", yytext, T_ADDRESS);} else {if(tok_flag != NULL){print_tok(T_ADDRESS);}incr(line_number,column_number,T_ADDRESS);yylval.tn = addr;return T_ADDRESS;}}
"Boolean" {if(DEBUG) {printf( "T_BOOLEAN: %s (%d)\n", yytext, T_BOOLEAN);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.words = strdup(yytext);return T_BOOLEAN;}} "Boolean" {if(DEBUG) {printf( "T_BOOLEAN: %s (%d)\n", yytext, T_BOOLEAN);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.tn = boo;return T_BOOLEAN;}}
"character" {if(DEBUG) {printf( "T_CHARACTER: %s (%d)\n", yytext, T_CHARACTER);} else {if(tok_flag != NULL){print_tok(T_CHARACTER);}incr(line_number,column_number,T_CHARACTER);yylval.words = strdup(yytext);return T_CHARACTER;}} "character" {if(DEBUG) {printf( "T_CHARACTER: %s (%d)\n", yytext, T_CHARACTER);} else {if(tok_flag != NULL){print_tok(T_CHARACTER);}incr(line_number,column_number,T_CHARACTER);yylval.tn = chara;return T_CHARACTER;}}
"while" {if(DEBUG) {printf( "WHILE: %s (%d)\n", yytext, WHILE);} else {if(tok_flag != NULL){print_tok(WHILE);}incr(line_number,column_number,WHILE);return WHILE;}} "while" {if(DEBUG) {printf( "WHILE: %s (%d)\n", yytext, WHILE);} else {if(tok_flag != NULL){print_tok(WHILE);}incr(line_number,column_number,WHILE);return WHILE;}}
"if" {if(DEBUG) {printf( "IF: %s (%d)\n", yytext, IF);} else {if(tok_flag != NULL){print_tok(IF);}incr(line_number,column_number,IF);return IF;}} "if" {if(DEBUG) {printf( "IF: %s (%d)\n", yytext, IF);} else {if(tok_flag != NULL){print_tok(IF);}incr(line_number,column_number,IF);return IF;}}
@ -73,8 +83,8 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
"->" {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;}} "->" {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(yytext)/*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;}} '{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.tn = chara;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;}} \"{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.tn = stri;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;*/}} {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;*/}}
"(" {if(DEBUG) {printf( "L_PAREN: %s (%d)\n", yytext, L_PAREN);} else {if(tok_flag != NULL){print_tok(L_PAREN);}incr(line_number,column_number,L_PAREN);return L_PAREN;}} "(" {if(DEBUG) {printf( "L_PAREN: %s (%d)\n", yytext, L_PAREN);} else {if(tok_flag != NULL){print_tok(L_PAREN);}incr(line_number,column_number,L_PAREN);return L_PAREN;}}
@ -84,9 +94,9 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
"{" {if(DEBUG) {printf( "L_BRACE: %s (%d)\n", yytext, L_BRACE);} else {if(tok_flag != NULL){print_tok(L_BRACE);}incr(line_number,column_number,L_BRACE);return L_BRACE;}} "{" {if(DEBUG) {printf( "L_BRACE: %s (%d)\n", yytext, L_BRACE);} else {if(tok_flag != NULL){print_tok(L_BRACE);}incr(line_number,column_number,L_BRACE);return L_BRACE;}}
"}" {if(DEBUG) {printf( "R_BRACE: %s (%d)\n", yytext, R_BRACE);} else {if(tok_flag != NULL){print_tok(R_BRACE);}incr(line_number,column_number,R_BRACE);return R_BRACE;}} "}" {if(DEBUG) {printf( "R_BRACE: %s (%d)\n", yytext, R_BRACE);} else {if(tok_flag != NULL){print_tok(R_BRACE);}incr(line_number,column_number,R_BRACE);return R_BRACE;}}
"true" {if(DEBUG) {printf( "C_TRUE: %s (%d)\n", yytext, C_TRUE);} else {if(tok_flag != NULL){print_tok(C_TRUE);}incr(line_number,column_number,C_TRUE);yylval.words = strdup("Boolean");return C_TRUE;}} "true" {if(DEBUG) {printf( "C_TRUE: %s (%d)\n", yytext, C_TRUE);} else {if(tok_flag != NULL){print_tok(C_TRUE);}incr(line_number,column_number,C_TRUE);yylval.tn = boo;return C_TRUE;}}
"false" {if(DEBUG) {printf( "C_FALSE: %s (%d)\n", yytext, C_FALSE);} else {if(tok_flag != NULL){print_tok(C_FALSE);}incr(line_number,column_number,C_FALSE);yylval.words = strdup("Boolean");return C_FALSE;}} "false" {if(DEBUG) {printf( "C_FALSE: %s (%d)\n", yytext, C_FALSE);} else {if(tok_flag != NULL){print_tok(C_FALSE);}incr(line_number,column_number,C_FALSE);yylval.tn = boo;return C_FALSE;}}
"null" {if(DEBUG) {printf( "C_NULL: %s (%d)\n", yytext, C_NULL);} else {if(tok_flag != NULL){print_tok(C_NULL);}incr(line_number,column_number,C_NULL);yylval.words = strdup("address");return C_NULL;}} "null" {if(DEBUG) {printf( "C_NULL: %s (%d)\n", yytext, C_NULL);} else {if(tok_flag != NULL){print_tok(C_NULL);}incr(line_number,column_number,C_NULL);yylval.tn = addr;return C_NULL;}}
{ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {if(tok_flag != NULL){print_tok(ID);}incr(line_number,column_number,ID);yylval.words = strdup(yytext); return ID;}} {ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {if(tok_flag != NULL){print_tok(ID);}incr(line_number,column_number,ID);yylval.words = strdup(yytext); return ID;}}

View File

@ -151,6 +151,7 @@ int run(FILE *alpha) {
printdebug("%s[FATAL] top is null", COLOR_LIGHTRED); printdebug("%s[FATAL] top is null", COLOR_LIGHTRED);
} }
print_symbol_table(top, st_flag); print_symbol_table(top, st_flag);
fclose(st_flag); fclose(st_flag);
if (yyin != NULL) { if (yyin != NULL) {

View File

@ -262,8 +262,8 @@ bool getAsKeyword(TableNode *definition) {
} }
if (strcmp(getType(definition), "primitive function") != 0) { if (strcmp(getType(definition), "primitive function") != 0) {
printdebug( printdebug(
"not checking if a function is called with as or not -- " "not checking if a function is called with as or not (%s) -- "
"invalid op"); "invalid op", getType(definition));
return 0; return 0;
} }
return definition->additionalinfo->FunDecAdInfo->regularoras; return definition->additionalinfo->FunDecAdInfo->regularoras;
@ -423,6 +423,7 @@ SymbolTable *init(SymbolTable *start) {
prime->theType = NULL; prime->theType = NULL;
prime->additionalinfo = NULL; prime->additionalinfo = NULL;
prime->next = NULL; prime->next = NULL;
prime->tag = TYPE_SYSTEM_DEFINED;
// not sure exatly how to get array types to look right so using a dummy // not sure exatly how to get array types to look right so using a dummy
// Table Node below and updating the print symbol table function to // Table Node below and updating the print symbol table function to
@ -432,7 +433,8 @@ SymbolTable *init(SymbolTable *start) {
arrayprim->theName = "array"; arrayprim->theName = "array";
arrayprim->theType = NULL; arrayprim->theType = NULL;
arrayprim->additionalinfo = NULL; arrayprim->additionalinfo = NULL;
prime->next = NULL; arrayprim->next = NULL;
prime->tag = TYPE_SYSTEM_DEFINED;
// funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL); // funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL);
@ -442,6 +444,7 @@ SymbolTable *init(SymbolTable *start) {
funprime->theType = NULL; funprime->theType = NULL;
funprime->additionalinfo = NULL; funprime->additionalinfo = NULL;
funprime->next = NULL; funprime->next = NULL;
funprime->tag = TYPE_SYSTEM_DEFINED;
// record // record
recprime = (TableNode *)malloc(sizeof(TableNode)); recprime = (TableNode *)malloc(sizeof(TableNode));
@ -449,18 +452,21 @@ SymbolTable *init(SymbolTable *start) {
recprime->theType = NULL; recprime->theType = NULL;
recprime->additionalinfo = NULL; recprime->additionalinfo = NULL;
recprime->next = NULL; recprime->next = NULL;
recprime->tag = TYPE_SYSTEM_DEFINED;
funtypeprime = (TableNode *)malloc(sizeof(TableNode)); funtypeprime = (TableNode *)malloc(sizeof(TableNode));
funtypeprime->theName = "primitive function type"; funtypeprime->theName = "primitive function type";
funtypeprime->theType = NULL; funtypeprime->theType = NULL;
funtypeprime->additionalinfo = NULL; funtypeprime->additionalinfo = NULL;
funtypeprime->next = NULL; funtypeprime->next = NULL;
funtypeprime->tag = TYPE_SYSTEM_DEFINED;
undefined = (TableNode *)malloc(sizeof(TableNode)); undefined = (TableNode *)malloc(sizeof(TableNode));
undefined->theName = "undefined"; undefined->theName = "undefined";
undefined->theType = NULL; undefined->theType = NULL;
undefined->additionalinfo = NULL; undefined->additionalinfo = NULL;
undefined->next = NULL; undefined->next = NULL;
undefined->tag = TYPE_SYSTEM_DEFINED;
// Undefined_function_type_info = CreateFunctionTypeInfo(undefined, // Undefined_function_type_info = CreateFunctionTypeInfo(undefined,
// undefined); // undefined);
@ -477,11 +483,17 @@ SymbolTable *init(SymbolTable *start) {
// be the size of these primitive types. We can change these if needed // be the size of these primitive types. We can change these if needed
// to not be hard coded numbers as a reminder, stri below is defined as // to not be hard coded numbers as a reminder, stri below is defined as
// a one dimensional array of characters // a one dimensional array of characters
integ->additionalinfo = CreatePrimitiveInfo(4); integ->additionalinfo = CreatePrimitiveInfo(SIZE_INT);
addr->additionalinfo = CreatePrimitiveInfo(8); addr->additionalinfo = CreatePrimitiveInfo(SIZE_ADDR);
chara->additionalinfo = CreatePrimitiveInfo(1); chara->additionalinfo = CreatePrimitiveInfo(SIZE_CHAR);
stri->additionalinfo = CreateArrayInfo(1, chara); stri->additionalinfo = CreateArrayInfo(1, chara);
boo->additionalinfo = CreatePrimitiveInfo(1); boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL);
integ->tag = TYPE_PRIMITIVE; // explicitly set the type for integ
addr->tag = TYPE_PRIMITIVE; // explicitly set the type for addr
chara->tag = TYPE_PRIMITIVE; // explicitly set the type for chara
stri->tag = TYPE_ARRAY_TYPE; // explicitly set the type for stri
boo->tag = TYPE_PRIMITIVE; // explicitly set the type for boo
// addr->additionalinfo = CreatePrimitiveInfo(8); // addr->additionalinfo = CreatePrimitiveInfo(8);
start->Line_Number = 1; start->Line_Number = 1;
@ -533,12 +545,33 @@ TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info) {
return tn; return tn;
} }
AdInfo *getAdInfo(TableNode *tn) {
if (tn == NULL) {
printdebug("passed a NULL table entry to getAdInfo. Invalid.");
return NULL;
}
if (tn == undefined) {
printdebug(
"passed an undefined table entry to getAdInfo. Invalid.");
return NULL;
}
if (tn->additionalinfo == NULL) {
printdebug(
"no additional info found in the table node. Invalid.");
return NULL;
}
return tn->additionalinfo;
}
//simplified getAdInfoType
int getAdInfoType(TableNode *tn) { int getAdInfoType(TableNode *tn) {
if (tn == NULL) { if (tn == NULL) {
printdebug( printdebug(
"passing in NULL table entry to getAdInfoType. Invalid"); "passing in NULL table entry to getAdInfoType. Invalid");
return -1; return -1;
} }
return tn->tag;
/*
if (tn == undefined) { if (tn == undefined) {
printdebug("passing in undefined table entry to getAdInfoType. " printdebug("passing in undefined table entry to getAdInfoType. "
"Invalid"); "Invalid");
@ -597,11 +630,10 @@ int getAdInfoType(TableNode *tn) {
"passed in an entry that is not a primitive type, array, " "passed in an entry that is not a primitive type, array, "
"or record. Invalid."); "or record. Invalid.");
return TYPE_FUNCTION_DECLARATION; return TYPE_FUNCTION_DECLARATION;
} }*/
} }
TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, AdInfo *ad) {
AdInfo *ad) {
if (table == NULL) { if (table == NULL) {
printdebug("Null reference to table"); printdebug("Null reference to table");
@ -624,7 +656,14 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
return undefined; return undefined;
} }
TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode)); TableNode *newEntry = (TableNode *)calloc(1, sizeof(TableNode));
if(tag<1 && tag>11){
printdebug("Note- not passing in valid 'tag' identifier to create entry function. Setting tag to undefined");
newEntry->tag = TYPE_UNDEFINED;
} else{
newEntry->tag = tag;
}
newEntry->theType = typeOf /*topDef*/; newEntry->theType = typeOf /*topDef*/;
newEntry->theName = id; newEntry->theName = id;
newEntry->additionalinfo = ad; newEntry->additionalinfo = ad;
@ -641,6 +680,28 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
} }
} }
TableNode *getTypeEntry(TableNode *tn) {
if (tn == NULL) {
printdebug("passed a NULL table entry to getType");
return undefined;
}
if (tn == undefined) {
printdebug("passed an undefined table entry to getType");
return undefined;
}
if (tn->theType == NULL) {
printdebug("type of entry is currently NULL type");
return undefined;
}
if (tn->theType == undefined) {
printdebug("type of entry is currently undefined type");
return undefined;
}
return tn->theType;
}
char *getType(TableNode *tn) { char *getType(TableNode *tn) {
if (tn == NULL) { if (tn == NULL) {
printdebug("passed a NULL table entry to getType"); printdebug("passed a NULL table entry to getType");
@ -705,9 +766,14 @@ TableNode *addName(TableNode *tn, char *str) {
return undefined; return undefined;
} }
if (tn->theName != NULL) { if (tn->theName != NULL) {
printdebug( //printdebug(
"Name doesn't look like it is empty before you change. " //"Name doesn't look like it is empty before you change. "
"Are you sure you need to update name?"); //"Are you sure you need to update name?");
if (str != NULL) {
tn->theName = str;
return tn;
}
printdebug("passed a NULL string to the addName function");
return undefined; return undefined;
} }
if (str == NULL) { if (str == NULL) {
@ -772,177 +838,178 @@ TableNode *look_up(SymbolTable *table, char *x) {
} }
void print_symbol_table(SymbolTable *table, FILE *file_ptr) { void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (table == NULL) {
printdebug(
"%s[FATAL] passed in NULL table to print_symbol_table",
COLOR_RED);
return;
}
if (table->Parent_Scope == NULL) { if (table == NULL) {
fprintf(file_ptr, "%-25s: %-6s : %-6s : %-25s: %-30s\n", "NAME", printdebug(
"SCOPE", "PARENT", "TYPE", "Extra annotation"); "%s[FATAL] passed in NULL table to print_symbol_table",
} COLOR_RED);
return;
}
TableNode *entrie = table->entries; if (table->Parent_Scope == NULL) {
fprintf(file_ptr, fprintf(file_ptr, "%-25s: %-6s : %-6s : %-25s: %-30s\n", "NAME",
"-------------------------:--------:--------:------------------" "SCOPE", "PARENT", "TYPE", "Extra annotation");
"--------:------------------------------\n"); }
int parant_scope = 0;
int current_scope = 0;
if (table->Parent_Scope != NULL) {
parant_scope = getParent(table)->Line_Number * 1000 +
getParent(table)->Column_Number;
current_scope =
table->Line_Number * 1000 + table->Column_Number;
} else {
current_scope = 1001;
}
if (entrie == NULL) {
fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "",
current_scope, parant_scope, "", "Empty Scope");
}
for (; entrie != NULL; entrie = getNextEntry(entrie)) {
if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) {
if (parant_scope == 0) {
fprintf(file_ptr, TableNode *entrie = table->entries;
"%-25s: %06d : : %d -> %-20s: " fprintf(file_ptr,
"%-30s\n", "-------------------------:--------:--------:------------------"
entrie->theName, current_scope, "--------:------------------------------\n");
entrie->additionalinfo->ArrayAdInfo int parant_scope = 0;
->numofdimensions, int current_scope = 0;
entrie->additionalinfo->ArrayAdInfo if (table->Parent_Scope != NULL) {
->typeofarray->theName, parant_scope = getParent(table)->Line_Number * 1000 +
"Type of Array"); getParent(table)->Column_Number;
} else { current_scope =
fprintf(file_ptr, table->Line_Number * 1000 + table->Column_Number;
"%-25s: %06d : : %d -> %-20s: " } else {
"%-30s\n", current_scope = 1001;
entrie->theName, current_scope, }
parant_scope, if (entrie == NULL) {
entrie->additionalinfo->ArrayAdInfo fprintf(file_ptr, "%-25s: %06d : %06d : %-25s: %-30s\n", "",
->numofdimensions, current_scope, parant_scope, "", "Empty Scope");
entrie->additionalinfo->ArrayAdInfo }
->typeofarray->theName, for (; entrie != NULL; entrie = getNextEntry(entrie)) {
"Type of Array"); if (getAdInfoType(entrie) == TYPE_ARRAY_TYPE) {
} if (parant_scope == 0) {
}
if (getAdInfoType(entrie) == TYPE_RECORD) {
if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : : %-25s: " "%-25s: %06d : : %d -> %-20s: "
"elements-%-30d\n", "%-30s\n",
entrie->theName, current_scope,
"record",
entrie->additionalinfo->RecAdInfo
->numofelements);
} else {
fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: "
"elements-%-30d\n",
entrie->theName, current_scope,
parant_scope, "record",
entrie->additionalinfo->RecAdInfo
->numofelements);
}
}
if (getAdInfoType(entrie) == TYPE_PRIMITIVE) {
if (parant_scope == 0) {
fprintf(
file_ptr,
"%-25s: %06d : : %-25s: size-%d "
"bytes\n",
entrie->theName, current_scope, "Primitive",
entrie->additionalinfo->PrimAdInfo->size);
} else {
fprintf(
file_ptr,
"%-25s: %06d : %06d : %-25s: size-%-30d "
"bytes\n",
entrie->theName, current_scope, entrie->theName, current_scope,
parant_scope, "Primitive", entrie->additionalinfo->ArrayAdInfo
entrie->additionalinfo->PrimAdInfo->size); ->numofdimensions,
} entrie->additionalinfo->ArrayAdInfo
} ->typeofarray->theName,
if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) { "Type of Array");
if (parant_scope == 0) { } else {
fprintf(file_ptr,
"%-25s: %06d : : %d -> %-20s: "
"%-30s\n",
entrie->theName, current_scope,
parant_scope,
entrie->additionalinfo->ArrayAdInfo
->numofdimensions,
entrie->additionalinfo->ArrayAdInfo
->typeofarray->theName,
"Type of Array");
}
}
if (getAdInfoType(entrie) == TYPE_RECORD) {
if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : : %-25s -> " "%-25s: %06d : : %-25s: "
"%-25s: %-30s\n", "elements-%-30d\n",
entrie->theName, current_scope, entrie->theName, current_scope,
entrie->additionalinfo->FunTypeAdInfo "record",
->parameter->theName, entrie->additionalinfo->RecAdInfo
entrie->additionalinfo->FunTypeAdInfo ->numofelements);
->returntype->theName, } else {
"Type of Function"); fprintf(file_ptr,
} else { "%-25s: %06d : %06d : %-25s: "
fprintf(file_ptr, "elements-%-30d\n",
"%-25s: %06d : %06d : %-25s -> %-21s: " entrie->theName, current_scope,
"%-30s\n", parant_scope, "record",
entrie->theName, current_scope, entrie->additionalinfo->RecAdInfo
parant_scope, ->numofelements);
entrie->additionalinfo->FunTypeAdInfo }
->parameter->theName, }
entrie->additionalinfo->FunTypeAdInfo if (getAdInfoType(entrie) == TYPE_PRIMITIVE) {
->returntype->theName, if (parant_scope == 0) {
"Type of Function");
}
}
if (getAdInfoType(entrie) == TYPE_FUNCTION_DECLARATION) {
if (parant_scope == 0) {
fprintf(file_ptr, fprintf(
"%-25s: %06d : : %-25s: %-30s\n", file_ptr,
entrie->theName, current_scope, "%-25s: %06d : : %-25s: size-%d "
getType(entrie), "User Defined"); "bytes\n",
} else { entrie->theName, current_scope, "Primitive",
fprintf(file_ptr, entrie->additionalinfo->PrimAdInfo->size);
"%-25s: %06d : %06d : %-25s: %-30s\n", } else {
entrie->theName, current_scope, fprintf(
parant_scope, getType(entrie), file_ptr,
"User Defined"); "%-25s: %06d : %06d : %-25s: size-%-30d "
} "bytes\n",
} entrie->theName, current_scope,
if (getAdInfoType(entrie) == TYPE_UNDEFINED) { parant_scope, "Primitive",
if (parant_scope == 0) { entrie->additionalinfo->PrimAdInfo->size);
}
}
if (getAdInfoType(entrie) == TYPE_FUNCTION_TYPE) {
if (parant_scope == 0) {
fprintf(file_ptr, fprintf(file_ptr,
"%-25s: %06d : : %-25s: %-30s\n", "%-25s: %06d : : %-25s -> "
entrie->theName, current_scope, "%-25s: %-30s\n",
"undefined", "undefined entry"); entrie->theName, current_scope,
} else { entrie->additionalinfo->FunTypeAdInfo
fprintf(file_ptr, ->parameter->theName,
"%-25s: %06d : %06d : %-25s: %-30s\n", entrie->additionalinfo->FunTypeAdInfo
entrie->theName, current_scope, ->returntype->theName,
parant_scope, "undefined", "Type of Function");
"undefined entry"); } else {
} fprintf(file_ptr,
} "%-25s: %06d : %06d : %-25s -> %-21s: "
} "%-30s\n",
if (getChildren(table) != NULL) { entrie->theName, current_scope,
ListOfTable *node = getChildren(table); parant_scope,
for (; node != NULL; node = node->next) { entrie->additionalinfo->FunTypeAdInfo
if ((node->table) == NULL) { ->parameter->theName,
print_symbol_table(node->table, file_ptr); entrie->additionalinfo->FunTypeAdInfo
} else { ->returntype->theName,
if ((node->table)->Line_Number == -1) { "Type of Function");
continue; }
} else { }
print_symbol_table(node->table, if (getAdInfoType(entrie) == TYPE_FUNCTION_DECLARATION) {
file_ptr); if (parant_scope == 0) {
}
} fprintf(file_ptr,
} "%-25s: %06d : : %-25s: %-30s\n",
} entrie->theName, current_scope,
if (getParent(table) == NULL) { getType(entrie), "User Defined");
fprintf(file_ptr, } else {
"-------------------------:--------:--------:----------" fprintf(file_ptr,
"----------------:------------------------------\n"); "%-25s: %06d : %06d : %-25s: %-30s\n",
} entrie->theName, current_scope,
parant_scope, getType(entrie),
"User Defined");
}
}
if (getAdInfoType(entrie) == TYPE_UNDEFINED) {
if (parant_scope == 0) {
fprintf(file_ptr,
"%-25s: %06d : : %-25s: %-30s\n",
entrie->theName, current_scope,
"undefined", "undefined entry");
} else {
fprintf(file_ptr,
"%-25s: %06d : %06d : %-25s: %-30s\n",
entrie->theName, current_scope,
parant_scope, "undefined",
"undefined entry");
}
}
}
if (getChildren(table) != NULL) {
ListOfTable *node = getChildren(table);
for (; node != NULL; node = node->next) {
if ((node->table) == NULL) {
print_symbol_table(node->table, file_ptr);
} else {
if ((node->table)->Line_Number == -1) {
continue;
} else {
print_symbol_table(node->table,
file_ptr);
}
}
}
}
if (getParent(table) == NULL) {
fprintf(file_ptr,
"-------------------------:--------:--------:----------"
"----------------:------------------------------\n");
}
} }
// get top most symbol table // get top most symbol table
SymbolTable *getAncestor(SymbolTable *table) { SymbolTable *getAncestor(SymbolTable *table) {
@ -1049,7 +1116,7 @@ SymbolTable *getParent(SymbolTable *st) {
if (st->Parent_Scope == NULL) { if (st->Parent_Scope == NULL) {
printdebug("passed a top level scope to getParent function. " printdebug("passed a top level scope to getParent function. "
"Invalid."); "Invalid.");
return NULL; return st;
} }
return st->Parent_Scope; return st->Parent_Scope;
} }

View File

@ -4,6 +4,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define SIZE_INT 4
#define SIZE_ADDR 8
#define SIZE_CHAR 1
#define SIZE_BOOL 4 //TODO: Ask Carl what this size should be
struct TableNode; struct TableNode;
typedef struct { typedef struct {
@ -43,8 +49,10 @@ typedef struct ListOfTable {
struct ListOfTable *next; struct ListOfTable *next;
} ListOfTable; } ListOfTable;
//Table node to store
typedef struct TableNode { typedef struct TableNode {
struct TableNode *theType; struct TableNode *theType;
int tag;
char *theName; char *theName;
AdInfo *additionalinfo; AdInfo *additionalinfo;
struct TableNode *next; struct TableNode *next;

View File

@ -32,3 +32,4 @@ entry (arg) := {
result := bar2(5,7); (* implicitly build a rec type value, assign 5 and 7 to fields x and y, but call them r and s *) result := bar2(5,7); (* implicitly build a rec type value, assign 5 and 7 to fields x and y, but call them r and s *)
return 0; return 0;
} }

View File

@ -0,0 +1,7 @@
type rec: [integer: x; integer: y]
type main: rec -> integer
function test: main
test (arg, arg2) := {
return 0;
}

View File

@ -0,0 +1,37 @@
type rec: [integer: x; integer: y]
type main: rec -> integer
function test: main
test (arg) := {
[integer:x; Boolean: b]
while (true) {
x := 0;
}
while (7) {
x := 1;
}
if (true) then {
x := 1;
} else {
x := 0;
}
if (x) then {
x := 0;
} else {
x := 1;
}
b := b | b;
b := b & b;
b := 1 | b;
b := b | 1;
b := b & 1;
b := 1 & b;
b := 1 = 1;
b := 1 = b;
return 0;
}

View File

@ -1,15 +1,19 @@
type main: string -> integer type main: string -> integer
type rec: [integer: x; integer: y]
function entry: main function entry: main
entry (arg) := { entry (arg) := {
[integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1; character : a] [integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1; character : a; rec : r]
x := 3 + 2 * 8; x := 3 + 2 * 8;
x := 3 - 2 / 8; x := 3 - 2 / 8;
x := a * 2 % 8; x := a * 2 % 8;
b2 := 3 * 2 % 8; b2 := 3 * 2 % 8;
x := 3 % 2 * 8; x := 3 % 2 * 8;
x := 3 + arr - 8; x := 3 + arr - 8;
x := r.x;
x := a.x;
return 0; return 0;
} }