Пример #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);
        }
Пример #2
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);
        }