Exemplo n.º 1
0
        public static object WrapInFrame(object expr, int pos)
        {
            var dict = Runtime.GetLexicalVariablesDictionary(pos);

            if (dict.Count != 0)
            {
                var block = new Vector();
                block.Add(Symbols.Do);
                foreach (var item in dict)
                {
                    block.Add(Runtime.MakeList(Symbols.Let, item.Key, item.Value));
                    block.Add(Runtime.MakeList(Symbols.Declare, Runtime.MakeList(Symbols.Ignore, item.Key)));
                }
                block.Add(expr);
                return(Runtime.AsList(block));
            }
            else
            {
                return(expr);
            }
        }
Exemplo n.º 2
0
        public static void EvalPrintCommand(string data, bool debugging)
        {
            var output = GetConsoleOut();

            if (data == null)
            {
                return;
            }

            Cons code = Runtime.ReadAllFromString(data);

            if (code == null)
            {
                return;
            }

            bool leadingSpace = char.IsWhiteSpace(data, 0);

            Runtime.RestoreStackAndFrame(state.Peek());

            var dotCommand = ExtractCommand(code);

            if (dotCommand != null)
            {
                var command        = "";
                var commandsPrefix = ReplCommands.Where(x => x.StartsWith(dotCommand)).ToList();
                var commandsExact  = ReplCommands.Where(x => x == dotCommand).ToList();

                if (commandsPrefix.Count == 0)
                {
                    Runtime.PrintLine(output, "Command not found");
                    return;
                }
                else if (commandsExact.Count == 1)
                {
                    command = commandsExact[0];
                }
                else if (commandsPrefix.Count == 1)
                {
                    command = commandsPrefix[0];
                }
                else
                {
                    Runtime.PrintLine(output, "Ambiguous command. Did you mean:");
                    for (var i = 0; i < commandsPrefix.Count; ++i)
                    {
                        var str = string.Format("{0} {1}", (i == 0 ? "" : i + 1 == commandsPrefix.Count ? " or" : ","), commandsPrefix[i]);
                        Runtime.PrintLine(output, str);
                    }
                    Runtime.PrintLine(output, "?");
                    return;
                }

                switch (command)
                {
                case "continue":
                {
                    if (debugging)
                    {
                        throw new ContinueFromBreakpointException();
                    }
                    break;
                }

                case "clear":
                {
                    History.Clear();
                    Console.Clear();
                    state = new Stack <ThreadContextState>();
                    state.Push(Runtime.SaveStackAndFrame());
                    break;
                }

                case "abort":
                {
                    if (state.Count > 1)
                    {
                        state.Pop();
                    }
                    else if (debugging)
                    {
                        throw new AbortingDebuggerException();
                    }
                    break;
                }

                case "top":
                {
                    while (state.Count > 1)
                    {
                        state.Pop();
                    }
                    break;
                }

                case "quit":
                {
                    Quit();
                    break;
                }

                case "globals":
                {
                    var pattern = (string)Runtime.Second(code);
                    Runtime.DumpDictionary(output, Runtime.GetGlobalVariablesDictionary(pattern));
                    break;
                }

                case "eval":
                {
                    var expr = Runtime.Second(code);
                    var pos  = Runtime.Integerp(Runtime.Third(code)) ? (int)Runtime.Third(code) : 0;
                    var val  = EvalCommand(expr, pos);
                    if (Runtime.ToBool(Runtime.GetDynamic(Symbols.ReplForceIt)))
                    {
                        val = Runtime.Force(val);
                    }
                    Runtime.SetSymbolValue(Symbols.It, val);
                    if (val != Runtime.MissingValue)
                    {
                        Runtime.PrintStream(output, "", "it: ");
                        Runtime.PrettyPrintLine(output, 4, null, val);
                    }
                    break;
                }

                case "modify":
                {
                    var name = (Symbol)Runtime.Second(code);
                    var expr = Runtime.Third(code);
                    var pos  = Runtime.Integerp(Runtime.Fourth(code)) ? (int)Runtime.Fourth(code) : 0;
                    ModifyCommand(name, expr, pos);
                    break;
                }

                case "variables":
                {
                    var pos = Runtime.Integerp(Runtime.Second(code)) ? (int)Runtime.Second(code) : 0;
                    Runtime.DumpDictionary(output, Runtime.GetLexicalVariablesDictionary(pos));
                    break;
                }

                case "$variables":
                {
                    var pos = Runtime.Integerp(Runtime.Second(code)) ? (int)Runtime.Second(code) : 0;
                    Runtime.DumpDictionary(output, Runtime.GetDynamicVariablesDictionary(pos));
                    break;
                }

                case "backtrace":
                {
                    Runtime.PrintLine(output, Runtime.GetEvaluationStack());
                    break;
                }

                case "Exception":
                {
                    Runtime.PrintLine(output, LastException.ToString());
                    break;
                }

                case "exception":
                {
                    Runtime.PrintLine(output, RemoveDlrReferencesFromException(LastException));
                    break;
                }

                case "force":
                {
                    var expr = Runtime.Second(code) ?? Symbols.It;
                    RunCommand(null, Runtime.MakeList(Runtime.MakeList(Symbols.Force, expr)));
                    break;
                }

                case "time":
                {
                    var expr = Runtime.Second(code) ?? Symbols.It;
                    RunCommand(null, Runtime.MakeList(expr), showTime: true);
                    break;
                }

                case "describe":
                {
                    RunCommand(x =>
                        {
                            Runtime.SetSymbolValue(Symbols.It, x);
                            Runtime.Describe(x);
                        }, Runtime.Cdr(code));
                    break;
                }

                case "reset":
                {
                    var level = Runtime.Integerp(Runtime.Second(code)) ? (int)Runtime.Second(code) : -1;
                    while (state.Count > 1)
                    {
                        state.Pop();
                    }
                    var t1 = Runtime.GetCpuTime();
                    Reset(level);
                    var t2  = Runtime.GetCpuTime();
                    var t   = t2 - t1;
                    var msg = String.Format("Reset {0:N3}s user {1:N3}s system", t.User, t.System);
                    Runtime.PrintTrace(msg);
                    break;
                }
                }
            }
            else
            if (leadingSpace || !Runtime.ToBool(Symbols.ReplOptionalParentheses.Value))
            {
                RunCommand(null, code, smartParens: false);
            }
            else
            {
                RunCommand(null, code, smartParens: true);
            }
        }
Exemplo n.º 3
0
        public static void EvalPrintCommand(string data, bool debugging)
        {
            var  output       = GetConsoleOut();
            bool leadingSpace = char.IsWhiteSpace(data, 0);
            Cons code         = Runtime.ReadAllFromString(data);

            if (code == null)
            {
                return;
            }

            Runtime.RestoreStackAndFrame(state.Peek());

            var head = Runtime.First(code) as Symbol;

            if (head != null && (Runtime.Keywordp(head) || head.Name == "?"))
            {
                var dotCommand     = Runtime.First(code).ToString();
                var command        = "";
                var commandsPrefix = ReplCommands.Where(x => x.StartsWith(dotCommand)).ToList();
                var commandsExact  = ReplCommands.Where(x => x == dotCommand).ToList();

                if (commandsPrefix.Count == 0)
                {
                    Runtime.PrintLine(output, "Command not found");
                    return;
                }
                else if (commandsExact.Count == 1)
                {
                    command = commandsExact[0];
                }
                else if (commandsPrefix.Count == 1)
                {
                    command = commandsPrefix[0];
                }
                else
                {
                    Runtime.PrintLine(output, "Ambiguous command. Did you mean:");
                    for (var i = 0; i < commandsPrefix.Count; ++i)
                    {
                        var str = string.Format("{0} {1}", (i == 0 ? "" : i + 1 == commandsPrefix.Count ? " or" : ","), commandsPrefix[i]);
                        Runtime.PrintLine(output, str);
                    }
                    Runtime.PrintLine(output, "?");
                    return;
                }

                switch (command)
                {
                case ":continue":
                {
                    if (debugging)
                    {
                        throw new ContinueFromBreakpointException();
                    }
                    break;
                }

                case ":clear":
                {
                    History.Clear();
                    ResetDisplayFunctionImp();
                    state = new Stack <ThreadContextState>();
                    state.Push(Runtime.SaveStackAndFrame());
                    break;
                }

                case ":abort":
                {
                    if (state.Count > 1)
                    {
                        state.Pop();
                    }
                    else if (debugging)
                    {
                        throw new AbortingDebuggerException();
                    }
                    break;
                }

                case ":top":
                {
                    while (state.Count > 1)
                    {
                        state.Pop();
                    }
                    break;
                }

                case ":quit":
                {
                    Quit();
                    break;
                }

                case ":globals":
                {
                    var pattern = (string)Runtime.Second(code);
                    Runtime.DumpDictionary(output, Runtime.GetGlobalVariablesDictionary(pattern));
                    break;
                }

                case ":variables":
                {
                    var pos = Runtime.Integerp(Runtime.Second(code)) ? (int)Runtime.Second(code) : 0;
                    Runtime.DumpDictionary(output, Runtime.GetLexicalVariablesDictionary(pos));
                    break;
                }

                case ":$variables":
                {
                    var pos = Runtime.Integerp(Runtime.Second(code)) ? (int)Runtime.Second(code) : 0;
                    Runtime.DumpDictionary(output, Runtime.GetDynamicVariablesDictionary(pos));
                    break;
                }

                case ":backtrace":
                {
                    Runtime.PrintLine(output, Runtime.GetEvaluationStack());
                    break;
                }

                case ":Exception":
                {
                    Runtime.PrintLine(output, LastException.ToString());
                    break;
                }

                case ":exception":
                {
                    Runtime.PrintLine(output, RemoveDlrReferencesFromException(LastException));
                    break;
                }

                case ":force":
                {
                    var expr = Runtime.Second(code) ?? Symbols.It;
                    RunCommand(null, Runtime.MakeList(Runtime.MakeList(Symbols.Force, expr)));
                    break;
                }

                case ":time":
                {
                    var expr = Runtime.Second(code) ?? Symbols.It;
                    RunCommand(null, Runtime.MakeList(expr), showTime: true);
                    break;
                }

                case ":describe":
                {
                    RunCommand(x =>
                        {
                            Runtime.SetSymbolValue(Symbols.It, x);
                            Runtime.Describe(x);
                        }, Runtime.Cdr(code));
                    break;
                }

                case ":reset":
                {
                    var level = Runtime.Integerp(Runtime.Second(code)) ? (int)Runtime.Second(code) : 0;
                    while (state.Count > 1)
                    {
                        state.Pop();
                    }
                    timer.Reset();
                    timer.Start();
                    ResetRuntimeFunctionImp(level);
                    timer.Stop();
                    var time = timer.ElapsedMilliseconds;
                    Runtime.PrintTrace("Startup time: ", time, "ms");
                    break;
                }
                }
            }
            else
            {
                RunCommand(null, code, smartParens: !leadingSpace);
            }
        }