private bool ParseVariables(Lexer lexer, Environment currentEnvironment, List <Local> localVariables)
        {
            var   successfullyParsed = true;
            Token token;

            do
            {
                var identifierToken = lexer.GetNextToken();
                ValidateToken(identifierToken, TokenType.Identifier);

                // SEM: Verifico que no exista un identificador con el mismo nombre en el entorno local.
                if (currentEnvironment.Locals.Exists(identifierToken.Lexeme) ||
                    currentEnvironment.FormalParameters.Exists(identifierToken.Lexeme) ||
                    localVariables.Any(x => x.Identifier == identifierToken.Lexeme))
                {
                    LogDuplicateIdentifierFound(identifierToken.Lexeme, identifierToken);
                    successfullyParsed = false;
                }

                token = lexer.GetNextToken();

                if (token.Is(TokenType.LeftSquareBracket))
                {
                    var arraySizeToken = lexer.GetNextToken();

                    ValidateToken(arraySizeToken, TokenType.LiteralNumber);
                    ValidateToken(lexer.GetNextToken(), TokenType.RightSquareBracket);

                    if (!currentEnvironment.IsGlobal)
                    {
                        LogArrayCantBeDeclaredOutsideGlobalEnvironment(identifierToken);
                        successfullyParsed = false;
                    }

                    var arraySize = Convert.ToInt32(arraySizeToken.Lexeme);

                    localVariables.Add(new Local()
                    {
                        Identifier = identifierToken.Lexeme,
                        ArraySize  = arraySize,
                        IsArray    = true
                    });

                    token = lexer.GetNextToken();
                }
                else
                {
                    localVariables.Add(new Local()
                    {
                        Identifier = identifierToken.Lexeme
                    });
                }
            } while (token.Is(TokenType.ListSeparator));

            return(successfullyParsed);
        }
Example #2
0
        private bool ParseParameters(Lexer lexer, Environment environment)
        {
            var successfullyParsed = true;

            do
            {
                var token = lexer.GetNextToken();

                if (token.Is(TokenType.RightParenthesis))
                {
                    return(true);
                }

                var parameter = new FormalParameter();

                if (token.Is(TokenType.ByVal) || token.Is(TokenType.ByRef))
                {
                    parameter.ParameterType = token.Is(TokenType.ByVal)
                        ? ParameterType.ByVal : ParameterType.ByRef;

                    token = lexer.GetNextToken();
                    ValidateToken(token, TokenType.Identifier);
                }
                else
                {
                    parameter.ParameterType = ParameterType.ByVal;

                    ValidateToken(token, TokenType.Identifier);
                }

                parameter.Identifier = token.Lexeme;

                // SEM: Validar que no haya parametros con nombres duplicados.
                if (environment.FormalParameters.Exists(parameter.Identifier))
                {
                    LogDuplicateIdentifierFound(parameter.Identifier, token);
                    successfullyParsed = false;
                }

                ValidateToken(lexer.GetNextToken(), TokenType.IdTypeSeparator);

                token = lexer.GetNextToken();
                ValidateToken(token, TokenType.IntegerDataType, TokenType.BooleanDataType);

                parameter.DataType = token.Type == TokenType.IntegerDataType
                    ? DataType.Integer : DataType.Boolean;

                environment.FormalParameters.AddParameter(parameter);
            } while (lexer.GetNextToken().Is(TokenType.ListSeparator));

            return(successfullyParsed);
        }
Example #3
0
        private Function ParseFunctionDeclaration(Lexer lexer, Token token,
                                                  Environment parentEnvironment, out bool successfullyParsed)
        {
            Function function = null;

            successfullyParsed = true;

            try
            {
                ValidateToken(token, TokenType.FunctionDefinition);

                var identifierToken = lexer.GetNextToken();
                ValidateToken(identifierToken, TokenType.Identifier);

                // SEM: Verificar que no exista una subrutina con el mismo nombre.
                if (parentEnvironment.Subroutines.Exists(identifierToken.Lexeme))
                {
                    LogDuplicateIdentifierFound(identifierToken.Lexeme, identifierToken);
                    successfullyParsed = false;
                }

                function = new Function(identifierToken.Lexeme);

                successfullyParsed &= ParserFactory.GetFormalParametersParser().Parse(
                    lexer, lexer.GetNextToken(), Attributes.Create(function.Environment, "ENV"));

                ValidateToken(lexer.GetNextToken(), TokenType.IdTypeSeparator);

                var returnTypeToken = lexer.GetNextToken();
                ValidateToken(returnTypeToken, TokenType.IntegerDataType, TokenType.BooleanDataType);

                function.ReturnType = returnTypeToken.Type == TokenType.BooleanDataType
                    ? DataType.Boolean
                    : DataType.Integer;

                ValidateToken(lexer.GetNextToken(), TokenType.EndOfInstruction);
            }
            catch (Exception ex)
            {
                successfullyParsed = false;
                Logger(ex.Message);
                ErrorRecovery(lexer);
            }

            return(function);
        }
        private bool ParseConstants(Lexer lexer, Environment currentEnvironment)
        {
            var successfullyParsed = true;

            do
            {
                var identifierToken = lexer.GetNextToken();
                ValidateToken(identifierToken, TokenType.Identifier);

                // SEM: Verifico que no exista un identificador con el mismo nombre en el entorno local.
                if (currentEnvironment.Locals.Exists(identifierToken.Lexeme) &&
                    currentEnvironment.FormalParameters.Exists(identifierToken.Lexeme))
                {
                    LogDuplicateIdentifierFound(identifierToken.Lexeme, identifierToken);
                    successfullyParsed = false;
                }

                ValidateToken(lexer.GetNextToken(), TokenType.IdTypeSeparator);

                var dataTypeToken = lexer.GetNextToken();
                ValidateToken(dataTypeToken, TokenType.IntegerDataType, TokenType.BooleanDataType);

                ValidateToken(lexer.GetNextToken(), TokenType.EqualOperator);

                var valueToken = lexer.GetNextToken();
                ValidateToken(valueToken, TokenType.LiteralNumber, TokenType.LiteralBoolean);

                var dataType = dataTypeToken.Type == TokenType.IntegerDataType
                        ? DataType.Integer : DataType.Boolean;

                currentEnvironment.Locals.AddLocal(new Local()
                {
                    Identifier = identifierToken.Lexeme,
                    IsConstant = true,
                    Type       = dataType,
                    Value      = valueToken.Lexeme
                });
            } while (lexer.GetNextToken().Is(TokenType.ListSeparator));

            return(successfullyParsed);
        }
Example #5
0
        private Procedure ParseProcedureDeclaration(Lexer lexer, Token token,
                                                    Environment parentEnvironment, out bool successfullyParsed)
        {
            Procedure procedure = null;

            successfullyParsed = true;

            try
            {
                ValidateToken(token, TokenType.ProcedureDefinition);

                var identifierToken = lexer.GetNextToken();
                ValidateToken(identifierToken, TokenType.Identifier);

                // SEM: Verificar que no exista una subrutina con el mismo nombre.
                if (parentEnvironment.Subroutines.Exists(identifierToken.Lexeme))
                {
                    LogDuplicateIdentifierFound(identifierToken.Lexeme, identifierToken);
                    successfullyParsed = false;
                }

                procedure = new Procedure(identifierToken.Lexeme);

                successfullyParsed &= ParserFactory.GetFormalParametersParser().Parse(
                    lexer, lexer.GetNextToken(), Attributes.Create(procedure.Environment, "ENV"));

                ValidateToken(lexer.GetNextToken(), TokenType.EndOfInstruction);
            }
            catch (Exception ex)
            {
                successfullyParsed = false;

                Logger(ex.Message);
                ErrorRecovery(lexer);
            }

            return(procedure);
        }