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