Program.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using MoonSharp.Interpreter;
  9. using MoonSharp.Interpreter.Execution;
  10. using MoonSharp.Interpreter.Loaders;
  11. using MoonSharp.RemoteDebugger;
  12. using MoonSharp.RemoteDebugger.Network;
  13. namespace MoonSharp
  14. {
  15. class Program
  16. {
  17. static StringBuilder g_TreeDump = new StringBuilder();
  18. [STAThread]
  19. static void Main(string[] args)
  20. {
  21. Script.DefaultOptions.ScriptLoader = new ReplInterpreterScriptLoader();
  22. Console.WriteLine("MoonSharp REPL {0} [{1}]", Script.VERSION, Script.Platform.GetPlatformName());
  23. Console.WriteLine("Copyright (C) 2014 Marco Mastropaolo");
  24. Console.WriteLine("http://www.moonsharp.org");
  25. Console.WriteLine("Based on Lua 5.1 - 5.3, Copyright (C) 1994-2014 Lua.org");
  26. Console.WriteLine("License: https://raw.githubusercontent.com/xanathar/moonsharp/master/LICENSE");
  27. Console.WriteLine();
  28. if (args.Length == 1)
  29. {
  30. Script script = new Script();
  31. script.DoFile(args[0]);
  32. Console.WriteLine("Done.");
  33. if (System.Diagnostics.Debugger.IsAttached)
  34. Console.ReadKey();
  35. }
  36. else
  37. {
  38. Console.WriteLine("Type <enter> twice to execute code.\n");
  39. Console.WriteLine("Type !help to see help.");
  40. Script script = new Script(CoreModules.Preset_Complete);
  41. string cmd = "";
  42. while (true)
  43. {
  44. Console.Write("{0}> ", string.IsNullOrEmpty(cmd) ? "" : ">");
  45. string s = Console.ReadLine();
  46. if (s.StartsWith("!"))
  47. {
  48. ParseCommand(script, s.Substring(1));
  49. continue;
  50. }
  51. if (s != "")
  52. {
  53. cmd += s + "\n";
  54. continue;
  55. }
  56. if (cmd.Length == 0)
  57. continue;
  58. //Console.WriteLine("=====");
  59. //Console.WriteLine("{0}", cmd);
  60. //Console.WriteLine("=====");
  61. if (cmd == "exit")
  62. return;
  63. try
  64. {
  65. DynValue result = null;
  66. if (cmd.StartsWith("?"))
  67. {
  68. var code = cmd.Substring(1);
  69. var exp = script.CreateDynamicExpression(code);
  70. result = exp.Evaluate();
  71. }
  72. else
  73. {
  74. var v = script.LoadString(cmd, null, "stdin");
  75. result = script.Call(v);
  76. }
  77. Console.WriteLine("={0}", result);
  78. }
  79. catch (ScriptRuntimeException ex)
  80. {
  81. Console.WriteLine("{0}", ex.DecoratedMessage ?? ex.Message);
  82. }
  83. catch (Exception ex)
  84. {
  85. Console.WriteLine("{0}", ex.Message);
  86. }
  87. cmd = "";
  88. }
  89. }
  90. }
  91. static RemoteDebuggerService m_Debugger;
  92. private static void ParseCommand(Script S, string p)
  93. {
  94. if (p == "help")
  95. {
  96. Console.WriteLine("Type Lua code followed by two <enter> keystrokes to execute Lua code, ");
  97. Console.WriteLine("or type one of the following commands to execute them.");
  98. Console.WriteLine("");
  99. Console.WriteLine("Commands:");
  100. Console.WriteLine("");
  101. Console.WriteLine(" !debug - Starts the debugger");
  102. Console.WriteLine(" !run <filename> - Executes the specified Lua script");
  103. Console.WriteLine(" !compile <filename> - Compiles the file in a binary format");
  104. Console.WriteLine("");
  105. }
  106. else if (p == "debug" && m_Debugger == null)
  107. {
  108. m_Debugger = new RemoteDebuggerService();
  109. m_Debugger.Attach(S, "MoonSharp REPL interpreter", false);
  110. Process.Start(m_Debugger.HttpUrlStringLocalHost);
  111. }
  112. else if (p.StartsWith("run"))
  113. {
  114. p = p.Substring(3).Trim();
  115. S.DoFile(p);
  116. }
  117. else if (p == "!")
  118. {
  119. ParseCommand(S, "debug");
  120. ParseCommand(S, @"run c:\temp\test.lua");
  121. }
  122. else if (p.StartsWith("compile"))
  123. {
  124. p = p.Substring("compile".Length).Trim();
  125. string targetFileName = p + "-compiled";
  126. DynValue chunk = S.LoadFile(p);
  127. using (Stream stream = new FileStream(targetFileName, FileMode.Create, FileAccess.Write))
  128. S.Dump(chunk, stream);
  129. }
  130. }
  131. static void m_Server_DataReceivedAny(object sender, Utf8TcpPeerEventArgs e)
  132. {
  133. Console.WriteLine("RCVD: {0}", e.Message);
  134. e.Peer.Send(e.Message.ToUpper());
  135. }
  136. }
  137. }