Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Metoda, ktera vrati zaznam (pokud existuje) z globalni nebo lokalni tabulky symbolu. Pokud jde o deklaraci, vyhledavam
        /// jen v lokalni tabulce symbolu
        /// </summary>
        /// <param name="fce"></param>
        /// <param name="decl"></param>
        /// <returns></returns>
        public static STRecord STSearch(string varName, bool decl = false)
        {
            // Pokud jde o deklaraci, hledam jen na aktualni urovni zasobniku tabulek symbolu
            STRecord symbol = null;

            if (decl)
            {
                // U deklarace se prvne podivam, jestli uz neexistuje funkce s timto jmenem, v tom pripade deklarace neni mozna
                symbol = FunctionsST.Records.Where(m => m.Name == varName).FirstOrDefault();

                if (symbol == null)
                {
                    symbol = STablesStack.Peek().Records.Where(m => m.Name == varName).FirstOrDefault();
                }
            }
            else
            {
                // Jestle zkusim vyhledat v tabulce symbolu pro funkce
                symbol = FunctionsST.Records.Where(m => m.Name == varName).FirstOrDefault();

                if (symbol != null)
                {
                    return(symbol);
                }

                for (int i = 0; i < STablesStack.Count; i++)
                {
                    symbol = STablesStack.ElementAt(i).Records.Where(m => m.Name == varName).FirstOrDefault();

                    if (symbol != null)
                    {
                        break;
                    }
                }
            }

            return(symbol);
        }
Ejemplo n.º 4
0
        private static IADExpression GetExpression(STRecord stRecord)
        {
            IADExpression result = null;

            if (stRecord.Type == STType.constant)
            {
                result = new ADConstant {
                    Value = stRecord.Value
                };
            }
            else if (stRecord.Type == STType.variable || stRecord.Type == STType.array)
            {
                result = new ADVariable {
                    Name = stRecord.Name, Type = ADVariable.VarType.variable, STRecord = stRecord
                };
            }
            else if (stRecord.Type == STType.function)
            {
                MainFSM.Index--;
                result = ParserFunctions.fce_call();
            }

            return(result);
        }
Ejemplo n.º 5
0
 public Term()
 {
     STRecord = new STRecord();
 }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
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);
        }