Пример #1
0
        private void ExecuteRubySourceUnit(SourceUnit /*!*/ sourceUnit, Scope /*!*/ globalScope, LoadFlags flags)
        {
            Assert.NotNull(sourceUnit, globalScope);

            // TODO: check file timestamp
            string       fullPath = Platform.GetFullPath(sourceUnit.Path);
            CompiledFile compiledFile;

            if (TryGetCompiledFile(fullPath, out compiledFile))
            {
                Utils.Log(String.Format("{0}: {1}", ++_cacheHitCount, sourceUnit.Path), "LOAD_CACHED");
                compiledFile.CompiledCode.Run(globalScope);
            }
            else
            {
                Utils.Log(String.Format("{0}: {1}", ++_compiledFileCount, sourceUnit.Path), "LOAD_COMPILED");

                RubyCompilerOptions options = new RubyCompilerOptions(_context.RubyOptions)
                {
                    IsIncluded = true,
                    IsWrapped  = (flags & LoadFlags.LoadIsolated) != 0,
                };

                long       ts1          = Stopwatch.GetTimestamp();
                ScriptCode compiledCode = sourceUnit.Compile(options, _context.RuntimeErrorSink);
                long       ts2          = Stopwatch.GetTimestamp();
                Interlocked.Add(ref _ScriptCodeGenerationTimeTicks, ts2 - ts1);

                AddCompiledFile(fullPath, compiledCode);

                CompileAndRun(globalScope, compiledCode, _context.Options.InterpretedMode);
            }
        }
Пример #2
0
        private IodineObject compileModule(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            IodineString source = args [0] as IodineString;
            SourceUnit   unit   = SourceUnit.CreateFromSource(source.Value);

            return(unit.Compile(vm.Context));
        }
Пример #3
0
        private ScriptCode /*!*/ CompileRubySource(SourceUnit /*!*/ sourceUnit, LoadFlags flags)
        {
            Assert.NotNull(sourceUnit);

            // TODO: check file timestamp
            string       fullPath = Platform.GetFullPath(sourceUnit.Path);
            CompiledFile compiledFile;

            if (TryGetCompiledFile(fullPath, out compiledFile))
            {
                Utils.Log(String.Format("{0}: {1}", ++_cacheHitCount, sourceUnit.Path), "LOAD_CACHED");

                return(compiledFile.CompiledCode);
            }
            else
            {
                Utils.Log(String.Format("{0}: {1}", ++_compiledFileCount, sourceUnit.Path), "LOAD_COMPILED");

                RubyCompilerOptions options = new RubyCompilerOptions(_context.RubyOptions)
                {
                    FactoryKind = (flags & LoadFlags.LoadIsolated) != 0 ? TopScopeFactoryKind.WrappedFile : TopScopeFactoryKind.File
                };

                long       ts1          = Stopwatch.GetTimestamp();
                ScriptCode compiledCode = sourceUnit.Compile(options, _context.RuntimeErrorSink);
                long       ts2          = Stopwatch.GetTimestamp();
                Interlocked.Add(ref _ScriptCodeGenerationTimeTicks, ts2 - ts1);

                AddCompiledFile(fullPath, compiledCode);

                return(compiledCode);
            }
        }
Пример #4
0
        private IodineObject eval(VirtualMachine host, string source, IodineHashMap dict)
        {
            VirtualMachine vm = host;

            if (dict != null)
            {
                vm = new VirtualMachine(host.Context, new Dictionary <string, IodineObject> ());

                foreach (string glob in host.Globals.Keys)
                {
                    vm.Globals [glob] = host.Globals [glob];
                }

                foreach (IodineObject key in dict.Keys.Values)
                {
                    vm.Globals [key.ToString()] = dict.Dict [key.GetHashCode()];
                }
            }
            IodineContext context = new IodineContext();
            SourceUnit    code    = SourceUnit.CreateFromSource(source);
            IodineModule  module  = null;

            try {
                module = code.Compile(context);
            } catch (SyntaxException ex) {
                vm.RaiseException(new IodineSyntaxException(ex.ErrorLog));
                return(null);
            }
            return(vm.InvokeMethod(module.Initializer, null, new IodineObject[] { }));
        }
Пример #5
0
        private ScriptCode /*!*/ CompileRubySource(SourceUnit /*!*/ sourceUnit, LoadFlags flags)
        {
            Assert.NotNull(sourceUnit);

            // TODO: check file timestamp
            string fullPath = Platform.GetFullPath(sourceUnit.Path);

#if FEATURE_FILESYSTEM
            CompiledFile compiledFile;
            if (TryGetCompiledFile(fullPath, out compiledFile))
            {
                Utils.Log(String.Format("{0}: {1}", ++_cacheHitCount, sourceUnit.Path), "LOAD_CACHED");

                return(compiledFile.CompiledCode);
            }

            Utils.Log(String.Format("{0}: {1}", ++_compiledFileCount, sourceUnit.Path), "LOAD_COMPILED");
#endif

            RubyCompilerOptions options = new RubyCompilerOptions(_context.RubyOptions)
            {
                FactoryKind = (flags & LoadFlags.LoadIsolated) != 0 ? TopScopeFactoryKind.WrappedFile : TopScopeFactoryKind.File
            };

            ScriptCode compiledCode = sourceUnit.Compile(options, _context.RuntimeErrorSink);

#if FEATURE_FILESYSTEM
            AddCompiledFile(fullPath, compiledCode);
#endif
            return(compiledCode);
        }
Пример #6
0
        private IodineObject Eval(VirtualMachine host, string source, IodineDictionary dict)
        {
            VirtualMachine vm = host;

            IodineContext context = host.Context;

            if (dict != null)
            {
                context = new IodineContext();
                context.Globals.Clear();

                vm = new VirtualMachine(host.Context);

                foreach (IodineObject key in dict.Keys)
                {
                    context.Globals [key.ToString()] = dict.Get(key);
                }
            }

            SourceUnit   code   = SourceUnit.CreateFromSource(source);
            IodineModule module = null;

            try {
                module = code.Compile(context);
            } catch (SyntaxException ex) {
                vm.RaiseException(new IodineSyntaxException(ex.ErrorLog));
                return(IodineNull.Instance);
            }
            return(module.Invoke(vm, new IodineObject[] { }));
        }
Пример #7
0
        /// <summary>
        /// Executes and loads an Iodine source file
        /// </summary>
        /// <returns>Last object evaluated during the execution of the file</returns>
        /// <param name="file">File path.</param>
        public dynamic DoFile(string file)
        {
            SourceUnit line = SourceUnit.CreateFromFile(file);

            module = line.Compile(Context);
            Context.Invoke(module, new IodineObject[] { });
            return(null);
        }
Пример #8
0
        internal static PythonModule ExecuteSourceUnit(PythonContext context, SourceUnit /*!*/ sourceUnit)
        {
            ScriptCode   compiledCode = sourceUnit.Compile();
            Scope        scope        = compiledCode.CreateScope();
            PythonModule res          = ((PythonScopeExtension)context.EnsureScopeExtension(scope)).Module;

            compiledCode.Run(scope);
            return(res);
        }
Пример #9
0
 private static void CheckSourceUnit(IodineOptions options, SourceUnit unit)
 {
     try {
         context.ShouldCache = false;
         unit.Compile(context);
     } catch (SyntaxException ex) {
         DisplayErrors(ex.ErrorLog);
     }
 }
Пример #10
0
        /// <summary>
        /// Parses a single interactive command and executes it.
        ///
        /// Returns null if successful and execution should continue, or the appropiate exit code.
        /// </summary>
        private int?RunOneInteraction()
        {
            bool   continueInteraction;
            string s = ReadStatement(out continueInteraction);

            if (continueInteraction == false)
            {
                PythonContext.DispatchCommand(null); // Notify dispatcher that we're done
                return(0);
            }

            if (String.IsNullOrEmpty(s))
            {
                // Is it an empty line?
                Console.Write(String.Empty, Style.Out);
                return(null);
            }


            SourceUnit            su  = Language.CreateSnippet(s, "<stdin>", SourceCodeKind.InteractiveCode);
            PythonCompilerOptions pco = (PythonCompilerOptions)Language.GetCompilerOptions(Scope);

            pco.Module |= ModuleOptions.ExecOrEvalCode;

            Action action = delegate() {
                try {
                    su.Compile(pco, ErrorSink).Run(Scope);
                } catch (Exception e) {
                    if (e is SystemExitException)
                    {
                        throw;
                    }
                    // Need to handle exceptions in the delegate so that they're not wrapped
                    // in a TargetInvocationException
                    UnhandledException(e);
                }
            };

            try {
                PythonContext.DispatchCommand(action);
            } catch (SystemExitException sx) {
                object dummy;
                return(sx.GetExitCode(out dummy));
            }

            return(null);
        }
Пример #11
0
        private IodineObject Compile(VirtualMachine vm, IodineObject self, IodineObject[] args)
        {
            if (args.Length < 1)
            {
                vm.RaiseException(new IodineArgumentException(1));
                return(IodineNull.Instance);
            }
            IodineString source = args [0] as IodineString;
            SourceUnit   unit   = SourceUnit.CreateFromSource(source.Value);

            try {
                return(unit.Compile(vm.Context));
            } catch (SyntaxException ex) {
                vm.RaiseException(new IodineSyntaxException(ex.ErrorLog));
                return(IodineNull.Instance);
            }
        }
Пример #12
0
        private static void EvalSourceUnit(IodineOptions options, SourceUnit unit)
        {
            try {
                IodineModule module = unit.Compile(context);

                if (context.Debug)
                {
                    context.VirtualMachine.SetTrace(WaitForDebugger);
                }

                do
                {
                    context.Invoke(module, new IodineObject[] { });

                    if (module.HasAttribute("main"))
                    {
                        context.Invoke(module.GetAttribute("main"), new IodineObject[] {
                            options.IodineArguments
                        });
                    }
                } while (options.LoopFlag);

                if (options.ReplFlag)
                {
                    LaunchRepl(options, module);
                }
            } catch (UnhandledIodineExceptionException ex) {
                HandleIodineException(ex);
            } catch (SyntaxException ex) {
                DisplayErrors(ex.ErrorLog);
                Panic("Compilation failed with {0} errors!", ex.ErrorLog.ErrorCount);
            } catch (ModuleNotFoundException ex) {
                Console.Error.WriteLine(ex.ToString());
                Panic("Program terminated.");
            } catch (Exception e) {
                Console.Error.WriteLine("Fatal exception has occured!");
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine("Stack trace: \n{0}", e.StackTrace);
                Console.Error.WriteLine(
                    "\nIodine stack trace \n{0}",
                    context.VirtualMachine.GetStackTrace()
                    );
                Panic("Program terminated.");
            }
        }
Пример #13
0
        public void Run()
        {
            var version = typeof(IodineContext).Assembly.GetName().Version;

            Console.WriteLine("Iodine v{0}-alpha", version.ToString(3));
            Console.WriteLine("Fallback REPL. Enter expressions to have them be evaluated");

            IodineContext context = new IodineContext();

            context.ShouldOptimize = false;

            context.Globals ["quit"] = new QuitObject();

            while (true)
            {
                Console.Write(">>> ");
                var source = Console.ReadLine().Trim();
                try {
                    if (source.Length > 0)
                    {
                        SourceUnit   unit   = SourceUnit.CreateFromSource(source);
                        var          result = unit.Compile(context);
                        IodineObject ret    = context.Invoke(result, new IodineObject[] { });
                        if (!(ret is IodineNull))
                        {
                            Console.WriteLine(ret.Represent(context.VirtualMachine));
                        }
                    }
                } catch (UnhandledIodineExceptionException ex) {
                    Console.Error.WriteLine("*** {0}", ex.OriginalException.GetAttribute("message"));
                    ex.PrintStack();
                    context.VirtualMachine.Top = null;
                    Console.Error.WriteLine();
                } catch (ModuleNotFoundException ex) {
                    Console.Error.WriteLine(ex.ToString());
                } catch (SyntaxException syntaxException) {
                    DisplayErrors(syntaxException.ErrorLog);
                } catch (Exception ex) {
                    Console.Error.WriteLine("Fatal exception has occured!");
                    Console.Error.WriteLine(ex.Message);
                    Console.Error.WriteLine("Stack trace: \n{0}", ex.StackTrace);
                    //Console.Error.WriteLine ("\nIodine stack trace \n{0}", engine.VirtualMachine.GetStackTrace ());
                }
            }
        }
Пример #14
0
        private void ExecuteRubySourceUnit(SourceUnit/*!*/ sourceUnit, Scope/*!*/ globalScope, LoadFlags flags) {
            Assert.NotNull(sourceUnit, globalScope);
            
            // TODO: check file timestamp
            string fullPath = Platform.GetFullPath(sourceUnit.Path);
            CompiledFile compiledFile;
            if (TryGetCompiledFile(fullPath, out compiledFile)) {
                Utils.Log(String.Format("{0}: {1}", ++_cacheHitCount, sourceUnit.Path), "LOAD_CACHED");
                compiledFile.CompiledCode.Run(globalScope);
            } else {
                Utils.Log(String.Format("{0}: {1}", ++_compiledFileCount, sourceUnit.Path), "LOAD_COMPILED");

                RubyCompilerOptions options = new RubyCompilerOptions(_context.RubyOptions) {
                    IsIncluded = true,
                    IsWrapped = (flags & LoadFlags.LoadIsolated) != 0,
                };

                long ts1 = Stopwatch.GetTimestamp();
                ScriptCode compiledCode = sourceUnit.Compile(options, _context.RuntimeErrorSink);
                long ts2 = Stopwatch.GetTimestamp();
                Interlocked.Add(ref _ScriptCodeGenerationTimeTicks, ts2 - ts1);

                AddCompiledFile(fullPath, compiledCode);

                CompileAndRun(globalScope, compiledCode, _context.Options.InterpretedMode);
            }
        }
Пример #15
0
 internal static PythonModule ExecuteSourceUnit(PythonContext context, SourceUnit/*!*/ sourceUnit) {
     ScriptCode compiledCode = sourceUnit.Compile();
     Scope scope = compiledCode.CreateScope();
     PythonModule res = ((PythonScopeExtension)context.EnsureScopeExtension(scope)).Module;
     compiledCode.Run(scope);
     return res;
 }
Пример #16
0
 internal static Scope ExecuteSourceUnit(SourceUnit/*!*/ sourceUnit) {
     ScriptCode compiledCode = sourceUnit.Compile();
     Scope scope = compiledCode.CreateScope();
     compiledCode.Run(scope);
     return scope;
 }
Пример #17
0
        private ScriptCode/*!*/ CompileRubySource(SourceUnit/*!*/ sourceUnit, LoadFlags flags) {
            Assert.NotNull(sourceUnit);
            
            // TODO: check file timestamp
            string fullPath = Platform.GetFullPath(sourceUnit.Path);
            CompiledFile compiledFile;
            if (TryGetCompiledFile(fullPath, out compiledFile)) {
                Utils.Log(String.Format("{0}: {1}", ++_cacheHitCount, sourceUnit.Path), "LOAD_CACHED");

                return compiledFile.CompiledCode;
            } else {
                Utils.Log(String.Format("{0}: {1}", ++_compiledFileCount, sourceUnit.Path), "LOAD_COMPILED");

                RubyCompilerOptions options = new RubyCompilerOptions(_context.RubyOptions) {
                    FactoryKind = (flags & LoadFlags.LoadIsolated) != 0 ? TopScopeFactoryKind.WrappedFile : TopScopeFactoryKind.File
                };

                long ts1 = Stopwatch.GetTimestamp();
                ScriptCode compiledCode = sourceUnit.Compile(options, _context.RuntimeErrorSink);
                long ts2 = Stopwatch.GetTimestamp();
                Interlocked.Add(ref _ScriptCodeGenerationTimeTicks, ts2 - ts1);

                AddCompiledFile(fullPath, compiledCode);

                return compiledCode;
            }
        }
Пример #18
0
        private ScriptCode/*!*/ CompileRubySource(SourceUnit/*!*/ sourceUnit, LoadFlags flags) {
            Assert.NotNull(sourceUnit);
            
            // TODO: check file timestamp
            string fullPath = Platform.GetFullPath(sourceUnit.Path);

#if FEATURE_FILESYSTEM
            CompiledFile compiledFile;
            if (TryGetCompiledFile(fullPath, out compiledFile)) {
                Utils.Log(String.Format("{0}: {1}", ++_cacheHitCount, sourceUnit.Path), "LOAD_CACHED");

                return compiledFile.CompiledCode;
            } 

            Utils.Log(String.Format("{0}: {1}", ++_compiledFileCount, sourceUnit.Path), "LOAD_COMPILED");
#endif

            RubyCompilerOptions options = new RubyCompilerOptions(_context.RubyOptions) {
                FactoryKind = (flags & LoadFlags.LoadIsolated) != 0 ? TopScopeFactoryKind.WrappedFile : TopScopeFactoryKind.File
            };

            ScriptCode compiledCode = sourceUnit.Compile(options, _context.RuntimeErrorSink);

#if FEATURE_FILESYSTEM
            AddCompiledFile(fullPath, compiledCode);
#endif
            return compiledCode;
        }