Answered step by step
Verified Expert Solution
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 javaxswing.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 javaxswing.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
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 ;
Integer memory new Integersize;
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 memorylocation value
halt ::halt terminates the program
add ::add ~ src ~ src ~ dest memorydest memorysrc memorysrc
mul ::mul ~ src ~ src ~ dest memorydest memorysrc memorysrc
bgt ::bgt ~ location ~ offset if memorylocation ip offset
blt ::blt ~ location ~ offset if memorylocation 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 The base, k is stored in memory In this case k n was and the result, is stored in memory Memory is the amount to decrement memory 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 parseIntegerIterator it throws Exception
if ithasNext
return Integer.parseIntitnext;
else
throw new ExceptionParsing error, integer expected";
public class MiniMacParser extends ParserUtils
static public List parseString program throws Exception
List result new ArrayList;
split program into a list of tokens ie numbers and operations:
List tokens Arrays.asListprogramsplits;ss;
Iterator it tokens.iterator;
whileithasNext
result.addparseNextit;
return result;
dispatcher method:
static public Instruction parseNextIterator it throws Exception
String opcode itnext;
if opcodeequalsIgnoreCasehalt return new Halt;
if opcodeequalsIgnoreCaseadd return parseAddit;
if opcodeequalsIgnoreCasemul return parseMulit;
if opcodeequalsIgnoreCaseload return parseLoadit;
if opcodeequalsIgnoreCaseloop return parseLoopit;
if opcodeequalsIgnoreCaseBgt return parseBgtit;
if opcodeequalsIgnoreCaseBlt return parseBltit;
if opcodeequalsIgnoreCase return parseBlockit;
if opcodeequalsIgnoreCase return null; used to indicate the end of a block
throw new ExceptionParsing error, unrecognized operator: opcode;
static protected Add parseAddIterator it throws Exception
int src parseIntegerit;
int src parseIntegerit;
int dest parseIntegerit;
return new Addsrc src dest;
etc.
Please finish the implementation code.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started