Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MiniMac is a virtual processor with a tiny but extendable memory and a tiny but extendable instruction set. The view panel on the right contains

MiniMac is a virtual processor with a tiny but extendable memory and a tiny but extendable instruction set. The view panel on the right contains two lists (javax.swing.JList). The top list shows the contents of memory. The bottom list shows the program currently being executed. The control panel on the left contains three buttons (javax.swing.JButton). The first button prompts the user for a program to parse. The second button executes the program. The third button resets all memory cells to 0.
These commands are duplicated under the Edit menu. The Help menu explains each command. The File menu contains the items: New, Save, Open, and Quit.
The MiniMax memory is an array of integers:
int size =32;
Integer[] memory = new Integer[size];
A MiniMax program is a list of instructions. An instruction pointer (ip) indicates the position in the list of the next instruction to be executed.
Grammar
Heres the instruction set grammar (note: ~ means followed by and all unquoted tokens are integers):
load ::=load ~ location ~ value // memory[location]= value
halt ::=halt// terminates the program
add ::=add ~ src1 ~ src2 ~ dest // memory[dest]= memory[src1]+ memory[src2]
mul ::=mul ~ src1 ~ src2 ~ dest // memory[dest]= memory[src1]* memory[src2]
bgt ::=bgt ~ location ~ offset // if 0< memory[location] ip += offset
blt ::=blt ~ location ~ offset // if memory[location]<0 ip += offset
loop ::=loop ~ count ~ instruction // executes instruction count times
A block is a list of one or more instructions separated by semicolons and bracketed by curly braces:
block ::={ ~ instruction ~ (; ~ instruction)* ~ }
Executing a block sequentially executes each instruction in the blocks body.
For example, the program shown in the screenshot computes kn, where n is stored in memory[0]. The base, k, is stored in memory[2]. In this case k =2, n was 7 and the result, 128, is stored in memory[1]. Memory[3] is the amount to decrement memory[0] each time the result is multiplied by k. Heres a partial implementation of MiniMacParser.java. Note that it is a utility class (aka singleton), all of its methods are static.
package minimac;
import java.util.*;
class ParserUtils {
protected static Integer parseInteger(Iterator it) throws Exception {
if (it.hasNext()){
return Integer.parseInt(it.next());
} else {
throw new Exception("Parsing error, integer expected");
}
}
}
public class MiniMacParser extends ParserUtils {
static public List parse(String program) throws Exception {
List result = new ArrayList();
// split program into a list of tokens (i.e. numbers and operations):
List tokens = Arrays.asList(program.split("\\s*;\\s*|\\s+"));
Iterator it = tokens.iterator();
while(it.hasNext()){
result.add(parseNext(it));
}
return result;
}
// dispatcher method:
static public Instruction parseNext(Iterator it) throws Exception {
String opcode = it.next();
if (opcode.equalsIgnoreCase("halt")) return new Halt();
if (opcode.equalsIgnoreCase("add")) return parseAdd(it);
if (opcode.equalsIgnoreCase("mul")) return parseMul(it);
if (opcode.equalsIgnoreCase("load")) return parseLoad(it);
if (opcode.equalsIgnoreCase("loop")) return parseLoop(it);
if (opcode.equalsIgnoreCase("Bgt")) return parseBgt(it);
if (opcode.equalsIgnoreCase("Blt")) return parseBlt(it);
if (opcode.equalsIgnoreCase("{")) return parseBlock(it);
if (opcode.equalsIgnoreCase("}")) return null; // used to indicate the end of a block
throw new Exception("Parsing error, unrecognized operator: "+ opcode);
}
static protected Add parseAdd(Iterator it) throws Exception {
int src1= parseInteger(it);
int src2= parseInteger(it);
int dest = parseInteger(it);
return new Add(src1, src2, dest);
}
// etc.
}
Please finish the implementation code.

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

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago