tweaking statement options
This commit is contained in:
@ -11,8 +11,8 @@ extern bool tc_flag;
|
||||
extern bool entry_flag;
|
||||
extern void insert_code_line(char *error_message, int line_number);
|
||||
extern bool contains_errors;
|
||||
|
||||
|
||||
extern int context;
|
||||
extern TableNode* comparator;
|
||||
typedef enum {
|
||||
ERROR_RUNTIME = 1,
|
||||
ERROR_SYNTAX = 2,
|
||||
|
117
src/grammar.y
117
src/grammar.y
@ -680,6 +680,14 @@ compound_statement:
|
||||
|
||||
simple_statement:
|
||||
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(FalseList, S_Init(), 0);
|
||||
} 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));
|
||||
}
|
||||
$$ = undefined;
|
||||
//resetting context
|
||||
context = 0;
|
||||
}
|
||||
|
||||
|
||||
@ -768,6 +778,7 @@ ablock:
|
||||
argument_list:
|
||||
expression{
|
||||
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(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));
|
||||
@ -793,6 +804,7 @@ argument_list:
|
||||
|
||||
{
|
||||
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(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));
|
||||
@ -1125,7 +1137,7 @@ expression:
|
||||
emit_reserve(node, tn_or_const(INTEGER, &v));
|
||||
$$ = node;
|
||||
}
|
||||
| RELEASE ID {
|
||||
/*| RELEASE ID {
|
||||
TableNode * n = look_up(cur, $2);
|
||||
if(getAdInfoType(n) != TYPE_RECORD){
|
||||
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);
|
||||
//emit release needed here
|
||||
$$ = node;
|
||||
}
|
||||
}*/
|
||||
| RESERVE ID {
|
||||
cur = CreateScope(cur, -2,-1);
|
||||
} ablock {
|
||||
@ -1159,50 +1171,8 @@ expression:
|
||||
$$=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 ID {
|
||||
cur = CreateScope(cur, -2,-1);
|
||||
} ablock {
|
||||
| RELEASE assignable{
|
||||
char* temp = temp_var_gen();
|
||||
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, integ, temp, NULL);
|
||||
int a = S_Size(S_Peek(stack)) + 1;
|
||||
@ -1217,49 +1187,11 @@ expression:
|
||||
$$=undefined;
|
||||
}
|
||||
//doing more complicated type checking in a block
|
||||
if(getNumArrDim(getTypeEntry(n)) != $4){
|
||||
throw_error(ERROR_SYNTAX, "expected %d dimensions for this array but got %d", getNumArrDim(getTypeEntry(n)), $4);
|
||||
$$=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;}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
//if(getNumArrDim(getTypeEntry(n)) != $4){
|
||||
// throw_error(ERROR_SYNTAX, "expected %d dimensions for this array but got %d", getNumArrDim(getTypeEntry(n)), $4);
|
||||
// $$=undefined;
|
||||
//}
|
||||
|
||||
}
|
||||
/*
|
||||
| RELEASE assignable
|
||||
@ -1300,11 +1232,18 @@ assignable:
|
||||
printdebug("%sBeginning rule 2 of assignable.", COLOR_CYAN);
|
||||
//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
|
||||
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);
|
||||
}
|
||||
}}
|
||||
//we have to consider emmissions in ablocks
|
||||
ablock
|
||||
{
|
||||
PopContext();
|
||||
//int type = getAdInfoType(look_up(getParent(cur), getName((TableNode*)$1)));
|
||||
int type = getAdInfoType(getTypeEntry((TableNode*)$1));
|
||||
printdebug("%stype is %d", COLOR_PURPLE, type);
|
||||
|
@ -57,6 +57,8 @@ FILE *cg_flag = NULL;
|
||||
bool tc_flag = false;
|
||||
bool DEBUG = 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 arg;
|
||||
bool contains_errors = false;
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include "symbol_table.h"
|
||||
|
||||
Constant_Stack *head = NULL;
|
||||
Context_stack *context_head = NULL;
|
||||
TableNode* comparator = NULL;
|
||||
int temp2_count = 0;
|
||||
int temp3_count = 0;
|
||||
|
||||
@ -53,6 +55,35 @@ Constant_Stack *Push(TableNode *type, void *value, bool isConst) {
|
||||
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() {
|
||||
if (head == NULL) {
|
||||
printf("cannot pop from an empty stack. Invalid.\n");
|
||||
|
@ -29,6 +29,11 @@ typedef struct Constant_Stack {
|
||||
bool isConst;
|
||||
} Constant_Stack;
|
||||
|
||||
typedef struct Context_stack {
|
||||
int con;
|
||||
struct Context_stack *next;
|
||||
} Context_stack;
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
} primitive_info;
|
||||
@ -107,7 +112,9 @@ void printdebug_impl(char *file, int line, const char *format, ...);
|
||||
char *temp_var_gen();
|
||||
char *arg_var_gen();
|
||||
Constant_Stack *Push(TableNode *type, void *value, bool isConst);
|
||||
Context_stack *PushContext(int context);
|
||||
Constant_Stack *Pop();
|
||||
int PopContext();
|
||||
Constant_Stack *Print_Stack();
|
||||
AdInfo *CreatePrimitiveInfo(int size);
|
||||
int getPrimSize(TableNode *definition);
|
||||
@ -184,6 +191,7 @@ extern TableNode *recprime;
|
||||
extern TableNode *funtypeprime;
|
||||
extern TableNode *undefined;
|
||||
extern Constant_Stack *head;
|
||||
extern Context_stack *context_head;
|
||||
|
||||
extern char *COLOR_RED;
|
||||
extern char *COLOR_GREEN;
|
||||
|
Reference in New Issue
Block a user