Tranvick

KR

Jun 11th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.util.Scanner;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JMenu;
  7. import javax.swing.JMenuBar;
  8. import javax.swing.JMenuItem;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JTextArea;
  12.  
  13.  
  14. public class Main extends JFrame {
  15.  
  16.    
  17.     private static final long serialVersionUID = 4648172894076113183L;
  18.  
  19.     private void parseSource(String source) {
  20.         Scanner scan = new Scanner(source);
  21.         int r1 = 0, r2 = 0;
  22.         while (scan.hasNextLine()) {
  23.             String line = scan.nextLine().split(";")[0].trim();
  24.             if (line.length() == 0) {
  25.                 continue;
  26.             }
  27.             try {
  28.                 String command = line.substring(0, 3).toLowerCase();
  29.                 switch (command) {
  30.                 case "add":
  31.                 case "sub":
  32.                 case "mov":
  33.                     break;
  34.                 default:
  35.                     JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
  36.                     return;
  37.                 }
  38.                 String operands[] = line.substring(3).split(",");
  39.                 if (operands.length != 2) {
  40.                     JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
  41.                     return;
  42.                 }
  43.                 String op1 = operands[0].toLowerCase().trim();
  44.                 String op2 = operands[1].toLowerCase().trim();
  45.                 if (op1.equals("r1")) {
  46.                     int c = 0;
  47.                     if (op2.equals("r1")) {
  48.                         c = r1;
  49.                     } else if (op2.equals("r2")) {
  50.                         c = r2;
  51.                     } else {
  52.                         try {
  53.                             c = Integer.parseInt(op2);
  54.                         } catch (Exception e) {
  55.                             JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
  56.                             return;
  57.                         }
  58.                     }
  59.                     switch (command) {
  60.                         case "add":
  61.                             r1 += c;
  62.                             break;
  63.                         case "sub":
  64.                             r1 -= c;
  65.                             break;
  66.                         case "mov":
  67.                             r1 = c;
  68.                     }
  69.                 } else if (op1.equals("r2")) {
  70.                     int c = 0;
  71.                     if (op2.equals("r1")) {
  72.                         c = r1;
  73.                     } else if (op2.equals("r2")) {
  74.                         c = r2;
  75.                     } else {
  76.                         try {
  77.                             c = Integer.parseInt(op2);
  78.                         } catch (Exception e) {
  79.                             JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
  80.                             return;
  81.                         }
  82.                     }
  83.                     switch (command) {
  84.                         case "add":
  85.                             r2 += c;
  86.                             break;
  87.                         case "sub":
  88.                             r2 -= c;
  89.                             break;
  90.                         case "mov":
  91.                             r2 = c;
  92.                     }
  93.                 } else {
  94.                     JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
  95.                     return;
  96.                 }
  97.             } catch (StringIndexOutOfBoundsException e) {
  98.                 JOptionPane.showMessageDialog(this, "Compilation error!", null, JOptionPane.ERROR_MESSAGE);
  99.                 return;
  100.             }
  101.         }
  102.         scan.close();
  103.         JOptionPane.showMessageDialog(this, "r1 = " + r1 + "\nr2 = " + r2);
  104.     }
  105.    
  106.     private void initControls() {
  107.         final JTextArea text = new JTextArea();
  108.         getContentPane().add(new JScrollPane(text));
  109.         JMenuBar menuBar = new JMenuBar();
  110.         JMenu menu = new JMenu("File");
  111.         JMenuItem miRun = new JMenuItem("Run");
  112.         JMenuItem miExit = new JMenuItem("Exit");
  113.         menu.add(miRun);
  114.         menu.add(miExit);
  115.         menuBar.add(menu);
  116.         setJMenuBar(menuBar);
  117.  
  118.         miRun.addActionListener(new ActionListener() {
  119.             @Override
  120.             public void actionPerformed(ActionEvent e) {
  121.                 parseSource(text.getText());
  122.             }
  123.         });
  124.        
  125.         miExit.addActionListener(new ActionListener() {
  126.             @Override
  127.             public void actionPerformed(ActionEvent arg0) {
  128.                 System.exit(0);
  129.             }
  130.         });
  131.     }
  132.    
  133.     private Main() {
  134.         initControls();
  135.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  136.         setSize(400, 400);
  137.     }
  138.    
  139.     public static void main(String[] args) {
  140.         new Main().setVisible(true);
  141.     }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment