protected override List<AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            hasChanged = false;

            if (!IsStatement || expression.Type != AphidNodeType.IdentifierExpression)
            {
                return null;
            }

            var id = expression.ToIdentifier();
            var attributes = AphidAttributeParser.Parse<DeclarativeStatementAttributes>(id);
            var nameId = new IdentifierExpression(attributes.Name);

            if (_tokenTypes.Contains(id.Identifier))
            {
                hasChanged = true;

                var match = new CallExpression(
                    new IdentifierExpression("Match"),
                    new IdentifierExpression(id.Identifier));

                return attributes.Name == null ?
                    new List<AphidExpression> { match, } :
                    new List<AphidExpression>
                    {
                        new BinaryOperatorExpression(
                            nameId,
                            AphidTokenType.AssignmentOperator,
                            new IdentifierExpression("TokenType")),
                        match,
                    };
            }
            else if (_parseFunctions.Contains(id.Identifier))
            {
                hasChanged = true;
                var call = new CallExpression(new IdentifierExpression(id.Identifier));

                return attributes.Name == null ?
                    new List<AphidExpression> { call } :
                    new List<AphidExpression>
                    {
                        new BinaryOperatorExpression(
                            nameId,
                            AphidTokenType.AssignmentOperator,
                            call)
                    };
            }
            else if (id.Identifier == "NextToken")
            {
                hasChanged = true;

                return new List<AphidExpression>
                {
                    new CallExpression(new IdentifierExpression(id.Identifier))
                };
            }

            return null;
        }
Exemplo n.º 2
0
        protected override List <AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            if (expression.Type != AphidExpressionType.ThisExpression &&
                (expression.Type != AphidExpressionType.IdentifierExpression ||
                 expression.ToIdentifier().Identifier != "this"))
            {
                hasChanged = false;

                return(null);
            }

            hasChanged = true;

            return(new List <AphidExpression> {
                new IdentifierExpression(AphidSerializer.Root)
            });
        }
Exemplo n.º 3
0
        protected override List<AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            hasChanged = false;

            CallExpression call;
            IdentifierExpression id;

            // Todo: handle member access e.g. decimal.Parse
            if (expression.Type == AphidNodeType.CallExpression &&
                (call = expression.ToCall()).FunctionExpression.Type == AphidNodeType.IdentifierExpression &&
                _parseFunctions.Contains(call.FunctionExpression.ToIdentifier().Identifier))
            {
                _skip = true;
            }
            //else if (expression.Type == AphidNodeType.BinaryOperatorExpression &&
            //    (binOp = expression.ToBinaryOperator()).LeftOperand.Type == AphidNodeType.IdentifierExpression &&
            //    _parseFunctions.Contains(binOp.LeftOperand.ToIdentifier().Identifier))
            //{
            //    _skip = true;
            //}
            else if (expression.Type == AphidNodeType.IdentifierExpression &&
                _parseFunctions.Contains((id = expression.ToIdentifier()).Identifier))
            {
                if (_skip)
                {
                    _skip = false;
                }
                else
                {
                    hasChanged = true;

                    return new List<AphidExpression> { new CallExpression(id) };
                }
            }

            return null;
        }
Exemplo n.º 4
0
        protected override List<AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            hasChanged = false;

            if (IsStatement && expression.Type == AphidNodeType.IdentifierExpression)
            {
                var idExp = expression.ToIdentifier();
                var currentId = ParserIdentifier.FromIdentifierExpression(idExp);
                ParserIdentifier inferredId;

                if (!_idTable.TryResolve(idExp.Identifier, out inferredId))
                {
                    _idTable.Add(idExp.Identifier, currentId);
                    hasChanged = true;
                }
                else
                {
                    if (!currentId.IsList && inferredId.IsList)
                    {
                        currentId.IsList = inferredId.IsList;
                        currentId.Type = inferredId.Type;
                        hasChanged = true;

                        return new List<AphidExpression>
                        {
                            new BinaryOperatorExpression(
                                currentId.ToIdentifierExpression(),
                                AphidTokenType.AssignmentOperator,
                                new IdentifierExpression(
                                    currentId.Type ?? _config.BaseClass,
                                    new List<IdentifierExpression> { new IdentifierExpression("list") }))
                        };
                    }
                }
            }

            CallExpression funcExp;
            BinaryOperatorExpression binOpExp;

            if (expression.Type == AphidNodeType.CallExpression &&
                (funcExp = expression.ToCall()).FunctionExpression.Type == AphidNodeType.BinaryOperatorExpression &&
                ((binOpExp = funcExp.FunctionExpression.ToBinaryOperator()).Operator == AphidTokenType.MemberOperator) &&
                binOpExp.LeftOperand.Type == AphidNodeType.IdentifierExpression &&
                binOpExp.RightOperand.Type == AphidNodeType.IdentifierExpression &&
                binOpExp.RightOperand.ToIdentifier().Identifier == "Add")
            {
                var id = binOpExp.LeftOperand.ToIdentifier().Identifier;
                ParserIdentifier ids;

                if (!_idTable.TryResolve(id, out ids))
                {
                    _idTable.Add(id, ids = new ParserIdentifier() { Name = id });
                }

                if (!ids.IsList)
                {
                    ids.IsList = true;

                    if (ids.Type == null)
                    {
                        ids.Type = _config.BaseClass;
                    }

                    hasChanged = true;
                }
            }

            UnaryOperatorExpression unOpExp;

            if (expression.Type == AphidNodeType.UnaryOperatorExpression &&
                (unOpExp = expression.ToUnaryOperator()).Operator == AphidTokenType.retKeyword)
            {
                if (unOpExp.Operand.Type == AphidNodeType.IdentifierExpression)
                {
                    var retId = unOpExp.Operand.ToIdentifier();

                    if (!_returnIds.Contains(retId.Identifier))
                    {
                        hasChanged = true;
                        _returnIds.Add(retId.Identifier);
                    }
                }
            }
            //else if (expression.Type == AphidNodeType.BinaryOperatorExpression &&
            //    (binOpExp = expression.ToBinaryOperator()).

            return new List<AphidExpression> { expression };
            //throw new NotImplementedException();
        }
Exemplo n.º 5
0
        protected override List <AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            hasChanged = false;

            if (!IsStatement || expression.Type != AphidExpressionType.IdentifierExpression)
            {
                return(null);
            }

            var id         = expression.ToIdentifier();
            var attributes = AphidAttributeParser.Parse <DeclarativeStatementAttributes>(id);
            var nameId     = new IdentifierExpression(attributes.Name);

            if (_tokenTypes.Contains(id.Identifier))
            {
                hasChanged = true;

                var match = new CallExpression(
                    new IdentifierExpression("Match"),
                    new IdentifierExpression(id.Identifier));

                return(attributes.Name == null ?
                       new List <AphidExpression> {
                    match
                } :
                       new List <AphidExpression>
                {
                    new BinaryOperatorExpression(
                        nameId,
                        AphidTokenType.AssignmentOperator,
                        new IdentifierExpression("TokenType")),
                    match
                });
            }
            else if (_parseFunctions.Contains(id.Identifier))
            {
                hasChanged = true;
                var call = new CallExpression(new IdentifierExpression(id.Identifier));

                return(attributes.Name == null ?
                       new List <AphidExpression> {
                    call
                } :
                       new List <AphidExpression>
                {
                    new BinaryOperatorExpression(
                        nameId,
                        AphidTokenType.AssignmentOperator,
                        call)
                });
            }
            else if (id.Identifier == "NextToken")
            {
                hasChanged = true;

                return(new List <AphidExpression> {
                    new CallExpression(new IdentifierExpression(id.Identifier))
                });
            }

            return(null);
        }