// Guide to Data Structures
// Section 4.5
// Copyright 2018, by J.T. Streib and T. Soma

import java.util.*;
import java.io.*;

class OpCpdes {
private static ListArrayGeneric<String> opTable
= new ListArrayGeneric<String>();       

public static void main(String[] args) throws IOException {
   
  // create op table
  createOpTable();
  // check op codes in assembly code
  checkOpCodes();
}

public static void createOpTable() throws IOException {
  String inStr;
  Scanner inFile = new Scanner(new File("g:opCodes.txt"));   
  while(inFile.hasNextLine()) {
     inStr = inFile.nextLine();
     opTable.insert(inStr);
  }
  inFile.close();
}

// check op codes in asssembly code
public static void checkOpCodes() throws IOException {
   String inStr;
   int len;

   System.out.println("Symbol    Opcode    Operand     Error");
   System.out.println("-------------------------------------"
                      + "-----------------------");

   Scanner inFile = new Scanner(new File("g:assemblyCode.txt"));   

   while(inFile.hasNextLine()) {
      inStr = inFile.nextLine();
      System.out.print(inStr);
      len = inStr.length();

      // line with operand
      if(len >= 20) {
         inStr = inStr.substring(10, 20);
         if(!opTable.search(inStr.trim()))
            System.out.print("\t\tInvalid Opcode "
                             + inStr.trim());
      }
      // line without operand
      else {
         inStr = inStr.substring(10, len);
         if(!opTable.search(inStr.trim()))
            System.out.print("\t\t\tInvalid Opcode "
            + inStr.trim());
      }
      System.out.println();        
   }
   inFile.close();
}
}

public class ListArrayGeneric<T extends Comparable<T>> {     

private final int N=20;
private int position, count;
private T[] lArray;
T reference;

public ListArrayGeneric() {
  count=0;
  lArray = (T[]) new Comparable[N];
}

private boolean empty() {
  return count <= 0;
}

private boolean full() {
  return count >= N;
}
 
public boolean insert(T item) {
  boolean inserted=false;
 
  if(!full()){
     if (!search(item)){
        for(int j=count; j>position; j--)
           lArray[j] = lArray[j-1];
        lArray[position] = item;
        count++;
        inserted = true;
     }
  }
  else
     System.out.println("List is Full");
  return inserted;
}

public boolean delete(T item) {
  boolean deleted=false;
  if(!empty()){
     if(search(item)){
        for(int j=position; j<count-1; j++)
           lArray[j] = lArray[j+1];
        count--;
        deleted = true;   
     }
  }   
  else
     System.out.println("List is Empty");
  return deleted;    
}

public boolean search(T item) {
  boolean found, stop;
  found = false;
  stop = false;
  position = 0;
  while(position != count && !stop)
     if(lArray[position].compareTo(item) >= 0){
        stop = true;
        if(lArray[position].compareTo(item) == 0)
           found = true;
     }
     else
        position++;
  return found;
}

public void output() {
  System.out.print("List: ");
  int j = 0;
  while(j != count) {
     System.out.print(lArray[j]+" ");
     j++;
  }
  System.out.println();
}

public int compareTo(T item) {
   int result;
   if(reference.compareTo(item) > 0)
      result = 1;
   else
     if(reference.compareTo(item) < 0)
        result = -1;
     else
        result = 0;
   return result;
}
}