2023-12-04 08:17:13 -05:00
|
|
|
using System.Text;
|
2022-05-31 16:12:46 -04:00
|
|
|
|
|
|
|
namespace Ryujinx.Horizon.Generators
|
|
|
|
{
|
|
|
|
class CodeGenerator
|
|
|
|
{
|
2023-01-18 17:25:16 -05:00
|
|
|
private const int IndentLength = 4;
|
|
|
|
|
2022-05-31 16:12:46 -04:00
|
|
|
private readonly StringBuilder _sb;
|
2023-01-18 17:25:16 -05:00
|
|
|
private int _currentIndentCount;
|
2022-05-31 16:12:46 -04:00
|
|
|
|
|
|
|
public CodeGenerator()
|
|
|
|
{
|
|
|
|
_sb = new StringBuilder();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void EnterScope(string header = null)
|
|
|
|
{
|
|
|
|
if (header != null)
|
|
|
|
{
|
|
|
|
AppendLine(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendLine("{");
|
|
|
|
IncreaseIndentation();
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
public void LeaveScope(string suffix = "")
|
2022-05-31 16:12:46 -04:00
|
|
|
{
|
|
|
|
DecreaseIndentation();
|
2023-01-04 17:15:45 -05:00
|
|
|
AppendLine($"}}{suffix}");
|
2022-05-31 16:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void IncreaseIndentation()
|
|
|
|
{
|
2023-01-18 17:25:16 -05:00
|
|
|
_currentIndentCount++;
|
2022-05-31 16:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DecreaseIndentation()
|
|
|
|
{
|
2023-01-18 17:25:16 -05:00
|
|
|
if (_currentIndentCount - 1 >= 0)
|
|
|
|
{
|
2023-06-26 01:35:19 -04:00
|
|
|
_currentIndentCount--;
|
2023-01-18 17:25:16 -05:00
|
|
|
}
|
2022-05-31 16:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AppendLine()
|
|
|
|
{
|
|
|
|
_sb.AppendLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AppendLine(string text)
|
|
|
|
{
|
2023-01-18 17:25:16 -05:00
|
|
|
_sb.Append(' ', IndentLength * _currentIndentCount);
|
|
|
|
_sb.AppendLine(text);
|
2022-05-31 16:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return _sb.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|