Пример #1
0
        public static void RunCommand(Action <object> func, Cons lispCode, bool showTime = false, bool smartParens = false)
        {
            var output = GetConsoleOut();

            if (lispCode != null)
            {
                var head = Runtime.First(lispCode) as Symbol;

                if (smartParens && head != null)
                {
                    if (Runtime.LooksLikeFunction(head))
                    {
                        lispCode = Runtime.MakeCons(lispCode, (Cons)null);
                    }
                }

                var t1 = Runtime.GetCpuTime();

                foreach (var expr in lispCode)
                {
                    var val     = EvalCommand(expr, 0);
                    var forcing = Runtime.ToBool(Runtime.GetDynamic(Symbols.ReplForceIt));

                    if (forcing)
                    {
                        val = Runtime.Force(val);
                    }

                    if (func == null)
                    {
                        Runtime.SetSymbolValue(Symbols.It, val);
                        if (val != Runtime.MissingValue)
                        {
                            Runtime.PrintStream(output, "", "it: ");
                            Runtime.PrettyPrintLine(output, 4, null, val);
                        }
                    }
                    else
                    {
                        func(val);
                    }
                }


                if (showTime)
                {
                    var t2  = Runtime.GetCpuTime();
                    var t   = t2 - t1;
                    var msg = String.Format("Time {0:N3}s user {1:N3}s system", t.User, t.System);
                    Runtime.PrintTrace(msg);
                }
            }
            else
            {
                func(Runtime.SymbolValue(Symbols.It));
            }
        }
Пример #2
0
        public static void RunCommand(Action <object> func, Cons lispCode, bool showTime = false, bool smartParens = false)
        {
            var output = GetConsoleOut();

            if (lispCode != null)
            {
                var head  = Runtime.First(lispCode) as Symbol;
                var scope = Runtime.ReplGetCurrentAnalysisScope();

                if (smartParens && head != null)
                {
                    if (Runtime.LooksLikeFunction(head))
                    {
                        lispCode = Runtime.MakeCons(lispCode, (Cons)null);
                    }
                }

                timer.Reset();

                foreach (var expr in lispCode)
                {
                    var expr2 = Runtime.Compile(expr, scope);
                    timer.Start();
                    object val = Runtime.Execute(expr2);
                    if (Runtime.ToBool(Runtime.GetDynamic(Symbols.ReplForceIt)))
                    {
                        val = Runtime.Force(val);
                    }
                    timer.Stop();
                    if (func == null)
                    {
                        Runtime.SetSymbolValue(Symbols.It, val);
                        if (val != VOID.Value)
                        {
                            Runtime.PrintStream(output, "", "it: ");
                            Runtime.PrettyPrintLine(output, 4, null, val);
                        }
                    }
                    else
                    {
                        func(val);
                    }
                }

                var time = timer.ElapsedMilliseconds;
                if (showTime)
                {
                    Runtime.PrintStream(GetConsoleLog(), "info", Runtime.MakeString("Elapsed time: ", time, " ms\n"));
                }
            }
            else
            {
                func(Runtime.SymbolValue(Symbols.It));
            }
        }
Пример #3
0
 internal static Window GetStdScr()
 {
     if (Symbols.StdScr != null && Symbols.StdScr.Usage != SymbolUsage.None)
     {
         return((Window)Runtime.GetDynamic(Symbols.StdScr));
     }
     else
     {
         return(StdScr);
     }
 }
Пример #4
0
        public static void Playback(string script, params object[] args)
        {
            var kwargs = Runtime.ParseKwargs(args, new string[] { "window", "delay", "delimiter" });
            var window = (TextWindow)(kwargs[0] ?? StdScr);
            var delay  = (int)(kwargs[1] ?? Runtime.GetDynamic(Symbols.PlaybackDelay));
            var prefix = (string)(kwargs[2] ?? Runtime.GetDynamic(Symbols.PlaybackDelimiter));
            var suffix = GetSuffixFromPrefix(prefix);
            var list   = GetPlaybackList(script, prefix, suffix);

            foreach (var key in list)
            {
                window.SendKey(key);
                if (delay > 0)
                {
                    Runtime.Sleep(delay);
                }
            }
        }
Пример #5
0
        //public static bool printCompact = true;
        public string ToString(bool escape, int radix = -1)
        {
            if (!(cdr is IEnumerator || cdr is DelayedExpression))
            {
                bool printCompact = Runtime.ToBool(Runtime.GetDynamic(Symbols.PrintCompact));

                if (escape && printCompact)
                {
                    var first  = Runtime.First(this);
                    var second = Runtime.Second(this);
                    var third  = Runtime.Third(this);

                    if (first == Symbols.Dot && second is string && third == null)
                    {
                        return(string.Format(".{0}", second));
                    }
                    else if (first == Symbols.NullableDot && second is string && third == null)
                    {
                        return(string.Format("?{0}", second));
                    }
                    else if (first == Symbols.Quote && third == null)
                    {
                        return(string.Format("'{0}", second));
                    }
                }
            }

            var buf = new StringWriter();

            if (escape)
            {
                buf.Write("(");
            }

            Cons list      = this;
            bool needcomma = false;

            while (list != null)
            {
                if (needcomma)
                {
                    buf.Write(" ");
                }

                buf.Write(Runtime.ToPrintString(list.Car, escape, radix));

                if (escape)
                {
                    if (list.cdr is IEnumerator || list.cdr is DelayedExpression)
                    {
                        buf.Write(" ...");
                        break;
                    }
                }

                needcomma = true;

                list = list.Cdr;
            }

            if (escape)
            {
                buf.Write(")");
            }

            return(buf.ToString());
        }
Пример #6
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);
            }
        }
Пример #7
0
        private ReadtableEntry GetEntry(char ch)
        {
            var readTable = (Readtable)Runtime.GetDynamic(Symbols.Readtable);

            return(readTable.GetEntry(ch));
        }
Пример #8
0
        public static string ConvertToString(BigInteger n, bool escape, int radix)
        {
            if (radix == -1)
            {
                radix = (int)Runtime.GetDynamic(Symbols.PrintBase);
            }

            if (radix == -1)
            {
                radix = 10;
            }
            else if (radix < 2 || radix > 36)
            {
                throw new LispException("Invalid number base: {0}", radix);
            }

            if (n == 0)
            {
                return("0");
            }

            var sign = (n >= 0) ? "" : "-";

            n = (n >= 0) ? n : -n;
            var stk = new Vector();

            while (n != 0)
            {
                var d = (int)(n % radix);
                if (d <= 9)
                {
                    stk.Add((char)(d + '0'));
                }
                else
                {
                    stk.Add((char)(d - 10 + 'a'));
                }
                n = n / radix;
            }
            stk.Reverse();
            if (escape)
            {
                switch (radix)
                {
                case 10:
                    return(sign + Runtime.MakeString(stk.ToArray()));

                case 16:
                    return(sign + "0x" + Runtime.MakeString(stk.ToArray()));

                case 8:
                    return(sign + "0" + Runtime.MakeString(stk.ToArray()));

                case 2:
                    return("#b" + sign + Runtime.MakeString(stk.ToArray()));

                default:
                    return("#" + radix + "r" + sign + Runtime.MakeString(stk.ToArray()));
                }
            }
            else
            {
                return(sign + Runtime.MakeString(stk.ToArray()));
            }
        }