/// <summary> /// Run a single read-evaluate-print iteration. /// </summary> void RunIteration() { // Read user input BufferedInput = ReadUserInput(); if (string.IsNullOrEmpty(BufferedInput)) { return; } // Compile the module var module = WrapIodineOperation(this, () => Iodine.CompileSource(BufferedInput)); if (module == null) { return; } // Invoke the module var result = WrapIodineOperation(this, () => Iodine.InvokeModule(module)); if (result == null) { return; } // Pretty print the result PrettyPrint.WriteLine(result); }
public static void HandleUnhandledIodineException(Shell instance, Iodine.Runtime.UnhandledIodineExceptionException e) { // Pretty print the underlying Iodine exception PrettyPrint.WriteLine(e.OriginalException); // Test if the error occurred in an actual module if (e.Frame.Module.Name != "__anonymous__") { // Print the stack trace e.PrintStack(); } }
/// <summary> /// Initializes a new instance of the <see cref="T:iox.Shell"/> class. /// </summary> public Shell(Configuration conf) { Conf = conf; // Get assembly version AssemblyVersion = ReflectionHelper.GetVersion(); // Define builtin exit function var iodineHookExit = new BuiltinMethodCallback((vm, self, arguments) => { shouldExit = true; return(IodineNull.Instance); }, null); iodineHookExit.SetAttribute("__doc__", new IodineString("Exits the iox REPL shell.")); // Define builtin help function var iodineHookHelp = new BuiltinMethodCallback((vm, self, arguments) => { // Test argument count if (arguments.Length == 0) { ANSI.WriteLine("Please pass an object to the help function!"); return(IodineNull.Instance); } // Get the target object var target = arguments [0]; // Test __doc__ attribute existence if (!target.HasAttribute("__doc__")) { ANSI.WriteLine($"The specified {White}{target.TypeDef.Name}{Default} does not provide any documentation :("); return(IodineNull.Instance); } // Get attribute var attr = target.GetAttribute(vm, "__doc__"); // Get documentation var doc = ( attr as IodineString ?? (attr as IodineProperty)?.Get(vm) as IodineString ?? (attr as InternalIodineProperty)?.Get(vm) as IodineString ); // Write documentation if (doc != null) { PrettyPrint.WriteLine(doc); } return(IodineNull.Instance); }, null); iodineHookHelp.SetAttribute("__doc__", new IodineString("Prints the documentation for the specified object.")); // Setup Iodine context Iodine = new IodineContext(); Iodine.ExposeGlobal("exit", iodineHookExit); Iodine.ExposeGlobal("help", iodineHookHelp); Iodine.AddSearchPaths("./iodine/modules"); if (Environment.GetEnvironmentVariable("IODINE_MODULES") != null) { Iodine.AddSearchPaths(Environment.GetEnvironmentVariable("IODINE_MODULES")); } if (Environment.GetEnvironmentVariable("IODINE_HOME") != null) { Iodine.AddSearchPaths(Path.Combine(Environment.GetEnvironmentVariable("IODINE_HOME"), "modules")); } // Set prompt Prompt = new Prompt("λ"); if (Conf.UsePowerlines) { Prompt = new Prompt(Powerline().Segment($"IoX {AssemblyVersion.ToString (2)} ", fg: DarkCyan, bg: White).ToAnsiString()); } // Set hinter Hinter = new HinterWrapper(this); }