Beispiel #1
0
        public static List <Arvore> CriaArvores(List <Token> Tokens)
        {
            List <Arvore> arvores = new List <Arvore>();

            foreach (Token token in Tokens)
            {
                Arvore arvore = MontaArvore(token);
                if (arvore == null)
                {
                    return(null);
                }
                arvores.Add(arvore);
            }

            return(arvores.ToList());
        }
Beispiel #2
0
        public static Arvore MontaArvore(Token token)
        {
            Arvore arvore   = null;
            No     auxDebug = null;

            if (!ValidaPrograma(token))
            {
                return(null);
            }

            if (Regex.IsMatch(token.Texto, Dicionario.ATRIBUIDOR))
            {
                //Não é apenas uma expressão;
                if (Regex.IsMatch(token.Texto, Dicionario.REGEX_OPERADORES))
                {
                    List <string> split     = token.Texto.Split('=').ToList();
                    string        variavel  = split[0].ToString();
                    List <string> expressao = split[1].Split(' ').ToList().Where(x => !String.IsNullOrEmpty(x)).ToList();
                    List <string> ordemOp   = precedencia(expressao);
                    arvore = new Arvore()
                    {
                        NoPrincipal = new No()
                    };
                    AdicionaAtribuidor(arvore.NoPrincipal, variavel);
                    auxDebug = MontaArvoreDaExpressao(arvore.NoPrincipal.NoDireito, expressao, ordemOp);
                }
                else
                {
                    List <string> declaracaoVariavelSimples = token.Texto.Split(' ').ToList().Where(x => !String.IsNullOrEmpty(x) && !x.Equals("var")).ToList();
                    arvore = new Arvore()
                    {
                        NoPrincipal = new No()
                        {
                            Valor     = declaracaoVariavelSimples[1],
                            NoDireito = new No()
                            {
                                Valor = declaracaoVariavelSimples[2]
                            },
                            NoEsquerda = new No()
                            {
                                Valor = declaracaoVariavelSimples[0]
                            }
                        }
                    };
                }
            }
            else if (Regex.IsMatch(token.Texto, Dicionario.REGEX_OPERADORES))
            {
                List <string> expressao = token.Texto.Split(' ').ToList().Where(x => !String.IsNullOrEmpty(x)).ToList();

                arvore = new Arvore()
                {
                    NoPrincipal = new No()
                };
                List <string> ordemOp = precedencia(expressao);
                auxDebug = MontaArvoreDaExpressao(arvore.NoPrincipal, expressao, ordemOp);
            }
            Console.WriteLine("Debug: ");
            printArvore(arvore.NoPrincipal, 0);
            return(arvore);
        }