tweaking statement options
This commit is contained in:
@ -11,8 +11,8 @@ extern bool tc_flag;
|
|||||||
extern bool entry_flag;
|
extern bool entry_flag;
|
||||||
extern void insert_code_line(char *error_message, int line_number);
|
extern void insert_code_line(char *error_message, int line_number);
|
||||||
extern bool contains_errors;
|
extern bool contains_errors;
|
||||||
|
extern int context;
|
||||||
|
extern TableNode* comparator;
|
||||||
typedef enum {
|
typedef enum {
|
||||||
ERROR_RUNTIME = 1,
|
ERROR_RUNTIME = 1,
|
||||||
ERROR_SYNTAX = 2,
|
ERROR_SYNTAX = 2,
|
||||||
|
117
src/grammar.y
117
src/grammar.y
@ -680,6 +680,14 @@ compound_statement:
|
|||||||
|
|
||||||
simple_statement:
|
simple_statement:
|
||||||
assignable{
|
assignable{
|
||||||
|
//updating context for reserve/release
|
||||||
|
if(getAdInfoType((TableNode*)$1) == TYPE_ARRAY){
|
||||||
|
context = 1;
|
||||||
|
}
|
||||||
|
if(getAdInfoType((TableNode*)$1) == TYPE_RECORD){
|
||||||
|
context = 2;
|
||||||
|
}
|
||||||
|
comparator = $1;
|
||||||
S_Push(TrueList, S_Init(), 0);
|
S_Push(TrueList, S_Init(), 0);
|
||||||
S_Push(FalseList, S_Init(), 0);
|
S_Push(FalseList, S_Init(), 0);
|
||||||
} ASSIGN expression
|
} ASSIGN expression
|
||||||
@ -721,6 +729,8 @@ simple_statement:
|
|||||||
throw_error(ERROR_TYPE, "Assignable Assign Expression - Object %s of type %s != Object %s of type %s", getName(node), getType(node), getName((TableNode*)$4), getType((TableNode*)$4));
|
throw_error(ERROR_TYPE, "Assignable Assign Expression - Object %s of type %s != Object %s of type %s", getName(node), getType(node), getName((TableNode*)$4), getType((TableNode*)$4));
|
||||||
}
|
}
|
||||||
$$ = undefined;
|
$$ = undefined;
|
||||||
|
//resetting context
|
||||||
|
context = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -768,6 +778,7 @@ ablock:
|
|||||||
argument_list:
|
argument_list:
|
||||||
expression{
|
expression{
|
||||||
TableNode * arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
|
TableNode * arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
|
||||||
|
//inside a scope of an array call if line number is -2
|
||||||
if(getLine(cur)==-2){
|
if(getLine(cur)==-2){
|
||||||
if(getTypeEntry(arg) != integ){
|
if(getTypeEntry(arg) != integ){
|
||||||
throw_error(ERROR_TYPE, "Argument %s of type %s is not of type integer for an array argument", getName(arg), getType(arg));
|
throw_error(ERROR_TYPE, "Argument %s of type %s is not of type integer for an array argument", getName(arg), getType(arg));
|
||||||
@ -793,6 +804,7 @@ argument_list:
|
|||||||
|
|
||||||
{
|
{
|
||||||
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
|
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
|
||||||
|
//inside a scope of an array call if line number is -2
|
||||||
if(getLine(cur)==-2){
|
if(getLine(cur)==-2){
|
||||||
if(getTypeEntry(arg) != integ){
|
if(getTypeEntry(arg) != integ){
|
||||||
throw_error(ERROR_TYPE, "Argument %s of type %s is not of type integer for an array argument", getName(arg), getType(arg));
|
throw_error(ERROR_TYPE, "Argument %s of type %s is not of type integer for an array argument", getName(arg), getType(arg));
|
||||||
@ -1125,7 +1137,7 @@ expression:
|
|||||||
emit_reserve(node, tn_or_const(INTEGER, &v));
|
emit_reserve(node, tn_or_const(INTEGER, &v));
|
||||||
$$ = node;
|
$$ = node;
|
||||||
}
|
}
|
||||||
| RELEASE ID {
|
/*| RELEASE ID {
|
||||||
TableNode * n = look_up(cur, $2);
|
TableNode * n = look_up(cur, $2);
|
||||||
if(getAdInfoType(n) != TYPE_RECORD){
|
if(getAdInfoType(n) != TYPE_RECORD){
|
||||||
throw_error(ERROR_TYPE, "Invalid Release expression with object %s of type %s.",
|
throw_error(ERROR_TYPE, "Invalid Release expression with object %s of type %s.",
|
||||||
@ -1136,7 +1148,7 @@ expression:
|
|||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
||||||
//emit release needed here
|
//emit release needed here
|
||||||
$$ = node;
|
$$ = node;
|
||||||
}
|
}*/
|
||||||
| RESERVE ID {
|
| RESERVE ID {
|
||||||
cur = CreateScope(cur, -2,-1);
|
cur = CreateScope(cur, -2,-1);
|
||||||
} ablock {
|
} ablock {
|
||||||
@ -1159,50 +1171,8 @@ expression:
|
|||||||
$$=undefined;
|
$$=undefined;
|
||||||
}
|
}
|
||||||
cur = getParent(cur);
|
cur = getParent(cur);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*TableNode * t = getFirstEntry(cur);
|
|
||||||
TableNode * n = look_up(cur, $2);
|
|
||||||
if(getAdInfoType(n) == TYPE_ARRAY){
|
|
||||||
int array_dims = getNumArrDim(getTypeEntry(n));
|
|
||||||
if ($5 != array_dims) {
|
|
||||||
throw_error(ERROR_SYNTAX, "expected %d dimensions for this array but got %d", array_dims, $5);
|
|
||||||
}else{
|
|
||||||
int traverse = 0;
|
|
||||||
while(t != NULL && t != undefined && getName(t)[0] != '&'){
|
|
||||||
t = getNextEntry(t);
|
|
||||||
}
|
|
||||||
if(getTypeEntry(t) != integ){
|
|
||||||
throw_error(ERROR_TYPE, "Arg for an array is not of type integer");
|
|
||||||
$$= undefined;
|
|
||||||
}else{
|
|
||||||
//seen first number
|
|
||||||
traverse++;
|
|
||||||
t = getNextEntry(t);
|
|
||||||
while(traverse<array_dims){
|
|
||||||
while(t !=NULL && t!=undefined && getName(t)[0]!='&'){
|
|
||||||
t = getNextEntry(t);
|
|
||||||
}
|
|
||||||
if(getTypeEntry(t) != integ){
|
|
||||||
throw_error(ERROR_TYPE, "Arg for an array is not of type integer");
|
|
||||||
$$= undefined;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
traverse++;
|
|
||||||
t = getNextEntry(t);
|
|
||||||
}
|
|
||||||
if(traverse != array_dims){
|
|
||||||
throw_error(ERROR_TYPE, "Invalid number of arguments for array %s. Expected %d but got %d", getName(n), array_dims, traverse);
|
|
||||||
$$= undefined;}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
| RELEASE ID {
|
| RELEASE assignable{
|
||||||
cur = CreateScope(cur, -2,-1);
|
|
||||||
} ablock {
|
|
||||||
char* temp = temp_var_gen();
|
char* temp = temp_var_gen();
|
||||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
||||||
int a = S_Size(S_Peek(stack)) + 1;
|
int a = S_Size(S_Peek(stack)) + 1;
|
||||||
@ -1217,49 +1187,11 @@ expression:
|
|||||||
$$=undefined;
|
$$=undefined;
|
||||||
}
|
}
|
||||||
//doing more complicated type checking in a block
|
//doing more complicated type checking in a block
|
||||||
if(getNumArrDim(getTypeEntry(n)) != $4){
|
//if(getNumArrDim(getTypeEntry(n)) != $4){
|
||||||
throw_error(ERROR_SYNTAX, "expected %d dimensions for this array but got %d", getNumArrDim(getTypeEntry(n)), $4);
|
// throw_error(ERROR_SYNTAX, "expected %d dimensions for this array but got %d", getNumArrDim(getTypeEntry(n)), $4);
|
||||||
$$=undefined;
|
// $$=undefined;
|
||||||
}
|
//}
|
||||||
cur = getParent(cur);
|
|
||||||
/*
|
|
||||||
TableNode * t = getFirstEntry(cur);
|
|
||||||
TableNode * n = look_up(cur, $2);
|
|
||||||
if(getAdInfoType(n) == TYPE_ARRAY){
|
|
||||||
int array_dims = getNumArrDim(getTypeEntry(n));
|
|
||||||
if ($5 != array_dims) {
|
|
||||||
throw_error(ERROR_SYNTAX, "expected %d dimensions for this array but got %d", array_dims, $5);
|
|
||||||
}else{
|
|
||||||
int traverse = 0;
|
|
||||||
while(t != NULL && t != undefined && getName(t)[0] != '&'){
|
|
||||||
t = getNextEntry(t);
|
|
||||||
}
|
|
||||||
if(getTypeEntry(t) != integ){
|
|
||||||
throw_error(ERROR_TYPE, "Arg for an array is not of type integer");
|
|
||||||
$$= undefined;
|
|
||||||
}else{
|
|
||||||
//seen first number
|
|
||||||
traverse++;
|
|
||||||
t = getNextEntry(t);
|
|
||||||
while(traverse<array_dims){
|
|
||||||
while(t !=NULL && t!=undefined && getName(t)[0]!='&'){
|
|
||||||
t = getNextEntry(t);
|
|
||||||
}
|
|
||||||
if(getTypeEntry(t) != integ){
|
|
||||||
throw_error(ERROR_TYPE, "Arg for an array is not of type integer");
|
|
||||||
$$= undefined;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
traverse++;
|
|
||||||
t = getNextEntry(t);
|
|
||||||
}
|
|
||||||
if(traverse != array_dims){
|
|
||||||
throw_error(ERROR_TYPE, "Invalid number of arguments for array %s. Expected %d but got %d", getName(n), array_dims, traverse);
|
|
||||||
$$= undefined;}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
| RELEASE assignable
|
| RELEASE assignable
|
||||||
@ -1300,11 +1232,18 @@ assignable:
|
|||||||
printdebug("%sBeginning rule 2 of assignable.", COLOR_CYAN);
|
printdebug("%sBeginning rule 2 of assignable.", COLOR_CYAN);
|
||||||
//Creating a dummy scope where we create entries for all the arguments of a function call
|
//Creating a dummy scope where we create entries for all the arguments of a function call
|
||||||
//Must also consider that we might be in an array access
|
//Must also consider that we might be in an array access
|
||||||
|
if(getAdInfoType((TableNode*)$1) == TYPE_ARRAY){
|
||||||
|
PushContext(3);
|
||||||
|
printdebug("%sCreating scope for array access", COLOR_CYAN);
|
||||||
|
//special scope for numbers for array access scope
|
||||||
|
cur = CreateScope(cur, -2,-1);
|
||||||
|
}else{
|
||||||
cur = CreateScope(cur, -1,-1);
|
cur = CreateScope(cur, -1,-1);
|
||||||
}
|
}}
|
||||||
//we have to consider emmissions in ablocks
|
//we have to consider emmissions in ablocks
|
||||||
ablock
|
ablock
|
||||||
{
|
{
|
||||||
|
PopContext();
|
||||||
//int type = getAdInfoType(look_up(getParent(cur), getName((TableNode*)$1)));
|
//int type = getAdInfoType(look_up(getParent(cur), getName((TableNode*)$1)));
|
||||||
int type = getAdInfoType(getTypeEntry((TableNode*)$1));
|
int type = getAdInfoType(getTypeEntry((TableNode*)$1));
|
||||||
printdebug("%stype is %d", COLOR_PURPLE, type);
|
printdebug("%stype is %d", COLOR_PURPLE, type);
|
||||||
|
@ -57,6 +57,8 @@ FILE *cg_flag = NULL;
|
|||||||
bool tc_flag = false;
|
bool tc_flag = false;
|
||||||
bool DEBUG = false;
|
bool DEBUG = false;
|
||||||
bool entry_flag = false;
|
bool entry_flag = false;
|
||||||
|
//1 is for default, 2 is in array context, 3 is in record context
|
||||||
|
int context = 1;
|
||||||
int no_flag = 0;
|
int no_flag = 0;
|
||||||
int arg;
|
int arg;
|
||||||
bool contains_errors = false;
|
bool contains_errors = false;
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
#include "symbol_table.h"
|
#include "symbol_table.h"
|
||||||
|
|
||||||
Constant_Stack *head = NULL;
|
Constant_Stack *head = NULL;
|
||||||
|
Context_stack *context_head = NULL;
|
||||||
|
TableNode* comparator = NULL;
|
||||||
int temp2_count = 0;
|
int temp2_count = 0;
|
||||||
int temp3_count = 0;
|
int temp3_count = 0;
|
||||||
|
|
||||||
@ -53,6 +55,35 @@ Constant_Stack *Push(TableNode *type, void *value, bool isConst) {
|
|||||||
return cs;
|
return cs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Context_stack *PushContext(int context) {
|
||||||
|
if (context != 1 && context != 2 && context != 3 && context != 0) {
|
||||||
|
printdebug(
|
||||||
|
"invalid context passed in");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Context_stack *cs = (Context_stack *)malloc(sizeof(Context_stack));
|
||||||
|
cs->con = context;
|
||||||
|
if (context_head == NULL) {
|
||||||
|
context_head = cs;
|
||||||
|
cs->next = NULL;
|
||||||
|
} else {
|
||||||
|
cs->next = context_head;
|
||||||
|
context_head = cs;
|
||||||
|
}
|
||||||
|
return cs;
|
||||||
|
}
|
||||||
|
|
||||||
|
int PopContext() {
|
||||||
|
if (context_head == NULL) {
|
||||||
|
printf("cannot pop from an empty stack. Invalid.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
Context_stack *cs = context_head;
|
||||||
|
context_head = context_head->next;
|
||||||
|
printf("Popped context off stack: number %d\n", cs->con);
|
||||||
|
return cs->con;
|
||||||
|
}
|
||||||
|
|
||||||
Constant_Stack *Pop() {
|
Constant_Stack *Pop() {
|
||||||
if (head == NULL) {
|
if (head == NULL) {
|
||||||
printf("cannot pop from an empty stack. Invalid.\n");
|
printf("cannot pop from an empty stack. Invalid.\n");
|
||||||
|
@ -29,6 +29,11 @@ typedef struct Constant_Stack {
|
|||||||
bool isConst;
|
bool isConst;
|
||||||
} Constant_Stack;
|
} Constant_Stack;
|
||||||
|
|
||||||
|
typedef struct Context_stack {
|
||||||
|
int con;
|
||||||
|
struct Context_stack *next;
|
||||||
|
} Context_stack;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int size;
|
int size;
|
||||||
} primitive_info;
|
} primitive_info;
|
||||||
@ -107,7 +112,9 @@ void printdebug_impl(char *file, int line, const char *format, ...);
|
|||||||
char *temp_var_gen();
|
char *temp_var_gen();
|
||||||
char *arg_var_gen();
|
char *arg_var_gen();
|
||||||
Constant_Stack *Push(TableNode *type, void *value, bool isConst);
|
Constant_Stack *Push(TableNode *type, void *value, bool isConst);
|
||||||
|
Context_stack *PushContext(int context);
|
||||||
Constant_Stack *Pop();
|
Constant_Stack *Pop();
|
||||||
|
int PopContext();
|
||||||
Constant_Stack *Print_Stack();
|
Constant_Stack *Print_Stack();
|
||||||
AdInfo *CreatePrimitiveInfo(int size);
|
AdInfo *CreatePrimitiveInfo(int size);
|
||||||
int getPrimSize(TableNode *definition);
|
int getPrimSize(TableNode *definition);
|
||||||
@ -184,6 +191,7 @@ extern TableNode *recprime;
|
|||||||
extern TableNode *funtypeprime;
|
extern TableNode *funtypeprime;
|
||||||
extern TableNode *undefined;
|
extern TableNode *undefined;
|
||||||
extern Constant_Stack *head;
|
extern Constant_Stack *head;
|
||||||
|
extern Context_stack *context_head;
|
||||||
|
|
||||||
extern char *COLOR_RED;
|
extern char *COLOR_RED;
|
||||||
extern char *COLOR_GREEN;
|
extern char *COLOR_GREEN;
|
||||||
|
Reference in New Issue
Block a user