...
This commit is contained in:
80
src/compiler/NubLang/Generation/QBE/QBEWriter.cs
Normal file
80
src/compiler/NubLang/Generation/QBE/QBEWriter.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user