예제 #1
0
파일: AphidMacro.cs 프로젝트: 5l1v3r1/Aphid
        public static ArraySegment <AphidMacro> Parse(List <AphidExpression> ast)
        {
            var macros     = new AphidMacro[ast.Count];
            var macroCount = 0;

            for (var i = 0; i < ast.Count; i++)
            {
                var node = ast[i];
                BinaryOperatorExpression binOp;
                CallExpression           call;

                if (node.Type == AphidExpressionType.BinaryOperatorExpression &&
                    ((binOp = (BinaryOperatorExpression)node).LeftOperand.Type ==
                     AphidExpressionType.IdentifierExpression) &&
                    binOp.RightOperand.Type == AphidExpressionType.CallExpression &&
                    (call = (CallExpression)binOp.RightOperand).FunctionExpression.Type ==
                    AphidExpressionType.IdentifierExpression &&
                    ((IdentifierExpression)call.FunctionExpression).Identifier == "macro" &&
                    call.Args.Count == 1 &&
                    call.Args[0].Type == AphidExpressionType.FunctionExpression)
                {
                    macros[macroCount++] = new AphidMacro(
                        ((IdentifierExpression)binOp.LeftOperand).Identifier,
                        (FunctionExpression)call.Args[0],
                        binOp);
                }
            }

            return(new ArraySegment <AphidMacro>(macros, 0, macroCount));
        }
예제 #2
0
        protected override List <AphidExpression> OnMutate(List <AphidExpression> ast)
        {
            var macros = AphidMacro.Parse(ast);

            for (var i = 0; i < macros.Count; i++)
            {
                var m = macros.Array[i];

                if (_macros.ContainsKey(m.Name))
                {
                    throw new AphidParserException(
                              "Macro with name '{0}' already exists.",
                              m.Name);
                }

                _macros.Add(m.Name, m);
            }

            var filtered = new List <AphidExpression>(ast.Count);

            for (var i = 0; i < ast.Count; i++)
            {
                var node    = ast[i];
                var include = true;

                for (var j = 0; j < macros.Count; j++)
                {
                    if (node == macros.Array[j].OriginalExpression)
                    {
                        include = false;
                        break;
                    }
                }

                if (include)
                {
                    filtered.Add(node);
                }
            }

            return(filtered);
        }
        private AphidMacro GetMacro(CallExpression call, AphidMacro[] macros)
        {
            var idExp = call.FunctionExpression as IdentifierExpression;

            if (idExp == null)
            {
                return null;
            }

            return macros.SingleOrDefault(x => x.Name == idExp.Identifier);
        }