Ejemplo n.º 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();
            }
        }
Ejemplo n.º 2
0
 // _addNamespacesAndTypes builds a tree of ExpandoObjects representing
 // .NET namespaces, with TypeModel objects at the leaves.  Though Sympl is
 // case-insensitive, we store the names as they appear in .NET reflection
 // in case our globals object or a namespace object gets passed as an IDO
 // to another language or library, where they may be looking for names
 // case-sensitively using EO's default lookup.
 //
 public void AddAssemblyNamesAndTypes()
 {
     foreach (var assm in _assemblies)
     {
         foreach (var typ in assm.GetExportedTypes())
         {
             string[] names = typ.FullName.Split('.');
             var      table = _globals;
             for (int i = 0; i < names.Length - 1; i++)
             {
                 string name = names[i].ToLower();
                 if (DynamicObjectHelpers.HasMember(
                         (IDynamicMetaObjectProvider)table, name))
                 {
                     // Must be Expando since only we have put objs in
                     // the tables so far.
                     table = (ExpandoObject)(DynamicObjectHelpers
                                             .GetMember(table, name));
                 }
                 else
                 {
                     var tmp = new ExpandoObject();
                     DynamicObjectHelpers.SetMember(table, name, tmp);
                     table = tmp;
                 }
             }
             DynamicObjectHelpers.SetMember(table, names[names.Length - 1],
                                            new TypeModel(typ));
         }
     }
 }
Ejemplo n.º 3
0
        public ExpandoObject ExecuteFile(string filename, string globalVar)
        {
            var moduleEO = CreateScope();

            ExecuteFileInScope(filename, moduleEO);

            globalVar = globalVar ?? Path.GetFileNameWithoutExtension(filename);
            DynamicObjectHelpers.SetMember(this._globals, globalVar, moduleEO);

            return(moduleEO);
        }
Ejemplo n.º 4
0
        public override Object Run(Scope scope)
        {
            compiledLambda ??= lambda.Compile();

            if (SourceUnit.Kind == SourceCodeKind.File)
            {
                // Simple way to convey script rundir for RuntimeHelpers.Import to load .sympl
                // files relative to the current script file.
                DynamicObjectHelpers.SetMember(scope, "__file__", Path.GetFullPath(SourceUnit.Path));
            }

            return(compiledLambda(symplContext.Context, scope));
        }
Ejemplo n.º 5
0
 public override object Run(Scope scope)
 {
     if (_compiledLambda == null)
     {
         _compiledLambda = _lambda.Compile();
     }
     if (this.SourceUnit.Kind == SourceCodeKind.File)
     {
         // Simple way to convey script rundir for RuntimeHelpers.SymplImport
         // to load .sympl files relative to the current script file.
         DynamicObjectHelpers.SetMember(scope, "__file__",
                                        Path.GetFullPath(this.SourceUnit.Path));
     }
     return(_compiledLambda(_sympl, scope));
 }
Ejemplo n.º 6
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,
                                       IDynamicMetaObjectProvider 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 moduleFun = ParseFileToLambda(filename, f);
                var d         = moduleFun.Compile();
                d(this, moduleEO);
            } finally {
                f.Close();
            }
        }
Ejemplo n.º 7
0
        public void AddInstanceObjectNamesAndTypes()
        {
            foreach (var instanceObject in _instanceObjects)
            {
                foreach (var methodName in instanceObject.GetType().GetMethods())
                {
                    var table = _globals;
                    if (DynamicObjectHelpers.HasMember(table, instanceObject.GetType().Name))
                    {
                        table = (ExpandoObject)(DynamicObjectHelpers.GetMember(table, instanceObject.GetType().Name));
                    }
                    else
                    {
                        var tmp = new ExpandoObject();
                        DynamicObjectHelpers.SetMember(table, instanceObject.GetType().Name, tmp);
                        table = tmp;
                    }

                    DynamicObjectHelpers.SetMember(table, methodName.Name, instanceObject);
                }
            }
        }
Ejemplo n.º 8
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 moduleNamespace)
        {
            var f = new StreamReader(filename);

            // Simple way to convey script rundir for RuntimeHelpes.CrispyImport
            // to load .arity files.
            DynamicObjectHelpers.SetMember(moduleNamespace, "__file__", Path.GetFullPath(filename));
            try
            {
                var asts    = new Parser(new Tokenizer(f)).ParseFile();
                var context = new Context(
                    null,
                    filename,
                    this,
                    Expression.Parameter(typeof(Crispy), "arityRuntime"),
                    Expression.Parameter(typeof(ExpandoObject), "fileModule")
                    )
                {
                    InstanceObjects = _instanceObjects
                };

                var body = new List <Expression>();
                foreach (var e in asts)
                {
                    body.Add(e.Eval(context));
                }
                var moduleFun = Expression.Lambda <Action <Crispy, ExpandoObject> >(
                    MakeBody(context, body),
                    context.RuntimeExpr,
                    context.ModuleExpr
                    );
                var d = moduleFun.Compile();
                d(this, moduleNamespace);
            }
            finally
            {
                f.Close();
            }
        }