80 lines
1.6 KiB
C#
80 lines
1.6 KiB
C#
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();
|
|
}
|
|
} |