Skip to content

Commit

Permalink
Fix array assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
theauk committed Apr 27, 2022
1 parent 1cb5c3f commit ba0516f
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 1,256 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.vscode
.idea
.idea
a.out.dSYM
a.out.dSYM/Contents/Info.plist
Binary file modified a.out
Binary file not shown.
40 changes: 29 additions & 11 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
void assignmentArray(char key[], float index, float val);
float getVar(char var[]);
void writeInput(char id[]);
int getLine();

struct SymbolTable s; // Symbol table structure from symbolTable.h
struct Stack stack;
bool currentIf;
int lastVarArrayIndex;
%}

// Types for tokens
Expand Down Expand Up @@ -166,7 +166,21 @@ assignmentStmt:
}
;

var: ID | ID LSQUARE additiveExpression RSQUARE;
var:
ID
| ID LSQUARE additiveExpression RSQUARE
{
if ((strcmp(getExpType($3), "float") == 0))
{
interpreterError("array subscript is not an integer", "");
}
else
{
lastVarArrayIndex = $3; // Store the specified index for value lookup
$$ = $1;
}
}
;

boolExpression:
additiveExpression relop additiveExpression
Expand Down Expand Up @@ -216,17 +230,18 @@ factor:
LPAREN additiveExpression RPAREN { $$ = $2;}
| var
{
printf("VAAR %s\n", $1);
$$ = getVar($1); /* Get the variable value from the symbol table */
}
| NUM { $$ = yylval.floating; } /* Get the NUM from the Lex scanner */
;

ioStmt:
WRITE ID ioWriteTail { writeInput($2); }
WRITE var ioWriteTail { writeInput($2); }
;

ioWriteTail:
COMMA ID ioWriteTail { writeInput($2); }
COMMA var ioWriteTail { writeInput($2); }
| /* epsilon */
;

Expand Down Expand Up @@ -303,7 +318,14 @@ float getVar(char var[]) {
if (containsIndex == -1) {
interpreterError("variable not declared", var);
}
return symbolTableGet(s, containsIndex).value;
if (isArray(s, var, containsIndex)) // Get the value at the specified array index
{
return symbolTableGet(s, containsIndex).array[lastVarArrayIndex];
}
else
{
return symbolTableGet(s, containsIndex).value;
}
}

/*
Expand Down Expand Up @@ -357,15 +379,11 @@ void assignmentArray(char key[], float index, float val) {
}
}

int getLine() {
return line;
}

/*
Prints out interpretation errors.
*/
void interpreterError(char error[], char val[]) {
printf("Ln %d: %s %s", line - 1, val, error);
printf("Ln %d: %s %s\n", line - 1, val, error);
exit(1); /* Terminates when encountering a semantic error */
}

Expand All @@ -376,7 +394,7 @@ void writeInput(char id[]) {
interpreterError("variable not declared", id);
} else {
struct Entry e = symbolTableGet(s, containsIndex);
printf("\nWrite for %s\n", id);
printf("\n- write var %s -\n", id);
if (e.isArray) {
printArray(id, e);
} else {
Expand Down
2 changes: 1 addition & 1 deletion stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct Stack initStack(struct Stack s) {

struct Stack pop(struct Stack s) {

s.top = s.top - 1;
if (s.top > 1) s.top = s.top - 1;
for (int i = 0; i < s.top + 1; i++) {

}
Expand Down
7 changes: 7 additions & 0 deletions symbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ int symbolTableContains(struct SymbolTable s, char k[]) {
}
}
return -1;
}

/*
Checks if an entry is an array.
*/
int isArray(struct SymbolTable s, char k[], int index) {
return s.values[index].isArray;
}
2 changes: 1 addition & 1 deletion test1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ int test (void) {
}
k = 2
}
}
}
29 changes: 6 additions & 23 deletions test2.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
/* A program with a missing '{' */

void main(void)
{
int x;
int y;
/* there is a missing { */
x = 2}
}

/* This is a program without syntax errors
but with lexical errors */
/* Error: Type mismatch */
int program (int u, int v){
float z;
int z;
float v[4];
int test;
int l;
float z[4];
int h;
{
l = 2
v[l] = 9
/* With a comment inside */
if ( test != 2) test = test + 2 * 4
else while (z != 7) v[1] = 1
write a,b;
z[2] = 4
write z[2]
h = z[2]
}
}

18 changes: 18 additions & 0 deletions test6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* This is a program without syntax errors
but with lexical errors */
int program (int u, int v){
float z;
int z;
float v[4];
int test;
int l;
{
l = 2
v[l] = 9
/* With a comment inside */
write test
if ( test != 2) test = test + 2 * 4

}
}

Loading

0 comments on commit ba0516f

Please sign in to comment.