예제 #1
0
파일: Program.cs 프로젝트: SealedSun/prx
        public static void Main(string[] args)
        {
            Console.CancelKeyPress += delegate { Environment.Exit(1); };
            var prexoniteConsole = new PrexoniteConsole(true);

            //Let the exceptions surface so they can more easily be debugged
            try
            {
                var engine = new Engine();
                engine.RegisterAssembly(Assembly.GetExecutingAssembly());

                //Load application
                var app = _loadApplication(engine, prexoniteConsole);

                //Run the applications main function.
                if (app != null) //errors have already been reported
                    _runApplication(engine, app, args);
            }
            // ReSharper disable once RedundantCatchClause
            catch (Exception ex)
            {
#if DEBUG
                _dummyUsageOf(ex);
                throw;
#else
                Console.WriteLine(ex);
#endif
            }
            finally
            {
                if (Debugger.IsAttached)
                {
                    Console.WriteLine(Prexonite.Properties.Resources.Program_DebugExit);
                    Console.ReadLine();
                }
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: SealedSun/prx
        private static Application _loadApplication(Engine engine, PrexoniteConsole prexoniteConsole)
        {
            var plan = Plan.CreateSelfAssembling();

            #region Stopwatch commands

            //prx.exe provides these three additional commands for high speed access to a stopwatch from your script code
            var timer = new Stopwatch();
            engine.Commands.AddHostCommand
                (
                    @"timer\start",
                    new DelegatePCommand
                        (
                        delegate
                        {
                            timer.Start();
                            return null;
                        }));

            engine.Commands.AddHostCommand
                (
                    @"timer\stop",
                    new DelegatePCommand
                        (
                        delegate
                        {
                            timer.Stop();
                            return (double)timer.ElapsedMilliseconds;
                        }));

            engine.Commands.AddHostCommand
                (
                    @"timer\reset",
                    new DelegatePCommand
                        (
                        delegate
                        {
                            timer.Reset();
                            return null;
                        }));

            engine.Commands.AddHostCommand
                (
                    @"timer\elapsed",
                    new DelegatePCommand
                        (
                        delegate { return (double)timer.ElapsedMilliseconds; }));

            #endregion

            #region Stack Manipulation commands

            engine.Commands.AddHostCommand
                (
                    @"__replace_call",
                    delegate(StackContext sctx, PValue[] cargs)
                    {
                        if (cargs == null)
                            cargs = new PValue[]
                            {
                            };
                        if (sctx == null)
                            throw new ArgumentNullException("sctx");

                        var e = sctx.ParentEngine;

                        if (cargs.Length < 1)
                            throw new PrexoniteException
                                (
                                "__replace_call requires the context or function to be replaced.");

                        var carg = cargs[0];
                        var rargs = new PValue[cargs.Length - 1];
                        Array.Copy(cargs, 1, rargs, 0, rargs.Length);

                        FunctionContext rctx = null;
                        PFunction f;
                        switch (carg.Type.ToBuiltIn())
                        {
                            case PType.BuiltIn.String:
                                if (
                                    !sctx.ParentApplication.Functions.TryGetValue
                                        (
                                            (string)carg.Value, out f))
                                    throw new PrexoniteException
                                        (
                                        "Cannot replace call to " + carg +
                                        " because no such function exists.");

                                rctx = f.CreateFunctionContext(e, rargs);
                                break;
                            case PType.BuiltIn.Object:
                                var clrType = ((ObjectPType)carg.Type).ClrType;
                                if (clrType == typeof(PFunction))
                                {
                                    f = (PFunction)carg.Value;
                                    rctx = f.CreateFunctionContext(e, rargs);
                                }
                                else if (clrType == typeof(Closure) &&
                                         clrType != typeof(Continuation))
                                {
                                    var c = (Closure)carg.Value;
                                    rctx = c.CreateFunctionContext(sctx, rargs);
                                }
                                else if (clrType == typeof(FunctionContext))
                                {
                                    rctx = (FunctionContext)carg.Value;
                                }
                                break;
                        }
                        if (rctx == null)
                            throw new PrexoniteException("Cannot replace a context based on " +
                                                         carg);

                        var node = e.Stack.Last;
                        do
                        {
                            var ectx = node.Value as FunctionContext;

                            if (ectx != null)
                            {
                                if (ReferenceEquals(ectx.Implementation, rctx.Implementation))
                                {
                                    node.Value = rctx;
                                    break;
                                }
                            }
                        } while ((node = node.Previous) != null);

                        return PType.Null.CreatePValue();
                    });

            #endregion

            #region Prexonite Console constant

            engine.Commands.AddHostCommand("__console", prexoniteConsole);

            #endregion

            #region Create benchmark command

            engine.Commands.AddHostCommand
                ("createBenchmark",
                    delegate(StackContext sctx, PValue[] cargs)
                    {
                        if (sctx == null)
                            throw new ArgumentNullException("sctx");
                        if (cargs == null)
                            cargs = new PValue[]
                            {
                            };

                        Engine teng;
                        int tit;

                        if (cargs.Length >= 2)
                        {
                            teng = cargs[0].ConvertTo<Engine>(sctx);
                            tit = cargs[1].ConvertTo<int>(sctx);
                        }
                        else if (cargs.Length >= 1)
                        {
                            teng = sctx.ParentEngine;
                            tit = cargs[0].ConvertTo<int>(sctx);
                        }
                        else
                        {
                            return sctx.CreateNativePValue(new Benchmark(sctx.ParentEngine));
                        }

                        return sctx.CreateNativePValue(new Benchmark(teng, tit));
                    });

            #endregion

            #region Self-assembling build plan reference

            engine.Commands.AddHostCommand(@"host\self_assembling_build_plan", (sctx, args) => sctx.CreateNativePValue(plan));

            #endregion

            var deleteSrc = false;
            var entryPath = GetPrxPath() + Path.DirectorySeparatorChar + PrxScriptFileName;
            if (!File.Exists(entryPath))
            {
                //Load default CLI app
                entryPath = GetPrxPath() + Path.DirectorySeparatorChar + @"src" +
                            Path.DirectorySeparatorChar + "prx_main.pxs";

                if (!File.Exists(entryPath))
                {
                    if (!Directory.Exists("src"))
                    {
                        var di =
                            Directory.CreateDirectory(GetPrxPath() + Path.DirectorySeparatorChar +
                                                      @"src");
                        di.Attributes = di.Attributes | FileAttributes.Hidden;
                        deleteSrc = true;
                    }

                    //Unpack source
                    writeFile(Resources.prx_main, "prx_main.pxs");
                    writeFile(Resources.prx_lib, "prx_lib.pxs");
                    writeFile(Resources.prx_interactive, "prx_interactive.pxs");
                }
            }

            Tuple<Application, ITarget> result;

            try
            {
                var entryDesc =
                    plan.AssembleAsync(Source.FromFile(entryPath, Encoding.UTF8), CancellationToken.None).Result;
                result = plan.Load(entryDesc.Name);
            }
            catch (BuildFailureException e)
            {
                _reportErrors(e.Messages);
                Console.WriteLine(e);
#if DEBUG
                throw;
#else
                result = null;
#endif
            }
            catch (BuildException e)
            {
                if(e.RelatedTarget != null)
                    _reportErrors(e.RelatedTarget.BuildMessages);

                Console.WriteLine(e);
#if DEBUG
                throw;
#else
                result = null;
#endif
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
#if DEBUG
                throw;
#else
                result = null;
#endif
            }


            if (deleteSrc)
                Directory.Delete(GetPrxPath() + Path.DirectorySeparatorChar + @"src", true);

            if (result == null)
            {
                return null;
            }
            else
            {
                return !_reportErrors(result.Item2.Messages) ? result.Item1 : null;
            }
        }