This commit is contained in:
nub31
2025-07-06 20:56:56 +02:00
parent f15073cac4
commit c79985bc05
26 changed files with 94 additions and 170 deletions

View File

@@ -0,0 +1,80 @@
using System.Text;
using NubLang.Syntax.Node;
namespace NubLang.Generation.QBE;
internal class QBEWriter
{
private readonly StringBuilder _builder = new();
private int _currentLine = -1;
public QBEWriter(string debugFile)
{
_builder.AppendLine($"dbgfile \"{debugFile}\"");
_builder.AppendLine();
}
public void StartFunction(string signature)
{
_currentLine = -1;
_builder.Append(signature);
_builder.AppendLine(" {");
_builder.AppendLine("@start");
}
public void EndFunction()
{
_builder.AppendLine("}");
}
private void WriteDebugLocation(SourceSpan span)
{
var line = span.Start.Line;
if (_currentLine != line)
{
_builder.AppendLine($" dbgloc {line}");
_currentLine = line;
}
}
public void WriteDebugLocation(BoundNode node)
{
var firstToken = node.Tokens.FirstOrDefault();
if (firstToken != null)
{
// WriteDebugLocation(firstToken.Span);
}
}
public void Indented(string value)
{
_builder.Append('\t');
_builder.AppendLine(value);
}
public void Comment(string comment)
{
_builder.AppendLine("# " + comment);
}
public void WriteLine(string text)
{
_builder.AppendLine(text);
}
public void Write(string text)
{
_builder.Append(text);
}
public void NewLine()
{
_builder.AppendLine();
}
public override string ToString()
{
return _builder.ToString();
}
}