Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theauk committed Apr 27, 2022
1 parent 3589fb1 commit 1cb5c3f
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 196 deletions.
Binary file modified a.out
Binary file not shown.
28 changes: 10 additions & 18 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ program: start LCURLY declarationList compoundStmt RCURLY;
start:
typeSpecifier ID LPAREN params RPAREN
{
printf("START HELT\n");

insertProgramEntry($1, $2);
}
;
Expand Down Expand Up @@ -132,42 +132,34 @@ param:
}
;

compoundStmt: LCURLY statementList RCURLY { printf("CURLE\n"); };
compoundStmt: LCURLY statementList RCURLY;

statementList: statement statementList | /* epsilon */;

statement: assignmentStmt { printf("staAss\n"); }| compoundStmt { printf("staCom\n"); } | selectionStmt | iterationStmt | ioStmt ;

selectionStmt: ifStmt LPAREN boolExpression RPAREN statement %prec IF_LOWER { printf("SHORT\n"); stack = pop(stack); } | ifStmt LPAREN boolExpression RPAREN statement elseStm statement { printf("SHORT\n"); stack = pop(stack); };
statement: assignmentStmt | compoundStmt | selectionStmt | iterationStmt | ioStmt ;

ifStmt: IF {currentIf = 1; printf("IF\n");};
selectionStmt: ifStmt LPAREN boolExpression RPAREN statement %prec IF_LOWER { stack = pop(stack); } | ifStmt LPAREN boolExpression RPAREN statement ELSE statement { stack = pop(stack); };

elseStm: ELSE {printf("ELSE\n");};
ifStmt: IF {currentIf = 1; };

iterationStmt:
WHILE LPAREN boolExpression RPAREN statement
{
printf("WHILE\n");
{ stack = pop(stack); }
}
{ stack = pop(stack); }
;

assignmentStmt:
ID ASSIGNMENT additiveExpression
{
printf("ID ASSIGNMENT additiveExpression %s %f\n", $1, $3);
printf("K %d\n", stack.top);
if (peek(stack) == 1) {
printf("ASSIGN %s %f", $1, $3);
assignmentSimple($1, $3);
} /* Perform non-array assignment */
stack = pop(stack);
}
| ID LSQUARE additiveExpression RSQUARE ASSIGNMENT additiveExpression
{
printf("ID ASSIGNMENT additiveExpression RSQUARE ASSIGNMENT additiveExpression\n");

if (peek(stack) == 1) {
printf("ASSIGN %s %f", $1, $6);

assignmentArray($1, $3, $6);
} /* Perform array assignment */
stack = pop(stack);
Expand All @@ -181,15 +173,15 @@ boolExpression:
{
if (currentIf == 1 && peek(stack) == 0)
{
printf("NESTED BOOL: %d from, %f, %f\n", getBoolExp($2, $1, $3), $1, $3);

$$ = getBoolExp($2, $1, $3);
stack = push(stack, 0);
stack = push(stack, 0);
currentIf = 0;
}
else
{
printf("BOOL: %d from, %f, %f\n", getBoolExp($2, $1, $3), $1, $3);

$$ = getBoolExp($2, $1, $3);
stack = push(stack, $$ == 0);
stack = push(stack, $$);
Expand Down
18 changes: 9 additions & 9 deletions stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ struct Stack {

struct Stack initStack(struct Stack s) {
s.stack[0] = 1;
printf("\nINIT TOP INDEX %d\n", s.top);

for (int i = 0; i < s.top + 1; i++) {
printf("i: %d val: %d\n", i, s.stack[i]);

}
return s;
}

struct Stack pop(struct Stack s) {
printf("\nPOP TOP INDEX %d\n", s.top);

s.top = s.top - 1;
for (int i = 0; i < s.top + 1; i++) {
printf("i: %d val: %d\n", i, s.stack[i]);

}
return s;
}

int peek(struct Stack s) {
printf("\nPEEK TOP INDEX %d\n", s.top);
printf("PEEK VAL: %d\n", s.stack[s.top]);


for (int i = 0; i < s.top + 1; i++) {
printf("i: %d val: %d\n", i, s.stack[i]);

}
return s.stack[s.top];
}

struct Stack push(struct Stack s, int val) {
s.top = s.top + 1;
s.stack[s.top] = val;
printf("\nPUSH TOP INDEX %d\n", s.top);

for (int i = 0; i < s.top + 1; i++) {
printf("i: %d val: %d\n", i, s.stack[i]);

}
return s;
}
18 changes: 5 additions & 13 deletions test1.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
/* This is a program with nested conditionals */
/* Error: Variable not declared */
int test (void) {
int l;
int r;
int z[4];
{
z = 2
if(2 == 2)

if (5 != 5)
{r = 2}
else
if (8 < 9)
r = 1

else {l = 4}

l = 5 + 9
if (l > 0) {
l = l + 2
}
k = 2
}
}
18 changes: 18 additions & 0 deletions test5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* This is a program with nested conditionals */
int test (void) {
int l;
int r;
{
if(2 == 2)

if (5 != 5)
{r = 2}
else
if (8 < 9)
r = 1

else {l = 4}

l = 5 + 9
}
}
Loading

0 comments on commit 1cb5c3f

Please sign in to comment.