Example #1
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);
            }
        }