Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[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

image text in transcribed

image text in transcribedimage text in transcribed

image text in transcribed

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 "); System.exit(1); } (new Interpreter(args[0])).run(); } }

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 codeTable; private CodeTable(){} public static void init(){ codeTable = new HashMap(); codeTable.put("HALT", "HaltCode"); codeTable.put("POP", "PopCode"); codeTable.put("FALSEBRANCH", "FalseBranchCode"); codeTable.put("GOTO", "GotoCode"); codeTable.put("STORE", "StoreCode"); codeTable.put("LOAD", "LoadCode"); codeTable.put("LIT", "LitCode"); codeTable.put("ARGS", "ArgsCode"); codeTable.put("CALL", "CallCode"); codeTable.put("RETURN", "ReturnCode"); codeTable.put("BOP", "BopCode"); codeTable.put("READ", "ReadCode"); codeTable.put("WRITE", "WriteCode"); codeTable.put("LABEL", "LabelCode"); codeTable.put("DUMP", "DumpCode"); }

/** * 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 program;

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 framePointer;

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

1lt All. t.he Hyt.eCode claasesst.ed he table on pag 129 or page b ot this document. Be sure to create the correct abstractions tor the byt.ecods. is possible to have mut.iple abst.ract.ions within t.he set ot byte codes classes 2. Complete the impleentation of the following classes a.ByteCodeLoader b. Program c.RuntimeStack d.Virtual Machine The Interpreter and CodeTable class have already been implemented for you. The Interpreter class is the entry point to this project. A1l projects will be graded using this entry point. 1lt All. t.he Hyt.eCode claasesst.ed he table on pag 129 or page b ot this document. Be sure to create the correct abstractions tor the byt.ecods. is possible to have mut.iple abst.ract.ions within t.he set ot byte codes classes 2. Complete the impleentation of the following classes a.ByteCodeLoader b. Program c.RuntimeStack d.Virtual Machine The Interpreter and CodeTable class have already been implemented for you. The Interpreter class is the entry point to this project. A1l projects will be graded using this entry point

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

Consider some type of redress for the customer, such as a coupon.

Answered: 1 week ago

Question

Sell the quality of your brand or products.

Answered: 1 week ago