Example #1
0
        // ExecuteFileInScope executes the file in the given module scope.  This
        // does NOT store the module scope on Globals.  This function returns
        // nothing.
        //
        public void ExecuteFileInScope(string filename, ExpandoObject moduleEO)
        {
            var f = new StreamReader(filename);

            // Simple way to convey script rundir for RuntimeHelpes.SymplImport
            // to load .sympl files.
            DynamicObjectHelpers.SetMember(moduleEO, "__file__",
                                           Path.GetFullPath(filename));
            try {
                var asts  = new Parser().ParseFile(f);
                var scope = new AnalysisScope(
                    null,
                    filename,
                    this,
                    Expression.Parameter(typeof(Sympl), "symplRuntime"),
                    Expression.Parameter(typeof(ExpandoObject), "fileModule")
                    );
                List <Expression> body = new List <Expression>();
                foreach (var e in asts)
                {
                    body.Add(ETGen.AnalyzeExpr(e, scope));
                }
                var moduleFun = Expression.Lambda <Action <Sympl, ExpandoObject> >(
                    Expression.Block(body),
                    scope.RuntimeExpr,
                    scope.ModuleExpr
                    );
                var d = moduleFun.Compile();
                d(this, moduleEO);
            } finally {
                f.Close();
            }
        }
Example #2
0
        ParseFileToLambda(string filename, TextReader reader)
        {
            var asts  = new Parser().ParseFile(reader);
            var scope = new AnalysisScope(
                null,
                filename,
                this,
                Expression.Parameter(typeof(Sympl), "symplRuntime"),
                Expression.Parameter(typeof(IDynamicMetaObjectProvider),
                                     "fileModule"));
            List <Expression> body = new List <Expression>();

            foreach (var e in asts)
            {
                body.Add(ETGen.AnalyzeExpr(e, scope));
            }
            body.Add(Expression.Constant(null));
            var moduleFun = Expression.Lambda <Func <Sympl, IDynamicMetaObjectProvider,
                                                     object> >(
                Expression.Block(body),
                scope.RuntimeExpr,
                scope.ModuleExpr);

            return(moduleFun);
        }
Example #3
0
        ParseExprToLambda(TextReader reader)
        {
            var ast   = new Parser().ParseExpr(reader);
            var scope = new AnalysisScope(
                null,
                "__snippet__",
                this,
                Expression.Parameter(typeof(Sympl), "symplRuntime"),
                Expression.Parameter(typeof(IDynamicMetaObjectProvider),
                                     "fileModule"));
            List <Expression> body = new List <Expression>();

            body.Add(Expression.Convert(ETGen.AnalyzeExpr(ast, scope),
                                        typeof(object)));
            var moduleFun = Expression.Lambda <Func <Sympl, IDynamicMetaObjectProvider,
                                                     object> >(
                Expression.Block(body),
                scope.RuntimeExpr,
                scope.ModuleExpr
                );

            return(moduleFun);
        }
Example #4
0
        // Execute a single expression parsed from string in the provided module
        // scope and returns the resulting value.
        //
        public object ExecuteExpr(string expr_str, ExpandoObject moduleEO)
        {
            var f     = new StringReader(expr_str);
            var ast   = new Parser().ParseExpr(f);
            var scope = new AnalysisScope(
                null,
                "__snippet__",
                this,
                Expression.Parameter(typeof(Sympl), "symplRuntime"),
                Expression.Parameter(typeof(ExpandoObject), "fileModule")
                );
            List <Expression> body = new List <Expression>();

            body.Add(Expression.Convert(ETGen.AnalyzeExpr(ast, scope),
                                        typeof(object)));
            var moduleFun = Expression.Lambda <Func <Sympl, ExpandoObject, object> >(
                Expression.Block(body),
                scope.RuntimeExpr,
                scope.ModuleExpr
                );
            var d = moduleFun.Compile();

            return(d(this, moduleEO));
        }