qamp/Qrakhen.Qamp.Core/Compilation/Builders/SegmentBuilder.cs

39 lines
1.4 KiB
C#

using Qrakhen.Qamp.Core.Collections;
using Qrakhen.Qamp.Core.Execution;
using Qrakhen.Qamp.Core.Values;
namespace Qrakhen.Qamp.Core.Compilation.Builders;
public class SegmentBuilder() : IBuilder<Segment>, IDebug<string>
{
public readonly PushStack<Instruction> Instructions = new();
public readonly PushStack<Value> Constants = new();
public Segment Build()
{
return new Segment(Instructions.ToArray(), Constants.ToArray());
}
public string Debug(DebugLevel level = DebugLevel.None)
{
string str = Debugger.GetContextString(this);
for (int i = 0; i < Instructions.Count; i++)
{
var instruction = Instructions[i];
if ((instruction.Code & 0x80) == 1) {
// dynamic
int length = instruction.Code & (0x80 - 1);
byte[] index = Instructions.Subset(i + 1, length).Select(n => n.Code).ToArray();
str += $"\n [{i:x4}]: Dynamic ({string.Join(' ', index)})";
i += length;
} else
str += $"\n [{i:x4}]: {instruction}".PadRight(32);
/*if (instruction.OpCode == OpCode.Constant) {
i++; // i know, i know
str += $" : {Constants[Instructions.Subset(i, 8).Select(n => n.Code).ToArray().ToInt64()]}";
i += 8;
}*/
}
return str;
}
}