Compilador Java Simpletron Curso Ciência da Computação Prof. Chau S. Shia (Noboru) import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import javax.swing.*; public class Simpletron extends JApplet implements ActionListener { // Define 100 slot memory and the accumulator int instruction_count = 0; int current_instruction = 0; boolean has_halted = false; double accumulator = 0.0; double memory[] = new double[100]; String instructions[] = new String[100]; // Define all optcodes final int READ = 10; final int WRITE = 11; final int LOAD = 20; final int STORE = 21; final int ADD = 30; final int SUBTRACT = 31; final int MULTIPLY = 33; final int DIVIDE = 32; final int HALT = 43; // Define all GUI components JButton addButton, nextButton, executeButton, doneButton; JTextArea codeArea; public void init() { // Get applets content pane Container container = getContentPane(); container.setLayout(new FlowLayout()); // Setup user interface buttons addButton = new JButton("Add"); addButton.addActionListener(this); container.add(addButton); nextButton = new JButton("Next"); nextButton.setEnabled(false); nextButton.addActionListener(this); container.add(nextButton); executeButton = new JButton("Execute"); executeButton.setEnabled(false); executeButton.addActionListener(this); container.add(executeButton); Compilador Java Simpletron Curso Ciência da Computação Prof. Chau S. Shia (Noboru) doneButton = new JButton("Done"); doneButton.addActionListener(this); container.add(doneButton); // Setup the code/output area codeArea = new JTextArea(20, 30); codeArea.setText("*** Welcome to Simpletron ***\n" + "*** Please enter your program one instruction\t***\n" + "*** at a time by clicking the Add button. When\t***\n" + "*** you are finished, click Done and then Exe-\t***\n" + "*** cute. You may click the Next button to st-\t***\n" + "*** ep through your program.\t\t***\n\n"); codeArea.setEditable(false); // Setup scroll bar for the code area JScrollPane scrollPane = new JScrollPane(codeArea); container.add(scrollPane); } public void actionPerformed(ActionEvent event) { // Determine which button was clicked and // perform an action accordingly if (event.getSource() == addButton) { // Display an input dialog to let the user input // an instruction and append to the instructions list String instruction = JOptionPane.showInputDialog("Enter an Instruction:"); // Check for valid user input if (instruction.length() == 0 || instruction.length() < 4 || instruction.length() > 4) { JOptionPane.showMessageDialog(null, "Your instruction was invalid.", "Failed", JOptionPane.ERROR_MESSAGE); return; } else instructions[instruction_count] = instruction; // Append the instruction to the code area codeArea.append(formatNumber(instruction_count) + " ? +" + instruction + "\n"); instruction_count++; } else if (event.getSource() == nextButton) { // Take the current instruction and send it // through the parser, then increment Compilador Java Simpletron Curso Ciência da Computação Prof. Chau S. Shia (Noboru) if (current_instruction <= instruction_count) { parse(instructions[current_instruction]); current_instruction++; } else { // Since the user has not stopped the program // and there are no more instructions to execute // we will send a halt for them parse("4300"); } } else if (event.getSource() == executeButton) { // Execute the first instruction and change the // buttons properties nextButton.setEnabled(true); executeButton.setText("Executing..."); executeButton.setEnabled(false); codeArea.append("\n*** Executing Program ***\n\n"); parse(instructions[current_instruction]); current_instruction++; } else // if getSource is doneButton { // Enable and disable buttons accordingly executeButton.setEnabled(true); addButton.setEnabled(false); doneButton.setEnabled(false); } } public String formatNumber(int number) { if (number < 10) return "0" + number; else return Integer.toString(number); } public void parse(String instruction) { // Split instruction up into two parts int optcode = Integer.parseInt(instruction.substring(0, 2)); int operand = Integer.parseInt(instruction.substring(2, 4)); // Figure out the optcode and perform the specified action switch (optcode) { Compilador Java Simpletron Curso Ciência da Computação Prof. Chau S. Shia (Noboru) case READ: // Read integer input from user and store at memory location memory[operand] = Double.parseDouble(JOptionPane.showInputDialog("Enter an integer:")); codeArea.append("Instruction " + formatNumber(current_instruction) + ": READ " + formatNumber(operand) + "\n"); break; case WRITE: // Display the value of the specified memory location JOptionPane.showMessageDialog(null, "Result: " + memory[operand], "Result", JOptionPane.PLAIN_MESSAGE); codeArea.append("Instruction " + formatNumber(current_instruction) + ": WRITE " + formatNumber(operand) + "\n"); break; case LOAD: // Load the data from the specified memory location into the accumulator accumulator = memory[operand]; codeArea.append("Instruction " + formatNumber(current_instruction) + ": LOAD " + formatNumber(operand) + "\n"); break; case STORE: // Store the value of the accumulator in the specified memory location memory[operand] = accumulator; codeArea.append("Instruction " + formatNumber(current_instruction) + ": STORE " + formatNumber(operand) + "\n"); break; case ADD: // Add the accumulator to the specified memory location and store the // result back in the accumulator accumulator += memory[operand]; codeArea.append("Instruction " + formatNumber(current_instruction) + ": ADD " + formatNumber(operand) + "\n"); break; case SUBTRACT: // Subtract the accumulator to the specified memory location and store the // result back in the accumulator accumulator -= memory[operand]; codeArea.append("Instruction " + formatNumber(current_instruction) + ": SUBTRACT " + formatNumber(operand) + "\n"); break; case MULTIPLY: // Multiply the accumulator to the specified memory location and store the // result back in the accumulator accumulator *= memory[operand]; codeArea.append("Instruction " + formatNumber(current_instruction) + ": MULTIPLY " + formatNumber(operand) + "\n"); break; case DIVIDE: // Check to see if the user tried to divide by zero if (accumulator == 0 || memory[operand] == 0) Compilador Java Simpletron Curso Ciência da Computação Prof. Chau S. Shia (Noboru) { // if they have, jump to the last instruction current_instruction = instruction_count; codeArea.append("\n*** Fatal Error: Division by Zero at Instruction " + formatNumber(current_instruction) + " ***\n\n"); } else { // Divide the accumulator to the specified memory location and store the // result back in the accumulator accumulator /= memory[operand]; codeArea.append("Instruction " + formatNumber(current_instruction) + ": DIVIDE " + formatNumber(operand) + "\n"); } break; case HALT: // Simply modify some widget properties and output // some messages to the user nextButton.setEnabled(false); executeButton.setText("Executed"); codeArea.append("Instruction " + formatNumber(current_instruction) + ": HALT " + formatNumber(operand) + "\n" + "\n*** Program Terminated ***\n"); break; } } } <html> <applet code="Simpletron.class" height="375" width="360"> </applet> </html>