Exemplo n.º 1
0
        public static ADVariableAssignment IDAssign(bool dereference)
        {
            var token = MainFSM.GetNextToken();

            if (token.Type != TokenType.idType)
            {
                ParserFunctions.SyntaxError("Byla ocekavana promenna.");
            }

            var record = ParserFunctions.STSearch(token.Attribute);

            if (record == null)
            {
                ParserFunctions.SemanticError($"Promenna {token.Attribute} nebyla deklarovana");
            }

            var asgnOperator = MainFSM.GetNextToken();

            if (!IsAssignOperator(asgnOperator.Type))
            {
                ParserFunctions.SyntaxError("Byl ocekavan prirazovaci operator");
            }

            var assign = new ADVariableAssignment()
            {
                Dereference = dereference, STRecord = record, AssignOperator = asgnOperator.Type
            };

            bool nextDereference = false;

            if (MainFSM.PeekNextToken().Type == TokenType.starType)
            {
                nextDereference = true;
                MainFSM.GetNextToken();
            }

            if (MainFSM.PeekNextToken().Type == TokenType.idType)
            {
                if (IsAssignOperator(MainFSM.PeekNextToken(2).Type))
                {
                    assign.Expression = IDAssign(nextDereference);
                }
                else
                {
                    assign.Expression = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType);
                }
            }
            else
            {
                assign.Expression = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType);
            }

            return(assign);
        }
Exemplo n.º 2
0
        public static void ArrayValuesDeclaration(ADVariable array, int dimensionIndex)
        {
            if (array.ArrayDimensions.Count > 0 && array.ArrayDimensions.Count < (dimensionIndex + 1))
            {
                ParserFunctions.SemanticError("Dane pole nema tolik dimenzi.");
            }

            array.Type = ADVariable.VarType.array;

            if (MainFSM.GetNextToken().Type == TokenType.leftCBType)
            {
                while (MainFSM.PeekNextToken().Type != TokenType.rightCBType)
                {
                    array.ArrayDimensions[dimensionIndex].Values.Add
                    (
                        PrecedenceSyntaxAnalysis.Precedence(TokenType.rightCBType)
                    );

                    if (MainFSM.PeekNextToken().Type == TokenType.comType)
                    {
                        MainFSM.GetNextToken();
                    }
                }

                if (MainFSM.GetNextToken().Type != TokenType.rightCBType)
                {
                    ParserFunctions.SyntaxError("Byl ocekavan znak \'}\'");
                }

                if (MainFSM.PeekNextToken().Type == TokenType.comType)
                {
                    ArrayValuesDeclaration(array, dimensionIndex++);
                }
            }
            else
            {
                ParserFunctions.SyntaxError("Byl ocekavan znak \'{\'");
            }
        }
Exemplo n.º 3
0
        public static void VariableDeclaration(ref ADVariableDeclarations list, bool recursive = false)
        {
            if (!recursive)
            {
                if (MainFSM.GetNextToken().Type != TokenType.longType)
                {
                    ParserFunctions.SyntaxError("Bylo ocekavano klicove slovo \'long\'");
                }
            }

            var token = MainFSM.GetNextToken();

            if (token.Type != TokenType.idType)
            {
                ParserFunctions.SyntaxError("Byl ocekavan identifikator");
            }

            if (ParserFunctions.STSearch(token.Attribute, true) != null)
            {
                ParserFunctions.SemanticError($"Promenna {token.Attribute} byla j*z deklarovana");
            }

            var newRecord = new STRecord()
            {
                Name   = token.Attribute,
                Access = STAccess.local
            };

            ParserFunctions.STablesStack.Peek().Records.Add(newRecord);

            var variableDeclaration = new ADVariable()
            {
                Name     = token.Attribute,
                STRecord = newRecord,
                Type     = ADVariable.VarType.variable
            };

            ParserFunctions.decl_type(variableDeclaration);


            if (MainFSM.PeekNextToken().Type == TokenType.comType)
            {
                MainFSM.GetNextToken();
                VariableDeclaration(ref list, true);
            }

            if (MainFSM.PeekNextToken().Type == TokenType.semiType)
            {
                list.Variables.Add(variableDeclaration);

                if (!recursive)
                {
                    MainFSM.GetNextToken();
                }
                return;
            }

            if (MainFSM.GetNextToken().Type != TokenType.asgnType)
            {
                ParserFunctions.SyntaxError("Byl ocekavan znak \';\' nebo \'=\'");
            }

            if (MainFSM.PeekNextToken().Type == TokenType.leftCBType)
            {
                variableDeclaration.STRecord.Type = STType.array;

                if (variableDeclaration.ArrayDimensions.Count == 0)
                {
                    variableDeclaration.ArrayDimensions.Add(new ADArrayDimension());
                }
                ArrayValuesDeclaration(variableDeclaration, 0);
            }
            else
            {
                variableDeclaration.STRecord.Type = STType.variable;

                variableDeclaration.Value = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType, true);
            }

            if (MainFSM.PeekNextToken().Type == TokenType.comType)
            {
                MainFSM.GetNextToken();
                VariableDeclaration(ref list, true);
            }

            if (!recursive)
            {
                if (MainFSM.GetNextToken().Type != TokenType.semiType)
                {
                    ParserFunctions.SyntaxError("Byl ocekavan znak \';\'");
                }
            }

            list.Variables.Add(variableDeclaration);
        }