Program.cs 2.0 KB

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