public static void global_decl_type(ADVariable variable)
        {
            if (MainFSM.PeekNextToken().Type == TokenType.leftABType) // Pokud je dalsi token leva hranata zavorka
            {
                MainFSM.GetNextToken();                               // Sezeru levou hranatou zavorku
                // Zavolam precedencni syntakticky analyzator na vyhodnoceni cisla
                var expression = PrecedenceSyntaxAnalysis.Precedence(TokenType.rightABType);

                variable.ArrayDimensions.Add(new ADArrayDimension()
                {
                    ValuesCount = expression
                });
                // Dalsi token musi byt prava hranata zavorka, jinak jde o syntaktickou chybu
                if (MainFSM.GetNextToken().Type != TokenType.rightABType)
                {
                    SyntaxError("Byl ocekavan znak /']/'");
                }

                global_decl_type(variable); // Znovu rekurzivne zavolam tuto metodu
            }

            var nextToken = MainFSM.PeekNextToken();

            if (nextToken.Type == TokenType.comType || nextToken.Type == TokenType.semiType || nextToken.Type == TokenType.asgnType)
            {
                return;
            }
            else
            {
                SyntaxError("Byl ocekavan znak \';\' nebo \',\'");
            }
        }
        /// <summary>
        /// Metoda, ktera priradi promenne (poli) hodnotu/y
        /// </summary>
        /// <param name="variable"></param>
        public static void global_assign_value(ADVariable variable)
        {
            // Pokud hodnota zacina na slozenou zavorku '{'
            if (MainFSM.PeekNextToken().Type == TokenType.leftCBType)
            {
                // Pokud slo o pole, je to chyba
                // TODO
                if (variable.Type == ADVariable.VarType.array)
                {
                    SyntaxError("Nelze priradit polozky do j*z deklarovaneho pole s pevnym poctem znaku.");
                }

                MainFSM.GetNextToken(); // Sezeru '{'
                global_array_item(variable);

                if (MainFSM.GetNextToken().Type != TokenType.rightCBType)
                {
                    SyntaxError("Byl ocekavan znak \'}\'");
                }
            }
            // Pokud to neni prirazeni hodnot do pole, prirazuji hodnotu do promenne
            else
            {
                var expression = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType, true);
                variable.Value = expression;
            }
        }
 public static IEnumerable <IADNode> st_main_list()
 {
     while (MainFSM.PeekNextToken().Type != TokenType.eofType)
     {
         yield return(st_main());
     }
 }
        public static IADNode WhileLoop()
        {
            MainFSM.GetNextToken();

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

            var result = new ADWhileLoop();

            result.Condition = PrecedenceSyntaxAnalysis.Precedence(TokenType.rightRBType);

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

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

            ParserFunctions.STablesStack.Push(new STable());
            result.Body = ParserFunctions.statement_list();

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

            ParserFunctions.STablesStack.Pop();

            return(result);
        }
        public static ADFunctionCall fce_call(string fceName = "")
        {
            STRecord fceRecord = null;
            var      result    = new ADFunctionCall();

            fceRecord = STSearch(fceName);

            if (MainFSM.GetNextToken().Type != TokenType.leftRBType)
            {
                SyntaxError("Byl ocekavan znak \'(\' pri volani funkce.");
            }

            var argumentsList = new List <IADExpression>();

            while (MainFSM.PeekNextToken().Type != TokenType.rightRBType)
            {
                argumentsList.Add(PrecedenceSyntaxAnalysis.Precedence(TokenType.rightRBType));

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

            result.Arguments = argumentsList;
            result.STRecord  = fceRecord;


            if (MainFSM.GetNextToken().Type != TokenType.rightRBType)
            {
                SyntaxError("Byl ocekavan znak \')\' pri volani funkce.");
            }

            return(result);
        }
        public static ADVariable global_variable_list()
        {
            // Pokud je dalsi token ','
            if (MainFSM.PeekNextToken().Type == TokenType.comType)
            {
                MainFSM.GetNextToken();          // Sezeru ','

                var id = MainFSM.GetNextToken(); // Dalsim znakem musi byt identifikator
                if (id.Type != TokenType.idType)
                {
                    SyntaxError("Byl ocekavan identifikator promenne.");
                }

                var variable = new ADVariable()
                {
                    Name = id.Attribute
                };
                global_decl_type(variable);
                global_assign(variable);

                // Pokud jde o pole, nastavim mu takovy typ
                if (variable.ArrayDimensions.Count > 0)
                {
                    variable.Type = ADVariable.VarType.array;
                }
                else
                {
                    variable.Type = ADVariable.VarType.variable;
                }

                return(variable);
            }

            return(null);
        }
 /// <summary>
 /// Metoda, ktera priradi (pokud je zadana) hodnotu promenne.
 /// </summary>
 /// <param name="variable"></param>
 public static void global_assign(ADVariable variable)
 {
     // Pokud je dalsi token znak /'='/
     if (MainFSM.PeekNextToken().Type == TokenType.asgnType)
     {
         var token = MainFSM.GetNextToken(); // Sezeru '='
         global_assign_value(variable);
     }
 }
        public static ADDeclaration global_decl_list(ADDeclaration declaration = null)
        {
            if (MainFSM.PeekNextToken().Type == TokenType.longType)
            {
                if (declaration == null)
                {
                    declaration = new ADDeclaration();
                }

                declaration.Variables = declaration.Variables.Concat(glob_decl().ToList()).ToList();
            }
            else if (MainFSM.PeekNextToken().Type == TokenType.idType ||
                     MainFSM.PeekNextToken().Type == TokenType.eofType)
            {
                STablesStack.Peek().Records.Clear();

                foreach (var item in declaration.Variables)
                {
                    STRecord newRecord = null;

                    if (item.Type == ADVariable.VarType.variable)
                    {
                        newRecord = new STRecord()
                        {
                            Access  = STAccess.global,
                            Type    = STType.variable,
                            Name    = item.Name,
                            Address = (item.STRecord != null) ? item.STRecord.Address : ""
                        };
                    }
                    else if (item.Type == ADVariable.VarType.array)
                    {
                        newRecord = new STRecord()
                        {
                            Access  = STAccess.global,
                            Type    = STType.array,
                            Name    = item.Name,
                            Address = (item.STRecord != null) ? item.STRecord.Address : ""
                        };
                    }

                    item.STRecord = newRecord;
                    STablesStack.Peek().Records.Add(newRecord);
                }

                return(declaration);
            }
            else
            {
                SyntaxError("Byla ocekavana deklarace globalni promenne, deklarace funkce nebo konec programu");
            }

            global_decl_list(declaration);

            return(declaration);
        }
        public static string next_decl_arg()
        {
            var token = MainFSM.GetNextToken();

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

            return(token.Attribute);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        public static IADNode Break()
        {
            if (MainFSM.GetNextToken().Type != TokenType.breakType)
            {
                ParserFunctions.SyntaxError("Bylo ocekavano slovo \'break\'");
            }

            if (MainFSM.GetNextToken().Type != TokenType.semiType)
            {
                ParserFunctions.SyntaxError("Byl ocekavan znak \';\'");
            }
            return(new ADBreak());
        }
Esempio n. 12
0
        public static IADNode Continue()
        {
            if (MainFSM.GetNextToken().Type != TokenType.continueType)
            {
                ParserFunctions.SyntaxError("Bylo ocekavano slovo \'continue\'");
            }

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

            return(new ADContinue());
        }
Esempio n. 13
0
        public void ComputeIndexes()
        {
            MainFSM.GetNextToken();
            var newIndex = PrecedenceSyntaxAnalysis.Precedence(TokenType.rightABType);

            MainFSM.GetNextToken();

            Indexes.Add(newIndex);

            if (MainFSM.PeekNextToken().Type == TokenType.leftABType)
            {
                ComputeIndexes();
            }
        }
        public static void next_global_array_item(ADVariable variable)
        {
            var nextToken = MainFSM.PeekNextToken();

            if (nextToken.Type == TokenType.comType)
            {
                MainFSM.GetNextToken(); // Sezeru carku
                global_array_item(variable);
            }

            if (MainFSM.PeekNextToken().Type == TokenType.rightCBType)
            {
                return;
            }
        }
        public static IEnumerable <string> decl_arg()
        {
            Token token = MainFSM.GetNextToken();

            while (token.Type != TokenType.rightRBType)
            {
                if (token.Type == TokenType.idType)
                {
                    yield return(token.Attribute);
                }
                else if (token.Type == TokenType.comType)
                {
                    yield return(next_decl_arg());
                }

                token = MainFSM.GetNextToken();
            }
        }
Esempio n. 16
0
        public static IADNode Return()
        {
            if (MainFSM.GetNextToken().Type != TokenType.returnType)
            {
                ParserFunctions.SyntaxError("Bylo ocekavano klicove slovo \'return\'");
            }

            var result = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType);

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

            return(new ADReturn {
                Expression = result
            });
        }
        public static List <IADNode> statement_list(bool fceDeclaration = false)
        {
            var result = new List <IADNode>();

            if (fceDeclaration)
            {
                PrecedenceSyntaxAnalysis.AnonymousDeclarations = result;
            }

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

                result.Add(statement());
            }

            return(result);
        }
Esempio n. 18
0
        public static void decl_type(ADVariable variable)
        {
            if (MainFSM.PeekNextToken().Type == TokenType.leftABType)
            {
                MainFSM.GetNextToken();
                variable.Type          = ADVariable.VarType.array;
                variable.STRecord.Type = STType.array;
                var arrayDimension = new ADArrayDimension()
                {
                    ValuesCount = value()
                };
                variable.ArrayDimensions.Add(arrayDimension);

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

                decl_type(variable);
            }
        }
Esempio n. 19
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 \'{\'");
            }
        }
Esempio n. 20
0
        public static IADNode InnerStatements()
        {
            MainFSM.GetNextToken();
            ParserFunctions.STablesStack.Push(new STable());

            var result = ParserFunctions.statement_list();

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

            ParserFunctions.STablesStack.Pop();

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

            return(new ADInnerStatements()
            {
                Statements = result
            });
        }
Esempio n. 21
0
        public static IADExpression value()
        {
            var token = MainFSM.PeekNextToken();

            if (token.Type == TokenType.idType)
            {
                if (MainFSM.PeekNextToken(2).Type == TokenType.leftRBType)
                {
                    return(fce_call());
                }
                else
                {
                    var symbol = STSearch(MainFSM.PeekNextToken().Attribute);
                    if (symbol == null)
                    {
                        SemanticError($"Promenna {token.Attribute} nebyla deklarovana.");
                    }

                    return(new ADVariable()
                    {
                        Name = symbol.Name, STRecord = symbol
                    });
                }
            }
            else
            {
                if (token.Type == TokenType.decNumberType ||
                    token.Type == TokenType.hexNumberType ||
                    token.Type == TokenType.octNumberType)
                {
                    return(PrecedenceSyntaxAnalysis.Precedence(TokenType.rightABType));
                }
            }

            return(null);
        }
        public static IADNode st_main()
        {
            var token = MainFSM.GetNextToken();

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

            if (STSearch(token.Attribute, true) != null)
            {
                SemanticError($"Funkce nebo globalni promenna se jmenem {token.Attribute} je j*z deklarovana.");
            }

            ADFunctionDeclaration fceDeclaration = new ADFunctionDeclaration()
            {
                Name = token.Attribute
            };

            var newRecord = new STRecord()
            {
                Access   = STAccess.global,
                Name     = token.Attribute,
                Type     = STType.function,
                Function = fceDeclaration
            };

            FunctionsST.Records.Add(newRecord);

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

            var arguments = decl_arg();

            var symbolTable = new STable();

            foreach (var arg in arguments)
            {
                var stRecord = new STRecord()
                {
                    Access = STAccess.local,
                    Name   = arg,
                    Type   = STType.variable
                };

                symbolTable.Records.Add(stRecord);
                fceDeclaration.Arguments.Add(new ADVariable {
                    Name = arg, Type = ADVariable.VarType.variable, STRecord = stRecord
                });
            }

            STablesStack.Push(symbolTable);

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

            fceDeclaration.Body = statement_list(true);

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

            STablesStack.Pop();
            return(fceDeclaration);
        }
Esempio n. 23
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);
        }
        public static IADNode statement()
        {
            var     nextToken = MainFSM.PeekNextToken();
            IADNode result    = null;

            switch (nextToken.Type)
            {
            case TokenType.longType:
                var list = new ADVariableDeclarations();
                StatementHelpers.VariableDeclaration(ref list);

                list.Variables.Reverse();
                result = list;
                break;

            case TokenType.starType:
                result = new ADStatementExpression {
                    Expression = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType)
                };

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

                break;

            case TokenType.idType:
            case TokenType.incrType:
            case TokenType.decrType:
                if (MainFSM.PeekNextToken(2).Type == TokenType.leftRBType)
                {
                    var fceToken = MainFSM.GetNextToken();

                    result = fce_call(fceToken.Attribute);

                    if (MainFSM.GetNextToken().Type != TokenType.semiType)
                    {
                        SyntaxError("Byl ocekavan znak \';\'");
                    }
                }
                else
                {
                    result = new ADStatementExpression {
                        Expression = PrecedenceSyntaxAnalysis.Precedence(TokenType.semiType)
                    };

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

            case TokenType.ifType:
                result = StatementHelpers.Condition();
                break;

            case TokenType.forType:
                result = StatementHelpers.ForLoop();
                break;

            case TokenType.whileType:
                result = StatementHelpers.WhileLoop();
                break;

            case TokenType.doType:
                result = StatementHelpers.DoWhileLoop();
                break;

            case TokenType.returnType:
                result = StatementHelpers.Return();
                break;

            case TokenType.breakType:
                result = StatementHelpers.Break();
                break;

            case TokenType.continueType:
                result = StatementHelpers.Continue();
                break;

            case TokenType.leftCBType:
                result = StatementHelpers.InnerStatements();
                break;

            default:
                SyntaxError("Byl ocekavan jeden z nasledujici klicovych slov nebo znaku: long, *, identifikator, if, for, while, do, return, break, continue");
                break;
            }
            return(result);
        }
Esempio n. 25
0
        private static Term GetNextTerm(TokenType endType, int numOfLeftB, int numOfRightB, ref Stack <Term> SS, bool decl = false)
        {
            var      token   = MainFSM.GetNextToken();
            TermType type    = TermType.error;
            Term     newTerm = new Term();

            #region Term type by token
            switch (token.Type)
            {
            case TokenType.exrType:
                type = TermType.not;
                break;

            case TokenType.decNumberType:
            case TokenType.hexNumberType:
            case TokenType.octNumberType:
            case TokenType.charType:
            case TokenType.idType:
            case TokenType.longType:
            case TokenType.sizeofType:
            case TokenType.stringType:
                type = TermType.id;
                break;

            case TokenType.asgnType:
                type = TermType.asgn;
                break;

            case TokenType.procType:
                type = TermType.mod;
                break;

            case TokenType.ampType:
                type = TermType.binAnd;
                break;

            case TokenType.andType:
                type = TermType.logAnd;
                break;

            case TokenType.starType:
                type = TermType.mul;
                break;

            case TokenType.plusType:
                type = TermType.plus;
                break;

            case TokenType.incrType:
                type = TermType.inc;
                break;

            case TokenType.minusType:
                type = TermType.minus;
                break;

            case TokenType.decrType:
                type = TermType.dec;
                break;

            case TokenType.divType:
                type = TermType.div;
                break;

            case TokenType.tElseType:
                type = TermType.cElse;
                break;

            case TokenType.leftRBType:
                type = TermType.leftBr;
                break;

            case TokenType.rightRBType:
                type = TermType.rightBr;
                break;

            case TokenType.lessType:
                type = TermType.less;
                break;

            case TokenType.leftType:
                type = TermType.left;
                break;

            case TokenType.lessEqType:
                type = TermType.lessEq;
                break;

            case TokenType.eqEqType:
                type = TermType.eq;
                break;

            case TokenType.grtType:
                type = TermType.grt;
                break;

            case TokenType.grtEqType:
                type = TermType.grtEq;
                break;

            case TokenType.rightType:
                type = TermType.right;
                break;

            case TokenType.tIfType:
                type = TermType.cCond;
                break;

            case TokenType.xorType:
                type = TermType.xor;
                break;

            case TokenType.bOrType:
                type = TermType.binOr;
                break;

            case TokenType.lOrType:
                type = TermType.logOr;
                break;

            case TokenType.plusEqType:
                type = TermType.plusEq;
                break;

            case TokenType.minusEqType:
                type = TermType.minusEq;
                break;

            case TokenType.multEqType:
                type = TermType.mulEq;
                break;

            case TokenType.divEqType:
                type = TermType.divEq;
                break;

            case TokenType.procEq:
                type = TermType.modEq;
                break;

            case TokenType.rightEqType:
                type = TermType.rightEq;
                break;

            case TokenType.leftEqType:
                type = TermType.leftEq;
                break;

            case TokenType.ampEqType:
                type = TermType.andEq;
                break;

            case TokenType.xorEqType:
                type = TermType.xorEq;
                break;

            case TokenType.bOrEqType:
                type = TermType.orEq;
                break;

            case TokenType.bNotType:
                type = TermType.compl;
                break;

            case TokenType.exrEqType:
                type = TermType.negEq;
                break;

            case TokenType.leftABType:
                type = TermType.leftABtype;
                break;
            }
            #endregion

            if ((endType == TokenType.rightCBType ||
                 endType == TokenType.rightRBType) &&
                token.Type == TokenType.comType &&
                (numOfLeftB == numOfRightB))
            {
                type = TermType.end;
            }

            if (endType == TokenType.semiType && decl && token.Type == TokenType.comType)
            {
                type = TermType.end;
            }

            if (token.Type == endType && (numOfLeftB == numOfRightB))
            {
                type = TermType.end;
            }

            if (token.Type == TokenType.leftCBType)
            {
                MainFSM.Index--;
                var array = new ADVariable()
                {
                    Type = ADVariable.VarType.array
                };
                array.ArrayDimensions.Add(new ADArrayDimension());

                StatementHelpers.ArrayValuesDeclaration(array, 0);

                type = TermType.id;

                array.STRecord = new STRecord {
                    Access = STAccess.local, Name = array.Name, Type = STType.array
                };
                newTerm.Expression = array;
                newTerm.STRecord   = array.STRecord;

                var declaration = new ADVariableDeclarations();
                declaration.Variables.Add(array);
                (AnonymousDeclarations as List <IADNode>).Add(declaration);
            }

            if (type != TermType.error)
            {
                if (token.Type == TokenType.decNumberType ||
                    token.Type == TokenType.hexNumberType ||
                    token.Type == TokenType.octNumberType ||
                    token.Type == TokenType.charType)
                {
                    string newId = Guid.NewGuid().ToString("N");

                    newTerm.STRecord.Name  = newId;
                    newTerm.STRecord.Value = token.Attribute;
                    newTerm.STRecord.Type  = STType.constant;
                }
                else if (token.Type == TokenType.stringType)
                {
                    string newId = Guid.NewGuid().ToString("N");
                    newTerm.STRecord.Name   = newId;
                    newTerm.STRecord.Value  = token.Attribute;
                    newTerm.STRecord.Type   = STType.str;
                    newTerm.STRecord.Access = STAccess.local;

                    var array = new ADVariable
                    {
                        STRecord = newTerm.STRecord,
                        Name     = newId,
                        Type     = ADVariable.VarType.array
                    };

                    if (MainFSM.PeekNextToken().Type == TokenType.leftABType)
                    {
                        var arrayValue = new ADArrayValue()
                        {
                            STRecord = newTerm.STRecord
                        };
                        arrayValue.ComputeIndexes();

                        newTerm.Expression = arrayValue;
                    }

                    var declaration = new ADVariableDeclarations();
                    declaration.Variables.Add(array);
                    (AnonymousDeclarations as List <IADNode>).Add(declaration);
                }
                else if (token.Type == TokenType.idType)
                {
                    var stRecord = ParserFunctions.STSearch(token.Attribute);

                    if (stRecord == null)
                    {
                        if (MainFSM.PeekNextToken().Type == TokenType.leftRBType)
                        {
                            stRecord = ParserFunctions.STablesStack.Peek().Records.Where(m => m.Name == token.Attribute).FirstOrDefault();

                            if (stRecord == null)
                            {
                                stRecord = new STRecord()
                                {
                                    Name = token.Attribute, Type = STType.function
                                };
                            }
                        }

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

                    if (stRecord.Type == STType.function)
                    {
                        if (SS.Peek().Type == TermType.reference)
                        {
                            stRecord.Address = stRecord.Name;
                            var variable = new ADVariable()
                            {
                                Name = stRecord.Name, STRecord = stRecord, Type = ADVariable.VarType.function
                            };
                            newTerm.Expression = variable;
                        }
                        else
                        {
                            var fceCall = ParserFunctions.fce_call(token.Attribute);
                            fceCall.STRecord   = stRecord;
                            newTerm.Expression = fceCall;
                        }
                    }

                    newTerm.STRecord = stRecord;
                }
                else if (token.Type == TokenType.sizeofType)
                {
                    if (MainFSM.GetNextToken().Type != TokenType.leftRBType)
                    {
                        ParserFunctions.SyntaxError("Byl ocekavan znak \'(\'");
                    }

                    if (MainFSM.GetNextToken().Type != TokenType.longType)
                    {
                        ParserFunctions.SyntaxError("Bylo ocekavano klicove slovo \'long\'");
                    }

                    if (MainFSM.GetNextToken().Type != TokenType.rightRBType)
                    {
                        ParserFunctions.SyntaxError("Byl ocekavan znak \')\'");
                    }
                    newTerm.Expression = new ADSizeOfValue();
                    type = TermType.id;
                }

                newTerm.Type = type;
                return(newTerm);
            }
            else
            {
                ParserFunctions.SyntaxError("Chyba ve vyrazu.");
                return(null);
            }
        }
Esempio n. 26
0
 public void Start()
 {
     _mainFsm = FindObjectOfType <MainFSM>();
     _mainFsm.AddRoutine(this);
 }
        public static IEnumerable <ADVariable> glob_decl()
        {
            if (MainFSM.GetNextToken().Type != TokenType.longType)
            {
                SyntaxError("Byla ocekavana deklarace globalni promenne nebo funkce.");
            }

            var id = MainFSM.GetNextToken();

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

            var variable = new ADVariable()
            {
                Name = id.Attribute
            };

            variable.STRecord = new STRecord
            {
                Access  = STAccess.global,
                Name    = variable.Name,
                Type    = STType.variable,
                Address = variable.Name
            };

            STablesStack.Peek().Records.Add(variable.STRecord);

            global_decl_type(variable);

            if (variable.ArrayDimensions.Count > 0)
            {
                variable.Type = ADVariable.VarType.array;
            }
            else
            {
                variable.Type = ADVariable.VarType.variable;
            }

            global_assign(variable);

            yield return(variable);

            List <ADVariable> variableList = new List <ADVariable>();

            variableList.Add(variable);

            while (true)
            {
                variable = global_variable_list();

                if (variable != null)
                {
                    variableList.Add(variable);
                    yield return(variable);
                }
                else
                {
                    foreach (var item in variableList)
                    {
                        if (item.Value == null)
                        {
                            item.Value = variableList.Last().Value;
                        }
                    }
                    break;
                }
            }

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

            yield break;
        }
        public static void prog(ADTree tree)
        {
            STablesStack.Push(new STable());
            var printLongFunction = new ADFunctionDeclaration()
            {
                Name = "print_long"
            };

            printLongFunction.Arguments.Add(new ADVariable());

            var printNlFunction = new ADFunctionDeclaration()
            {
                Name = "print_nl"
            };

            var printCharFunction = new ADFunctionDeclaration()
            {
                Name = "print_char"
            };

            printCharFunction.Arguments.Add(new ADVariable());

            FunctionsST.Records.Add(new STRecord()
            {
                Access = STAccess.global, Function = printLongFunction, Name = "print_long", Type = STType.function
            });
            FunctionsST.Records.Add(new STRecord()
            {
                Access = STAccess.global, Function = printNlFunction, Name = "print_nl", Type = STType.function
            });
            FunctionsST.Records.Add(new STRecord()
            {
                Access = STAccess.global, Function = printCharFunction, Name = "print_char", Type = STType.function
            });


            if (MainFSM.PeekNextToken().Type == TokenType.longType)
            {
                var global_declarations = global_decl_list();

                tree.Nodes.Add(global_declarations);
            }

            if (MainFSM.PeekNextToken().Type == TokenType.idType)
            {
                var statements_list = st_main_list();
                foreach (var item in statements_list)
                {
                    tree.Nodes.Add(item);
                }
            }

            if (MainFSM.PeekNextToken().Type == TokenType.eofType)
            {
                return;
            }
            else
            {
                SyntaxError("Byla ocekavana deklarace globalni promenne nebo funkce.");
            }
        }