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)); } }
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); } }