CodeGenerator.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Text;
  2. namespace Ryujinx.Horizon.Generators
  3. {
  4. class CodeGenerator
  5. {
  6. private const int IndentLength = 4;
  7. private readonly StringBuilder _sb;
  8. private int _currentIndentCount;
  9. public CodeGenerator()
  10. {
  11. _sb = new StringBuilder();
  12. }
  13. public void EnterScope(string header = null)
  14. {
  15. if (header != null)
  16. {
  17. AppendLine(header);
  18. }
  19. AppendLine("{");
  20. IncreaseIndentation();
  21. }
  22. public void LeaveScope(string suffix = "")
  23. {
  24. DecreaseIndentation();
  25. AppendLine($"}}{suffix}");
  26. }
  27. public void IncreaseIndentation()
  28. {
  29. _currentIndentCount++;
  30. }
  31. public void DecreaseIndentation()
  32. {
  33. if (_currentIndentCount - 1 >= 0)
  34. {
  35. _currentIndentCount--;
  36. }
  37. }
  38. public void AppendLine()
  39. {
  40. _sb.AppendLine();
  41. }
  42. public void AppendLine(string text)
  43. {
  44. _sb.Append(' ', IndentLength * _currentIndentCount);
  45. _sb.AppendLine(text);
  46. }
  47. public override string ToString()
  48. {
  49. return _sb.ToString();
  50. }
  51. }
  52. }