Program.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.OpenStandardLibraries();
  8. state.Environment["vec3"] = new LVec3();
  9. try
  10. {
  11. var source = File.ReadAllText("test.lua");
  12. var syntaxTree = LuaSyntaxTree.Parse(source, "test.lua");
  13. Console.WriteLine("Source Code " + new string('-', 50));
  14. var debugger = new DisplayStringSyntaxVisitor();
  15. Console.WriteLine(debugger.GetDisplayString(syntaxTree));
  16. var chunk = LuaCompiler.Default.Compile(syntaxTree, "test.lua");
  17. DebugChunk(chunk, 0);
  18. Console.WriteLine("Output " + new string('-', 50));
  19. var results = new LuaValue[64];
  20. var resultCount = await state.RunAsync(chunk, results);
  21. Console.WriteLine("Result " + new string('-', 50));
  22. for (int i = 0; i < resultCount; 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. }
  32. static void DebugChunk(Chunk chunk, int id)
  33. {
  34. Console.WriteLine($"Chunk[{id}]" + new string('=', 50));
  35. Console.WriteLine($"Parameters:{chunk.ParameterCount}");
  36. Console.WriteLine("Instructions " + new string('-', 50));
  37. var index = 0;
  38. foreach (var inst in chunk.Instructions.ToArray())
  39. {
  40. Console.WriteLine($"[{index}]\t{chunk.SourcePositions[index]}\t\t{inst}");
  41. index++;
  42. }
  43. Console.WriteLine("Constants " + new string('-', 50)); index = 0;
  44. foreach (var constant in chunk.Constants.ToArray())
  45. {
  46. Console.WriteLine($"[{index}]\t{constant}");
  47. index++;
  48. }
  49. Console.WriteLine("UpValues " + new string('-', 50)); index = 0;
  50. foreach (var upValue in chunk.UpValues.ToArray())
  51. {
  52. Console.WriteLine($"[{index}]\t{upValue.Name}\t{(upValue.IsInRegister ? 1 : 0)}\t{upValue.Index}");
  53. index++;
  54. }
  55. Console.WriteLine();
  56. var nestedChunkId = 0;
  57. foreach (var localChunk in chunk.Functions)
  58. {
  59. DebugChunk(localChunk, nestedChunkId);
  60. nestedChunkId++;
  61. }
  62. }