Esempio n. 1
0
        ParseExprToLambda(TextReader reader)
        {
            var scanner    = new VB.Scanner(reader);
            var errorTable = new List <VB.SyntaxError>();
            var ast        = new VB.Parser().ParseScriptFile(scanner, errorTable);

            VBScriptSourceCodeReader sourceReader = reader as VBScriptSourceCodeReader;
            ISourceMapper            mapper       = null;

            if (sourceReader != null)
            {
                mapper = sourceReader.SourceMapper;
            }

            var scope = new AnalysisScope(
                null,
                "__snippet__",
                this,
                Expression.Parameter(typeof(VBScript), "vbscriptRuntime"),
                Expression.Parameter(typeof(IDynamicMetaObjectProvider), "fileModule"),
                mapper
                );

            List <Expression> body = new List <Expression>();

            body.Add(Expression.Convert(VBScriptGenerator.GenerateExpr(ast, scope), typeof(object)));

            if (scope.Errors.Count > 0)
            {
                throw new VBScriptCompilerException(scope.Errors);
            }

            var moduleFun = Expression.Lambda <Action <VBScript, IDynamicMetaObjectProvider> >(
                Expression.Block(body),
                scope.RuntimeExpr,
                scope.ModuleExpr
                );

            return(moduleFun.Compile());
        }
Esempio n. 2
0
        ParseFileToLambda(string filename, TextReader reader)
        {
            var scanner    = new VB.Scanner(reader);
            var errorTable = new List <VB.SyntaxError>();

            var block = new VB.Parser().ParseScriptFile(scanner, errorTable);

            if (errorTable.Count > 0)
            {
                List <VBScriptSyntaxError> errors = new List <VBScriptSyntaxError>();
                foreach (VB.SyntaxError error in errorTable)
                {
                    errors.Add(new VBScriptSyntaxError(
                                   filename,
                                   SourceUtil.ConvertSpan(error.Span),
                                   (int)error.Type,
                                   error.Type.ToString())
                               );
                }
                throw new VBScriptCompilerException(errors);
            }

            VBScriptSourceCodeReader sourceReader = reader as VBScriptSourceCodeReader;
            ISourceMapper            mapper       = null;

            if (sourceReader != null)
            {
                mapper = sourceReader.SourceMapper;
            }

            var scope = new AnalysisScope(
                null,
                filename,
                this,
                Expression.Parameter(typeof(VBScript), "vbscriptRuntime"),
                Expression.Parameter(typeof(IDynamicMetaObjectProvider), "fileModule"),
                mapper);

            //Generate function table
            List <Expression> body = new List <Expression>();

            //Add the built in globals
            ParameterExpression err = Expression.Parameter(typeof(ErrObject), ERR_PARAMETER);

            scope.Names.Add(ERR_PARAMETER, err);
            body.Add(
                Expression.Assign(
                    err,
                    Expression.New(typeof(ErrObject))
                    )
                );

            if (Trace)
            {
                ParameterExpression trace = Expression.Parameter(typeof(ITrace), TRACE_PARAMETER);
                scope.Names.Add(TRACE_PARAMETER, trace);
                body.Add(
                    Expression.Assign(
                        trace,
                        Expression.Convert(
                            Expression.Dynamic(
                                scope.GetRuntime().GetGetMemberBinder(TRACE_PARAMETER),
                                typeof(object),
                                scope.GetModuleExpr()
                                ),
                            typeof(ITrace)
                            )
                        )
                    );
            }

            //Put module variables and functions into the scope
            VBScriptAnalyzer.AnalyzeFile(block, scope);

            //Generate the module level code other than the methods:
            if (block.Statements != null)
            {
                foreach (var s in block.Statements)
                {
                    if (s is VB.MethodDeclaration)
                    {
                        //Make sure methods are created first before being executed
                        body.Insert(0, VBScriptGenerator.GenerateExpr(s, scope));
                    }
                    else
                    {
                        Expression stmt = VBScriptGenerator.GenerateExpr(s, scope);
                        if (scope.VariableScope.IsOnErrorResumeNextOn)
                        {
                            stmt = VBScriptGenerator.WrapTryCatchExpression(stmt, scope);
                        }
                        Expression debugInfo      = null;
                        Expression clearDebugInfo = null;
                        if (Trace && s is VB.Statement && !(s is VB.BlockStatement))
                        {
                            debugInfo = VBScriptGenerator.GenerateDebugInfo(s, scope, out clearDebugInfo);
                            body.Add(debugInfo);
                        }

                        body.Add(stmt);

                        if (clearDebugInfo != null)
                        {
                            body.Add(clearDebugInfo);
                        }
                    }
                }
            }

            body.Add(Expression.Constant(null)); //Stop anything from returning

            if (scope.Errors.Count > 0)
            {
                throw new VBScriptCompilerException(scope.Errors);
            }

            //if (Debug)
            //{
            //    Expression registerRuntimeVariables = VBScriptGenerator.GenerateRuntimeVariablesExpression(scope);

            //    body.Insert(0, registerRuntimeVariables);
            //}

            var moduleFun = Expression.Lambda <Action <VBScript, IDynamicMetaObjectProvider> >(
                Expression.Block(
                    scope.Names.Values,
                    body),
                scope.RuntimeExpr,
                scope.ModuleExpr);

            //if (!Debug)
            //{
            return(moduleFun.Compile());
            //}
            //else
            //{
            //    Expression<Action<VBScript, IDynamicMetaObjectProvider>> lambda =  (Expression<Action<VBScript, IDynamicMetaObjectProvider>>)DebugContext.TransformLambda(moduleFun);
            //    return lambda.Compile();
            //}
        }