private object CompileList(CompileHelper helper, params string[] opers) { var list = new Vector(); CompileList(list, helper, opers); // Return the shortest code. if (list.Count == 0) { return(null); } else if (list.Count == 1) { return(list[0]); } else { object code = list[0]; for (var i = 1; i < list.Count; i += 2) { var lispOper = GetLispOperator(list[i]); if (SupportsMany(lispOper) && code is Cons && Runtime.First(code) == lispOper) { code = Runtime.Append((Cons)code, Runtime.MakeList(list[i + 1])); } else { code = Runtime.MakeList(lispOper, code, list[i + 1]); } } return(code); } }
public static string ExtractCommand(Cons code) { var head = Runtime.First(code); var sym = head as Symbol; var cons = head as Cons; if (sym != null) { if (Runtime.Keywordp(sym)) { return(sym.Name); } else if (sym.Name == "?") { return("?"); } } if (cons != null) { var head2 = Runtime.First(cons); if (head2 == Symbols.Unquote) { return(Runtime.MakeString(Runtime.Second(cons))); } } return(null); }
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 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)); } }
//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()); }
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); } }
public static void EvalPrintCommand(string data, bool debugging) { var output = GetConsoleOut(); if (String.IsNullOrWhiteSpace(data)) { return; } Runtime.RestoreStackAndFrame(state.Peek()); var leadingSpace = data[0] == ' '; var haveCommand = data[0] == ':' || data[0] == ';'; if (haveCommand) { var code = Runtime.ReadAllFromString(data.Substring(1)); if (code == null) { return; } 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.Print(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.Print(output, str); } Runtime.PrintLine(output, "?"); return; } switch (command) { case "continue": { if (debugging) { throw new ContinueFromBreakpointException(); } break; } case "clear": { History.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 { var code = Runtime.ReadAllFromString(data); RunCommand(null, code); } }