diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a41a741 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +GRM=yacc.y +LEX=lex.l +BIN=out + +CC=gcc +CFLAGS=-Wall -g + +OBJ=y.tab.o lex.yy.o table.o + +all: $(BIN) + @touch testFile # to prevent an error in case of deletion + ./out < testFile + + +%.o: %.c + $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ + +y.tab.c: $(GRM) + bison --yacc -d $< + +lex.yy.c: $(LEX) + flex $< + +$(BIN): $(OBJ) + $(CC) $(CFLAGS) $(CPPFLAGS) $^ -o $@ + +clean: + rm $(OBJ) y.tab.c y.tab.h lex.yy.c + diff --git a/table.c b/table.c index 07f7548..8f11416 100644 --- a/table.c +++ b/table.c @@ -42,6 +42,7 @@ Symbol getStruct(char* name){ } } error("No structure found"); + return (createNewStructure("error", 0)); } /* Returns the index with this name*/ @@ -52,6 +53,7 @@ int getIndex(char* name){ } } error("No index found"); + return (0); } /* removes all symbols associated with the current Depth*/ @@ -67,7 +69,6 @@ void clearOutOfScopeVariable(){ i++; } - int delta = currentIndex - i ; // the number of elements we remove int futureCurrentIndex = i; while(i < currentIndex) { @@ -156,7 +157,7 @@ void displayTable(){ printf("\n"); doubleLine(); printf("Table of Symbols, depth = %d, length = %d, ESP = %d, EBP = %d\n", currentDepth, currentIndex, esp ,ebp); - printf("Name | init?, varType, offset, depth\n", currentDepth, currentIndex); + printf("Name | init?, varType, offset, depth\n"); doubleLine(); for (int i = 0; i < currentIndex; ++i) { Symbol a = symbolTable[i]; @@ -173,22 +174,4 @@ void line(){ } void doubleLine(){ printf("============================================================\n"); -} - -int main(){ - initSymbolTable(); - addElement("variable1", INT); - displayTable(); - increaseDepth(); - displayTable(); - toggleInit("variable1"); - displayTable(); - addElement("variable2", INT); - displayTable(); - addElement("variable3", FLOAT); - displayTable(); - decreaseDepth(); - displayTable(); - addElement("variable4", INT); - displayTable(); } \ No newline at end of file diff --git a/table.h b/table.h index 554a363..4fb17c4 100644 --- a/table.h +++ b/table.h @@ -9,10 +9,10 @@ #define NAME_MAX_LENGTH 30 // a list of all type -typedef enum enumVarType {INT, FLOAT} enumVarType; // TODO : update +typedef enum enumVarType {INT, FLOAT} enumVarType; // a list of all type's sizes -int memorySizes[2] ={1,1}; // TODO : update +extern int memorySizes[2] = {1,1}; // TODO : PROBLEM DOES'NT COMPILE typedef struct { char name[NAME_MAX_LENGTH]; @@ -22,8 +22,6 @@ typedef struct { int depth; } Symbol; -// TODO : move comments here - /*============================ Array and Reallocation ============================*/ @@ -62,7 +60,7 @@ Symbol createNewStructure(char* name, enumVarType type); void toggleInit(char *name); /*============================ - Element Acess + Element Access ============================*/ /* Returns the index with this name*/ diff --git a/testFile b/testFile new file mode 100644 index 0000000..d3c3dba --- /dev/null +++ b/testFile @@ -0,0 +1,18 @@ +int compute(int a, int d) { + b = a; + while (c > 0) { + b = b + a * 4; + } + return b; +} + + +void main(void) { + int a; + if (a == 3) { + print(a); + } else { + int b = compute(a, 2 * a); + print(b); + } +}