Skip to content

Commit

Permalink
The program works!
Browse files Browse the repository at this point in the history
all that is needed to do is format the output and turn it in.  I will do
that within a couple of hours.
  • Loading branch information
KevinDuarte committed Jul 11, 2015
1 parent 4a22af4 commit f7150c3
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 101 deletions.
13 changes: 10 additions & 3 deletions Driver/input.txt
Original file line number Diff line number Diff line change
@@ -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.
95 changes: 17 additions & 78 deletions Driver/parserwithcodegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();
}
Expand Down Expand Up @@ -241,12 +220,6 @@ void TERM()
printToFile(2,0,5);
lines++;
}
/*
OPR 0 4 (for mult)
OPR 0 5 (for div)
printToFile()
lines +=2;
*/
}
}

Expand All @@ -265,12 +238,6 @@ void EXPRESSION()
{
GETTOKEN();
}
/*
if(minussym)
OPR 0 1
printToFile()
lines++;
*/
}
TERM();
while(TOKEN == plussym || TOKEN == minussym)
Expand All @@ -289,12 +256,6 @@ lines++;
printToFile(2,0,3);
lines++;
}
/*
OPR 0 2 (for add)
OPR 0 3 (for sub)
printToFile()
lines +=2;
*/
}
}

Expand All @@ -306,11 +267,7 @@ void CONDITION()
EXPRESSION();
//OPR 0 6
printToFile(2,0,6);
/*
OPR 0 6
printToFile()
lines ++;
*/
lines++;
}
else
{
Expand All @@ -328,12 +285,7 @@ lines ++;

//OPR 0 M
printToFile(2,0,conditionValue);

/*
OPR 0 [conditionValue]
printToFile()
lines ++;
*/
lines++;
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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++;
}
}
Expand Down Expand Up @@ -571,11 +515,6 @@ void BLOCK()
}
GETTOKEN();
}
/*
INC 0 (4 + numOfVariables)
printToFile()
lines++
*/
//INC O (4+numOfVariables)
printToFile(6,0,4+numOfVariables);
lines++;
Expand Down
26 changes: 23 additions & 3 deletions Driver/pmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -75,6 +84,9 @@ int pmachinemain()
fprintf(ofp," \n ");
}

fclose(ifp);
fclose(ofp);

}

//Fetch Cycle
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -306,6 +322,7 @@ void printTrace(void)
{
int temp;
int i;
int current = 0;

stringSwitch(ir->op);

Expand All @@ -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,"| ");
}

}
}

Expand Down
5 changes: 5 additions & 0 deletions Driver/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//TEAM
//First team member name: Kevin Duarte
//Second team member name: Nestor Maysonet

//How to compile and run:
43 changes: 43 additions & 0 deletions Driver/sometestprograms.txt
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 6 additions & 5 deletions pmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand All @@ -82,7 +83,7 @@ int main(int argc, char** argv)
printTrace();
fprintf(ofp," \n ");
}

fclose(ifp);
fclose(ofp);

Expand Down
Loading

0 comments on commit f7150c3

Please sign in to comment.