Пример #1
0
        private void analisaEnquanto()
        {
            int auxrot1, auxrot2;

            auxrot1 = rotulo;
            CodeGenerator.gera(auxrot1.ToString(), NULL, EMPTY_STRING, EMPTY_STRING);
            rotulo++;

            updateToken();

            Semantico.cleanExpression();
            analisaExpressao();

            try
            {
                returnType = Semantico.analyzeExpression();
            }
            catch (CompiladorException e)
            {
                throwError(e, ANALYZING_EXPRESSION_ERROR, analyzeExpressionStarterLine);
            }

            if (!returnType.Equals(TIPO_BOOLEANO))
            {
                throwError(new CompiladorException(ERRO_SEMANTICO), EXPRESSION_MUST_BE_BOOL, analyzeExpressionStarterLine);
            }
            else
            {
                CodeGenerator.geraCodeExpression(Semantico.getPosFixExpression());
            }

            if (!hasEndedTokens && isSimbol(FACA))
            {
                auxrot2 = rotulo;
                CodeGenerator.gera(EMPTY_STRING, JMPF, auxrot2.ToString(), EMPTY_STRING);
                rotulo++;

                updateToken();

                analisaComandoSimples();

                returnMade = false;

                CodeGenerator.gera(EMPTY_STRING, JMP, auxrot1.ToString(), EMPTY_STRING);
                CodeGenerator.gera(auxrot2.ToString(), NULL, EMPTY_STRING, EMPTY_STRING);
            }
            else
            {
                throwError(new CompiladorException(ERRO_SINTATICO), ERRO_FALTA);
            }
        }
Пример #2
0
        private void analisaAtribuicao()
        {
            updateToken();

            Semantico.cleanExpression();
            analisaExpressao();

            try
            {
                returnType = Semantico.analyzeExpression();
            }
            catch (CompiladorException e)
            {
                throwError(e, ANALYZING_EXPRESSION_ERROR, analyzeExpressionStarterLine);
            }

            if (!returnType.Equals(structReceivedForAssignment.tipo))
            {
                if (structReceivedForAssignment.tipo.Equals(TIPO_BOOLEANO))
                {
                    throwError(new CompiladorException(ERRO_SEMANTICO), ASSIGNMENT_EXPRESSION_MUST_BE_BOOL, analyzeExpressionStarterLine);
                }
                else
                {
                    throwError(new CompiladorException(ERRO_SEMANTICO), ASSIGNMENT_EXPRESSION_MUST_BE_INT, analyzeExpressionStarterLine);
                }
            }
            else
            {
                CodeGenerator.geraCodeExpression(Semantico.getPosFixExpression());
            }

            if (structReceivedForAssignment.nome.Equals(NOME_FUNCAO))
            {
                CodeGenerator.gera(EMPTY_STRING, STR, FUNCTION_RETURN_LABEL, EMPTY_STRING);
            }
            else
            {
                CodeGenerator.gera(EMPTY_STRING, STR, structReceivedForAssignment.rotulo.ToString(), EMPTY_STRING);
            }
        }
Пример #3
0
        private void analisaSe()
        {
            int auxrot1 = 0, auxrot2 = 0;

            updateToken();

            Semantico.cleanExpression();
            analisaExpressao();

            try
            {
                returnType = Semantico.analyzeExpression();
            }
            catch (CompiladorException e)
            {
                throwError(e, ANALYZING_EXPRESSION_ERROR, analyzeExpressionStarterLine);
            }

            if (!returnType.Equals(TIPO_BOOLEANO))
            {
                throwError(new CompiladorException(ERRO_SEMANTICO), EXPRESSION_MUST_BE_BOOL, analyzeExpressionStarterLine);
            }
            else
            {
                CodeGenerator.geraCodeExpression(Semantico.getPosFixExpression());

                auxrot1 = rotulo;
                CodeGenerator.gera(EMPTY_STRING, JMPF, auxrot1.ToString(), EMPTY_STRING);
                rotulo++;
            }

            if (!hasEndedTokens && isSimbol(ENTAO))
            {
                bool entaoReturnMade = false;
                bool senaoReturnMade = false;

                updateToken();

                analisaComandoSimples();

                if (functionReturnsExpected > 0)
                {
                    entaoReturnMade = returnMade;

                    if (!hasEndedTokens && isSimbol(SENAO))
                    {
                        auxrot2 = rotulo;
                        CodeGenerator.gera(EMPTY_STRING, JMP, auxrot2.ToString(), EMPTY_STRING);
                        CodeGenerator.gera(auxrot1.ToString(), NULL, EMPTY_STRING, EMPTY_STRING);
                        rotulo++;

                        returnAlreadyMade = false;
                        updateToken();

                        analisaComandoSimples();

                        CodeGenerator.gera(auxrot2.ToString(), NULL, EMPTY_STRING, EMPTY_STRING);
                        senaoReturnMade = returnMade;
                    }
                    else
                    {
                        CodeGenerator.gera(auxrot1.ToString(), NULL, EMPTY_STRING, EMPTY_STRING);
                    }

                    returnMade        = entaoReturnMade && senaoReturnMade;
                    returnAlreadyMade = returnMade;
                }
                else
                {
                    if (!hasEndedTokens && isSimbol(SENAO))
                    {
                        auxrot2 = rotulo;
                        CodeGenerator.gera(EMPTY_STRING, JMP, auxrot2.ToString(), EMPTY_STRING);
                        CodeGenerator.gera(auxrot1.ToString(), NULL, EMPTY_STRING, EMPTY_STRING);
                        rotulo++;

                        updateToken();
                        analisaComandoSimples();

                        CodeGenerator.gera(auxrot2.ToString(), NULL, EMPTY_STRING, EMPTY_STRING);
                    }
                    else
                    {
                        CodeGenerator.gera(auxrot1.ToString(), NULL, EMPTY_STRING, EMPTY_STRING);
                    }
                }
            }
            else
            {
                throwError(new CompiladorException(ERRO_SINTATICO), ERRO_FALTA);
            }
        }