diff --git a/src/main/java/net/logicsquad/pal/Code.java b/src/main/java/net/logicsquad/pal/Code.java index 55524c7..2df3332 100644 --- a/src/main/java/net/logicsquad/pal/Code.java +++ b/src/main/java/net/logicsquad/pal/Code.java @@ -12,7 +12,7 @@ public class Code { * * @see Mnemonic */ - private String mnemonic; + private Mnemonic mnemonic; /** The first argument to the instruction. */ private int first; @@ -45,7 +45,7 @@ public class Code { * originated. */ public Code(String mnemonic, int first, Object second, int lineno) { - this.mnemonic = mnemonic; + this.mnemonic = Mnemonic.valueOf(mnemonic); this.first = first; this.second = second; this.lineno = lineno; @@ -58,7 +58,7 @@ public Code(String mnemonic, int first, Object second, int lineno) { * @return A String containing the three letter mnemonic for * this instruction. */ - public String getMnemonic() { + public Mnemonic getMnemonic() { return mnemonic; } diff --git a/src/main/java/net/logicsquad/pal/Mnemonic.java b/src/main/java/net/logicsquad/pal/Mnemonic.java index c1e8ec3..b39313d 100644 --- a/src/main/java/net/logicsquad/pal/Mnemonic.java +++ b/src/main/java/net/logicsquad/pal/Mnemonic.java @@ -1,87 +1,104 @@ package net.logicsquad.pal; -import java.util.Arrays; - /** - * A class to define constant ints to represent mnemonics, and to provide - * conversion methods between int and String - * representations. + * Represents the set of instruction mnemonics. * * @author Philip Roberts <philip.roberts@gmail.com> * @author Paul Hoadley <paulh@logicsquad.net> */ -public class Mnemonic { - /** - * A lexicographically ordered array of the available mnemonics. - */ - private static final String[] mnemonicList = { "CAL", "INC", "JIF", "JMP", - "LCI", "LCR", "LCS", "LDA", "LDI", "LDU", "LDV", "MST", "OPR", - "RDI", "RDR", "REH", "SIG", "STI", "STO" }; - - // Important: The following constants must correspond to their - // index in the "mnemonicList" array. - - /** Constant to represent the CAL mnemonic. */ - public static final int CAL = 0; - /** Constant to represent the INC mnemonic. */ - public static final int INC = 1; - /** Constant to represent the JIF mnemonic. */ - public static final int JIF = 2; - /** Constant to represent the JMP mnemonic. */ - public static final int JMP = 3; - /** Constant to represent the LCI mnemonic. */ - public static final int LCI = 4; - /** Constant to represent the LCR mnemonic. */ - public static final int LCR = 5; - /** Constant to represent the LCS mnemonic. */ - public static final int LCS = 6; - /** Constant to represent the LDA mnemonic. */ - public static final int LDA = 7; - /** Constant to represent the LDI mnemonic. */ - public static final int LDI = 8; - /** Constant to represent the LDU mnemonic. */ - public static final int LDU = 9; - /** Constant to represent the LDV mnemonic. */ - public static final int LDV = 10; - /** Constant to represent the MST mnemonic. */ - public static final int MST = 11; - /** Constant to represent the OPR mnemonic. */ - public static final int OPR = 12; - /** Constant to represent the RDI mnemonic. */ - public static final int RDI = 13; - /** Constant to represent the RDR mnemonic. */ - public static final int RDR = 14; - /** Constant to represent the REH mnemonic. */ - public static final int REH = 15; - /** Constant to represent the SIG mnemonic. */ - public static final int SIG = 16; - /** Constant to represent the STI mnemonic. */ - public static final int STI = 17; - /** Constant to represent the STO mnemonic. */ - public static final int STO = 18; - - /** - * Returns the int representing the supplied mnemonic. - * - * @param m - * A String containing the mnemonic. - * @return An int representing the supplied mnemonic. - */ - public static int mnemonicToInt(String m) { - return Arrays.binarySearch(mnemonicList, m); - } - - /** - * Returns the String corresponding to the supplied - * int code. - * - * @param i - * An int representing a mnemonic. - * @return The corresponding mnemonic. - */ - public static String intToMnemonic(int i) { - if (i < mnemonicList.length && i >= 0) - return mnemonicList[i]; - return "XXX"; - } +public enum Mnemonic { + /** + * CAL + */ + CAL, + + /** + * INC + */ + INC, + + /** + * JIF + */ + JIF, + + /** + * JMP + */ + JMP, + + /** + * LCI + */ + LCI, + + /** + * LCR + */ + LCR, + + /** + * LCS + */ + LCS, + + /** + * LDA + */ + LDA, + + /** + * LDI + */ + LDI, + + /** + * LDU + */ + LDU, + + /** + * LDV + */ + LDV, + + /** + * MST + */ + MST, + + /** + * OPR + */ + OPR, + + /** + * RDI + */ + RDI, + + /** + * RDR + */ + RDR, + + /** + * REH + */ + REH, + + /** + * SIG + */ + SIG, + + /** + * STI + */ + STI, + + /** + * STO + */ + STO; } diff --git a/src/main/java/net/logicsquad/pal/PAL.java b/src/main/java/net/logicsquad/pal/PAL.java index fc3bb98..d76c2fc 100644 --- a/src/main/java/net/logicsquad/pal/PAL.java +++ b/src/main/java/net/logicsquad/pal/PAL.java @@ -212,7 +212,7 @@ ExitStatus execute() { Data tos, ntos, returnPoint, loadedVal; - switch (Mnemonic.mnemonicToInt(currInst.getMnemonic())) { + switch (currInst.getMnemonic()) { case Mnemonic.CAL: // Procedure/function call.