Advertisement
DavidsonDFGL

Interpretador - pParalela

Sep 23rd, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.04 KB | None | 0 0
  1.    import jnpout32.*;
  2.    import java.util.ArrayList;
  3.    import java.util.regex.Pattern;
  4.    import java.io.*;
  5.    import java.lang.*;
  6.  
  7.    public class Interpretador
  8.    {
  9.       private ArrayList<Byte> varNum    = new ArrayList<>();
  10.       private ArrayList<String> varNome = new ArrayList<>();
  11.       private RandomAccessFile arquivo;
  12.       private FileOutputStream output;
  13.    
  14.       Interpretador()
  15.       {
  16.      
  17.       }
  18.    
  19.    /**
  20.    * processa todos os simbolos lidos no arquivo
  21.    */
  22.       private boolean processSimbol()
  23.       {
  24.          boolean stop = false;
  25.          String symb = "";
  26.          char tmp;
  27.      
  28.          try
  29.          {
  30.             output = new FileOutputStream(new File("out.txt"),true);
  31.             do
  32.             {
  33.                tmp = (char)arquivo.readByte();
  34.                if(tmp != '\n' && tmp != ';')
  35.                {
  36.                   symb += tmp;
  37.                }
  38.             }while(tmp != '(' && tmp != '=' && tmp != '\n' && tmp != '.');
  39.          
  40.          //se simbolo lido == vazio, chegou fim arquivo
  41.             if(!symb.equals("") && !symb.equals("fim."))
  42.             {    
  43.             // ** checa palavras reservadas **
  44.                switch(symb)
  45.                {
  46.                   case "An":
  47.                      func(0);
  48.                      break;
  49.                   case "nAoB":
  50.                      func(1);
  51.                      break;
  52.                   case "AnB":
  53.                      func(2);
  54.                      break;
  55.                   case "zeroL":
  56.                      func(3);
  57.                      break;
  58.                   case "nAeB":
  59.                      func(4);
  60.                      break;
  61.                   case "Bn":
  62.                      func(5);
  63.                      break;
  64.                   case "AxB":
  65.                      func(6);
  66.                      break;
  67.                   case "ABn":
  68.                      func(7);
  69.                      break;
  70.                   case "AnoB":
  71.                      func(8);
  72.                      break;
  73.                   case "nAxB":
  74.                      func(9);
  75.                      break;
  76.                   case "B":
  77.                      func(10);
  78.                      break;
  79.                   case "AB":
  80.                      func(11);
  81.                      break;
  82.                   case "umL":
  83.                      func(12);
  84.                      break;
  85.                   case "AoBn":
  86.                      func(13);
  87.                      break;
  88.                   case "AoB":
  89.                      func(14);
  90.                      break;
  91.                   case "A":
  92.                      func(15);
  93.                      break;
  94.                }
  95.            
  96.             //** verifica se é variavel **
  97.                if(symb.charAt(symb.length()-1) == '=')
  98.                {
  99.                //remove o sinal '='
  100.                   symb = symb.substring(0,symb.length()-1);
  101.                   String symbTemp = "";
  102.                   byte numeroVar = 0;
  103.                
  104.                //obtem o numero da variavel
  105.                   do
  106.                   {
  107.                      tmp = (char)arquivo.readByte();
  108.                      if(tmp != '\n' && tmp != '.' && tmp != ';')
  109.                      {
  110.                         symbTemp += tmp;
  111.                      }
  112.                   }while(tmp != '\n' && tmp != '.');
  113.                
  114.                   try
  115.                   {
  116.                      symbTemp = symbTemp.toLowerCase();
  117.                      if(symbTemp.charAt(0) >= 'a' && symbTemp.charAt(0) <= 'f')
  118.                      {
  119.                         symbTemp = ""+(((int)(symbTemp.charAt(0)))-87);
  120.                      }
  121.                  
  122.                      numeroVar = (byte)Integer.parseInt(symbTemp);
  123.                  
  124.                      if(!chgVar(symb,numeroVar))
  125.                      {
  126.                         createVar(symb, numeroVar);
  127.                      }
  128.                   }
  129.                      catch(NumberFormatException nfe)
  130.                      {
  131.                         System.out.println("ErRor: So entendo numeros, tente novamente mais tarde");
  132.                      }
  133.                      catch(SecurityException se)
  134.                      {
  135.                         System.out.println("ErRor: Erro na escrita do arquivo, :(, verifique suas permissoes!");
  136.                      }
  137.                }  
  138.             }
  139.             else
  140.             {
  141.                stop = true;
  142.                output.write(new String(".").getBytes());
  143.                output.close();
  144.             }
  145.          }
  146.             catch(IOException ioe)
  147.             {
  148.                System.out.println("ErRor: Arquivo de entrada invalido, corrija-o");
  149.             }
  150.          return stop;
  151.       }
  152.    
  153.    //=============================================================
  154.    //=============== Manipulacao das variaveis ===================
  155.    //=============================================================
  156.    
  157.       private byte getVar(String nome)
  158.       {
  159.          byte ret = -1;
  160.      
  161.          for(int i=0; i<varNome.size(); i++)
  162.          {
  163.             if( nome.equals(varNome.get(i)) )
  164.             {
  165.                ret = varNum.get(i);
  166.                i=varNum.size();
  167.             }
  168.          }
  169.          return ret;
  170.       }
  171.    
  172.       private boolean varExists(String nome)
  173.       {
  174.          boolean exist = false;
  175.      
  176.          for(int i=0; i<varNome.size(); i++)
  177.          {
  178.             if( nome.equals(varNome.get(i)) )
  179.             {
  180.                exist = true;
  181.                i=varNome.size();
  182.             }
  183.          }
  184.          return exist;
  185.       }
  186.    
  187.       private boolean chgVar(String nome, byte n)
  188.       {
  189.          boolean success = false;
  190.          for(int i=0; i<varNome.size(); i++)
  191.          {
  192.             if( nome.equals(varNome.get(i)) )
  193.             {
  194.                varNum.set(i, (byte)n );
  195.                success = true;
  196.                System.out.println("Variavel alterada: |"+nome+"="+n+"|");
  197.                i=varNome.size();
  198.             }
  199.          }
  200.          return success;
  201.       }
  202.    
  203.       private void createVar(String nome, byte n)
  204.       {
  205.          varNum.add( (byte)n );
  206.          varNome.add( nome );
  207.          System.out.println("Variavel criada: |"+nome+"="+n+"|");
  208.       }
  209.    
  210.       private void statusVar()
  211.       {
  212.          if(varNum.size() > 0)
  213.          {
  214.             System.out.print("\nVariaveis: ");
  215.             for(int i=0; i<varNum.size(); i++)
  216.             {
  217.                System.out.print(varNome.get(i) + "=" + varNum.get(i) + " // ");
  218.             }
  219.             System.out.print("\n");
  220.          }
  221.       }
  222.    
  223.    //=============================================================
  224.    //=========== Entrada/Saida ===================================
  225.    //=============================================================
  226.    
  227.       private void pause()
  228.       {
  229.          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  230.      
  231.          try
  232.          {
  233.             System.out.print("Enviar para porta [ENTER]");
  234.             in.read();
  235.          }
  236.             catch(IOException ioe)
  237.             {
  238.                System.out.println("Erro de E/S!"); 
  239.             }
  240.       }
  241.    
  242.       private void write_data(short dado)
  243.       {
  244.          pPort lpt = new pPort();
  245.          System.out.println("Escrevendo na porta (0x378): "+  Integer.toString(dado));
  246.          lpt.output((short)(0x378),dado);
  247.       }
  248.    
  249.       private void write_operand(short dado)
  250.       {
  251.          pPort lpt = new pPort();
  252.          System.out.println("Escrevendo na porta (0x37A): "+  Integer.toString(dado));
  253.          lpt.output((short)(0x37A),dado);
  254.       }
  255.    
  256.    //=============================================================
  257.    //=========== Manipulacao individual da cada funcao ===========
  258.    //=============================================================
  259.    
  260.       private void func(int number)
  261.       {
  262.          String pA,pB,pNum,pFinal;
  263.          byte pC = 0;
  264.      
  265.          if( (number == 10 && getVar("B") != -1) || (number == 15 && getVar("A") != -1) )
  266.          {
  267.             pA = getVar("A") < 10 ? ""+getVar("A") : ""+((char)( (getVar("A") + 55) ));
  268.             pB = getVar("B") < 10 ? ""+getVar("B") : ""+((char)( (getVar("B") + 55) ));
  269.             pNum = number < 10 ? ""+number : ""+((char)( (number + 55) ));
  270.          
  271.             pFinal = pA + pB + pNum + "\n";
  272.          
  273.             try
  274.             {
  275.                output.write(pFinal.getBytes());
  276.             }
  277.                catch(IOException ioe)
  278.                {
  279.                   System.out.println("ErRor: Falha ao escrever em arquivo!");
  280.                }
  281.          }
  282.          else
  283.          {
  284.             if( !(number == 10 || number == 15) )
  285.             {
  286.                pA = getVar("A") < 10 ? ""+getVar("A") : ""+((char)( (getVar("A") + 55) ));
  287.                pB = getVar("B") < 10 ? ""+getVar("B") : ""+((char)( (getVar("B") + 55) ));
  288.                pNum = number < 10 ? ""+number : ""+((char)( (number + 55) ));
  289.            
  290.                pFinal = pA + pB + pNum + "\n";
  291.            
  292.                try
  293.                {
  294.                   output.write(pFinal.getBytes());
  295.                }
  296.                   catch(IOException ioe)
  297.                   {
  298.                      System.out.println("ErRor: Falha ao escrever em arquivo!");
  299.                   }
  300.             }
  301.          }
  302.       }
  303.    
  304.       public void compile(String fileName)
  305.       {
  306.          boolean stop = false;
  307.      
  308.          try
  309.          {
  310.             arquivo = new RandomAccessFile(new File(fileName), "r");
  311.          }
  312.             catch(FileNotFoundException fnfe)
  313.             {
  314.                System.out.println("Error: Arquivo nao encontrado!");
  315.             }
  316.      
  317.          do
  318.          {
  319.             stop = processSimbol();
  320.          }while(!stop);  
  321.       }
  322.    
  323.       public void run(String fileName)
  324.       {
  325.          try
  326.          {
  327.             String linha = "";
  328.             arquivo = new RandomAccessFile(new File(fileName), "r");
  329.             while(!linha.equals("."))
  330.             {
  331.                linha = arquivo.readLine();
  332.                linha = linha.toUpperCase();
  333.            
  334.                if(!linha.equals(".") && linha.length() == 3) //impedir de ler linhas incorretas
  335.                {
  336.                   byte pA=0,pB=0,pC=0,pFinal=0;
  337.                   char A,B,C;
  338.                   A = linha.charAt(0); B = linha.charAt(1); C = linha.charAt(2);
  339.                
  340.                   pA = ((int)A >= 48 && (int)A <= 57) ? (byte)( ((int)A)-48 ) : (byte)( ((int)A)-55 );
  341.                   pB = ((int)B >= 48 && (int)B <= 57) ? (byte)( ((int)B)-48 ) : (byte)( ((int)B)-55 );
  342.                   pC = ((int)C >= 48 && (int)C <= 57) ? (byte)( ((int)C)-48 ) : (byte)( ((int)C)-55 );
  343.                
  344.                   if( (pA < 0 || pA > 15) || (pB < 0 || pB > 15) || (pB < 0 || pB > 15) ) // se 1 valor incorreto, todos valem zero.
  345.                   {
  346.                      pA = 0; pB = 0; pC = 0;
  347.                   }
  348.                
  349.                   pFinal = pA;
  350.                   pFinal <<= 4;
  351.                   pFinal = (byte)(pFinal | pB);
  352.                
  353.                   String binForm = Integer.toBinaryString((int)pFinal);
  354.                   System.out.println("----------------------");
  355.                   System.out.printf("A: %d, B: %d, Operando: %d \n",pA,pB,pC);
  356.                   System.out.print("0x378: "+ Integer.toBinaryString((int)pA) + " " + Integer.toBinaryString((int)pB) + "  //  0x37A: " + Integer.toBinaryString((int)pC) + "\n");
  357.                   pause();
  358.                   System.out.println("----------------------");
  359.                
  360.                   write_data((short)pFinal);
  361.                   write_operand((short)pC);
  362.                }
  363.             }
  364.          }
  365.             catch(FileNotFoundException fnfe)
  366.             {
  367.                System.out.println("Error: Arquivo nao encontrado!");
  368.             }
  369.             catch(IOException ioe)
  370.             {
  371.                System.out.println("ErRor: Err.. compilador esta na versao beta :/ ou voce me passou arquivo errado!");
  372.             }
  373.       }
  374.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement