Skip to content

Commit

Permalink
#14 Makes Mnemonic an enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhoadley committed Apr 1, 2024
1 parent c82295c commit bc46eb6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/main/java/net/logicsquad/pal/Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Code {
*
* @see Mnemonic
*/
private String mnemonic;
private Mnemonic mnemonic;

/** The first argument to the instruction. */
private int first;
Expand Down Expand Up @@ -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;
Expand All @@ -58,7 +58,7 @@ public Code(String mnemonic, int first, Object second, int lineno) {
* @return A <code>String</code> containing the three letter mnemonic for
* this instruction.
*/
public String getMnemonic() {
public Mnemonic getMnemonic() {
return mnemonic;
}

Expand Down
175 changes: 96 additions & 79 deletions src/main/java/net/logicsquad/pal/Mnemonic.java
Original file line number Diff line number Diff line change
@@ -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 <code>int</code> and <code>String</code>
* representations.
* Represents the set of instruction mnemonics.
*
* @author Philip Roberts &lt;[email protected]&gt;
* @author Paul Hoadley &lt;[email protected]&gt;
*/
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 <code>CAL</code> mnemonic. */
public static final int CAL = 0;
/** Constant to represent the <code>INC</code> mnemonic. */
public static final int INC = 1;
/** Constant to represent the <code>JIF</code> mnemonic. */
public static final int JIF = 2;
/** Constant to represent the <code>JMP</code> mnemonic. */
public static final int JMP = 3;
/** Constant to represent the <code>LCI</code> mnemonic. */
public static final int LCI = 4;
/** Constant to represent the <code>LCR</code> mnemonic. */
public static final int LCR = 5;
/** Constant to represent the <code>LCS</code> mnemonic. */
public static final int LCS = 6;
/** Constant to represent the <code>LDA</code> mnemonic. */
public static final int LDA = 7;
/** Constant to represent the <code>LDI</code> mnemonic. */
public static final int LDI = 8;
/** Constant to represent the <code>LDU</code> mnemonic. */
public static final int LDU = 9;
/** Constant to represent the <code>LDV</code> mnemonic. */
public static final int LDV = 10;
/** Constant to represent the <code>MST</code> mnemonic. */
public static final int MST = 11;
/** Constant to represent the <code>OPR</code> mnemonic. */
public static final int OPR = 12;
/** Constant to represent the <code>RDI</code> mnemonic. */
public static final int RDI = 13;
/** Constant to represent the <code>RDR</code> mnemonic. */
public static final int RDR = 14;
/** Constant to represent the <code>REH</code> mnemonic. */
public static final int REH = 15;
/** Constant to represent the <code>SIG</code> mnemonic. */
public static final int SIG = 16;
/** Constant to represent the <code>STI</code> mnemonic. */
public static final int STI = 17;
/** Constant to represent the <code>STO</code> mnemonic. */
public static final int STO = 18;

/**
* Returns the <code>int</code> representing the supplied mnemonic.
*
* @param m
* A <code>String</code> containing the mnemonic.
* @return An <code>int</code> representing the supplied mnemonic.
*/
public static int mnemonicToInt(String m) {
return Arrays.binarySearch(mnemonicList, m);
}

/**
* Returns the <code>String</code> corresponding to the supplied
* <code>int</code> code.
*
* @param i
* An <code>int</code> 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 {
/**
* <code>CAL</code>
*/
CAL,

/**
* <code>INC</code>
*/
INC,

/**
* <code>JIF</code>
*/
JIF,

/**
* <code>JMP</code>
*/
JMP,

/**
* <code>LCI</code>
*/
LCI,

/**
* <code>LCR</code>
*/
LCR,

/**
* <code>LCS</code>
*/
LCS,

/**
* <code>LDA</code>
*/
LDA,

/**
* <code>LDI</code>
*/
LDI,

/**
* <code>LDU</code>
*/
LDU,

/**
* <code>LDV</code>
*/
LDV,

/**
* <code>MST</code>
*/
MST,

/**
* <code>OPR</code>
*/
OPR,

/**
* <code>RDI</code>
*/
RDI,

/**
* <code>RDR</code>
*/
RDR,

/**
* <code>REH</code>
*/
REH,

/**
* <code>SIG</code>
*/
SIG,

/**
* <code>STI</code>
*/
STI,

/**
* <code>STO</code>
*/
STO;
}
2 changes: 1 addition & 1 deletion src/main/java/net/logicsquad/pal/PAL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit bc46eb6

Please sign in to comment.