Program.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Lua.CodeAnalysis.Syntax;
  2. using Lua.CodeAnalysis.Compilation;
  3. using Lua.Runtime;
  4. using Lua;
  5. using Lua.Standard;
  6. var state = LuaState.Create();
  7. state.OpenBasicLibrary();
  8. try
  9. {
  10. var source =
  11. """
  12. -- メインコルーチンの定義
  13. local co_main = coroutine.create(function ()
  14. print("Main coroutine starts")
  15. -- コルーチンAの定義
  16. local co_a = coroutine.create(function()
  17. for i = 1, 3 do
  18. print("Coroutine A, iteration "..i)
  19. coroutine.yield()
  20. end
  21. print("Coroutine A ends")
  22. end)
  23. --コルーチンBの定義
  24. local co_b = coroutine.create(function()
  25. print("Coroutine B starts")
  26. coroutine.yield()-- 一時停止
  27. print("Coroutine B resumes")
  28. end)
  29. -- コルーチンCの定義(コルーチンBを呼び出す)
  30. local co_c = coroutine.create(function()
  31. print("Coroutine C starts")
  32. coroutine.resume(co_b)-- コルーチンBを実行
  33. print("Coroutine C calls B and resumes")
  34. coroutine.yield()-- 一時停止
  35. print("Coroutine C resumes")
  36. end)
  37. -- コルーチンAとCの交互実行
  38. for _ = 1, 2 do
  39. coroutine.resume(co_a)
  40. coroutine.resume(co_c)
  41. end
  42. -- コルーチンAを再開し完了させる
  43. coroutine.resume(co_a)
  44. -- コルーチンCを再開し完了させる
  45. coroutine.resume(co_c)
  46. print("Main coroutine ends")
  47. end)
  48. --メインコルーチンを開始
  49. coroutine.resume(co_main)
  50. """;
  51. var syntaxTree = LuaSyntaxTree.Parse(source, "main.lua");
  52. Console.WriteLine("Source Code " + new string('-', 50));
  53. var debugger = new DisplayStringSyntaxVisitor();
  54. Console.WriteLine(debugger.GetDisplayString(syntaxTree));
  55. var chunk = LuaCompiler.Default.Compile(syntaxTree, "main.lua");
  56. var id = 0;
  57. DebugChunk(chunk, ref id);
  58. Console.WriteLine("Output " + new string('-', 50));
  59. var results = new LuaValue[64];
  60. var resultCount = await state.RunAsync(chunk, results);
  61. Console.WriteLine("Result " + new string('-', 50));
  62. for (int i = 0; i < resultCount; i++)
  63. {
  64. Console.WriteLine(results[i]);
  65. }
  66. Console.WriteLine("End " + new string('-', 50));
  67. }
  68. catch (Exception ex)
  69. {
  70. Console.WriteLine(ex);
  71. }
  72. static void DebugChunk(Chunk chunk, ref int id)
  73. {
  74. Console.WriteLine($"Chunk[{id++}]" + new string('=', 50));
  75. Console.WriteLine("Instructions " + new string('-', 50));
  76. var index = 0;
  77. foreach (var inst in chunk.Instructions.ToArray())
  78. {
  79. Console.WriteLine($"[{index}]\t{chunk.SourcePositions[index]}\t{inst}");
  80. index++;
  81. }
  82. Console.WriteLine("Constants " + new string('-', 50)); index = 0;
  83. foreach (var constant in chunk.Constants.ToArray())
  84. {
  85. Console.WriteLine($"[{index}]\t{constant}");
  86. index++;
  87. }
  88. Console.WriteLine("UpValues " + new string('-', 50)); index = 0;
  89. foreach (var upValue in chunk.UpValues.ToArray())
  90. {
  91. Console.WriteLine($"[{index}]\t{upValue.Name}\t{(upValue.IsInRegister ? 1 : 0)}\t{upValue.Index}");
  92. index++;
  93. }
  94. Console.WriteLine();
  95. foreach (var localChunk in chunk.Functions)
  96. {
  97. DebugChunk(localChunk, ref id);
  98. }
  99. }