From f7150c3046400390667b034df736b4477621076b Mon Sep 17 00:00:00 2001 From: Kevin Duarte Date: Sat, 11 Jul 2015 15:53:30 -0400 Subject: [PATCH] The program works! all that is needed to do is format the output and turn it in. I will do that within a couple of hours. --- Driver/input.txt | 13 +++-- Driver/parserwithcodegen.c | 95 +++++++------------------------------ Driver/pmachine.c | 26 ++++++++-- Driver/readme.txt | 5 ++ Driver/sometestprograms.txt | 43 +++++++++++++++++ pmachine.c | 11 +++-- sometestprograms.txt | 18 +++---- 7 files changed, 110 insertions(+), 101 deletions(-) create mode 100644 Driver/readme.txt create mode 100644 Driver/sometestprograms.txt diff --git a/Driver/input.txt b/Driver/input.txt index d3a89bc..8bf5729 100644 --- a/Driver/input.txt +++ b/Driver/input.txt @@ -1,5 +1,12 @@ -var x, y; +const m = 1; +var a, b; begin -y := 3; /* This is a comment */ -x := y + 56; + a := 4 + m; /*5*/ + b := a + 2; /*7*/ + a := a * b; /*35*/ + while b < a do + begin + if odd b then + b := b + 4; + end /*not sure if the while end needs a '.' */ end. \ No newline at end of file diff --git a/Driver/parserwithcodegen.c b/Driver/parserwithcodegen.c index 5a732f2..7e1b0de 100644 --- a/Driver/parserwithcodegen.c +++ b/Driver/parserwithcodegen.c @@ -32,7 +32,6 @@ int numOfVariables = 0; //used for the branching in conditionals int inConditional = 0; -int jumpLine = 0; //counts the amount of lines of assembly that have been written int lines = 0; @@ -121,7 +120,7 @@ int isRelationalOperator(int token) symbol getSymbol(char identifier[]) { int i, found = 0; - for(i = 0; i < numOfVariables; i++) + for(i = 0; i < numOfSymbols; i++) { if(strcmp(symbol_table[i].name, identifier) == 0) return symbol_table[i]; @@ -155,18 +154,6 @@ void FACTOR() { if(TOKEN == identsym) { -/* -if getSymbol() == variable - possible ERROR(variable has not been initialized) - LOD getSymbol().L getSymbol().M - printToFile() - lines++; -else if getSymbol() == constant - INC 0 1 - LIT 0 getSymbol().val - printToFile() - lines += 2; -*/ symbol current = getSymbol(IDENTIFIER); if(current.kind == 2) { @@ -189,17 +176,9 @@ else if getSymbol() == constant } else if(TOKEN == numbersym) { -/* -INC 0 1 -LIT 0 NUMBER -printToFile() -lines += 2; -*/ - //INC 0 1 -- We don't need this because LIT increases sp by one already - //printToFile(1,0,1); //LIT 0 NUMBER printToFile(1,0,NUMBER); - lines += 2; + lines++; GETTOKEN(); } @@ -241,12 +220,6 @@ void TERM() printToFile(2,0,5); lines++; } - /* - OPR 0 4 (for mult) - OPR 0 5 (for div) - printToFile() - lines +=2; - */ } } @@ -265,12 +238,6 @@ void EXPRESSION() { GETTOKEN(); } -/* -if(minussym) -OPR 0 1 -printToFile() -lines++; -*/ } TERM(); while(TOKEN == plussym || TOKEN == minussym) @@ -289,12 +256,6 @@ lines++; printToFile(2,0,3); lines++; } -/* -OPR 0 2 (for add) -OPR 0 3 (for sub) -printToFile() -lines +=2; -*/ } } @@ -306,11 +267,7 @@ void CONDITION() EXPRESSION(); //OPR 0 6 printToFile(2,0,6); -/* -OPR 0 6 -printToFile() -lines ++; -*/ + lines++; } else { @@ -328,12 +285,7 @@ lines ++; //OPR 0 M printToFile(2,0,conditionValue); - -/* -OPR 0 [conditionValue] -printToFile() -lines ++; -*/ + lines++; } } @@ -364,11 +316,6 @@ void STATEMENT() //STO 0 M printToFile(4,current.level,current.addr); lines++; -/* -STO getSymbol(name).L getSymbol(name).M -printToFile() -lines++; -*/ } //procedure call (not in tiny PL/0) else if(TOKEN == callsym) @@ -434,18 +381,19 @@ lines++; //returns the file to the previous position fsetpos(ifp, &filePos); lines = currentLines; + //Lines increment for prinToFile used in for loop + //lines++; } - //the line for the branch is added - //lines++; + lines++; GETTOKEN(); STATEMENT(); } - //Lines increment for prinToFile used in for loop - lines++; + } else if(TOKEN == whilesym) { + int jumpBackLine = lines; GETTOKEN(); CONDITION(); //top of the stack has whether it is true or false @@ -467,29 +415,25 @@ lines++; if(i == 1) { inConditional--; -//make branch here (lines contains the line that you jump to if the condition is not met) +//make branch here (lines + 1 contains the line that you jump to if the condition is not met) //printToFile() - //JPC 0 M = lines - printToFile(8,0,lines); + //JPC 0 M = l + printToFile(8,0,lines + 1); //returns the file to the previous position fsetpos(ifp, &filePos); lines = currentLines; + //Lines increment for the printToFile used in for loop + //lines++; } //the line for the branch is added - //lines++; + lines++; GETTOKEN(); STATEMENT(); - -//Jump back to condition (currentLines is where you jump to) -//printToFile() -//lines++ } - //Lines increment for the printToFile used in for loop - lines++; - //JMP 0 M = currentLines - printToFile(7,0,currentLines); + //JMP 0 M = jumpBackLines + printToFile(7,0,jumpBackLine); lines++; } } @@ -571,11 +515,6 @@ void BLOCK() } GETTOKEN(); } -/* -INC 0 (4 + numOfVariables) -printToFile() -lines++ -*/ //INC O (4+numOfVariables) printToFile(6,0,4+numOfVariables); lines++; diff --git a/Driver/pmachine.c b/Driver/pmachine.c index 5296b60..22bfac8 100644 --- a/Driver/pmachine.c +++ b/Driver/pmachine.c @@ -27,6 +27,11 @@ instruction code[MAX_CODE_LENGTH]; int stack[MAX_STACK_HEIGHT]; //Current line of code int line = 0; +//Lexical level array +int lexi[3]; + +//Lexi array pointer +int lexilvl = 0; //CPU globals int bp = 1; //base of current AR int sp = 0; //top of stack @@ -53,6 +58,10 @@ int pmachinemain() stack[0] = 0; stack[1] = 0; stack[2] = 0; + lexi[0] = 0; + lexi[1] = 0; + lexi[2] = 0; + lexi[3] = 0; int count = codeStore(); @@ -75,6 +84,9 @@ int pmachinemain() fprintf(ofp," \n "); } + fclose(ifp); + fclose(ofp); + } //Fetch Cycle @@ -120,6 +132,9 @@ void execute(void) stack[sp + 4] = pc; //return address (RA) bp = sp + 1; pc = ir->m; + //For printing out correct stack + lexi[lexilvl] = sp; + lexilvl++; strcpy(opString,"cal"); break; case 6: //INC 0 M @@ -186,6 +201,7 @@ void oprSwitch(int m) switch(m) { case 0: //RET + lexilvl--; sp = bp - 1; pc = stack[sp + 4]; bp = stack[sp + 3]; @@ -306,6 +322,7 @@ void printTrace(void) { int temp; int i; + int current = 0; stringSwitch(ir->op); @@ -318,10 +335,13 @@ void printTrace(void) for(i = 1; i <= sp; i++) { + if(lexi[current] > 0 && i > lexi[current]) + { + fprintf(ofp,"|"); + current++; + } fprintf(ofp,"%d ",stack[i]); - if(sp >= 8 && i==6){ - fprintf(ofp,"| "); - } + } } diff --git a/Driver/readme.txt b/Driver/readme.txt new file mode 100644 index 0000000..d743446 --- /dev/null +++ b/Driver/readme.txt @@ -0,0 +1,5 @@ +//TEAM +//First team member name: Kevin Duarte +//Second team member name: Nestor Maysonet + +//How to compile and run: diff --git a/Driver/sometestprograms.txt b/Driver/sometestprograms.txt new file mode 100644 index 0000000..1de67f5 --- /dev/null +++ b/Driver/sometestprograms.txt @@ -0,0 +1,43 @@ +//Test for while + +var a; +begin + while a < 20 do + a := a + 1; +end. + +//Test for if +var a, b; +begin + a := 1; + b := 2; + if a > 2 then + b := a; +end. + +//addition with vars + +var a, b; +begin + a := 1; + b := 1; + a := b + 1; /* a = 2 */ + a := a + 1; /* a = 3 */ + b := a + 1; /* b = 4 */ + a := b + 1; /* a = 5 */ +end. + +//something more complicated + +const m = 1; +var a, b; +begin + a := 4 + m; /*5*/ + b := a + 2; /*6*/ + a := a * b; /*30*/ + while b < a do + begin + if odd b then + b := b + 4; + end /*not sure if the while end needs a '.' */ +end. diff --git a/pmachine.c b/pmachine.c index 7f12581..180f96f 100644 --- a/pmachine.c +++ b/pmachine.c @@ -29,10 +29,7 @@ int stack[MAX_STACK_HEIGHT]; int line = 0; //Lexical level array int lexi[3]; - lexi[0] = 0; - lexi[1] = 0; - lexi[2] = 0; - lexi[3] = 0; + //Lexi array pointer int lexilvl = 0; //CPU globals @@ -61,6 +58,10 @@ int main(int argc, char** argv) stack[0] = 0; stack[1] = 0; stack[2] = 0; + lexi[0] = 0; + lexi[1] = 0; + lexi[2] = 0; + lexi[3] = 0; int count = codeStore(); @@ -82,7 +83,7 @@ int main(int argc, char** argv) printTrace(); fprintf(ofp," \n "); } - + fclose(ifp); fclose(ofp); diff --git a/sometestprograms.txt b/sometestprograms.txt index 95907c4..2ea8a89 100644 --- a/sometestprograms.txt +++ b/sometestprograms.txt @@ -2,7 +2,7 @@ var a; begin - while a < 20 + while a < 20 do a := a + 1; end. @@ -13,14 +13,11 @@ begin b := 2 if a > 2 then b := a - else - a := b; end. //addition with vars -var a; -var b; +var a, b; begin a := 1; b := 1; @@ -31,18 +28,15 @@ begin end. //something more complicated - -var a, b; const m = 1; +var a, b; begin a := 4 + m; /*5*/ - b := a + m; /*6*/ - a := a * b; /*30*/ + b := a; /*5*/ + a := a * b; /*25*/ while b < a do begin if odd b then - b := b + 3; - else - b := b + m; + b := b + 10; end /*not sure if the while end needs a '.' */ end.