You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
625 lines
14 KiB
625 lines
14 KiB
3 years ago
|
/*
|
||
|
* @(#)Interpreter.java 2.1 2003/10/07
|
||
|
*
|
||
|
* Copyright (C) 1999, 2003 D.A. Watt and D.F. Brown
|
||
|
* Dept. of Computing Science, University of Glasgow, Glasgow G12 8QQ Scotland
|
||
|
* and School of Computer and Math Sciences, The Robert Gordon University,
|
||
|
* St. Andrew Street, Aberdeen AB25 1HG, Scotland.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This software is provided free for educational use only. It may
|
||
|
* not be used for commercial purposes without the prior written permission
|
||
|
* of the authors.
|
||
|
*/
|
||
|
|
||
3 years ago
|
package triangle.abstractMachine;
|
||
3 years ago
|
|
||
|
import java.io.DataInputStream;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.FileNotFoundException;
|
||
|
import java.io.IOException;
|
||
|
|
||
|
public class Interpreter {
|
||
|
|
||
3 years ago
|
static String objectName;
|
||
|
|
||
|
// DATA STORE
|
||
|
|
||
|
static int[] data = new int[1024];
|
||
|
|
||
|
// DATA STORE REGISTERS AND OTHER REGISTERS
|
||
|
|
||
|
final static int CB = 0, SB = 0, HB = 1024; // = upper bound of data array + 1
|
||
|
|
||
|
static int CT, CP, ST, HT, LB, status;
|
||
|
|
||
|
// status values
|
||
|
final static int running = 0, halted = 1, failedDataStoreFull = 2, failedInvalidCodeAddress = 3,
|
||
|
failedInvalidInstruction = 4, failedOverflow = 5, failedZeroDivide = 6, failedIOError = 7;
|
||
|
|
||
|
static long accumulator;
|
||
|
|
||
|
static int content(int r) {
|
||
3 years ago
|
var register = Register.values()[r];
|
||
|
return content(register);
|
||
|
}
|
||
|
|
||
|
static int content(Register r) {
|
||
3 years ago
|
// Returns the current content of register r,
|
||
|
// even if r is one of the pseudo-registers L1..L6.
|
||
|
|
||
|
switch (r) {
|
||
3 years ago
|
case CB:
|
||
3 years ago
|
return CB;
|
||
3 years ago
|
case CT:
|
||
3 years ago
|
return CT;
|
||
3 years ago
|
case PB:
|
||
3 years ago
|
return Machine.PB;
|
||
3 years ago
|
case PT:
|
||
3 years ago
|
return Machine.PT;
|
||
3 years ago
|
case SB:
|
||
3 years ago
|
return SB;
|
||
3 years ago
|
case ST:
|
||
3 years ago
|
return ST;
|
||
3 years ago
|
case HB:
|
||
3 years ago
|
return HB;
|
||
3 years ago
|
case HT:
|
||
3 years ago
|
return HT;
|
||
3 years ago
|
case LB:
|
||
3 years ago
|
return LB;
|
||
3 years ago
|
case L1:
|
||
3 years ago
|
return data[LB];
|
||
3 years ago
|
case L2:
|
||
3 years ago
|
return data[data[LB]];
|
||
3 years ago
|
case L3:
|
||
3 years ago
|
return data[data[data[LB]]];
|
||
3 years ago
|
case L4:
|
||
3 years ago
|
return data[data[data[data[LB]]]];
|
||
3 years ago
|
case L5:
|
||
3 years ago
|
return data[data[data[data[data[LB]]]]];
|
||
3 years ago
|
case L6:
|
||
3 years ago
|
return data[data[data[data[data[data[LB]]]]]];
|
||
3 years ago
|
case CP:
|
||
3 years ago
|
return CP;
|
||
|
default:
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// PROGRAM STATUS
|
||
|
|
||
|
static void dump() {
|
||
|
// Writes a summary of the machine state.
|
||
|
|
||
|
System.out.println("");
|
||
|
System.out.println("State of data store and registers:");
|
||
|
System.out.println("");
|
||
|
if (HT == HB)
|
||
|
System.out.println(" |--------| (heap is empty)");
|
||
|
else {
|
||
|
System.out.println(" HB-->");
|
||
|
System.out.println(" |--------|");
|
||
3 years ago
|
for (var addr = HB - 1; addr >= HT; addr--) {
|
||
3 years ago
|
System.out.print(addr + ":");
|
||
|
if (addr == HT)
|
||
|
System.out.print(" HT-->");
|
||
|
else
|
||
|
System.out.print(" ");
|
||
|
System.out.println("|" + data[addr] + "|");
|
||
|
}
|
||
|
System.out.println(" |--------|");
|
||
|
}
|
||
|
System.out.println(" |////////|");
|
||
|
System.out.println(" |////////|");
|
||
|
if (ST == SB)
|
||
|
System.out.println(" |--------| (stack is empty)");
|
||
|
else {
|
||
3 years ago
|
var dynamicLink = LB;
|
||
|
var staticLink = LB;
|
||
|
var localRegNum = Register.LB;
|
||
3 years ago
|
System.out.println(" ST--> |////////|");
|
||
|
System.out.println(" |--------|");
|
||
3 years ago
|
for (var addr = ST - 1; addr >= SB; addr--) {
|
||
3 years ago
|
System.out.print(addr + ":");
|
||
|
if (addr == SB)
|
||
|
System.out.print(" SB-->");
|
||
|
else if (addr == staticLink) {
|
||
|
switch (localRegNum) {
|
||
3 years ago
|
case LB:
|
||
3 years ago
|
System.out.print(" LB-->");
|
||
|
break;
|
||
3 years ago
|
case L1:
|
||
3 years ago
|
System.out.print(" L1-->");
|
||
|
break;
|
||
3 years ago
|
case L2:
|
||
3 years ago
|
System.out.print(" L2-->");
|
||
|
break;
|
||
3 years ago
|
case L3:
|
||
3 years ago
|
System.out.print(" L3-->");
|
||
|
break;
|
||
3 years ago
|
case L4:
|
||
3 years ago
|
System.out.print(" L4-->");
|
||
|
break;
|
||
3 years ago
|
case L5:
|
||
3 years ago
|
System.out.print(" L5-->");
|
||
|
break;
|
||
3 years ago
|
case L6:
|
||
3 years ago
|
System.out.print(" L6-->");
|
||
|
break;
|
||
3 years ago
|
default:
|
||
|
break;
|
||
3 years ago
|
}
|
||
|
staticLink = data[addr];
|
||
3 years ago
|
localRegNum = Register.values()[localRegNum.ordinal() + 1];
|
||
3 years ago
|
} else
|
||
|
System.out.print(" ");
|
||
|
if ((addr == dynamicLink) && (dynamicLink != SB))
|
||
|
System.out.print("|SL=" + data[addr] + "|");
|
||
|
else if ((addr == dynamicLink + 1) && (dynamicLink != SB))
|
||
|
System.out.print("|DL=" + data[addr] + "|");
|
||
|
else if ((addr == dynamicLink + 2) && (dynamicLink != SB))
|
||
|
System.out.print("|RA=" + data[addr] + "|");
|
||
|
else
|
||
|
System.out.print("|" + data[addr] + "|");
|
||
|
System.out.println("");
|
||
|
if (addr == dynamicLink) {
|
||
|
System.out.println(" |--------|");
|
||
|
dynamicLink = data[addr + 1];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
System.out.println("");
|
||
|
}
|
||
|
|
||
|
static void showStatus() {
|
||
|
// Writes an indication of whether and why the program has terminated.
|
||
|
System.out.println("");
|
||
|
switch (status) {
|
||
|
case running:
|
||
|
System.out.println("Program is running.");
|
||
|
break;
|
||
|
case halted:
|
||
|
System.out.println("Program has halted normally.");
|
||
|
break;
|
||
|
case failedDataStoreFull:
|
||
|
System.out.println("Program has failed due to exhaustion of Data Store.");
|
||
|
break;
|
||
|
case failedInvalidCodeAddress:
|
||
|
System.out.println("Program has failed due to an invalid code address.");
|
||
|
break;
|
||
|
case failedInvalidInstruction:
|
||
|
System.out.println("Program has failed due to an invalid instruction.");
|
||
|
break;
|
||
|
case failedOverflow:
|
||
|
System.out.println("Program has failed due to overflow.");
|
||
|
break;
|
||
|
case failedZeroDivide:
|
||
|
System.out.println("Program has failed due to division by zero.");
|
||
|
break;
|
||
|
case failedIOError:
|
||
|
System.out.println("Program has failed due to an IO error.");
|
||
|
break;
|
||
|
}
|
||
|
if (status != halted)
|
||
|
dump();
|
||
|
}
|
||
|
|
||
|
// INTERPRETATION
|
||
|
|
||
|
static void checkSpace(int spaceNeeded) {
|
||
|
// Signals failure if there is not enough space to expand the stack or
|
||
|
// heap by spaceNeeded.
|
||
|
|
||
|
if (HT - ST < spaceNeeded)
|
||
|
status = failedDataStoreFull;
|
||
|
}
|
||
|
|
||
|
static boolean isTrue(int datum) {
|
||
|
// Tests whether the given datum represents true.
|
||
|
return (datum == Machine.trueRep);
|
||
|
}
|
||
|
|
||
|
static boolean equal(int size, int addr1, int addr2) {
|
||
|
// Tests whether two multi-word objects are equal, given their common
|
||
|
// size and their base addresses.
|
||
|
|
||
|
boolean eq;
|
||
|
int index;
|
||
|
|
||
|
eq = true;
|
||
|
index = 0;
|
||
|
while (eq && (index < size))
|
||
|
if (data[addr1 + index] == data[addr2 + index])
|
||
|
index = index + 1;
|
||
|
else
|
||
|
eq = false;
|
||
|
return eq;
|
||
|
}
|
||
|
|
||
|
static int overflowChecked(long datum) {
|
||
|
// Signals failure if the datum is too large to fit into a single word,
|
||
|
// otherwise returns the datum as a single word.
|
||
|
|
||
|
if ((-Machine.maxintRep <= datum) && (datum <= Machine.maxintRep))
|
||
|
return (int) datum;
|
||
|
else {
|
||
|
status = failedOverflow;
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static int toInt(boolean b) {
|
||
|
return b ? Machine.trueRep : Machine.falseRep;
|
||
|
}
|
||
|
|
||
|
static int currentChar;
|
||
|
|
||
|
static int readInt() throws java.io.IOException {
|
||
|
int temp = 0;
|
||
|
int sign = 1;
|
||
|
|
||
|
do {
|
||
|
currentChar = System.in.read();
|
||
|
} while (Character.isWhitespace((char) currentChar));
|
||
|
|
||
|
if ((currentChar == '-') || (currentChar == '+'))
|
||
|
do {
|
||
|
sign = (currentChar == '-') ? -1 : 1;
|
||
|
currentChar = System.in.read();
|
||
|
} while ((currentChar == '-') || currentChar == '+');
|
||
|
|
||
|
if (Character.isDigit((char) currentChar))
|
||
|
do {
|
||
|
temp = temp * 10 + (currentChar - '0');
|
||
|
currentChar = System.in.read();
|
||
|
} while (Character.isDigit((char) currentChar));
|
||
|
|
||
|
return sign * temp;
|
||
|
}
|
||
|
|
||
|
static void callPrimitive(int primitiveDisplacement) {
|
||
|
// Invokes the given primitive routine.
|
||
|
|
||
|
int addr, size;
|
||
|
char ch;
|
||
|
|
||
3 years ago
|
var primitive = Primitive.values()[primitiveDisplacement];
|
||
|
switch (primitive) {
|
||
|
case ID:
|
||
3 years ago
|
break; // nothing to be done
|
||
3 years ago
|
case NOT:
|
||
3 years ago
|
data[ST - 1] = toInt(!isTrue(data[ST - 1]));
|
||
|
break;
|
||
3 years ago
|
case AND:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
data[ST - 1] = toInt(isTrue(data[ST - 1]) & isTrue(data[ST]));
|
||
|
break;
|
||
3 years ago
|
case OR:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
data[ST - 1] = toInt(isTrue(data[ST - 1]) | isTrue(data[ST]));
|
||
|
break;
|
||
3 years ago
|
case SUCC:
|
||
3 years ago
|
data[ST - 1] = overflowChecked(data[ST - 1] + 1);
|
||
|
break;
|
||
3 years ago
|
case PRED:
|
||
3 years ago
|
data[ST - 1] = overflowChecked(data[ST - 1] - 1);
|
||
|
break;
|
||
3 years ago
|
case NEG:
|
||
3 years ago
|
data[ST - 1] = -data[ST - 1];
|
||
|
break;
|
||
3 years ago
|
case ADD:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
accumulator = data[ST - 1];
|
||
|
data[ST - 1] = overflowChecked(accumulator + data[ST]);
|
||
|
break;
|
||
3 years ago
|
case SUB:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
accumulator = data[ST - 1];
|
||
|
data[ST - 1] = overflowChecked(accumulator - data[ST]);
|
||
|
break;
|
||
3 years ago
|
case MULT:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
accumulator = data[ST - 1];
|
||
|
data[ST - 1] = overflowChecked(accumulator * data[ST]);
|
||
|
break;
|
||
3 years ago
|
case DIV:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
accumulator = data[ST - 1];
|
||
|
if (data[ST] != 0)
|
||
|
data[ST - 1] = (int) (accumulator / data[ST]);
|
||
|
else
|
||
|
status = failedZeroDivide;
|
||
|
break;
|
||
3 years ago
|
case MOD:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
accumulator = data[ST - 1];
|
||
|
if (data[ST] != 0)
|
||
|
data[ST - 1] = (int) (accumulator % data[ST]);
|
||
|
else
|
||
|
status = failedZeroDivide;
|
||
|
break;
|
||
3 years ago
|
case LT:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
data[ST - 1] = toInt(data[ST - 1] < data[ST]);
|
||
|
break;
|
||
3 years ago
|
case LE:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
data[ST - 1] = toInt(data[ST - 1] <= data[ST]);
|
||
|
break;
|
||
3 years ago
|
case GE:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
data[ST - 1] = toInt(data[ST - 1] >= data[ST]);
|
||
|
break;
|
||
3 years ago
|
case GT:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
data[ST - 1] = toInt(data[ST - 1] > data[ST]);
|
||
|
break;
|
||
3 years ago
|
case EQ:
|
||
3 years ago
|
size = data[ST - 1]; // size of each comparand
|
||
|
ST = ST - 2 * size;
|
||
|
data[ST - 1] = toInt(equal(size, ST - 1, ST - 1 + size));
|
||
|
break;
|
||
3 years ago
|
case NE:
|
||
3 years ago
|
size = data[ST - 1]; // size of each comparand
|
||
|
ST = ST - 2 * size;
|
||
|
data[ST - 1] = toInt(!equal(size, ST - 1, ST - 1 + size));
|
||
|
break;
|
||
3 years ago
|
case EOL:
|
||
3 years ago
|
data[ST] = toInt(currentChar == '\n');
|
||
|
ST = ST + 1;
|
||
|
break;
|
||
3 years ago
|
case EOF:
|
||
3 years ago
|
data[ST] = toInt(currentChar == -1);
|
||
|
ST = ST + 1;
|
||
|
break;
|
||
3 years ago
|
case GET:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
addr = data[ST];
|
||
|
try {
|
||
|
currentChar = System.in.read();
|
||
|
} catch (java.io.IOException s) {
|
||
|
status = failedIOError;
|
||
|
}
|
||
|
data[addr] = currentChar;
|
||
|
break;
|
||
3 years ago
|
case PUT:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
ch = (char) data[ST];
|
||
|
System.out.print(ch);
|
||
|
break;
|
||
3 years ago
|
case GETEOL:
|
||
3 years ago
|
try {
|
||
|
while ((currentChar = System.in.read()) != '\n')
|
||
|
;
|
||
|
} catch (java.io.IOException s) {
|
||
|
status = failedIOError;
|
||
|
}
|
||
|
break;
|
||
3 years ago
|
case PUTEOL:
|
||
3 years ago
|
System.out.println("");
|
||
|
break;
|
||
3 years ago
|
case GETINT:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
addr = data[ST];
|
||
|
try {
|
||
|
accumulator = readInt();
|
||
|
} catch (java.io.IOException s) {
|
||
|
status = failedIOError;
|
||
|
}
|
||
|
data[addr] = (int) accumulator;
|
||
|
break;
|
||
3 years ago
|
case PUTINT:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
accumulator = data[ST];
|
||
|
System.out.print(accumulator);
|
||
|
break;
|
||
3 years ago
|
case NEW:
|
||
3 years ago
|
size = data[ST - 1];
|
||
|
checkSpace(size);
|
||
|
HT = HT - size;
|
||
|
data[ST - 1] = HT;
|
||
|
break;
|
||
3 years ago
|
case DISPOSE:
|
||
3 years ago
|
ST = ST - 1; // no action taken at present
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void interpretProgram() {
|
||
|
// Runs the program in code store.
|
||
|
|
||
|
Instruction currentInstr;
|
||
|
|
||
|
// Initialize registers ...
|
||
|
ST = SB;
|
||
|
HT = HB;
|
||
|
LB = SB;
|
||
|
CP = CB;
|
||
|
status = running;
|
||
|
do {
|
||
|
// Fetch instruction ...
|
||
|
currentInstr = Machine.code[CP];
|
||
|
// Decode instruction ...
|
||
3 years ago
|
var op = currentInstr.opCode;
|
||
|
var r = currentInstr.register;
|
||
|
var n = currentInstr.length;
|
||
|
var d = currentInstr.operand;
|
||
|
int addr;
|
||
|
|
||
3 years ago
|
// Execute instruction ...
|
||
|
switch (op) {
|
||
3 years ago
|
case LOAD:
|
||
3 years ago
|
addr = d + content(r);
|
||
|
checkSpace(n);
|
||
3 years ago
|
for (var index = 0; index < n; index++)
|
||
3 years ago
|
data[ST + index] = data[addr + index];
|
||
|
ST = ST + n;
|
||
|
CP = CP + 1;
|
||
|
break;
|
||
3 years ago
|
case LOADA:
|
||
3 years ago
|
addr = d + content(r);
|
||
|
checkSpace(1);
|
||
|
data[ST] = addr;
|
||
|
ST = ST + 1;
|
||
|
CP = CP + 1;
|
||
|
break;
|
||
3 years ago
|
case LOADI:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
addr = data[ST];
|
||
|
checkSpace(n);
|
||
3 years ago
|
for (var index = 0; index < n; index++)
|
||
3 years ago
|
data[ST + index] = data[addr + index];
|
||
|
ST = ST + n;
|
||
|
CP = CP + 1;
|
||
|
break;
|
||
3 years ago
|
case LOADL:
|
||
3 years ago
|
checkSpace(1);
|
||
|
data[ST] = d;
|
||
|
ST = ST + 1;
|
||
|
CP = CP + 1;
|
||
|
break;
|
||
3 years ago
|
case STORE:
|
||
3 years ago
|
addr = d + content(r);
|
||
|
ST = ST - n;
|
||
3 years ago
|
for (var index = 0; index < n; index++)
|
||
3 years ago
|
data[addr + index] = data[ST + index];
|
||
|
CP = CP + 1;
|
||
|
break;
|
||
3 years ago
|
case STOREI:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
addr = data[ST];
|
||
|
ST = ST - n;
|
||
3 years ago
|
for (var index = 0; index < n; index++)
|
||
3 years ago
|
data[addr + index] = data[ST + index];
|
||
|
CP = CP + 1;
|
||
|
break;
|
||
3 years ago
|
case CALL:
|
||
3 years ago
|
addr = d + content(r);
|
||
|
if (addr >= Machine.PB) {
|
||
|
callPrimitive(addr - Machine.PB);
|
||
|
CP = CP + 1;
|
||
|
} else {
|
||
|
checkSpace(3);
|
||
3 years ago
|
if (0 <= n && n <= 15)
|
||
3 years ago
|
data[ST] = content(n); // static link
|
||
|
else
|
||
|
status = failedInvalidInstruction;
|
||
|
data[ST + 1] = LB; // dynamic link
|
||
|
data[ST + 2] = CP + 1; // return address
|
||
|
LB = ST;
|
||
|
ST = ST + 3;
|
||
|
CP = addr;
|
||
|
}
|
||
|
break;
|
||
3 years ago
|
case CALLI:
|
||
3 years ago
|
ST = ST - 2;
|
||
|
addr = data[ST + 1];
|
||
|
if (addr >= Machine.PB) {
|
||
|
callPrimitive(addr - Machine.PB);
|
||
|
CP = CP + 1;
|
||
|
} else {
|
||
|
// data[ST] = static link already
|
||
|
data[ST + 1] = LB; // dynamic link
|
||
|
data[ST + 2] = CP + 1; // return address
|
||
|
LB = ST;
|
||
|
ST = ST + 3;
|
||
|
CP = addr;
|
||
|
}
|
||
|
break;
|
||
3 years ago
|
case RETURN:
|
||
3 years ago
|
addr = LB - d;
|
||
|
CP = data[LB + 2];
|
||
|
LB = data[LB + 1];
|
||
|
ST = ST - n;
|
||
3 years ago
|
for (var index = 0; index < n; index++)
|
||
3 years ago
|
data[addr + index] = data[ST + index];
|
||
|
ST = addr + n;
|
||
|
break;
|
||
3 years ago
|
case PUSH:
|
||
3 years ago
|
checkSpace(d);
|
||
|
ST = ST + d;
|
||
|
CP = CP + 1;
|
||
|
break;
|
||
3 years ago
|
case POP:
|
||
3 years ago
|
addr = ST - n - d;
|
||
|
ST = ST - n;
|
||
3 years ago
|
for (var index = 0; index < n; index++)
|
||
3 years ago
|
data[addr + index] = data[ST + index];
|
||
|
ST = addr + n;
|
||
|
CP = CP + 1;
|
||
|
break;
|
||
3 years ago
|
case JUMP:
|
||
3 years ago
|
CP = d + content(r);
|
||
|
break;
|
||
3 years ago
|
case JUMPI:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
CP = data[ST];
|
||
|
break;
|
||
3 years ago
|
case JUMPIF:
|
||
3 years ago
|
ST = ST - 1;
|
||
|
if (data[ST] == n)
|
||
|
CP = d + content(r);
|
||
|
else
|
||
|
CP = CP + 1;
|
||
|
break;
|
||
3 years ago
|
case HALT:
|
||
3 years ago
|
status = halted;
|
||
|
break;
|
||
|
}
|
||
|
if ((CP < CB) || (CP >= CT))
|
||
|
status = failedInvalidCodeAddress;
|
||
|
} while (status == running);
|
||
|
}
|
||
|
|
||
|
// LOADING
|
||
|
|
||
|
static void loadObjectProgram(String objectName) {
|
||
|
// Loads the TAM object program into code store from the named file.
|
||
|
|
||
|
FileInputStream objectFile = null;
|
||
|
DataInputStream objectStream = null;
|
||
|
|
||
|
int addr;
|
||
|
boolean finished = false;
|
||
|
|
||
|
try {
|
||
|
objectFile = new FileInputStream(objectName);
|
||
|
objectStream = new DataInputStream(objectFile);
|
||
|
|
||
|
addr = Machine.CB;
|
||
|
while (!finished) {
|
||
|
Machine.code[addr] = Instruction.read(objectStream);
|
||
|
if (Machine.code[addr] == null)
|
||
|
finished = true;
|
||
|
else
|
||
|
addr = addr + 1;
|
||
|
}
|
||
|
CT = addr;
|
||
|
objectFile.close();
|
||
|
} catch (FileNotFoundException s) {
|
||
|
CT = CB;
|
||
|
System.err.println("Error opening object file: " + s);
|
||
|
} catch (IOException s) {
|
||
|
CT = CB;
|
||
|
System.err.println("Error reading object file: " + s);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// RUNNING
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
System.out.println("********** TAM Interpreter (Java Version 2.1) **********");
|
||
|
|
||
|
if (args.length == 1)
|
||
|
objectName = args[0];
|
||
|
else
|
||
|
objectName = "obj.tam";
|
||
|
|
||
|
loadObjectProgram(objectName);
|
||
|
if (CT != CB) {
|
||
|
interpretProgram();
|
||
|
showStatus();
|
||
|
}
|
||
|
}
|
||
3 years ago
|
}
|