Skip to content

Commit

Permalink
Implemented read/write
Browse files Browse the repository at this point in the history
Tested the implementation, it seemed to work.
  • Loading branch information
KevinDuarte committed Jul 13, 2015
1 parent 5c5868b commit 125cc42
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
Binary file removed Driver2/Module3.zip
Binary file not shown.
9 changes: 5 additions & 4 deletions Driver2/input.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var a, b;
begin
a := 1;
b := 2;
if a > 2 then
b := a;
read a;
b := a + 1;
if a >= 2 then
b := b - a;
write b;
end.
54 changes: 54 additions & 0 deletions Driver2/parserwithcodegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,60 @@ void STATEMENT()
printToFile(7,0,jumpBackLine);
lines++;
}
else if(TOKEN == readsym)
{
GETTOKEN();
if(TOKEN != identsym)
{
ERROR("Error number 28, identifier expected after read.");
}

symbol current = getSymbol(IDENTIFIER);

if(current.kind != 2)
{
ERROR("Error number 29, writing to a constant or procedure is not allowed.");
}

//SIO 0 1
//STO 0 M
printToFile(9, 0, 1);
printToFile(4,current.level,current.addr);
lines += 2;

GETTOKEN();
}
else if(TOKEN == writesym)
{
GETTOKEN();
if(TOKEN != identsym)
{
ERROR("Error number 30, identifier expected after write.");
}

symbol current = getSymbol(IDENTIFIER);

if(current.kind == 3)
{
ERROR("Error number 31, cannot write a procedure.");
}

if(current.kind == 2)
{
//LOD L M
printToFile(3, current.level, current.addr);
}
if(current.kind == 1)
{
//LIT 0 val
printToFile(1, 0, current.val);
}
//SIO 0 0
printToFile(9, 0, 0);
lines +=2;

GETTOKEN();
}
}

void BLOCK()
Expand Down
4 changes: 2 additions & 2 deletions Driver2/pmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ void execute(void)
case 9: //SIO - pop & print, read & push, halt
if( ir->m == 0)
{
printf("\n%d\n",stack[sp]);
printf("Output: %d\n\n",stack[sp]);
sp--;
}else if(ir-> m == 1)
{
sp++;
printf("\nEnter value to push to stack: ");
printf("Enter value to push to stack: ");
scanf("%d",&stack[sp]);
printf("\n");
}else if(ir->m == 2)
Expand Down

0 comments on commit 125cc42

Please sign in to comment.