Program.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using MoonSharp.Interpreter;
  8. using MoonSharp.Interpreter.Execution;
  9. using MoonSharp.RemoteDebugger;
  10. using MoonSharp.RemoteDebugger.Network;
  11. namespace MoonSharp
  12. {
  13. class Program
  14. {
  15. static DynValue Print(ScriptExecutionContext executionContext, CallbackArguments values)
  16. {
  17. string prn = string.Join(" ", values.GetArray().Where(v => v.IsNotVoid()).Select(v => v.ToPrintString()).ToArray());
  18. Console.WriteLine("{0}", prn);
  19. return DynValue.Nil;
  20. }
  21. static DynValue Read(ScriptExecutionContext executionContext, CallbackArguments values)
  22. {
  23. double d = double.Parse(Console.ReadLine());
  24. return DynValue.NewNumber(d);
  25. }
  26. static StringBuilder g_TreeDump = new StringBuilder();
  27. [STAThread]
  28. static void Main(string[] args)
  29. {
  30. Console.WriteLine("Moon# {0}\nCopyright (C) 2014 Marco Mastropaolo\nhttp://www.moonsharp.org",
  31. Assembly.GetAssembly(typeof(Script)).GetName().Version);
  32. Console.WriteLine("Based on Lua 5.1 - 5.3, Copyright (C) 1994-2014 Lua.org");
  33. Console.WriteLine();
  34. if (args.Length == 1)
  35. {
  36. Script script = new Script();
  37. script.Globals.Set("print", DynValue.NewCallback(new CallbackFunction(Print)));
  38. script.DoFile(args[0]);
  39. Console.WriteLine("Done.");
  40. if (System.Diagnostics.Debugger.IsAttached)
  41. Console.ReadKey();
  42. }
  43. else
  44. {
  45. Console.WriteLine("Type <enter> twice to execute code.\n");
  46. Script script = new Script();
  47. script.Globals.Set("print", DynValue.NewCallback(new CallbackFunction(Print)));
  48. string cmd = "";
  49. while (true)
  50. {
  51. Console.Write("{0}> ", string.IsNullOrEmpty(cmd) ? "" : ">");
  52. string s = Console.ReadLine();
  53. if (s.StartsWith("!"))
  54. {
  55. ParseCommand(script, s.Substring(1));
  56. continue;
  57. }
  58. if (s != "")
  59. {
  60. cmd += s + "\n";
  61. continue;
  62. }
  63. if (cmd.Length == 0)
  64. continue;
  65. //Console.WriteLine("=====");
  66. //Console.WriteLine("{0}", cmd);
  67. //Console.WriteLine("=====");
  68. if (cmd == "exit")
  69. return;
  70. try
  71. {
  72. var v = script.LoadString(cmd, null, "stdin");
  73. Console.WriteLine("={0}", script.Call(v));
  74. }
  75. catch (ScriptRuntimeException ex)
  76. {
  77. Console.WriteLine("{0}", ex.DecoratedMessage);
  78. }
  79. catch (Exception ex)
  80. {
  81. Console.WriteLine("{0}", ex.Message);
  82. }
  83. cmd = "";
  84. }
  85. }
  86. }
  87. static Utf8TcpServer m_Server;
  88. static HttpServer m_Http;
  89. static DebugServer m_DbgS;
  90. private static void ParseCommand(Script S, string p)
  91. {
  92. if (p == "net")
  93. {
  94. m_Server = new Utf8TcpServer(1912, 8 << 20, '\n', Utf8TcpServerOptions.SingleClientOnly);
  95. m_Server.DataReceived += m_Server_DataReceivedAny;
  96. m_Server.Start();
  97. }
  98. if (p == "http")
  99. {
  100. m_Http = new HttpServer(1994, Utf8TcpServerOptions.Default);
  101. m_Http.RegisterResource("/", HttpResource.CreateText(HttpResourceType.PlainText, "Hello, world!\n"));
  102. m_Http.RegisterResource("/1.png", HttpResource.CreateBinary(HttpResourceType.Png, File.ReadAllBytes(@"c:\temp\1.png")));
  103. m_Http.Authenticator = (usr, pwd) => usr == pwd;
  104. m_Http.Start();
  105. }
  106. if (p == "dbg")
  107. {
  108. m_DbgS = new DebugServer("MoonSharp REPL interpreter", S, 20001, false);
  109. S.AttachDebugger(m_DbgS);
  110. }
  111. }
  112. static void m_Server_DataReceivedAny(object sender, Utf8TcpPeerEventArgs e)
  113. {
  114. Console.WriteLine("RCVD: {0}", e.Message);
  115. e.Peer.Send(e.Message.ToUpper());
  116. }
  117. }
  118. }