kimo12

Untitled

Apr 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 KB | None | 0 0
  1. package assembler;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7.  
  8. public class Pass1 {
  9.     private int cursor;
  10.     private static ArrayList<String> labels = new ArrayList<>();
  11.     private static ArrayList<String> operationCodes = new ArrayList<>();
  12.     private static ArrayList<String> operands = new ArrayList<>();
  13.     private static ArrayList<String> Addresses = new ArrayList<>();
  14.     private static final String FILENAME = "C:\\Users\\karem\\Downloads\\SIC-Example.txt";
  15.  
  16.     private void readfile() {
  17.         BufferedReader br = null;
  18.         FileReader fr = null;
  19.         try {
  20.             fr = new FileReader(FILENAME);
  21.             br = new BufferedReader(fr);
  22.             String CurrentLine;
  23.             while ((CurrentLine = br.readLine()) != null) {
  24.                 // call for a function that uses the line
  25.                 this.line(CurrentLine);
  26.             }
  27.  
  28.         } catch (IOException e) {
  29.             e.printStackTrace();
  30.         } finally {
  31.             try {
  32.                 if (br != null)
  33.                     br.close();
  34.                 if (fr != null)
  35.                     fr.close();
  36.             } catch (IOException ex) {
  37.                 ex.printStackTrace();
  38.             }
  39.         }
  40.     }
  41.  
  42.     private void line(String line) {
  43.         // check if comment
  44.         char firstChar = line.charAt(0);
  45.         if (firstChar == '.') {
  46.             System.out.println("hello from the other side");
  47.         } else {
  48.             // checking format
  49.             line = this.checkFormat(line);
  50.             // get Label From Line
  51.             // if no Label in the line get Label will return null
  52.             String label = getLabel(line);
  53.             if (label != null) {
  54.                 label = label.toUpperCase();
  55.             }
  56.             labels.add(label);
  57.             System.out.print(label);
  58.             // get OperationCode From Line
  59.             String operationCode = getOperationCode(line).toUpperCase();
  60.             operationCodes.add(operationCode);
  61.             System.out.print("\t" + operationCode);
  62.             // get Operand From Line
  63.             // if no operand in the line get operand will return null
  64.             String operand = getOperand(line);
  65.             if (operand != null)
  66.                 operand = operand.toUpperCase();
  67.             operands.add(operand);
  68.             System.out.print("\t" + operand);
  69.         }
  70.         System.out.println();
  71.     }
  72.  
  73.     private String checkFormat(String line) {
  74.         if (line.charAt(0) == '\t') {
  75.             line = line.replaceFirst("\t", "    ");
  76.         }
  77.         if (line.length() > 1 && line.charAt(1) == '\t') {
  78.             line = line.replaceFirst("\t", "   ");
  79.         }
  80.         if (line.length() > 2 && line.charAt(2) == '\t') {
  81.             line = line.replaceFirst("\t", "  ");
  82.         }
  83.         if (line.length() > 4 && line.charAt(4) == '\t') {
  84.             line = line.replaceFirst("\t", "    ");
  85.         }
  86.         if (line.length() > 5 && line.charAt(5) == '\t') {
  87.             line = line.replaceFirst("\t", "   ");
  88.         }
  89.         if (line.length() > 6 && line.charAt(6) == '\t') {
  90.             line = line.replaceFirst("\t", "  ");
  91.         }
  92.         if (line.length() > 10 && line.charAt(10) == '\t') {
  93.             line = line.replaceFirst("\t", "  ");
  94.         }
  95.         if (line.length() > 12 && line.charAt(12) == '\t') {
  96.             line = line.replaceFirst("\t", "    ");
  97.         }
  98.         if (line.length() > 13 && line.charAt(13) == '\t') {
  99.             line = line.replaceFirst("\t", "   ");
  100.         }
  101.         if (line.length() > 14 && line.charAt(14) == '\t') {
  102.             line = line.replaceFirst("\t", "  ");
  103.         }
  104.         if (line.charAt(8) != ' ') {
  105.             System.out.println("Error in Format");
  106.         }
  107.         if (line.length() > 15 && line.charAt(15) != ' ') {
  108.             System.out.println("Error in Format");
  109.         }
  110.         if (line.length() > 16 && line.charAt(16) != ' ') {
  111.             System.out.println("Error in Format");
  112.         }
  113.         return line;
  114.     }
  115.  
  116.     private String getLabel(String line) {
  117.         boolean flag = false;
  118.         int i = 0;
  119.         char firstChar = line.charAt(0);
  120.         if (firstChar == ' ' || firstChar == '\t') {
  121.             flag = true;
  122.         }
  123.         if (!flag) {
  124.             while (i < 8) {
  125.                 if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  126.                 } else {
  127.                     break;
  128.                 }
  129.                 i++;
  130.             }
  131.             String label = line.substring(0, i);
  132.             return label;
  133.         } else
  134.             return null;
  135.     }
  136.  
  137.     private String getOperationCode(String line) {
  138.         int i = 9;
  139.         while (i < 15) {
  140.             if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  141.             } else {
  142.                 break;
  143.             }
  144.             i++;
  145.         }
  146.         String opertionCode = line.substring(9, i);
  147.         return opertionCode;
  148.     }
  149.  
  150.     private String getOperand(String line) {
  151.         boolean flag = false;
  152.         int i = 17;
  153.         while (i < 35) {
  154.             if (line.length() > i && line.charAt(i) != ' ' && line.charAt(i) != '\t') {
  155.                 flag = true;
  156.             } else {
  157.                 break;
  158.             }
  159.             i++;
  160.         }
  161.         if (flag) {
  162.             String operand = line.substring(17, i);
  163.             return operand;
  164.         } else
  165.             return null;
  166.     }
  167.  
  168.     public static void main(String[] args) {
  169.         Pass1 k = new Pass1();
  170.         k.readfile();
  171.         Pass2 g = new Pass2(labels, operationCodes, operands, Addresses);
  172.  
  173.     }
  174. }
Add Comment
Please, Sign In to add comment