private static void InterpreterLoop(ReplInterpreter interpreter, ShellContext shellContext) { Console.Write(interpreter.ClassicPrompt + " "); string s = Console.ReadLine(); if (!interpreter.HasPendingCommand && s.StartsWith("!")) { ExecuteCommand(shellContext, s.Substring(1)); return; } try { DynValue result = interpreter.Evaluate(s); if (result != null && result.Type != DataType.Void) { Console.WriteLine("{0}", result); } } catch (InterpreterException ex) { Console.WriteLine("{0}", ex.DecoratedMessage ?? ex.Message); } catch (Exception ex) { Console.WriteLine("{0}", ex.Message); } }
public MoonEngine( IShellServer shell, IOptions <KernelContext> context, ILogger <MoonEngine> logger ) : base(shell, context, logger) { RegisterJsonEncoder( new DynValueConverter() ); RegisterDisplayEncoder( MimeTypes.Markdown, displayable => { if (displayable is IEnumerable <string> list) { return(String.Join( "\n", list.Select(item => $"- {item}") )); } else { return($"`{displayable}`"); } } ); var script = new Script(); script.Options.DebugPrint = str => printFn?.Invoke(str); interp = new ReplInterpreter(script); }
static void Main(string[] args) { CommandManager.Initialize(); Script.DefaultOptions.ScriptLoader = new ReplInterpreterScriptLoader(); Script script = new Script(CoreModules.Preset_Complete); script.Globals["makestatic"] = (Func <string, DynValue>)(MakeStatic); if (CheckArgs(args, new ShellContext(script))) { return; } Banner(); ReplInterpreter interpreter = new ReplInterpreter(script) { HandleDynamicExprs = true, HandleClassicExprsSyntax = true }; while (true) { InterpreterLoop(interpreter, new ShellContext(script)); } }
static async Task Main(string[] args) { CommandManager.Initialize(); Script.DefaultOptions.ScriptLoader = new ReplInterpreterScriptLoader(); Script script = new Script(CoreModules.Preset_Complete); script.Options.ZeroIndexTables = true; script.Globals["makestatic"] = (Func <string, DynValue>)(MakeStatic); script.Globals["mojeip"] = (Func <Task <DynValue> >)(GetMyIp); script.Globals["mojeip_sync"] = (Func <DynValue>)(GetMyIpSync); script.Globals["ds_list_filter"] = (Func <Script, CallbackArguments, DynValue>)(DsListFilter); if (CheckArgs(args, new ShellContext(script))) { return; } Banner(); ReplInterpreter interpreter = new ReplInterpreter(script) { HandleDynamicExprs = true, HandleClassicExprsSyntax = true }; while (true) { InterpreterLoop(interpreter, new ShellContext(script)); } }
public static void ReplMode() { var environment = new ReplEnvironment(new OperationCodeFactory(), new ValueFactory()); var interpreter = new ReplInterpreter(); environment.AddClassesDerivedFromClassInAssembly(typeof(Eilang)); environment.AddExportedFunctionsFromAssembly(typeof(Eilang)); environment.AddExportedModulesFromAssembly(typeof(Eilang)); while (true) { Console.Write(">"); var code = Console.ReadLine() ?? ""; if (string.IsNullOrWhiteSpace(code)) { continue; } try { code = Simplify(code); var reader = new ScriptReader(code, "repl"); var lexer = new ScriptLexer(reader, new CommonLexer(reader)); var parser = new Parser(lexer); var ast = parser.Parse(); Compiler.Compile(environment, ast); var eval = interpreter.Interpret(environment); if (eval.Type != EilangType.Void) { ExportedFunctions.PrintLine(new State(environment, null, null, null, null, new ValueFactory()), Arguments.Create(eval, "eval")); } } catch (ExitException e) { if (!string.IsNullOrWhiteSpace(e.Message)) { var oldColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(e.Message); Console.ForegroundColor = oldColor; } Environment.Exit(e.ExitCode); } catch (ErrorMessageException e) { Program.LogLine(ConsoleColor.Red, e.Message); } catch (Exception e) { Console.WriteLine(e); } } }
static void Main(string[] args) { var programPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var pobDirectory = Path.Combine(programPath, "PathOfBuilding"); EnsurePobExists(pobDirectory).GetAwaiter().GetResult(); var wrapper = new PobWrapper(pobDirectory); wrapper.LoadBuildFromFile(Path.Combine(programPath, "Builds", "Test-Character.xml")); Console.WriteLine("REPL Ready"); var repl = new ReplInterpreter(wrapper.Script); var line = ""; while (line != "quit") { Console.Write(repl.ClassicPrompt); line = Console.ReadLine(); try { var result = repl.Evaluate(line); if (result.IsNotNil()) { Console.WriteLine(result); } } catch (Exception e) { Console.WriteLine($"Exception: {e.Message}"); } } /* * wrapper.RunLua(@" * local build = mainObject.main.modes[""BUILD""] * * local f = io.open(""Player.txt"", ""w"") * f:write(inspect(build.calcsTab.mainEnv.player)) * f:close() * "); */ if (Debugger.IsAttached) { Console.ReadLine(); } }
/// <summary> /// REPLのセットアップ /// </summary> public void SetUpREPL() { var lua = new Script(); UserData.RegisterType <LuaREPLExtension>(); lua.Globals.Set(ExtensionObjectName, UserData.Create(new LuaREPLExtension(this))); _luaInterpreter = new ReplInterpreter(lua); //拡張用のスクリプトを読み込み using (var sr = new StreamReader(ExtensionFilePath, Encoding.GetEncoding("UTF-8"))) { //テキストをインタプリタ用に変換 var text = sr.ReadToEnd() .Split(new string[] { Environment.NewLine, "\t" }, StringSplitOptions.RemoveEmptyEntries) .Aggregate((s1, s2) => s1 + " " + s2); //拡張用スクリプトをあらかじめ実行 _luaInterpreter.Evaluate(text); } }
public static DynValue debug(ScriptExecutionContext executionContext, CallbackArguments args) { Script script = executionContext.GetScript(); if (script.Options.DebugInput == null) { throw new ScriptRuntimeException("debug.debug not supported on this platform/configuration"); } ReplInterpreter interpreter = new ReplInterpreter(script) { HandleDynamicExprs = false, HandleClassicExprsSyntax = true }; while (true) { string s = script.Options.DebugInput(interpreter.ClassicPrompt + " "); try { DynValue result = interpreter.Evaluate(s); if (result != null && result.Type != DataType.Void) { script.Options.DebugPrint(string.Format("{0}", result)); } } catch (InterpreterException ex) { script.Options.DebugPrint(string.Format("{0}", ex.DecoratedMessage ?? ex.Message)); } catch (Exception ex) { script.Options.DebugPrint(string.Format("{0}", ex.Message)); } } }
public void Run <T>(T startup, string exit, string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("-----------------------------------------------------------"); Console.WriteLine("用法:"); Console.WriteLine($" >> module = Startup.ModuleManager.GetModule(\"LoginModule\")"); Console.WriteLine("-----------------------------------------------------------"); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; var script = new Script(CoreModules.Preset_Complete); var interpreter = new ReplInterpreter(script) { HandleDynamicExprs = true, HandleClassicExprsSyntax = true }; script.Globals["Startup"] = startup; while (true) { Console.Write("$Ssf-lua " + interpreter.ClassicPrompt + " "); var s = Console.ReadLine(); try { var result = interpreter.Evaluate(s); if (result != null && result.Type != DataType.Void) { Console.WriteLine("{0}", result); } } catch (InterpreterException ex) { Console.WriteLine("{0}", ex.DecoratedMessage ?? ex.Message); } catch (Exception ex) { Console.WriteLine("{0}", ex.Message); } } }
private static void InterpreterLoop(ReplInterpreter interpreter, ShellContext shellContext) { Console.Write(interpreter.ClassicPrompt + " "); string s = Console.ReadLine(); try { DynValue result = interpreter.Evaluate(s); if (result != null && result.Type != DataType.Void) { Console.WriteLine("{0}", result); } } catch (InterpreterException ex) { Console.WriteLine("{0}", ex.DecoratedMessage ?? ex.Message); } catch (Exception ex) { Console.WriteLine("{0}", ex.Message); } }
/// <summary> /// Asynchronously evaluates a REPL command. /// This method returns the result of the computation, or null if more input is needed for having valid code. /// In case of errors, exceptions are propagated to the caller. /// /// This method is supported only on .NET 4.x and .NET 4.x PCL targets. /// </summary> /// <param name="interpreter">The interpreter.</param> /// <param name="input">The input.</param> /// <returns> /// This method returns the result of the computation, or null if more input is needed for a computation. /// </returns> public static Task <DynValue> EvaluateAsync(this ReplInterpreter interpreter, string input) { return(ExecAsync(() => interpreter.Evaluate(input))); }
static void Main(string[] args) { Script.DefaultOptions.ScriptLoader = new ReplInterpreterScriptLoader(); Console.WriteLine("MoonSharp REPL {0} [{1}]", Script.VERSION, Script.GlobalOptions.Platform.GetPlatformName()); Console.WriteLine("Copyright (C) 2014-2015 Marco Mastropaolo"); Console.WriteLine("http://www.moonsharp.org"); Console.WriteLine(); if (args.Length == 1) { Script script = new Script(); script.DoFile(args[0]); Console.WriteLine("Done."); if (System.Diagnostics.Debugger.IsAttached) { Console.ReadKey(); } } else { Console.WriteLine("Type Lua code to execute it or type !help to see help on commands.\n"); Console.WriteLine("Welcome.\n"); Script script = new Script(CoreModules.Preset_Complete); ReplInterpreter interpreter = new ReplInterpreter(script) { HandleDynamicExprs = true, HandleClassicExprsSyntax = true }; while (true) { Console.Write(interpreter.ClassicPrompt + " "); string s = Console.ReadLine(); if (!interpreter.HasPendingCommand && s.StartsWith("!")) { ParseCommand(script, s.Substring(1)); continue; } try { DynValue result = interpreter.Evaluate(s); if (result != null && result.Type != DataType.Void) { Console.WriteLine("{0}", result); } } catch (InterpreterException ex) { Console.WriteLine("{0}", ex.DecoratedMessage ?? ex.Message); } catch (Exception ex) { Console.WriteLine("{0}", ex.Message); } } } }