updated to change strings to nodes in most locations
This commit is contained in:
151
src/grammar.y
151
src/grammar.y
@ -43,21 +43,23 @@
|
||||
TableNode * tn;
|
||||
%}
|
||||
|
||||
%locations
|
||||
|
||||
%union {
|
||||
int integ;
|
||||
char* words;
|
||||
TableNode* tn;
|
||||
void* tn;
|
||||
}
|
||||
|
||||
%locations
|
||||
|
||||
%type <integ> idlist
|
||||
%type <tn> assignable
|
||||
%type <tn> expression
|
||||
%type <tn> constant
|
||||
%type <tn> id_or_types
|
||||
%type <tn> types
|
||||
%token <tn> ID 101
|
||||
%type <integ> argument_list
|
||||
%type <integ> ablock
|
||||
%token <words> ID 101
|
||||
%token <tn> T_INTEGER 201
|
||||
%token <tn> T_ADDRESS 202
|
||||
%token <tn> T_BOOLEAN 203
|
||||
@ -143,13 +145,15 @@ definition:
|
||||
TYPE ID COLON
|
||||
{
|
||||
printdebug("Currently see a record definition for %s", $2);
|
||||
tn = CreateEntry(getAncestor(cur), recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0)));
|
||||
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
|
||||
{
|
||||
//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(cur, $2), getRecSize(cur));
|
||||
cur = getParent(cur);
|
||||
}
|
||||
@ -157,7 +161,7 @@ definition:
|
||||
| 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, getName($6), $4);
|
||||
CreateEntry(cur, arrayprim, $2, CreateArrayInfo($4, $6));
|
||||
CreateEntry(cur,TYPE_ARRAY_TYPE, arrayprim, $2, CreateArrayInfo($4, $6));
|
||||
printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, getName($6));
|
||||
}
|
||||
|
||||
@ -165,8 +169,8 @@ definition:
|
||||
|
||||
| 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", getName($2), getName($4), getName($6));
|
||||
CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo($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,TYPE_FUNCTION_TYPE,funtypeprime,$2,CreateFunctionTypeInfo($4 ,$6));
|
||||
}
|
||||
|
||||
| ID
|
||||
@ -185,11 +189,25 @@ definition:
|
||||
L_PAREN ID
|
||||
{
|
||||
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)))), $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);
|
||||
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);
|
||||
}
|
||||
|
||||
CreateEntry(cur,type_of_param_type, type_of_param, $4, getAdInfo(type_of_param));
|
||||
}
|
||||
R_PAREN ASSIGN sblock
|
||||
R_PAREN ASSIGN sblock //leaving scope is being taken care of in sblock
|
||||
|
||||
| ID
|
||||
/* | ID
|
||||
{
|
||||
TableNode *node = table_lookup(getAncestor(cur), $1);
|
||||
if (node == undefined) {
|
||||
@ -208,13 +226,13 @@ definition:
|
||||
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
|
||||
}R_PAREN ASSIGN sblock */
|
||||
| ID {
|
||||
TableNode *node = table_lookup(getAncestor(cur), $1);
|
||||
if (node == undefined) {
|
||||
printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column);
|
||||
printdebug(" undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column);
|
||||
}else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){
|
||||
printdebug("function not declared at line %d, column %d", @1.first_line, @1.first_column);
|
||||
printdebug("not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column);
|
||||
}
|
||||
else {
|
||||
printdebug("setting as keyword to true");
|
||||
@ -232,9 +250,26 @@ definition:
|
||||
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)){
|
||||
printdebug("creating entry of type %s for function", getType(entry));
|
||||
CreateEntry(cur, entry, "undefined", NULL);
|
||||
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
|
||||
@ -244,12 +279,12 @@ definition:
|
||||
function_declaration:
|
||||
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
|
||||
{
|
||||
CreateEntry(cur, look_up(cur, $5), $3, NULL);
|
||||
CreateEntry(cur,TYPE_FUNCTION_DECLARATION, look_up(cur, $5), $3, NULL);
|
||||
}
|
||||
;
|
||||
|
||||
@ -265,7 +300,7 @@ idlist:
|
||||
if (getNextEntry(entry) == NULL) {
|
||||
printdebug("too many parameters at line %d column %d", @1.first_line, @1.first_column);
|
||||
}
|
||||
addName(entry, 1);
|
||||
addName(entry, $1);
|
||||
}
|
||||
COMMA idlist
|
||||
{
|
||||
@ -290,8 +325,11 @@ idlist:
|
||||
sblock:
|
||||
L_BRACE
|
||||
{
|
||||
if (getLine(cur) != 0 && getColumn(cur)) {
|
||||
if (getLine(cur) != 0) {
|
||||
cur = CreateScope(cur,@1.first_line,@1.first_column);
|
||||
} else {
|
||||
setLineNumber(cur, @1.first_line);
|
||||
setColumnNumber(cur,@1.first_line);
|
||||
}
|
||||
}
|
||||
statement_list
|
||||
@ -302,8 +340,11 @@ sblock:
|
||||
|
||||
| L_BRACE
|
||||
{
|
||||
if (getLine(cur) != 0 && getColumn(cur)) {
|
||||
if (getLine(cur) != 0) {
|
||||
cur = CreateScope(cur,@1.first_line,@1.first_column);
|
||||
} else {
|
||||
setLineNumber(cur, @1.first_line);
|
||||
setColumnNumber(cur,@1.first_line);
|
||||
}
|
||||
}
|
||||
dblock
|
||||
@ -320,7 +361,15 @@ sblock:
|
||||
|
||||
|
||||
dblock:
|
||||
L_BRACKET declaration_list R_BRACKET;
|
||||
L_BRACKET
|
||||
{if(getLine(cur)==0){
|
||||
setLineNumber(cur, @1.first_line);
|
||||
setColumnNumber(cur,@1.first_line);}
|
||||
else{
|
||||
cur = CreateScope(cur,@1.first_line,@1.first_column);
|
||||
}
|
||||
}
|
||||
declaration_list R_BRACKET;
|
||||
|
||||
|
||||
|
||||
@ -335,7 +384,7 @@ declaration:
|
||||
id_or_types COLON ID
|
||||
{
|
||||
printdebug("ID/TYPE: %s, ID: %s", getName($1), $3) ;
|
||||
CreateEntry(cur,$1,$3,NULL);
|
||||
CreateEntry(cur,getAdInfoType($1),$1,$3,getAdInfo($1));
|
||||
}
|
||||
;
|
||||
|
||||
@ -369,7 +418,7 @@ statement_list:
|
||||
compound_statement:
|
||||
WHILE L_PAREN expression R_PAREN sblock
|
||||
| IF L_PAREN expression R_PAREN THEN sblock ELSE sblock
|
||||
| sblock
|
||||
| sblock
|
||||
;
|
||||
|
||||
|
||||
@ -377,14 +426,14 @@ compound_statement:
|
||||
simple_statement:
|
||||
assignable ASSIGN expression
|
||||
{
|
||||
if(strcmp(getType($1), getType($3)) == 0) {
|
||||
if(strcmp(getName($1), getName($3)) == 0) {
|
||||
printdebug("Passed standard type check; assignable = expression");
|
||||
} else if((strcmp(getType($1), "array") == 0) && (strcmp($3, "address") == 0)) {
|
||||
printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getType($1), getType($3));
|
||||
} else if((strcmp(getType($1), "record") == 0) && (strcmp($3, "address") == 0)) {
|
||||
printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getType($1), getType($3));
|
||||
} else if((strcmp(getType($1), "function type primitive") == 0) && (strcmp(getType($3), "address") == 0)) {
|
||||
printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getType($1), getType($3));
|
||||
} else if((strcmp(getName($1), "array") == 0) && (strcmp(getName($3), "address") == 0)) {
|
||||
printdebug("%s[☺] Passed array type check; %s = %s", COLOR_GREEN, getName($1), getName($3));
|
||||
} else if((strcmp(getName($1), "record") == 0) && (strcmp(getName($3), "address") == 0)) {
|
||||
printdebug("%s[☺] Passed address type check; %s = %s", COLOR_GREEN, getName($1), getName($3));
|
||||
} else if((strcmp(getName($1), "function type primitive") == 0) && (strcmp(getName($3), "address") == 0)) {
|
||||
printdebug("%s[☺] Passed function type primitive type check; %s = %s", COLOR_GREEN, getName($1), getName($3));
|
||||
// } else if () {
|
||||
|
||||
// } else if(strcmp(getType(table_lookup(cur, $1)), getType(table_lookup(cur, $3))) == 0) {
|
||||
@ -419,14 +468,14 @@ ablock:
|
||||
argument_list:
|
||||
expression COMMA argument_list
|
||||
{
|
||||
CreateEntry(cur, $1, getName($1), NULL);
|
||||
CreateEntry(cur,getAdInfoType($1), $1, getName($1), NULL);
|
||||
$$ = $3 + 1;
|
||||
printdebug("[ARGUMENT_LIST] argument list is %d", $$);
|
||||
}
|
||||
|
||||
| expression
|
||||
{
|
||||
CreateEntry(cur, $1, getName($1), NULL);
|
||||
CreateEntry(cur,getAdInfoType($1),$1, getName($1), NULL);
|
||||
$$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$);
|
||||
}
|
||||
;
|
||||
@ -498,7 +547,7 @@ expression:
|
||||
| expression DIV expression
|
||||
{
|
||||
printdebug("divide expression");
|
||||
if(strcmp($1 == $3 && $1 == integ) {
|
||||
if((strcmp(getName($1),getName($3))==0) && ($1 == integ)) {
|
||||
$$=$1;
|
||||
} else {
|
||||
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
|
||||
@ -520,7 +569,7 @@ expression:
|
||||
| expression AND expression
|
||||
{
|
||||
printdebug("AND expression");
|
||||
if($1 == $3 && $1 == boo {
|
||||
if($1 == $3 && $1 == boo){
|
||||
$$=$1;
|
||||
} else{
|
||||
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
|
||||
@ -531,7 +580,7 @@ expression:
|
||||
| expression OR expression
|
||||
{
|
||||
printdebug("OR");
|
||||
if(strcmp($1 == $3 && $1 == boo) {
|
||||
if((strcmp(getName($1),getName($3))==0) && $1 == boo) {
|
||||
$$=$1;
|
||||
} else {
|
||||
printdebug("mismatch at line %d and column %d. Invalid types %s and %s.", @2.first_line,@2.first_column,getName($1),getName($3));
|
||||
@ -599,17 +648,17 @@ assignable:
|
||||
}
|
||||
ablock
|
||||
{
|
||||
int type = getAdInfoType(look_up(getParent(cur), $1));
|
||||
int type = getAdInfoType(look_up(cur, getName($1)));
|
||||
printdebug("%stype is %d", COLOR_PURPLE, type);
|
||||
printdebug("%s", $1);
|
||||
|
||||
if (type == TYPE_FUNCTION_DECLARATION) {
|
||||
printdebug("%sEntering function call", COLOR_LIGHTGREEN);
|
||||
if (look_up(getParent(cur), $1)->additionalinfo->FunDecAdInfo->regularoras) {
|
||||
if (look_up(getParent(cur), getName($1))->additionalinfo->FunDecAdInfo->regularoras) {
|
||||
printdebug("as function");
|
||||
//char *funtype = getType(look_up(cur, $1));
|
||||
printdebug("%s", getType(look_up(cur, $1)));
|
||||
TableNode *param = getParameter(look_up(cur, getType(look_up(cur, $1))));
|
||||
printdebug("%s", getType(look_up(cur, getName($1))));
|
||||
TableNode *param = getParameter(look_up(cur, getType(look_up(cur, getName($1)))));
|
||||
SymbolTable *recList = getRecList(param);
|
||||
TableNode *lastCheckedRef = getFirstEntry(recList);
|
||||
TableNode *lastCheckedAct = getFirstEntry(cur);
|
||||
@ -636,32 +685,32 @@ assignable:
|
||||
}
|
||||
|
||||
} 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));
|
||||
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);
|
||||
}
|
||||
}
|
||||
$$ = getName(getReturn((look_up(cur, getType(look_up(cur, $1))))));
|
||||
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1);
|
||||
$$ = getReturn((look_up(cur, getType(look_up(cur, getName($1))))));
|
||||
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName($1));
|
||||
|
||||
} else if (type == TYPE_ARRAY_TYPE) {
|
||||
printdebug("%sEntering array call", COLOR_LIGHTGREEN);
|
||||
if (getNumArrDim(look_up(getParent(cur), $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);
|
||||
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, getName($1))), $<integ>2, @2.first_line, @2.first_column);
|
||||
}
|
||||
$$ = getName(getArrType(look_up(getParent(cur), $1)));
|
||||
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", $$, $1);
|
||||
$$ = getArrType(look_up(getParent(cur), getName($1)));
|
||||
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName($$), getName($1));
|
||||
}
|
||||
cur = getParent(cur);
|
||||
}
|
||||
|
||||
| assignable rec_op ID
|
||||
{
|
||||
if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3)) {
|
||||
$$ = getName(table_lookup(getRecList(table_lookup(getAncestor(cur), $1)), $3));
|
||||
if(undefined != table_lookup(getRecList(table_lookup(getAncestor(cur), getName($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);
|
||||
}
|
||||
|
||||
;
|
||||
|
Reference in New Issue
Block a user