Ejemplo n.º 1
0
        private bool ParseOperand(Lexer lexer, Token token, Attributes attributes)
        {
            var successfullyParsed = true;

            if (token.Is(TokenType.LeftParenthesis))
            {
                successfullyParsed &= Parse(lexer, lexer.GetNextToken(), attributes);
                ValidateToken(lexer.GetCurrentToken(), TokenType.RightParenthesis);
            }
            else
            {
                Expression tree = null;

                if (token.Is(TokenType.LiteralNumber))
                {
                    tree = new LiteralNumberExpression(Convert.ToInt16(token.Lexeme));
                }
                else if (token.Is(TokenType.LiteralBoolean))
                {
                    tree = new LiteralBooleanExpression(Convert.ToBoolean(token.Lexeme));
                }
                else if (token.Is(TokenType.Identifier))
                {
                    var identifierName = token.Lexeme;

                    token = lexer.GetNextToken();

                    if (token.Is(TokenType.LeftSquareBracket))
                    {
                        successfullyParsed &= ParsePositionInArray(lexer, attributes, identifierName, out tree);
                    }
                    else if (token.Is(TokenType.LeftParenthesis))
                    {
                        successfullyParsed &= ParseFunctionCall(lexer, attributes, identifierName, out tree);
                    }
                    else
                    {
                        successfullyParsed &= ParseVariableOrConstantOrParameter(lexer, attributes, identifierName, out tree);
                        UpdateExpressionAttribute(attributes, tree);

                        return(successfullyParsed);
                    }
                }

                UpdateExpressionAttribute(attributes, tree);
            }

            lexer.GetNextToken();

            return(successfullyParsed);
        }
Ejemplo n.º 2
0
        private bool ParseVariableOrConstantOrParameter(Lexer lexer, Attributes attributes, string identifierName, out Expression tree)
        {
            var environments = attributes["ENVS"] as Environments;

            // SEM: Verifico que exista un identificador en los entornos.
            if (environments.ExistsLocal(identifierName))
            {
                var localReference = environments.FindLocal(identifierName);
                var local          = environments.GetLocal(localReference);

                if (local != null)
                {
                    // Las constantes se traducen directamente a los literales correspondientes.
                    if (local.IsConstant)
                    {
                        if (local.Type == DataType.Integer)
                        {
                            tree = new LiteralNumberExpression(Convert.ToInt16(local.Value));
                        }
                        else
                        {
                            tree = new LiteralBooleanExpression(Convert.ToBoolean(local.Value));
                        }
                    }
                    else
                    {
                        var identifierExpression = new IdentifierExpression();

                        identifierExpression.Name      = identifierName;
                        identifierExpression.Reference = localReference;
                        identifierExpression.Type      = local.Type;

                        tree = identifierExpression;
                    }
                }
                else
                {
                    tree = null;
                }
            }
            else if (environments[0].ExistsParameter(identifierName))
            {
                var parameterReference = environments[0].FindParameter(identifierName);
                var parameter          = environments[0].GetParameter(parameterReference);

                if (parameter != null)
                {
                    var identifierExpression = new IdentifierExpression();

                    identifierExpression.Name      = identifierName;
                    identifierExpression.Reference = parameterReference;
                    identifierExpression.Type      = parameter.DataType;

                    tree = identifierExpression;
                }
                else
                {
                    tree = null;
                }
            }
            else
            {
                LogIdentifierNotFound(identifierName, lexer.GetCurrentToken());

                tree = null;

                return(false);
            }

            return(true);
        }