Beispiel #1
0
        protected override List <AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            LoadScriptExpression loadExp;
            AphidExpression      scriptExp;

            if (expression.Type != AphidExpressionType.LoadScriptExpression)
            {
                hasChanged = false;

                return(null);
            }
            else if ((loadExp = (LoadScriptExpression)expression).FileExpression.Type !=
                     AphidExpressionType.StringExpression)
            {
                var constantFolder = new ConstantFoldingMutator();
                var mutated        = constantFolder.Mutate(new List <AphidExpression> {
                    loadExp.FileExpression
                });

                if (mutated.Count != 1 || (scriptExp = mutated[0]).Type != AphidExpressionType.StringExpression)
                {
                    hasChanged = false;

                    return(null);
                    //throw new AphidParserException("Invalid load script operand", loadExp);
                }
            }
            else
            {
                scriptExp = loadExp.FileExpression;
            }

            var scriptStr = StringParser.Parse(((StringExpression)scriptExp).Value);

            var script = Loader.FindScriptFile(_applicationDirectory, scriptStr);

            if (!File.Exists(script))
            {
                throw new AphidParserException(
                          string.Format(
                              "Could not find script {0}",
                              scriptStr),
                          scriptExp);
            }

            Included.Add(script);

            List <AphidExpression> ast;

            if (AphidConfig.Current.ScriptCaching && !DisableCaching)
            {
                AphidByteCodeCache cache;

                if (UseImplicitReturns)
                {
                    cache = new AphidByteCodeCache(Loader.SearchPaths.ToArray());
                }
                else
                {
                    cache = new AphidByteCodeCache(Loader.SearchPaths.ToArray(), 0x1);
                }

                ast = cache.Read(script, out var cacheSources);

                for (var i = 0; i < cacheSources.Length; i++)
                {
                    Included.Add(cacheSources[i].Name);
                }
            }
            else
            {
                var code = AphidScript.Read(script);
                ast = AphidParser.Parse(code, script, useImplicitReturns: UseImplicitReturns);
            }

            if (PerformCommonTransformations)
            {
                var mutatedAst = new PartialOperatorMutator().Mutate(ast);
                mutatedAst = new AphidMacroMutator().Mutate(mutatedAst);
                mutatedAst = new AphidPreprocessorDirectiveMutator().Mutate(mutatedAst);
                mutatedAst = new ConstantFoldingMutator().Mutate(mutatedAst);
                hasChanged = true;

                return(mutatedAst);
            }

            hasChanged = true;

            return(ast);
        }
Beispiel #2
0
        protected override List <AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            LoadScriptExpression loadExp;
            AphidExpression      scriptExp;

            if (expression.Type != AphidExpressionType.LoadScriptExpression)
            {
                hasChanged = false;

                return(null);
            }
            else if ((loadExp = (LoadScriptExpression)expression).FileExpression.Type !=
                     AphidExpressionType.StringExpression)
            {
                var mutated = new ConstantFoldingMutator()
                              .MutateSingle(loadExp.FileExpression);

                if ((scriptExp = mutated).Type != AphidExpressionType.StringExpression)
                {
                    hasChanged = false;

                    return(null);
                    //throw new AphidParserException("Invalid load script operand", loadExp);
                }
            }
            else
            {
                scriptExp = loadExp.FileExpression;
            }

            var scriptStr = StringParser.Parse(((StringExpression)scriptExp).Value);

            if (!EnvironmentHelper.IsWindows)
            {
                scriptStr = scriptStr.ToLower();
            }

            var script = Loader.FindScriptFile(_applicationDirectory, scriptStr);

            if (script == null && scriptExp.Filename != null)
            {
                script = Loader.FindScriptFile(Path.GetDirectoryName(scriptExp.Filename), scriptStr);
            }

            if (!File.Exists(script))
            {
                throw new AphidParserException(
                          string.Format(
                              "Could not find script {0}",
                              scriptStr),
                          scriptExp);
            }

            AddIncluded(script);

            List <AphidExpression> ast;

            if (AphidConfig.Current.ScriptCaching && !DisableCaching)
            {
                AphidByteCodeCache cache = UseImplicitReturns
                    ? new AphidByteCodeCache(Loader.SearchPaths.ToArray())
                {
                    DisableConstantFolding = false,
                    InlineScripts          = true,
                }
                    : new AphidByteCodeCache(Loader.SearchPaths.ToArray(), 0x1);
                ast = cache.Read(script, out var cacheSources);

                for (var i = 0; i < cacheSources.Length; i++)
                {
                    AddIncluded(cacheSources[i].Name);
                }
            }
            else
            {
                var code = AphidScript.Read(script);
                ast = AphidParser.Parse(code, script, useImplicitReturns: UseImplicitReturns);
            }

            if (PerformCommonTransformations)
            {
                var mut = MutatorGroups.GetStandard().Mutate(ast);
                hasChanged = true;

                return(mut);
            }

            hasChanged = true;

            return(ast);
        }