Ybadoo - Soluções em Software Livre
Tutoriais
Programação Orientada a Objetos

Desenvolva um programa que realize a interpretação dos programas da máquina 2_REG.

 

Arquivo Source.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Interface de leitura do codigo-fonte
 */
public interface Source
{
  /**
   * Idiomas permitidos para a codificacao do codigo-fonte
   */
  public static enum Idiom {ENGLISH, PORTUGUESE};

  /**
   * Estilos permitidos para a codificacao do codigo-fonte
   */
  public static enum Style {LONG, MIDDLE, SHORT};

  /**
   * Idioma padrao para a codificacao do codigo-fonte
   */
  public static final Idiom IDIOM_DEFAULT = Idiom.PORTUGUESE;

  /**
   * Estilo padrao para a codificacao do codigo-fonte
   */
  public static final Style STYLE_DEFAULT = Style.LONG;

  /**
   * Retornar o idioma de codificacao do codigo-fonte
   * 
   * @return idioma de codificacao do codigo-fonte
   */
  public Idiom getIdiom();

  /**
   * Retornar o codigo-fonte como um texto
   * 
   * @return codigo-fonte como um texto
   */
  public String getSource();

  /**
   * Retornar o estilo de codificacao do codigo-fonte
   * 
   * @return estilo de codificacao do codigo-fonte
   */
  public Style getStyle();
}

Arquivo FileSource.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * Leitura do codigo-fonte de um arquivo
 */
public class FileSource implements Source
{
  /**
   * Idioma de codificacao do codigo-fonte
   */
  private Idiom idiom;

  /**
   * Buffer contendo o codigo-fonte como um texto
   */
  private StringBuilder source;

  /**
   * Estilo de codificacao do codigo-fonte
   */
  private Style style;

  /**
   * Construtor para a leitura do codigo-fonte de um arquivo
   * 
   * @param file arquivo contendo o codigo-fonte
   * @throws FileNotFoundException arquivo nao encontrado
   * @throws IOException problemas na leitura do arquivo
   */
  public FileSource(File file) throws FileNotFoundException, IOException
  {
    this(file, Source.IDIOM_DEFAULT, Source.STYLE_DEFAULT);
  }

  /**
   * Construtor para a leitura do codigo-fonte de um arquivo
   * 
   * @param file arquivo contendo o codigo-fonte
   * @param idiom idioma de codificacao do codigo-fonte
   * @param style estilo de codificacao do codigo-fonte
   * @throws FileNotFoundException arquivo nao encontrado
   * @throws IOException problemas na leitura do arquivo
   */
  public FileSource(File file, Idiom idiom, Style style) throws FileNotFoundException,
                                                                IOException
  {
    this.source = new StringBuilder();

    this.idiom = idiom;

    this.style = style;

    BufferedReader reader = new BufferedReader(new FileReader(file));

    while(true)
    {
      String line = reader.readLine();

      if(line == null)
      {
        reader.close();

        return;
      }

      this.source.append(line);
    }
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Source#getIdiom()
   */
  public Idiom getIdiom()
  {
    return this.idiom;
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Source#getSource()
   */
  public String getSource()
  {
    return this.source.toString();
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Source#getStyle()
   */
  public Style getStyle()
  {
    return this.style;
  }
}

Arquivo TextSource.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Leitura do codigo-fonte de um texto
 */
public class TextSource implements Source
{
  /**
   * Idioma de codificacao do codigo-fonte
   */
  private Idiom idiom;

  /**
   * Codigo-fonte como um texto
   */
  private String source;

  /**
   * Estilo de codificacao do codigo-fonte
   */
  private Style style;

  /**
   * Construtor para a leitura do codigo-fonte de um texto
   * 
   * @param text texto contendo o codigo-fonte
   */
  public TextSource(String text)
  {
    this(text, Source.IDIOM_DEFAULT, Source.STYLE_DEFAULT);
  }

  /**
   * Construtor para a leitura do codigo-fonte de um texto
   * 
   * @param text texto contendo o codigo-fonte
   * @param idiom idioma de codificacao do codigo-fonte
   * @param style estilo de codificacao do codigo-fonte
   */
  public TextSource(String text, Idiom idiom, Style style)
  {
    this.source = text;

    this.idiom = idiom;

    this.style = style;
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Source#getIdiom()
   */
  public Idiom getIdiom()
  {
    return this.idiom;
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Source#getSource()
   */
  public String getSource()
  {
    return this.source;
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Source#getStyle()
   */
  public Style getStyle()
  {
    return this.style;
  }
}

Arquivo Instruction.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Representacao de uma instrucao na Maquina de Dois Registradores (2_REG)
 */
public abstract class Instruction
{
  /**
   * Rotulo da instrucao corrente
   */
  private String label;

  /**
   * Executar a instrucao na Maquina de Dois Registradores (2_REG)
   * 
   * @param memory memoria da Maquina de Dois Registradores (2_REG)
   * @return proxima instrucao a ser executada
   */
  public abstract String execute(Memory memory);

  /**
   * Retornar o rotulo da instrucao corrente
   * 
   * @return rotulo da instrucao corrente
   */
  public String getLabel()
  {
    return this.label;
  }

  /**
   * Configurar o rotulo da instrucao corrente
   * 
   * @param label rotulo da instrucao corrente
   */
  public void setLabel(String label)
  {
    this.label = label;
  }
}

Arquivo Operation.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Representacao de uma operacao na Maquina de Dois Registradores (2_REG)
 */
public abstract class Operation extends Instruction
{
  /**
   * Rotulo da proxima instrucao
   */
  private String labelGoto;

  /**
   * Retornar o rotulo da proxima instrucao
   * 
   * @return rotulo da proxima instrucao
   */
  protected String getLabelGoto()
  {
    return this.labelGoto;
  }

  /**
   * Configurar o rotulo da proxima instrucao
   * 
   * @param labelGoto rotulo da proxima instrucao
   */
  public void setLabelGoto(String labelGoto) 
  {
    this.labelGoto = labelGoto;
  }
}

Arquivo OperationSubtract_a.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Representacao da operacao SUBTRAIR_A na Maquina de Dois Registradores (2_REG)
 */
public class OperationSubtract_a extends Operation
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Instruction#execute(com.ybadoo.tutoriais.poo.Memory)
   */
  public String execute(Memory memory) 
  {
    int value = memory.load(Memory.Register.A);

    if(value > 0)
    {
      value = value - 1;
    }

    memory.store(Memory.Register.A, value);

    return getLabelGoto();
  }
}

Arquivo OperationIncrease_b.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Representacao da operacao ADICIONAR_B na Maquina de Dois Registradores (2_REG)
 */
public class OperationIncrease_b extends Operation
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Instruction#execute(com.ybadoo.tutoriais.poo.Memory)
   */
  public String execute(Memory memory) 
  {
    int value = memory.load(Memory.Register.B);

    value = value + 1;

    memory.store(Memory.Register.B, value);

    return getLabelGoto();
  }
}

Arquivo Test.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Representacao de um teste na Maquina de Dois Registradores (2_REG)
 */
public abstract class Test extends Instruction
{
  /**
   * Rotulo da instrucao do SENAO
   */
  private String labelElse;

  /**
   * Rotulo da instrucao do ENTAO
   */
  private String labelThen;

  /**
   * Retornar o rotulo da instrucao do SENAO
   * 
   * @return rotulo da instrucao do SENAO
   */
  public String getLabelElse() 
  {
    return this.labelElse;
  }

  /**
   * Retornar o rotulo da instrucao do ENTAO
   * 
   * @return rotulo da instrucao do ENTAO
   */
  public String getLabelThen()
  {
    return this.labelThen;
  }

  /**
   * Configurar o rotulo da instrucao do SENAO
   * 
   * @param labelElse rotulo da instrucao do SENAO
   */
  public void setLabelElse(String labelElse) 
  {
    this.labelElse = labelElse;
  }

  /**
   * Configurar o rotulo da instrucao do ENTAO
   * 
   * @param thenLabel rotulo da instrucao do ENTAO
   */
  public void setLabelThen(String labelThen) 
  {
    this.labelThen = labelThen;
  }
}

Arquivo TestZero_a.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Representacao do teste A_ZERO na Maquina de Dois Registradores (2_REG)
 */
public class TestZero_a extends Test
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Instruction#execute(com.ybadoo.tutoriais.poo.Memory)
   */
  public String execute(Memory memory) 
  {
    int value = memory.load(Memory.Register.A);

    if(value == 0)
    {
      return getLabelThen();
    }
    else
    {
      return getLabelElse();
    }
  }
}

Arquivo Language.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Palavras reservadas, operacoes e testes reconhecidos pela
 * Maquina de Dois Registradores (2_REG)
 */
public abstract class Language
{
  /**
   * Retornar o identificador da operacao ADICIONAR_B
   * 
   * @return identificador da operacao ADICIONAR_B
   */
  public abstract String getOperationIncrease_b();

  /**
   * Retornar o identificador da operacao SUBTRAIR_A
   * 
   * @return identificador da operacao SUBTRAIR_A
   */
  public abstract String getOperationSubtract_a();

  /**
   * Retornar o identificador do teste A_ZERO
   * 
   * @return identificador do teste A_ZERO
   */
  public abstract String getTestZero_a();

  /**
   * Retornar a palavra reservada FACA
   * 
   * @return palavra reservada FACA
   */
  public abstract String getWordDo();

  /**
   * Retornar a palavra reservada SENAO
   * 
   * @return palavra reservada SENAO
   */
  public abstract String getWordElse();

  /**
   * Retornar a palavra reservada VA_PARA
   * 
   * @return palavra reservada VA_PARA
   */
  public abstract String getWordGoto();

  /**
   * Retornar a palavra reservada SE
   * 
   * @return palavra reservada SE
   */
  public abstract String getWordIf();

  /**
   * Retornar a palavra reservada ENTAO
   * 
   * @return palavra reservada ENTAO
   */
  public abstract String getWordThen();

  /**
   * Reconhecer o identificador da operacao ADICIONAR_B
   * 
   * @param token terminal a ser avaliado
   * @return true caso o token seja o identificador da operacao ADICIONAR_B,
   *              false caso contrario
   */
  public boolean isOperationIncrease_b(String token) 
  {
    return getOperationIncrease_b().equals(standardize(token));
  }

  /**
   * Reconhecer o identificador da operacao SUBTRAIR_A
   * 
   * @param token terminal a ser avaliado
   * @return true caso o token seja o identificaodor da operacao SUBTRAIR_A,
   *              false caso contrario
   */
  public boolean isOperationSubtract_a(String token) 
  {
    return getOperationSubtract_a().equals(standardize(token));
  }

  /**
   * Reconhecer o identificador do teste A_ZERO
   * 
   * @param token terminal a ser avaliado
   * @return true caso o token seja o identificador do teste A_ZERO, false caso contrario
   */
  public boolean isTestZero_a(String token) 
  {
    return getTestZero_a().equals(standardize(token));
  }

  /**
   * Reconhecer a palavra reservada FACA
   * 
   * @param token terminal a ser avaliado
   * @return true caso o token seja a palavra reservada FACA, false caso contrario
   */
  public boolean isWordDo(String token) 
  {
    return getWordDo().equals(standardize(token));
  }

  /**
   * Reconhecer a palavra reservada SENAO
   * 
   * @param token terminal a ser avaliado
   * @return true caso o token seja a palavra reservada SENAO, false caso contrario
   */
  public boolean isWordElse(String token) 
  {
    return getWordElse().equals(standardize(token));
  }

  /**
   * Reconhecer a palavra reservada VA_PARA
   * 
   * @param token terminal a ser avaliado
   * @return true caso o token seja a palavra reservada VA_PARA, false caso contrario
   */
  public boolean isWordGoto(String token) 
  {
    return getWordGoto().equals(standardize(token));
  }

  /**
   * Reconhecer a palavra reservada SE
   * 
   * @param token terminal a ser avaliado
   * @return true caso o token seja a palavra reservada SE, false caso contrario
   */
  public boolean isWordIf(String token) 
  {
    return getWordIf().equals(standardize(token));
  }

  /**
   * Reconhecer a palavra reservada ENTAO
   * 
   * @param token terminal a ser avaliado
   * @return true caso o token seja a palavra reservada ENTAO, false caso contrario
   */
  public boolean isWordThen(String token) 
  {
    return getWordThen().equals(standardize(token));
  }

  /**
   * Retornar o token padronizado
   * 
   * @param token token a ser padronizado
   * @return token padronizado
   */
  protected abstract String standardize(String token);
}

Arquivo LanguageEnglishLong.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Palavras reservadas, operacoes e testes reconhecidos pela
 * Maquina de Dois Registradores (2_REG)
 * Idioma - ingles
 * Estilo - completo
 * 
 * R1: if zero_a then goto Rx else goto R2;
 * R2: do subtract_a goto R3;
 * R3: do increase_b goto R1;
 */
public class LanguageEnglishLong extends Language
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getOperationIncrease_b()
   */
  public String getOperationIncrease_b()
  {
    return "INCREASE_B";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getOperationSubtract_a()
   */
  public String getOperationSubtract_a()
  {
    return "SUBTRACT_A";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getTestZero_a()
   */
  public String getTestZero_a() 
  {
    return "ZERO_A";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordDo()
   */
  public String getWordDo() 
  {
    return "DO";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordElse()
   */
  public String getWordElse() 
  {
    return "ELSE";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordGoto()
   */
  public String getWordGoto() 
  {
    return "GOTO";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordIf()
   */
  public String getWordIf() 
  {
    return "IF";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordThen()
   */
  public String getWordThen() 
  {
    return "THEN";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#standardize(java.lang.String)
   */
  protected String standardize(String token)
  {
    return token.toUpperCase();
  }
}

Arquivo LanguageEnglishMiddle.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Palavras reservadas, operacoes e testes reconhecidos pela
 * Maquina de Dois Registradores (2_REG)
 * Idioma - ingles
 * Estilo - mediano
 * 
 * R1: if zero then Rx else R2;
 * R2: do sub R3;
 * R3: do add R1;
 */
public class LanguageEnglishMiddle extends LanguageEnglishLong
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.LanguageEnglishLong#getOperationIncrease_b()
   */
  public String getOperationIncrease_b()
  {
    return "ADD";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.LanguageEnglishLong#getOperationSubtract_a()
   */
  public String getOperationSubtract_a()
  {
    return "SUB";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.LanguageEnglishLong#getTestZero_a()
   */
  public String getTestZero_a() 
  {
    return "ZERO";
  }
}

Arquivo LanguageEnglishShort.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Palavras reservadas, operacoes e testes reconhecidos pela
 * Maquina de Dois Registradores (2_REG)
 * Idioma - ingles
 * Estilo - reduzido
 * 
 * R1: zero Rx R2;
 * R2: sub R3;
 * R3: add R1;
 */
public class LanguageEnglishShort extends LanguageEnglishMiddle
{

}

Arquivo LanguagePortugueseLong.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Palavras reservadas, operacoes e testes reconhecidos pela
 * Maquina de Dois Registradores (2_REG)
 * Idioma - portugues
 * Estilo - completo
 * 
 * R1: se a_zero entao va_para Rx senao va_para R2;
 * R2: faca subtrair_a va_para R3;
 * R3: faca adicionar_b va_para R1;
 */
public class LanguagePortugueseLong extends Language
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getOperationIncrease_b()
   */
  public String getOperationIncrease_b()
  {
    return "ADICIONAR_B";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getOperationSubtract_a()
   */
  public String getOperationSubtract_a()
  {
    return "SUBTRAIR_A";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getTestZero_a()
   */
  public String getTestZero_a() 
  {
    return "A_ZERO";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordDo()
   */
  public String getWordDo() 
  {
    return "FACA";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordElse()
   */
  public String getWordElse() 
  {
    return "SENAO";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordGoto()
   */
  public String getWordGoto() 
  {
    return "VA_PARA";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordIf()
   */
  public String getWordIf() 
  {
    return "SE";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#getWordThen()
   */
  public String getWordThen() 
  {
    return "ENTAO";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Language#standardize(java.lang.String)
   */
  protected String standardize(String token)
  {
    token = token.toUpperCase();

    token = token.replaceAll("Á", "A");
    token = token.replaceAll("Ã", "A");
    token = token.replaceAll("Ç", "C");

    return token;
  }
}

Arquivo LanguagePortugueseMiddle.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Palavras reservadas, operacoes e testes reconhecidos pela
 * Maquina de Dois Registradores (2_REG)
 * Idioma - portugues
 * Estilo - mediano
 * 
 * R1: se zero entao Rx senao R2;
 * R2: faca sub R3;
 * R3: faca add R1;
 */
public class LanguagePortugueseMiddle extends LanguagePortugueseLong
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.LanguagePortugueseLong#getOperationIncrease_b()
   */
  public String getOperationIncrease_b()
  {
    return "ADD";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.LanguagePortugueseLong#getOperationSubtract_a()
   */
  public String getOperationSubtract_a()
  {
    return "SUB";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.LanguagePortugueseLong#getTestZero_a()
   */
  public String getTestZero_a() 
  {
    return "ZERO";
  }
}

Arquivo LanguagePortugueseShort.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Palavras reservadas, operacoes e testes reconhecidos pela
 * Maquina de Dois Registradores (2_REG)
 * Idioma - portugues
 * Estilo - reduzido
 * 
 * R1: zero Rx R2;
 * R2: sub R3;
 * R3: add R1;
 */
public class LanguagePortugueseShort extends LanguagePortugueseMiddle
{

}

Arquivo ParserException.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Excecao lancada durante o processo de analise sintatica das instrucoes reconhecidas pela
 * Maquina de Dois Registradores (2_REG)
 */
public class ParserException extends Exception
{
  /**
   * Identificador de serializacao da classe
   */
  private static final long serialVersionUID = 1L;

  /**
   * Construtor para inicializar a mensagem de erro
   * 
   * @param message mensagem de erro
   */
  public ParserException(String message)
  {
    super(message);
  }
}

Arquivo Parser.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

import java.util.StringTokenizer;

/**
 * Interface para a analise sintatica das instrucoes reconhecidas pela
 * Maquina de Dois Registradores (2_REG)
 */
public interface Parser
{
  /**
   * Realizar a analise sintatica de uma instrucao reconhecida pela
   * Maquina de Dois Registradores (2_REG)
   * 
   * @param tokens tokens tokens da instrucao reconhecida pela
   *               Maquina de Dois Registradores (2_REG)
   * @param idiom idioma de codificacao do codigo-fonte
   * @return instrucao reconhecida pela Maquina de Dois Registradores (2_REG)
   * @throws ParserException problemas durante a analise sintatica da instrucao
   */
  public Instruction parser(StringTokenizer tokens, Idiom idiom) throws ParserException;
}

Arquivo ParserLong.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

import java.util.StringTokenizer;

/**
 * Analise sintatica das instrucoes reconhecidas pela Maquina de Dois Registradores (2_REG)
 * Estilo - completo
 */
public class ParserLong implements Parser
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Parser#parser(java.util.StringTokenizer,
   *                                      com.ybadoo.tutoriais.poo.Idiom)
   */
  public Instruction parser(StringTokenizer tokens, Idiom idiom) throws ParserException
  {
    /*
     *  Obter o idioma para o codigo-fonte passado pelo usuario, via reflexao
     */

    String idiomText = idiom.toString().toLowerCase();

    idiomText = idiomText.substring(0, 1).toUpperCase() + 
                idiomText.substring(1, idiomText.length());

    Language language = null;

    try
    {
      language = (Language)Class.forName("com.ybadoo.tutoriais.poo.Language" + 
                                         idiomText + "Long").newInstance();
    }
    catch(Exception exception)
    {
      throw new ParserException("Parser error: idiom '" + idiom + "' is undefined");
    }

    if(tokens.countTokens() == 5)
    {
      /*
       *  Operacoes - ROTULO FACA OPERACAO VA_PARA ROTULO
       */

      Operation operation = null;

      // ROTULO
      String label = tokens.nextToken();

      // FACA
      if(!language.isWordDo(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordDo() + " expected");
      }

      // OPERACAO
      String command = tokens.nextToken();

      if(language.isOperationSubtract_a(command))
      {
        operation = new OperationSubtract_a();
      }
      else if(language.isOperationIncrease_b(command))
      {
        operation = new OperationIncrease_b();
      }
      else
      {
        throw new ParserException("Syntax error: Operation '" + command + "' is undefined");
      }

      operation.setLabel(label);

      // VA_PARA
      if(!language.isWordGoto(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordGoto() + " expected");
      }

      // ROTULO
      operation.setLabelGoto(tokens.nextToken());

      return operation;
    }
    else if(tokens.countTokens() == 9)
    {
      /*
       *  Testes - ROTULO SE TESTE ENTAO VA_PARA ROTULO SENAO VA_PARA ROTULO
       */

      Test test = null;

      // ROTULO
      String label = tokens.nextToken();

      // SE
      if(!language.isWordIf(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordIf() + " expected");
      }

      // TESTE
      String testToken = tokens.nextToken();

      if(language.isTestZero_a(testToken))
      {
        test = new TestZero_a();
      }
      else
      {
        throw new ParserException("Syntax error: Test '" + testToken + "' is undefined");
      }

      test.setLabel(label);

      // ENTAO
      if(!language.isWordThen(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordThen() + " expected");
      }

      // VA_PARA
      if(!language.isWordGoto(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordGoto() + " expected");
      }

      // ROTULO
      test.setLabelThen(tokens.nextToken());

      // SENAO
      if(!language.isWordElse(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordElse() + " expected");
      }

      // VA_PARA
      if(!language.isWordGoto(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordGoto() + " expected");
      }

      // ROTULO
      test.setLabelElse(tokens.nextToken());

      return test;
    }

    throw new ParserException("Syntax error: instruction is undefined");
  }
}

Arquivo ParserMiddle.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

import java.util.StringTokenizer;

/**
 * Analise sintatica das instrucoes reconhecidas pela Maquina de Dois Registradores (2_REG)
 * Estilo - mediano
 */
public class ParserMiddle implements Parser
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Parser#parser(java.util.StringTokenizer,
                                          com.ybadoo.tutoriais.poo.Idiom)
   */
  public Instruction parser(StringTokenizer tokens, Idiom idiom) throws ParserException
  {
    /*
     *  Obter o idioma para o codigo-fonte passado pelo usuario, via reflexao
     */

    String idiomText = idiom.toString().toLowerCase();

    idiomText = idiomText.substring(0, 1).toUpperCase() + 
                idiomText.substring(1, idiomText.length());

    Language language = null;

    try
    {
      language = (Language)Class.forName("com.ybadoo.tutoriais.poo.Language" + 
                                         idiomText + "Middle").newInstance();
    }
    catch(Exception exception)
    {
      throw new ParserException("Parser error: idiom '" + idiom + "' is undefined");
    }

    if(tokens.countTokens() == 4)
    {
      /*
       *  Operacoes - ROTULO FACA OPERACAO ROTULO
       */

      Operation operation = null;

      // ROTULO
      String label = tokens.nextToken();

      // FACA
      if(!language.isWordDo(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordDo() + " expected");
      }

      // OPERACAO
      String command = tokens.nextToken();

      if(language.isOperationSubtract_a(command))
      {
        operation = new OperationSubtract_a();
      }
      else if(language.isOperationIncrease_b(command))
      {
        operation = new OperationIncrease_b();
      }
      else
      {
        throw new ParserException("Syntax error: Operation '" + command + "' is undefined");
      }

      operation.setLabel(label);

      // ROTULO
      operation.setLabelGoto(tokens.nextToken());

      return operation;
    }
    else if(tokens.countTokens() == 7)
    {
      /*
       *  Testes - ROTULO SE TESTE ENTAO ROTULO SENAO ROTULO
       */

      Test test = null;

      // ROTULO
      String label = tokens.nextToken();

      // SE
      if(!language.isWordIf(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordIf() + " expected");
      }

      String testToken = tokens.nextToken();

      // TESTE
      if(language.isTestZero_a(testToken))
      {
        test = new TestZero_a();
      }
      else
      {
        throw new ParserException("Syntax error: Test '" + testToken + "' is undefined");
      }

      test.setLabel(label);

      // ENTAO
      if(!language.isWordThen(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordThen() + " expected");
      }

      // ROTULO
      test.setLabelThen(tokens.nextToken());

      // SENAO
      if(!language.isWordElse(tokens.nextToken()))
      {
        throw new ParserException("Syntax error: " + language.getWordElse() + " expected");
      }

      // ROTULO
      test.setLabelElse(tokens.nextToken());

      return test;
    }

    throw new ParserException("Syntax error: instruction is undefined");
  }
}

Arquivo ParserShort.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

import java.util.StringTokenizer;

/**
 * Analise sintatica das instrucoes reconhecidas pela Maquina de Dois Registradores (2_REG)
 * Estilo - reduzido
 */
public class ParserShort implements Parser
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Parser#parser(java.util.StringTokenizer,
                                          com.ybadoo.tutoriais.poo.Idiom)
   */
  public Instruction parser(StringTokenizer tokens, Idiom idiom) throws ParserException
  {
    /*
     *  Obter o idioma para o codigo-fonte passado pelo usuario, via reflexao
     */

    String idiomText = idiom.toString().toLowerCase();

    idiomText = idiomText.substring(0, 1).toUpperCase() + 
                idiomText.substring(1, idiomText.length());

    Language language = null;

    try
    {
      language = (Language)Class.forName("com.ybadoo.tutoriais.poo.Language" + 
                                         idiomText + "Short").newInstance();
    }
    catch(Exception exception)
    {
      throw new ParserException("Parser error: idiom '" + idiom + "' is undefined");
    }

    if(tokens.countTokens() == 3)
    {
     /*
      *  Operacoes - ROTULO OPERACAO ROTULO
      */

      Operation operation = null;

      // ROTULO
      String label = tokens.nextToken();

      // OPERACAO
      String command = tokens.nextToken();

      if(language.isOperationSubtract_a(command))
      {
        operation = new OperationSubtract_a();
      }
      else if(language.isOperationIncrease_b(command))
      {
        operation = new OperationIncrease_b();
      }
      else
      {
        throw new ParserException("Syntax error: Operation '" + command + "' is undefined");
      }

      operation.setLabel(label);

      // ROTULO
      operation.setLabelGoto(tokens.nextToken());

      return operation;
    }
    else if(tokens.countTokens() == 4)
    {
     /*
      *  Testes - ROTULO TESTE ROTULO ROTULO
      */

      Test test = null;

      // ROTULO
      String label = tokens.nextToken();

      // TESTE
      String testToken = tokens.nextToken();

      if(language.isTestZero_a(testToken))
      {
        test = new TestZero_a();
      }
      else
      {
        throw new ParserException("Syntax error: Test '" + testToken + "' is undefined");
      }

      test.setLabel(label);

      // ROTULO
      test.setLabelThen(tokens.nextToken());

      // ROTULO
      test.setLabelElse(tokens.nextToken());

      return test;
    }

    throw new ParserException("Syntax error: instruction is undefined");
  }
}

Arquivo Memory.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

import java.util.Hashtable;
import java.util.Map;

/**
 * Representacao da memoria na Maquina de Dois Registradores (2_REG)
 */
public class Memory
{
  /**
   * Registradores na Maquina de Dois Registradores (2_REG)
   */
  public static enum Register {A, B};

  /**
   * Memoria na Maquina de Dois Registradores (2_REG) 
   */
  public Map<Register, Integer> memory;

  /**
   * Construtor padrao
   */
  public Memory(int initial)
  {
    this.memory = new Hashtable<Register, Integer>();

    store(Register.A, initial);
    store(Register.B, 0);
  }

  /**
   * Recuperar o valor armazenado no registrador
   * 
   * @param offset registrador
   * @return valor armazenado no registrador
   */
  public int load(Register offset)
  {
    return this.memory.get(offset).intValue();
  }

  /**
   * Armazenar o valor no registrador
   * 
   * @param offset registrador
   * @param value valor
   */
  public void store(Register offset, int value)
  {
    this.memory.put(offset, new Integer(value));
  }

  /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  public String toString()
  {
    return "(" + load(Register.A) + ", " + load(Register.B) + ")";  
  }
}

Arquivo TwoRegistersMachine.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * Maquina de Dois Registradores (2_REG)
 */
public class TwoRegistersMachine
{
  /**
   * Programa fornecido pelo usuario 
   */
  private Map<String, Instruction> program;

  /**
   * Construtor padrao
   */
  public TwoRegistersMachine()
  {
    this.program = new LinkedHashMap<String, Instruction>();
  }

  /**
   * Realizar a analise sintatica das instrucoes reconhecidas pela
   * Maquina de Dois Registradores (2_REG)
   * 
   * @param source codigo-fonte
   * @throws ParserException problemas ocorridos durante a analise sintatica
   */
  public void parsing(Source source) throws ParserException
  {
    /*
     *  Obter o analisar sintatico para o codigo-fonte passado pelo usuario, via reflexao
     */

    String styleText = source.getStyle().toString().toLowerCase();

    styleText = styleText.substring(0, 1).toUpperCase() +
                styleText.substring(1, styleText.length());

    Parser parser = null;

    try
    {
      parser = (Parser)Class.forName("com.ybadoo.tutoriais.poo.Parser" + styleText).newInstance();
    }
    catch(Exception exception)
    {
      throw new ParserException("Parser error: style '" + source.getStyle() + "' is undefined");
    }

    // Separar o codigo-fonte por instrucao, usando o ponto-e-virgula como delimitador
    StringTokenizer instructions = new StringTokenizer(source.getSource(), ";");
    
    while(instructions.hasMoreTokens())
    {
      String instructionTxt = instructions.nextToken();

      // Obter os tokens da instrucao, utilizado o espaco em branco e os dois-pontos
      // como delimitadores
      StringTokenizer tokens = new StringTokenizer(instructionTxt, ": ");
      
      try
      {
        Instruction instruction = parser.parser(tokens, source.getIdiom());
        
        this.program.put(instruction.getLabel(), instruction);
      }
      catch(ParserException exception)
      {
        throw new ParserException(instructionTxt + "\n" + exception.getMessage());
      }
    }
  }
  
  /**
   * Executar o programa do usuario
   * 
   * @param initial valor inicial
   * @param verbose imprimir a computacao da execucao
   * @return valor de saida
   */
  public int execute(int initial, boolean verbose)
  {
    Memory memory = new Memory(initial);

    String instruction = this.program.keySet().iterator().next();

    while(this.program.containsKey(instruction))
    {
      if(verbose)
      {
        System.out.println("(" + instruction + ", " + memory.toString() + ")");
      }

      instruction = this.program.get(instruction).execute(memory);
    }

    return memory.load(Memory.Register.B);
  }
}

Arquivo Application.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Programa de teste do interpretador da maquina 2_REG
 */
public class Application
{
  /**
   * Metodo principal da linguagem de programacao Java
   * 
   * @param args argumentos da linha de comando (nao utilizado)
   */
  public static void main(String[] args)
  {
    try
    {
      Source source = new FileSource(new File("PortugueseLong.2reg"));

      TwoRegistersMachine machine = new TwoRegistersMachine();

      machine.parsing(source);

      System.out.println(machine.execute(10, true));
    }
    catch(FileNotFoundException exception)
    {
      exception.printStackTrace();
    }
    catch(IOException exception)
    {
      exception.printStackTrace();
    }
    catch(ParserException exception)
    {
      System.out.println(exception.getMessage());
    }
  }
}
R1: if zero_a then goto Rx else goto R2; R2: do subtract_a goto R3; R3: do increase_b goto R1;
R1: if zero then Rx else R2; R2: do sub R3; R3: do add R1;
R1: zero Rx R2; R2: sub R3; R3: add R1;
R1: se a_zero então vá_para Rx senão vá_para R2; R2: faça subtrair_a vá_para R3; R3: faça adicionar_b vá_para R1;
R1: se zero então Rx senão R2; R2: faça sub R3; R3: faça add R1;
R1: zero Rx R2; R2: sub R3; R3: add R1;