Example #1
0
        private static int wsrepl(string filename)
        {
            var ws     = new Grace.Execution.WebSocketServer();
            var wss    = ws.Start();
            var ls     = new LocalScope("repl-inner");
            var obj    = new UserObject();
            var interp = REPL.CreateInterpreter(obj, ls,
                                                new WSOutputSink(wss));

            interp.LoadPrelude();
            var dir = Path.GetFullPath(".");

            interp.AddModuleRoot(dir);
            ErrorReporting.SilenceError("P1001");
            var         memo  = interp.Memorise();
            string      accum = String.Empty;
            bool        unfinished;
            GraceObject result;

            wss.JsonReceived += (o, e) => {
                var je   = (JsonWSEvent)e;
                var root = je.Root;
                var cn   = root.XPathSelectElement("//code");
                if (cn == null)
                {
                    return;
                }
                var line = cn.Value;
                //var line = ((TextWSEvent)e).Text;
                Console.WriteLine("got text: " + line);
                accum += line.Replace("\u0000", "") + "\n";
                var r = REPL.RunLine(
                    interp, obj, memo, accum, out unfinished,
                    out result);
                if (result != null)
                {
                    ls["LAST"] = result;
                }
                if (unfinished)
                {
                    // "Unexpected end of file" is expected here
                    // for unfinished statements.
                    unfinished = false;
                }
                else if (r != 0)
                {
                    // All other errors are errors, and should
                    // clear the accumulated buffer and let the
                    // user start again.
                    accum = String.Empty;
                }
                else
                {
                    accum = String.Empty;
                }
            };
            wss.Run();
            return(0);
        }