Question: [In JAVA] Implement an Interpreter for the mock language X Interpreter.java package interpreter; import java.io.*; /** * * Interpreter class runs the interpreter: * 1.
[In JAVA] Implement an Interpreter for the mock language X
![[In JAVA] Implement an Interpreter for the mock language X Interpreter.java package](https://s3.amazonaws.com/si.experts.images/answers/2024/08/66b0956d83abd_62166b0956d20a84.jpg)



Interpreter.java package interpreter; import java.io.*; /** *
* Interpreter class runs the interpreter: * 1. Perform all initializations * 2. Load the bytecodes from file * 3. Run the virtual machine *
*/ public class Interpreter { private ByteCodeLoader bcl; public Interpreter(String codeFile) { try { CodeTable.init(); bcl = new ByteCodeLoader(codeFile); } catch (IOException e) { System.out.println("**** " + e); } } void run() { Program program = bcl.loadCodes(); VirtualMachine vm = new VirtualMachine(program); vm.executeProgram(); } public static void main(String args[]) { if (args.length == 0) { System.out.println("***Incorrect usage, try: java interpreter.Interpreter ByteCodeLoader.java package interpreter; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ByteCodeLoader extends Object { private BufferedReader byteSource; private Program program; public ByteCodeLoader(String file) throws IOException { this.byteSource = new BufferedReader(new FileReader(file)); } /** * This function should read one line of source code at a time. * For each line it should: * Tokenize string to break it into parts. * Grab correct class name for the given ByteCode from CodeTable * Create an instance of the ByteCode class name returned from code table. * Parse any additional arguments for the given ByteCode and send them to * the newly created ByteCode instance via the init function. */ public Program loadCodes() { return null; } } CodeTable.java /** * Code table of byte codes in language X * @key name of a specific byte code * @value name of the class that the key belongs to. * returns Class name as a string. */ package interpreter; import java.util.HashMap; public class CodeTable { private static HashMap /** * A method to facilitate the retrieval of the names * of a specific byte code class. * @param key for byte code. * @return class name of desired byte code. */ public static String getClassName(String key){ return codeTable.get(key); } } Program.java package interpreter; import java.util.ArrayList; public class Program { private ArrayList public Program() { program = new ArrayList(); } protected ByteCode getCode(int pc) { return this.program.get(pc); } public int getSize() { return this.program.size(); } /** * This function should go through the program and resolve all addresses. * Currently all labels look like LABEL >>, these need to be converted into * correct addresses so the VirtualMachine knows what to set the Program Counter(PC) * HINT: make note what type of data-stucture bytecodes are stored in. * * @param program Program object that holds a list of ByteCodes */ public void resolveAddrs(Program program) { } } RunTimeStack.java package interpreter; import java.util.ArrayList; import java.util.Stack; public class RunTimeStack { private ArrayList runTimeStack; private Stack public RunTimeStack() { runTimeStack = new ArrayList(); framePointer = new Stack(); //Add initial Frame Pointer, main is the entry // point of our language, so its frame pointer is 0. framePointer.add(0); } } VirtualMachine.java package interpreter; import java.util.Stack; public class VirtualMachine { private RunTimeStack runStack; private Stack returnAddrs; private Program program; private int pc; private boolean isRunning; protected VirtualMachine(Program program) { this.program = program; } } SOURCE FILES: factorial.x.cod GOTO start> LABEL Read READ RETURN LABEL Write LOAD 0 dummyFormal WRITE RETURN LABEL start> GOTO continue> LABEL factorial> LOAD 0 n LIT 2 BOP > LIT 1 RETURN factorial> POP 0 GOTO continue> LABEL else> LOAD 0 n LOAD 0 n LIT 1 BOP - ARGS 1 CALL factorial> BOP * RETURN factorial> POP 0 LABEL continue> POP 0 LIT 0 GRATIS-RETURN-VALUE RETURN factorial> LABEL continue> ARGS 0 CALL Read ARGS 1 CALL factorial> ARGS 1 CALL Write POP 3 HALT
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
