Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Scanner;
- import javax.swing.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- public class Main extends JFrame {
- private static final long serialVersionUID = 4648172894076113183L;
- private void parseSource(String source) {
- Scanner scan = new Scanner(source);
- int r1 = 0, r2 = 0;
- while (scan.hasNextLine()) {
- String line = scan.nextLine().split(";")[0].trim();
- if (line.length() == 0) {
- continue;
- }
- try {
- String command = line.substring(0, 3).toLowerCase();
- switch (command) {
- case "add":
- case "sub":
- case "mov":
- break;
- default:
- JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
- return;
- }
- String operands[] = line.substring(3).split(",");
- if (operands.length != 2) {
- JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
- return;
- }
- String op1 = operands[0].toLowerCase().trim();
- String op2 = operands[1].toLowerCase().trim();
- if (op1.equals("r1")) {
- int c = 0;
- if (op2.equals("r1")) {
- c = r1;
- } else if (op2.equals("r2")) {
- c = r2;
- } else {
- try {
- c = Integer.parseInt(op2);
- } catch (Exception e) {
- JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
- switch (command) {
- case "add":
- r1 += c;
- break;
- case "sub":
- r1 -= c;
- break;
- case "mov":
- r1 = c;
- }
- } else if (op1.equals("r2")) {
- int c = 0;
- if (op2.equals("r1")) {
- c = r1;
- } else if (op2.equals("r2")) {
- c = r2;
- } else {
- try {
- c = Integer.parseInt(op2);
- } catch (Exception e) {
- JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
- switch (command) {
- case "add":
- r2 += c;
- break;
- case "sub":
- r2 -= c;
- break;
- case "mov":
- r2 = c;
- }
- } else {
- JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
- return;
- }
- } catch (StringIndexOutOfBoundsException e) {
- JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
- return;
- }
- }
- scan.close();
- JOptionPane.showMessageDialog(this, "r1 = " + r1 + "\nr2 = " + r2);
- }
- private void initControls() {
- final JTextArea text = new JTextArea();
- getContentPane().add(new JScrollPane(text));
- JMenuBar menuBar = new JMenuBar();
- JMenu menu = new JMenu("File");
- JMenuItem miRun = new JMenuItem("Run");
- JMenuItem miExit = new JMenuItem("Exit");
- menu.add(miRun);
- menu.add(miExit);
- menuBar.add(menu);
- setJMenuBar(menuBar);
- miRun.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- parseSource(text.getText());
- }
- });
- miExit.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- System.exit(0);
- }
- });
- }
- private Main() {
- initControls();
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setSize(400, 400);
- }
- public static void main(String[] args) {
- new Main().setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment