⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interpreter.java

📁 The triangle language processor will be consist of a compiler, an interpreter, and a disassembler
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      case Machine.addDisplacement:        ST = ST - 1;        accumulator = data[ST - 1];        data[ST - 1] = overflowChecked(accumulator + data[ST]);        break;      case Machine.subDisplacement:        ST = ST - 1;        accumulator = data[ST - 1];        data[ST - 1] = overflowChecked(accumulator - data[ST]);        break;      case Machine.multDisplacement:        ST = ST - 1;        accumulator = data[ST - 1];        data[ST - 1] = overflowChecked(accumulator * data[ST]);        break;      case Machine.divDisplacement:        ST = ST - 1;        accumulator = data[ST - 1];        if (data[ST] != 0)          data[ST - 1] = (int) (accumulator / data[ST]);        else          status = failedZeroDivide;        break;      case Machine.modDisplacement:        ST = ST - 1;        accumulator = data[ST - 1];        if (data[ST] != 0)          data[ST - 1] = (int) (accumulator % data[ST]);        else          status = failedZeroDivide;        break;      case Machine.ltDisplacement:        ST = ST - 1;        data[ST - 1] = toInt(data[ST - 1] < data[ST]);        break;      case Machine.leDisplacement:        ST = ST - 1;        data[ST - 1] = toInt(data[ST - 1] <= data[ST]);        break;      case Machine.geDisplacement:        ST = ST - 1;        data[ST - 1] = toInt(data[ST - 1] >= data[ST]);        break;      case Machine.gtDisplacement:        ST = ST - 1;        data[ST - 1] = toInt(data[ST - 1] > data[ST]);        break;      case Machine.eqDisplacement:        size = data[ST - 1]; // size of each comparand        ST = ST - 2 * size;        data[ST - 1] = toInt(equal(size, ST - 1, ST - 1 + size));        break;      case Machine.neDisplacement:        size = data[ST - 1]; // size of each comparand        ST = ST - 2 * size;        data[ST - 1] = toInt(! equal(size, ST - 1, ST - 1 + size));        break;      case Machine.eolDisplacement:        data[ST] = toInt(currentChar == '\n');        ST = ST + 1;        break;      case Machine.eofDisplacement:        data[ST] = toInt(currentChar == -1);        ST = ST + 1;        break;      case Machine.getDisplacement:        ST = ST - 1;        addr = data[ST];        try {          currentChar = System.in.read();        } catch (java.io.IOException s) {          status = failedIOError;        }        data[addr] = (int) currentChar;        break;      case Machine.putDisplacement:        ST = ST - 1;        ch = (char) data[ST];        System.out.print(ch);        break;      case Machine.geteolDisplacement:        try {          while ((currentChar = System.in.read()) != '\n');        } catch (java.io.IOException s) {          status = failedIOError;        }        break;      case Machine.puteolDisplacement:        System.out.println ("");        break;      case Machine.getintDisplacement:        ST = ST - 1;        addr = data[ST];        try {          accumulator = readInt();        } catch (java.io.IOException s) {          status = failedIOError;        }        data[addr] = (int) accumulator;        break;      case Machine.putintDisplacement:        ST = ST - 1;        accumulator = data[ST];        System.out.print(accumulator);        break;      case Machine.newDisplacement:        size = data[ST - 1];        checkSpace(size);        HT = HT - size;        data[ST - 1] = HT;        break;      case Machine.disposeDisplacement:        ST = ST - 1; // no action taken at present        break;    }  }  static void interpretProgram() {    // Runs the program in code store.    Instruction currentInstr;    int op, r, n, d, addr, index;    // Initialize registers ...    ST = SB;    HT = HB;    LB = SB;    CP = CB;    status = running;    do {      // Fetch instruction ...      currentInstr = Machine.code[CP];      // Decode instruction ...      op = currentInstr.op;      r = currentInstr.r;      n = currentInstr.n;      d = currentInstr.d;      // Execute instruction ...      switch (op) {        case Machine.LOADop:          addr = d + content(r);          checkSpace(n);          for (index = 0; index < n; index++)            data[ST + index] = data[addr + index];          ST = ST + n;          CP = CP + 1;          break;        case Machine.LOADAop:          addr = d + content(r);          checkSpace(1);          data[ST] = addr;          ST = ST + 1;          CP = CP + 1;          break;        case Machine.LOADIop:          ST = ST - 1;          addr = data[ST];          checkSpace(n);          for (index = 0; index < n; index++)            data[ST + index] = data[addr + index];          ST = ST + n;          CP = CP + 1;          break;        case Machine.LOADLop:          checkSpace(1);          data[ST] = d;          ST = ST + 1;          CP = CP + 1;          break;        case Machine.STOREop:          addr = d + content(r);          ST = ST - n;          for (index = 0; index < n; index++)            data[addr + index] = data[ST + index];          CP = CP + 1;          break;        case Machine.STOREIop:          ST = ST - 1;          addr = data[ST];          ST = ST - n;          for (index = 0; index < n; index++)            data[addr + index] = data[ST + index];          CP = CP + 1;          break;        case Machine.CALLop:          addr = d + content(r);          if (addr >= Machine.PB) {            callPrimitive(addr - Machine.PB);            CP = CP + 1;          } else {            checkSpace(3);            if ((0 <= n) && (n <= 15))              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;        case Machine.CALLIop:          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;        case Machine.RETURNop:          addr = LB - d;          CP = data[LB + 2];          LB = data[LB + 1];          ST = ST - n;          for (index = 0; index < n; index++)            data[addr + index] = data[ST + index];          ST = addr + n;          break;        case Machine.PUSHop:          checkSpace(d);          ST = ST + d;          CP = CP + 1;          break;        case Machine.POPop:          addr = ST - n - d;          ST = ST - n;          for (index = 0; index < n; index++)            data[addr + index] = data[ST + index];          ST = addr + n;          CP = CP + 1;          break;        case Machine.JUMPop:          CP = d + content(r);          break;        case Machine.JUMPIop:          ST = ST - 1;          CP = data[ST];          break;        case Machine.JUMPIFop:          ST = ST - 1;          if (data[ST] == n)            CP = d + content(r);          else            CP = CP + 1;          break;        case Machine.HALTop:          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();    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -