Program.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. using var results = await state.RunAsync(chunk);
  21. Console.WriteLine("Result " + new string('-', 50));
  22. for (int i = 0; i < results.Count; i++)
  23. {
  24. Console.WriteLine(results[i]);
  25. }
  26. Console.WriteLine("End " + new string('-', 50));
  27. }
  28. catch (Exception ex)
  29. {
  30. Console.WriteLine(ex);
  31. if (ex is LuaRuntimeException { InnerException: not null } luaEx)
  32. {
  33. Console.WriteLine(luaEx.InnerException);
  34. }
  35. }
  36. static string GetAbsolutePath(string relativePath, [CallerFilePath] string callerFilePath = "")
  37. {
  38. return Path.Combine(Path.GetDirectoryName(callerFilePath)!, relativePath);
  39. }
  40. static void DebugChunk(Chunk chunk, int id)
  41. {
  42. Console.WriteLine($"Chunk[{id}]" + new string('=', 50));
  43. Console.WriteLine($"Parameters:{chunk.ParameterCount}");
  44. Console.WriteLine("Instructions " + new string('-', 50));
  45. var index = 0;
  46. foreach (var inst in chunk.Instructions.ToArray())
  47. {
  48. Console.WriteLine($"[{index}]\t{chunk.SourcePositions[index]}\t\t{inst}");
  49. index++;
  50. }
  51. Console.WriteLine("Locals " + new string('-', 50));
  52. index = 0;
  53. foreach (var local in chunk.Locals.ToArray())
  54. {
  55. Console.WriteLine($"[{index}]\t{local.Index}\t{local.Name}\t{local.StartPc}\t{local.EndPc}");
  56. index++;
  57. }
  58. Console.WriteLine("Constants " + new string('-', 50));
  59. index = 0;
  60. foreach (var constant in chunk.Constants.ToArray())
  61. {
  62. Console.WriteLine($"[{index}]\t{constant}");
  63. index++;
  64. }
  65. Console.WriteLine("UpValues " + new string('-', 50));
  66. index = 0;
  67. foreach (var upValue in chunk.UpValues.ToArray())
  68. {
  69. Console.WriteLine($"[{index}]\t{upValue.Name}\t{(upValue.IsInRegister ? 1 : 0)}\t{upValue.Index}");
  70. index++;
  71. }
  72. Console.WriteLine();
  73. var nestedChunkId = 0;
  74. foreach (var localChunk in chunk.Functions)
  75. {
  76. DebugChunk(localChunk, nestedChunkId);
  77. nestedChunkId++;
  78. }
  79. }