From 99dffaee01f9dad2edea4306d618e29e9e6b7ed5 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Thu, 1 May 2025 15:22:37 -0400 Subject: [PATCH] It worksgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit statusgit status! --- src/grammar.y | 44 +++++++++++------------------------------ src/intermediate_code.c | 8 ++++---- src/runner.c | 6 +++--- src/runner.h | 5 ++++- 4 files changed, 23 insertions(+), 40 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 8688a6b..3431a18 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -130,20 +130,11 @@ prototype: definition: TYPE ID COLON { - printdebug("Currently see a record definition for %s", $2); tn = CreateEntry(getAncestor(cur),TYPE_RECORD_TYPE, recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0))); printdebug("Created a new scope"); - //if (look_up(cur, $2) == undefined) { - // 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. + } dblock { setRecSize(look_up(getParent(cur), $2), getRecSize(cur)); - //putting in all the offsets setRecOffsetInfo(cur, look_up(getParent(cur),$2)); printdebug("Moving up a scope after seeing a record definition"); cur = getParent(cur); @@ -155,9 +146,7 @@ definition: CreateEntry(cur,TYPE_ARRAY_TYPE, arrayprim, $2, CreateArrayInfo($4, (TableNode*)$6)); printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, getName((TableNode*)$6)); } - | function_declaration - | 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, getName((TableNode*)$4), getName((TableNode*)$6)); @@ -655,7 +644,11 @@ rec_op: ablock: L_PAREN{ - emit_push_all(S_Pop(stack)); + if (stack == NULL){ + stack = S_Init(); + } + Stack * t = S_Init(); + S_Push(stack, t, 0); } argument_list { } R_PAREN @@ -675,13 +668,10 @@ argument_list: TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL); // this emits params for function and arrays TODO: fix Stack * t = S_Peek(stack); - if (stack == NULL){ - stack = S_Init(); - } - if(t == NULL){ - t = S_Init(); - S_Push(stack, t, 0); - } + if(t==NULL){ + t = S_Init(); + S_Push(stack, t, 1); + } emit_parameter(tn_or_const(NODE,arg)); S_Push(t, current, 1); emit_detach(); @@ -694,17 +684,7 @@ argument_list: { TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL); - Stack * t = S_Peek(stack); - if (stack == NULL){ - stack = S_Init(); - } - if(t == NULL){ - t = S_Init(); - S_Push(stack, t, 0); - } emit_parameter(tn_or_const(NODE,arg)); - S_Push(t, current, 1); - emit_detach(); $$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$); } @@ -940,7 +920,6 @@ assignable: $$ = pass; printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", getType(pass), getName(pass)); } - | assignable { printdebug("%sBeginning rule 2 of assignable.", COLOR_CYAN); @@ -1035,7 +1014,8 @@ assignable: int a = S_Size(S_Peek(stack)); } */ - emit_push_all(S_Pop(stack)); + emit_push_all(S_Peek(stack)); + S_Pop(stack); emit_function_call(node, a, tn_or_const(NODE, $1)); $$ = node; //NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK for function call) diff --git a/src/intermediate_code.c b/src/intermediate_code.c index 9074047..8f22de3 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -25,7 +25,7 @@ void S_Push(Stack * s, void *v, int i) { } void * S_Pop(Stack *s) { - if (s == NULL || !S_IsEmpty(s)) { + if (s == NULL || S_IsEmpty(s)) { return NULL; } __Node * node = s->n; @@ -38,14 +38,14 @@ void * S_Pop(Stack *s) { void * S_Peek(Stack *s){ - if (s == NULL || !S_IsEmpty(s)) { + if (s == NULL || S_IsEmpty(s)) { return NULL; } return s->n->v; } bool S_IsEmpty(Stack *s){ - if(!s) { + if(s == NULL || s->size == 0) { return true; } return false; @@ -69,7 +69,7 @@ char * temp = NULL; */ void emit_push_all(Stack * s){ - for (Instruction * i = S_Pop(s); s; i = S_Pop(s)){ + for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){ current->next = i; i->prev = current; current = i; diff --git a/src/runner.c b/src/runner.c index dd5ea33..b8f709a 100644 --- a/src/runner.c +++ b/src/runner.c @@ -102,9 +102,6 @@ void print_tok(int tok) { int run(FILE *alpha) { int token; top = cur = init(CreateScope(NULL, 1, 1)); - Stack *stack = S_Init(); - Stack *TrueList = S_Init(); - Stack *FalseList = S_Init(); // If file is not found @@ -130,6 +127,9 @@ int run(FILE *alpha) { fseek(alpha, 0, SEEK_SET); yyin = alpha; + stack = S_Init(); + TrueList = S_Init(); + FalseList = S_Init(); yyparse(); if (tok_flag != NULL) { diff --git a/src/runner.h b/src/runner.h index 9971eed..979c36b 100644 --- a/src/runner.h +++ b/src/runner.h @@ -70,6 +70,9 @@ TableNode *recprime; TableNode *funtypeprime; TableNode *undefined; extern Instruction *begin; +extern Stack* stack; +extern Stack* TrueList; +extern Stack* FalseList; int main(int argc, char *argv[]); int check_flag(char *arg, char *alpha); @@ -108,4 +111,4 @@ CodeLine *code_head; char *file_read_line(FILE *fp); void insert_code_line(char * error_message, int line_number); void append_code_line(CodeLine *code_line); -void print_code_lines(); \ No newline at end of file +void print_code_lines();