Ejemplo n.º 1
0
        private ArvoreNo AnalisarCondicional()
        {
            ProximoToken(TipoToken.Se);
            ProximoToken(TipoToken.AbreParenteses);
            ArvoreNo no = AnalisarBooleana();

            ProximoToken(TipoToken.FechaParenteses);

            ProximoToken(TipoToken.AbreChaves);
            ArvoreNo comandos = AnalisarListaComandos();

            while (tokenAtual.Tipo != TipoToken.FechaChaves)
            {
                ArvoreNo comando = AnalisarComando();

                if (comando is VazioNo)
                {
                    break;
                }

                (comandos as ComandosNo).Comandos.Add(comando);
            }

            ProximoToken(TipoToken.FechaChaves);
            ArvoreNo senao = new VazioNo();

            if (tokenAtual.Tipo == TipoToken.Senao)
            {
                senao = AnalisarSenao();
            }

            return(new CondicionalNo(no, comandos, senao));
        }
Ejemplo n.º 2
0
        public object GetBooleana(ArvoreNo esquerda, Token operador, ArvoreNo direita)
        {
            var esq = esquerda.GetValor(this);
            var dir = direita.GetValor(this);
            IComparer <object> comparer = Comparer <object> .Default;

            switch (operador.Tipo)
            {
            case TipoToken.Igual:
                return(comparer.Compare(esq, dir) == 0 ? true : false);

            case TipoToken.Maior:
                return(comparer.Compare(esq, dir) > 0 ? true : false);

            case TipoToken.Menor:
                return(comparer.Compare(esq, dir) < 0 ? true : false);

            case TipoToken.MaiorIgual:
                return(comparer.Compare(esq, dir) >= 0 ? true : false);

            case TipoToken.MenorIgual:
                return(comparer.Compare(esq, dir) <= 0 ? true : false);

            case TipoToken.Diferente:
                return(comparer.Compare(esq, dir) != 0 ? true : false);

            default:
                throw new SemanticoException("Erro ao analisar expressão booleana");
            }
        }
Ejemplo n.º 3
0
 private ArvoreNo AnalisarFator()
 {
     if (tokenAtual.Tipo == TipoToken.Identificador)
     {
         var id = ProximoToken(TipoToken.Identificador);
         return(new FatorNo(id));
     }
     else if (tokenAtual.Tipo == TipoToken.Numero)
     {
         var id = ProximoToken(TipoToken.Numero);
         return(new FatorNo(id));
     }
     else if (tokenAtual.Tipo == TipoToken.NumeroDecimal)
     {
         var id = ProximoToken(TipoToken.NumeroDecimal);
         return(new FatorNo(id));
     }
     else if (tokenAtual.Tipo == TipoToken.AbreParenteses)
     {
         ProximoToken(TipoToken.AbreParenteses);
         ArvoreNo no = AnalisarExpressao();
         ProximoToken(TipoToken.FechaParenteses);
         return(no);
     }
     else
     {
         var id = ProximoToken(TipoToken.Literal);
         return(new FatorNo(id));
     }
 }
Ejemplo n.º 4
0
        private ArvoreNo AnalisarLoop()
        {
            ProximoToken(TipoToken.Enquanto);
            ProximoToken(TipoToken.AbreParenteses);
            ArvoreNo no = AnalisarBooleana();

            ProximoToken(TipoToken.FechaParenteses);

            ProximoToken(TipoToken.AbreChaves);
            ArvoreNo comandos = AnalisarListaComandos();

            while (tokenAtual.Tipo != TipoToken.FechaChaves)
            {
                ArvoreNo comando = AnalisarComando();

                if (comando is VazioNo)
                {
                    break;
                }

                (comandos as ComandosNo).Comandos.Add(comando);
            }

            ProximoToken(TipoToken.FechaChaves);

            return(new LoopNo(no, comandos));
        }
Ejemplo n.º 5
0
        private ArvoreNo AnalisarListaComandos()
        {
            List <ArvoreNo> comandos = new List <ArvoreNo>();
            ArvoreNo        no       = AnalisarComando();

            comandos.Add(no);

            return(new ComandosNo(comandos));
        }
Ejemplo n.º 6
0
        private ArvoreNo AnalisarAtribuicao()
        {
            Token identificador = ProximoToken(TipoToken.Identificador);

            ProximoToken(TipoToken.Atribuicao);
            ArvoreNo expressao = AnalisarExpressao();

            return(new AtribuicaoNo(identificador, expressao));
        }
Ejemplo n.º 7
0
        public object GetDeclaracao(Token tipo, Token identificador, ArvoreNo atribuicao)
        {
            Variaveis.Add(identificador.Valor, null);
            VariaveisTipo.Add(identificador.Valor, tipo.Valor);

            if (!(atribuicao is VazioNo))
            {
                return(atribuicao.GetValor(this));
            }

            return(identificador);
        }
Ejemplo n.º 8
0
        public object GetLoop(ArvoreNo booleana, ArvoreNo corpo)
        {
            bool   condicao  = (bool)booleana.GetValor(this);
            object resultado = new object();

            while (condicao)
            {
                resultado = corpo.GetValor(this);
                condicao  = (bool)booleana.GetValor(this);
                RemoverDeclaracaoLocal(corpo as ComandosNo);
            }

            return(resultado);
        }
Ejemplo n.º 9
0
        private ArvoreNo AnalisarTermo()
        {
            ArvoreNo fator = AnalisarFator();

            while (tokenAtual.Tipo == TipoToken.Vezes || tokenAtual.Tipo == TipoToken.Dividir)
            {
                Token operacao = tokenAtual;
                ProximoToken(operacao.Tipo);
                ArvoreNo dir = AnalisarFator();
                fator = new ExpressaoNo(fator, operacao, dir);
            }

            return(fator);
        }
Ejemplo n.º 10
0
        private ArvoreNo AnalisarExpressao()
        {
            ArvoreNo esq = AnalisarTermo();

            while (tokenAtual.Tipo == TipoToken.Mais || tokenAtual.Tipo == TipoToken.Menos)
            {
                Token operacao = tokenAtual;
                ProximoToken(operacao.Tipo);
                ArvoreNo dir = AnalisarTermo();
                esq = new ExpressaoNo(esq, operacao, dir);
            }

            return(esq);
        }
Ejemplo n.º 11
0
        private void Analisar()
        {
            if (string.IsNullOrEmpty(CodeDocument.Text))
            {
                return;
            }

            try
            {
                VariaveisCollection.Clear();
                CompilerResult = string.Empty;

                analexico = new AnaLexico(CodeDocument.Text);
                var result = analexico.Analisar();

                Sintatico sintatico = new Sintatico(result);
                ArvoreNo  no        = sintatico.Analisar();

                Semantico semantico = new Semantico();
                var       x         = no.GetValor(semantico);

                var list = x as List <Variaveis>;

                foreach (var item in list)
                {
                    VariaveisCollection.Add(item);
                }

                CompilerResult = "Compilação bem-sucedida.";
            }
            catch (LexicoException ex)
            {
                CompilerResult = $"{ex.Message} ({ex.Caracter}) na linha {ex.Linha}";
            }
            catch (SintaticoException ex)
            {
                CompilerResult = ex.Message;
            }
            catch (SemanticoException ex)
            {
                CompilerResult = ex.Message;
            }
            catch (Exception ex)
            {
                CompilerResult = $"Erro: {ex.Message}";
            }
        }
Ejemplo n.º 12
0
        public object GetCondicional(ArvoreNo booleana, ArvoreNo corpo, ArvoreNo senao)
        {
            bool   condicao = (bool)booleana.GetValor(this);
            object retorno  = null;

            if (condicao)
            {
                retorno = corpo.GetValor(this);
            }
            else
            {
                if (!(senao is VazioNo))
                {
                    retorno = senao.GetValor(this);
                }
            }

            RemoverDeclaracaoLocal(corpo as ComandosNo);

            return(retorno);
        }
Ejemplo n.º 13
0
        public object GetAtribuicao(Token identificador, ArvoreNo expressao)
        {
            object result;

            if (Variaveis.ContainsKey(identificador.Valor))
            {
                result = expressao.GetValor(this);
                Variaveis[identificador.Valor] = result;

                if (result.GetType() == typeof(int))
                {
                    if (VariaveisTipo[identificador.Valor] != "int")
                    {
                        throw new SemanticoException("Tipo errado foi atribuído a variável");
                    }
                    return(Convert.ToInt32(result));
                }
                else if (result.GetType() == typeof(float))
                {
                    if (VariaveisTipo[identificador.Valor] != "float")
                    {
                        throw new SemanticoException("Tipo errado foi atribuído a variável");
                    }
                    return(float.Parse(result.ToString(), CultureInfo.InvariantCulture.NumberFormat));
                }
                else
                {
                    if (VariaveisTipo[identificador.Valor] != "char")
                    {
                        throw new SemanticoException("Tipo errado foi atribuído a variável");
                    }
                    return(Convert.ToChar(result));
                }
            }
            else
            {
                throw new SemanticoException($"Variável {identificador.Valor} não existe.");
            }
        }
Ejemplo n.º 14
0
        private ArvoreNo AnalisarSenao()
        {
            ProximoToken(TipoToken.Senao);
            ProximoToken(TipoToken.AbreChaves);
            ArvoreNo senaoCorpo = AnalisarListaComandos();

            while (tokenAtual.Tipo != TipoToken.FechaChaves)
            {
                ArvoreNo comando = AnalisarComando();

                if (comando is VazioNo)
                {
                    break;
                }

                (senaoCorpo as ComandosNo).Comandos.Add(comando);
            }

            ProximoToken(TipoToken.FechaChaves);

            return(senaoCorpo);
        }
Ejemplo n.º 15
0
 public BooleanaNo(Token operador, ArvoreNo esq, ArvoreNo dir)
 {
     Operador = operador;
     Esquerda = esq;
     Direita  = dir;
 }
Ejemplo n.º 16
0
        public object GetExpressao(ArvoreNo esquerda, Token operacao, ArvoreNo direita)
        {
            var esq = esquerda.GetValor(this);
            var dir = direita.GetValor(this);

            var tipoEsq = esq.GetType();
            var tipoDir = dir.GetType();

            if (tipoDir.Equals(tipoEsq))
            {
                switch (operacao.Tipo)
                {
                case TipoToken.Mais:
                    if (tipoDir.Equals(typeof(int)))
                    {
                        return(Convert.ToInt32(esq) + Convert.ToInt32(dir));
                    }
                    else
                    {
                        return(Convert.ToSingle(esq) + Convert.ToSingle(dir));
                    }

                case TipoToken.Menos:
                    if (tipoDir.Equals(typeof(int)))
                    {
                        return(Convert.ToInt32(esq) - Convert.ToInt32(dir));
                    }
                    else
                    {
                        return(Convert.ToSingle(esq) - Convert.ToSingle(dir));
                    }

                case TipoToken.Vezes:
                    if (tipoDir.Equals(typeof(int)))
                    {
                        return(Convert.ToInt32(esq) * Convert.ToInt32(dir));
                    }
                    else
                    {
                        return(Convert.ToSingle(esq) * Convert.ToSingle(dir));
                    }

                case TipoToken.Dividir:
                    if (tipoDir.Equals(typeof(int)))
                    {
                        return(Convert.ToInt32(esq) / Convert.ToInt32(dir));
                    }
                    else
                    {
                        return(Convert.ToSingle(esq) / Convert.ToSingle(dir));
                    }

                default:
                    throw new SemanticoException($"Não foi possível realizar operações entre {esq} e {dir}");
                }
            }
            else
            {
                throw new SemanticoException($"Não é possível realizar operação entre {esq.GetType()} e {dir.GetType().Name}");
            }
        }