Program.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Runtime.CompilerServices;
  2. using Lua.CodeAnalysis.Syntax;
  3. using Lua.CodeAnalysis.Compilation;
  4. using Lua.Runtime;
  5. using Lua;
  6. using Lua.Standard;
  7. var state = LuaState.Create();
  8. state.OpenStandardLibraries();
  9. state.Environment["vec3"] = new LVec3();
  10. try
  11. {
  12. var source = File.ReadAllText(GetAbsolutePath("test.lua"));
  13. var syntaxTree = LuaSyntaxTree.Parse(source, "test.lua");
  14. Console.WriteLine("Source Code " + new string('-', 50));
  15. var debugger = new DisplayStringSyntaxVisitor();
  16. Console.WriteLine(debugger.GetDisplayString(syntaxTree));
  17. var chunk = LuaCompiler.Default.Compile(syntaxTree, "test.lua");
  18. DebugChunk(chunk, 0);
  19. Console.WriteLine("Output " + new string('-', 50));
  20. var results = new LuaValue[64];
  21. var resultCount = await state.RunAsync(chunk, results);
  22. Console.WriteLine("Result " + new string('-', 50));
  23. for (int i = 0; i < resultCount; i++)
  24. {
  25. Console.WriteLine(results[i]);
  26. }
  27. Console.WriteLine("End " + new string('-', 50));
  28. }
  29. catch (Exception ex)
  30. {
  31. Console.WriteLine(ex);
  32. if(ex is LuaRuntimeException { InnerException: not null } luaEx)
  33. {
  34. Console.WriteLine(luaEx.InnerException);
  35. }
  36. }
  37. static string GetAbsolutePath(string relativePath, [CallerFilePath] string callerFilePath = "")
  38. {
  39. return Path.Combine(Path.GetDirectoryName(callerFilePath)!, relativePath);
  40. }
  41. static void DebugChunk(Chunk chunk, int id)
  42. {
  43. Console.WriteLine($"Chunk[{id}]" + new string('=', 50));
  44. Console.WriteLine($"Parameters:{chunk.ParameterCount}");
  45. Console.WriteLine("Instructions " + new string('-', 50));
  46. var index = 0;
  47. foreach (var inst in chunk.Instructions.ToArray())
  48. {
  49. Console.WriteLine($"[{index}]\t{chunk.SourcePositions[index]}\t\t{inst}");
  50. index++;
  51. }
  52. Console.WriteLine("Locals " + new string('-', 50));
  53. index = 0;
  54. foreach (var local in chunk.Locals.ToArray())
  55. {
  56. Console.WriteLine($"[{index}]\t{local.Index}\t{local.Name}\t{local.StartPc}\t{local.EndPc}");
  57. index++;
  58. }
  59. Console.WriteLine("Constants " + new string('-', 50));
  60. index = 0;
  61. foreach (var constant in chunk.Constants.ToArray())
  62. {
  63. Console.WriteLine($"[{index}]\t{constant}");
  64. index++;
  65. }
  66. Console.WriteLine("UpValues " + new string('-', 50));
  67. index = 0;
  68. foreach (var upValue in chunk.UpValues.ToArray())
  69. {
  70. Console.WriteLine($"[{index}]\t{upValue.Name}\t{(upValue.IsInRegister ? 1 : 0)}\t{upValue.Index}");
  71. index++;
  72. }
  73. Console.WriteLine();
  74. var nestedChunkId = 0;
  75. foreach (var localChunk in chunk.Functions)
  76. {
  77. DebugChunk(localChunk, nestedChunkId);
  78. nestedChunkId++;
  79. }
  80. }