Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jnpout32.*;
- import java.util.ArrayList;
- import java.util.regex.Pattern;
- import java.io.*;
- import java.lang.*;
- public class Interpretador
- {
- private ArrayList<Byte> varNum = new ArrayList<>();
- private ArrayList<String> varNome = new ArrayList<>();
- private RandomAccessFile arquivo;
- private FileOutputStream output;
- Interpretador()
- {
- }
- /**
- * processa todos os simbolos lidos no arquivo
- */
- private boolean processSimbol()
- {
- boolean stop = false;
- String symb = "";
- char tmp;
- try
- {
- output = new FileOutputStream(new File("out.txt"),true);
- do
- {
- tmp = (char)arquivo.readByte();
- if(tmp != '\n' && tmp != ';')
- {
- symb += tmp;
- }
- }while(tmp != '(' && tmp != '=' && tmp != '\n' && tmp != '.');
- //se simbolo lido == vazio, chegou fim arquivo
- if(!symb.equals("") && !symb.equals("fim."))
- {
- // ** checa palavras reservadas **
- switch(symb)
- {
- case "An":
- func(0);
- break;
- case "nAoB":
- func(1);
- break;
- case "AnB":
- func(2);
- break;
- case "zeroL":
- func(3);
- break;
- case "nAeB":
- func(4);
- break;
- case "Bn":
- func(5);
- break;
- case "AxB":
- func(6);
- break;
- case "ABn":
- func(7);
- break;
- case "AnoB":
- func(8);
- break;
- case "nAxB":
- func(9);
- break;
- case "B":
- func(10);
- break;
- case "AB":
- func(11);
- break;
- case "umL":
- func(12);
- break;
- case "AoBn":
- func(13);
- break;
- case "AoB":
- func(14);
- break;
- case "A":
- func(15);
- break;
- }
- //** verifica se é variavel **
- if(symb.charAt(symb.length()-1) == '=')
- {
- //remove o sinal '='
- symb = symb.substring(0,symb.length()-1);
- String symbTemp = "";
- byte numeroVar = 0;
- //obtem o numero da variavel
- do
- {
- tmp = (char)arquivo.readByte();
- if(tmp != '\n' && tmp != '.' && tmp != ';')
- {
- symbTemp += tmp;
- }
- }while(tmp != '\n' && tmp != '.');
- try
- {
- symbTemp = symbTemp.toLowerCase();
- if(symbTemp.charAt(0) >= 'a' && symbTemp.charAt(0) <= 'f')
- {
- symbTemp = ""+(((int)(symbTemp.charAt(0)))-87);
- }
- numeroVar = (byte)Integer.parseInt(symbTemp);
- if(!chgVar(symb,numeroVar))
- {
- createVar(symb, numeroVar);
- }
- }
- catch(NumberFormatException nfe)
- {
- System.out.println("ErRor: So entendo numeros, tente novamente mais tarde");
- }
- catch(SecurityException se)
- {
- System.out.println("ErRor: Erro na escrita do arquivo, :(, verifique suas permissoes!");
- }
- }
- }
- else
- {
- stop = true;
- output.write(new String(".").getBytes());
- output.close();
- }
- }
- catch(IOException ioe)
- {
- System.out.println("ErRor: Arquivo de entrada invalido, corrija-o");
- }
- return stop;
- }
- //=============================================================
- //=============== Manipulacao das variaveis ===================
- //=============================================================
- private byte getVar(String nome)
- {
- byte ret = -1;
- for(int i=0; i<varNome.size(); i++)
- {
- if( nome.equals(varNome.get(i)) )
- {
- ret = varNum.get(i);
- i=varNum.size();
- }
- }
- return ret;
- }
- private boolean varExists(String nome)
- {
- boolean exist = false;
- for(int i=0; i<varNome.size(); i++)
- {
- if( nome.equals(varNome.get(i)) )
- {
- exist = true;
- i=varNome.size();
- }
- }
- return exist;
- }
- private boolean chgVar(String nome, byte n)
- {
- boolean success = false;
- for(int i=0; i<varNome.size(); i++)
- {
- if( nome.equals(varNome.get(i)) )
- {
- varNum.set(i, (byte)n );
- success = true;
- System.out.println("Variavel alterada: |"+nome+"="+n+"|");
- i=varNome.size();
- }
- }
- return success;
- }
- private void createVar(String nome, byte n)
- {
- varNum.add( (byte)n );
- varNome.add( nome );
- System.out.println("Variavel criada: |"+nome+"="+n+"|");
- }
- private void statusVar()
- {
- if(varNum.size() > 0)
- {
- System.out.print("\nVariaveis: ");
- for(int i=0; i<varNum.size(); i++)
- {
- System.out.print(varNome.get(i) + "=" + varNum.get(i) + " // ");
- }
- System.out.print("\n");
- }
- }
- //=============================================================
- //=========== Entrada/Saida ===================================
- //=============================================================
- private void pause()
- {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- try
- {
- System.out.print("Enviar para porta [ENTER]");
- in.read();
- }
- catch(IOException ioe)
- {
- System.out.println("Erro de E/S!");
- }
- }
- private void write_data(short dado)
- {
- pPort lpt = new pPort();
- System.out.println("Escrevendo na porta (0x378): "+ Integer.toString(dado));
- lpt.output((short)(0x378),dado);
- }
- private void write_operand(short dado)
- {
- pPort lpt = new pPort();
- System.out.println("Escrevendo na porta (0x37A): "+ Integer.toString(dado));
- lpt.output((short)(0x37A),dado);
- }
- //=============================================================
- //=========== Manipulacao individual da cada funcao ===========
- //=============================================================
- private void func(int number)
- {
- String pA,pB,pNum,pFinal;
- byte pC = 0;
- if( (number == 10 && getVar("B") != -1) || (number == 15 && getVar("A") != -1) )
- {
- pA = getVar("A") < 10 ? ""+getVar("A") : ""+((char)( (getVar("A") + 55) ));
- pB = getVar("B") < 10 ? ""+getVar("B") : ""+((char)( (getVar("B") + 55) ));
- pNum = number < 10 ? ""+number : ""+((char)( (number + 55) ));
- pFinal = pA + pB + pNum + "\n";
- try
- {
- output.write(pFinal.getBytes());
- }
- catch(IOException ioe)
- {
- System.out.println("ErRor: Falha ao escrever em arquivo!");
- }
- }
- else
- {
- if( !(number == 10 || number == 15) )
- {
- pA = getVar("A") < 10 ? ""+getVar("A") : ""+((char)( (getVar("A") + 55) ));
- pB = getVar("B") < 10 ? ""+getVar("B") : ""+((char)( (getVar("B") + 55) ));
- pNum = number < 10 ? ""+number : ""+((char)( (number + 55) ));
- pFinal = pA + pB + pNum + "\n";
- try
- {
- output.write(pFinal.getBytes());
- }
- catch(IOException ioe)
- {
- System.out.println("ErRor: Falha ao escrever em arquivo!");
- }
- }
- }
- }
- public void compile(String fileName)
- {
- boolean stop = false;
- try
- {
- arquivo = new RandomAccessFile(new File(fileName), "r");
- }
- catch(FileNotFoundException fnfe)
- {
- System.out.println("Error: Arquivo nao encontrado!");
- }
- do
- {
- stop = processSimbol();
- }while(!stop);
- }
- public void run(String fileName)
- {
- try
- {
- String linha = "";
- arquivo = new RandomAccessFile(new File(fileName), "r");
- while(!linha.equals("."))
- {
- linha = arquivo.readLine();
- linha = linha.toUpperCase();
- if(!linha.equals(".") && linha.length() == 3) //impedir de ler linhas incorretas
- {
- byte pA=0,pB=0,pC=0,pFinal=0;
- char A,B,C;
- A = linha.charAt(0); B = linha.charAt(1); C = linha.charAt(2);
- pA = ((int)A >= 48 && (int)A <= 57) ? (byte)( ((int)A)-48 ) : (byte)( ((int)A)-55 );
- pB = ((int)B >= 48 && (int)B <= 57) ? (byte)( ((int)B)-48 ) : (byte)( ((int)B)-55 );
- pC = ((int)C >= 48 && (int)C <= 57) ? (byte)( ((int)C)-48 ) : (byte)( ((int)C)-55 );
- if( (pA < 0 || pA > 15) || (pB < 0 || pB > 15) || (pB < 0 || pB > 15) ) // se 1 valor incorreto, todos valem zero.
- {
- pA = 0; pB = 0; pC = 0;
- }
- pFinal = pA;
- pFinal <<= 4;
- pFinal = (byte)(pFinal | pB);
- String binForm = Integer.toBinaryString((int)pFinal);
- System.out.println("----------------------");
- System.out.printf("A: %d, B: %d, Operando: %d \n",pA,pB,pC);
- System.out.print("0x378: "+ Integer.toBinaryString((int)pA) + " " + Integer.toBinaryString((int)pB) + " // 0x37A: " + Integer.toBinaryString((int)pC) + "\n");
- pause();
- System.out.println("----------------------");
- write_data((short)pFinal);
- write_operand((short)pC);
- }
- }
- }
- catch(FileNotFoundException fnfe)
- {
- System.out.println("Error: Arquivo nao encontrado!");
- }
- catch(IOException ioe)
- {
- System.out.println("ErRor: Err.. compilador esta na versao beta :/ ou voce me passou arquivo errado!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement