Exemplo n.º 1
0
        override public EvaluateReturn Evaluate(SymbolTable st)
        {
            EvaluateReturn evalReturn = children.ElementAt(0).Evaluate(st);

            if ((TokenType)evalReturn.type != TokenType.BOOL)
            {
                throw new Exception("While must receiva a expression that returns a bool");
            }
            bool condition = (bool)evalReturn.value;

            while (condition)
            {
                children.ElementAt(1).Evaluate(st);
                evalReturn = children.ElementAt(0).Evaluate(st);
                if ((TokenType)evalReturn.type != TokenType.BOOL)
                {
                    throw new Exception("While must receiva a expression that returns a bool");
                }
                condition = (bool)evalReturn.value;
            }
            return(new EvaluateReturn()
            {
                value = null, type = null
            });
        }
Exemplo n.º 2
0
        override public EvaluateReturn Evaluate(SymbolTable st)
        {
            Token          token      = (Token)this.value;
            EvaluateReturn evalReturn = this.children.ElementAt(0).Evaluate(st);

            if (token.type == TokenType.MINUS && ((TokenType)evalReturn.type == TokenType.INT))
            {
                int value = -(int)evalReturn.value;
                return(new EvaluateReturn()
                {
                    value = value, type = TokenType.INT
                });
            }
            else if (token.type == TokenType.PLUS && ((TokenType)evalReturn.type == TokenType.INT))
            {
                int value = (int)evalReturn.value;
                return(new EvaluateReturn()
                {
                    value = value, type = TokenType.INT
                });
            }
            else if (token.type == TokenType.NOT && ((TokenType)evalReturn.type == TokenType.BOOL))
            {
                bool value = !(bool)evalReturn.value;
                return(new EvaluateReturn()
                {
                    value = value, type = TokenType.BOOL
                });
            }
            else
            {
                throw new Exception("Operação unária com tipos inválidos");
            }
            throw new Exception("Um Nó foi classificado como UnOp sem ser um token do tipo PLUS,MINUS ou NOT");
        }
Exemplo n.º 3
0
        override public EvaluateReturn Evaluate(SymbolTable st)
        {
            EvaluateReturn conditionEval = children.ElementAt(0).Evaluate(st);

            if ((TokenType)conditionEval.type != TokenType.BOOL)
            {
                throw new Exception("Condition must be based on a boolean expression");
            }
            bool condition = (bool)conditionEval.value;
            bool hasElse   = children.Count() == 3;

            if (condition)
            {
                children.ElementAt(1).Evaluate(st);
            }
            else
            {
                if (hasElse)
                {
                    children.ElementAt(2).Evaluate(st);
                }
            }
            return(new EvaluateReturn()
            {
                value = null, type = null
            });
        }
Exemplo n.º 4
0
        override public EvaluateReturn Evaluate(SymbolTable st)
        {
            EvaluateReturn evalReturn = this.children.ElementAt(0).Evaluate(st);

            Console.WriteLine(evalReturn.value);
            return(new EvaluateReturn()
            {
                value = null, type = null
            });
        }
Exemplo n.º 5
0
        override public EvaluateReturn Evaluate(SymbolTable st)
        {
            Token          childrenToken = (Token)this.children.ElementAt(0).value;
            string         stKey         = (string)childrenToken.value;
            EvaluateReturn evalReturn    = this.children.ElementAt(1).Evaluate(st);
            TokenType      varType       = (TokenType)st.GetType(stKey);

            if (varType == TokenType.INTEGER)
            {
                if ((TokenType)evalReturn.type == TokenType.INT)
                {
                    int assignmentValue = (int)evalReturn.value;
                    st.SetValue(stKey, assignmentValue);
                }
                else
                {
                    throw new Exception("Variable is INT but boolean was given");
                }
            }
            else
            {
                if ((TokenType)evalReturn.type == TokenType.BOOL)
                {
                    bool assignmentValue = (bool)evalReturn.value;
                    st.SetValue(stKey, assignmentValue);
                }
                else
                {
                    throw new Exception("Variable is Bool but Integer was given");
                }
            }
            return(new EvaluateReturn()
            {
                value = null, type = null
            });
        }
Exemplo n.º 6
0
        override public EvaluateReturn Evaluate(SymbolTable st)
        {
            //criando nova symbol table para escopo
            SymbolTable newScope = new SymbolTable();

            newScope.Daddy = st;

            //resgatando função da st
            Token  token = (Token)value;
            string key   = (string)token.value;


            Node function;

            function = (Node)st.GetValue(key, true);

            //checking if arguments passed has the same length
            int n_args = function.children.Count() - 1;

            if (children.Count() != (n_args))
            {
                throw new Exception($"Func {key} receives {n_args} but {children.Count()} were given");
            }
            int counter = 0;

            //reserva uma variavel para retorno da função na st
            TokenType functionType = (TokenType)function.type.Evaluate(st).type;

            if (functionType == TokenType.INTEGER)
            {
                newScope.SetType(key, TokenType.INTEGER);
            }
            else if (functionType == TokenType.BOOLEAN)
            {
                newScope.SetType(key, TokenType.BOOLEAN);
            }

            //inicializacao das variaveis no novo scopo
            while (counter < n_args)
            {
                //inicializa vardecs
                function.children.ElementAt(counter).Evaluate(newScope);

                //seta valor das variaveis com base nos argumentos passados
                EvaluateReturn arg = children.ElementAt(counter).Evaluate(st);
                Token          declarationVariableNameToken = (Token)function.children.ElementAt(counter).children.ElementAt(0).value;
                string         declarationVariableName      = (string)declarationVariableNameToken.value;
                TokenType      stType = (TokenType)newScope.GetType(declarationVariableName);
                if (stType == TokenType.INTEGER)
                {
                    if ((TokenType)arg.type != TokenType.INT)
                    {
                        throw new Exception("Cant convert to int");
                    }
                    newScope.SetValue(declarationVariableName, (int)arg.value);
                }
                else if (stType == TokenType.BOOLEAN)
                {
                    if ((TokenType)arg.type != TokenType.BOOL)
                    {
                        throw new Exception("Cant convert to bool");
                    }
                    newScope.SetValue(declarationVariableName, (bool)arg.value);
                }

                counter += 1;
            }

            //running function code
            int statementsIndex = n_args;

            function.children.ElementAt(statementsIndex).Evaluate(newScope);

            //return
            if (functionType != TokenType.BOOLEAN)
            {
                return(new EvaluateReturn()
                {
                    value = newScope.GetValue(key), type = TokenType.INT
                });
            }
            else
            {
                return(new EvaluateReturn()
                {
                    value = newScope.GetValue(key), type = TokenType.BOOL
                });
            }
        }
Exemplo n.º 7
0
        override public EvaluateReturn Evaluate(SymbolTable st)
        {
            EvaluateReturn firstNumber  = this.children.ElementAt(0).Evaluate(st);
            EvaluateReturn secondNumber = this.children.ElementAt(1).Evaluate(st);

            if ((TokenType)firstNumber.type != (TokenType)secondNumber.type)
            {
                throw new Exception("Operação com dois tipos diferentes de variável");
            }

            Token token = (Token)this.value;

            if (token.type == TokenType.MINUS && ((TokenType)firstNumber.type == TokenType.INT))
            {
                return(new EvaluateReturn()
                {
                    value = (int)firstNumber.value - (int)secondNumber.value, type = TokenType.INT
                });
            }
            else if (token.type == TokenType.PLUS && ((TokenType)firstNumber.type == TokenType.INT))
            {
                return(new EvaluateReturn()
                {
                    value = (int)firstNumber.value + (int)secondNumber.value, type = TokenType.INT
                });
            }
            else if (token.type == TokenType.MULTIPLY && ((TokenType)firstNumber.type == TokenType.INT))
            {
                return(new EvaluateReturn()
                {
                    value = (int)firstNumber.value * (int)secondNumber.value, type = TokenType.INT
                });
            }
            else if (token.type == TokenType.DIVIDE && ((TokenType)firstNumber.type == TokenType.INT))
            {
                return(new EvaluateReturn()
                {
                    value = (int)firstNumber.value / (int)secondNumber.value, type = TokenType.INT
                });
            }
            else if (token.type == TokenType.BIGGERTHEN && ((TokenType)firstNumber.type == TokenType.INT))
            {
                return(new EvaluateReturn()
                {
                    value = (int)firstNumber.value > (int)secondNumber.value, type = TokenType.BOOL
                });
            }
            else if (token.type == TokenType.SMALLERTHEN && ((TokenType)firstNumber.type == TokenType.INT))
            {
                return(new EvaluateReturn()
                {
                    value = (int)firstNumber.value < (int)secondNumber.value, type = TokenType.BOOL
                });
            }
            else if (token.type == TokenType.AND && ((TokenType)firstNumber.type == TokenType.BOOL))
            {
                return(new EvaluateReturn()
                {
                    value = (bool)firstNumber.value && (bool)secondNumber.value, type = TokenType.BOOL
                });
            }
            else if (token.type == TokenType.OR && ((TokenType)firstNumber.type == TokenType.BOOL))
            {
                return(new EvaluateReturn()
                {
                    value = (bool)firstNumber.value || (bool)secondNumber.value, type = TokenType.BOOL
                });
            }
            else if (token.type == TokenType.EQUAL)
            {
                if ((TokenType)firstNumber.type == TokenType.INT)
                {
                    return(new EvaluateReturn()
                    {
                        value = (int)firstNumber.value == (int)secondNumber.value, type = TokenType.BOOL
                    });
                }
                else
                {
                    return(new EvaluateReturn()
                    {
                        value = (bool)firstNumber.value == (bool)secondNumber.value, type = TokenType.BOOL
                    });
                }
            }
            else
            {
                throw new Exception("Tipo de variável não compatível com a operação realizada");
            }
            throw new Exception("Um Nó foi classificado como BinOp sem ser um token do tipo PLUS | MINUS | MULTIPLY | DIVIDE || SMALLERTHEN || BIGGERTHEN || EQUAL");
        }