66 lines
1.6 KiB
C#
66 lines
1.6 KiB
C#
using Qrakhen.Qamp.Core.Collections;
|
|
using Qrakhen.Qamp.Core.Values;
|
|
using String = Qrakhen.Qamp.Core.Values.Objects.String;
|
|
|
|
namespace Qrakhen.Qamp.Core.Execution;
|
|
|
|
public class InstructionPtr : Pointer<Instruction>
|
|
{
|
|
public Segment Segment;
|
|
|
|
public Instruction Instruction => Segment.Instructions[Cursor];
|
|
|
|
public InstructionPtr(Segment segment, int position = 0) : base(segment.Instructions, position)
|
|
{
|
|
Segment = segment;
|
|
}
|
|
|
|
public long NextLong()
|
|
{
|
|
long value = Segment.ReadLong(Cursor);
|
|
Cursor += sizeof(long);
|
|
return value;
|
|
}
|
|
|
|
public Value NextConstant()
|
|
{
|
|
return Segment.Constants[NextDynamic()];
|
|
}
|
|
|
|
public String NextString()
|
|
{
|
|
Value value = NextConstant();
|
|
if (value.IsString)
|
|
return value.Ptr.As<String>()!;
|
|
throw new Exception($"Expected string, got {value}");
|
|
}
|
|
|
|
public long NextDynamic()
|
|
{
|
|
var result = Segment.ReadDynamic(Cursor, out int read);
|
|
Cursor += read;
|
|
return result;
|
|
}
|
|
|
|
public String? GetStringConstant(long offset)
|
|
=> Segment.Constants[offset].Ptr.Value as String;
|
|
|
|
public static Instruction operator +(InstructionPtr ptr, int value)
|
|
=> ptr.Segment.Instructions[ptr.Cursor + value];
|
|
|
|
public static Instruction operator -(InstructionPtr ptr, int value)
|
|
=> ptr.Segment.Instructions[ptr.Cursor - value];
|
|
|
|
public static InstructionPtr operator ++(InstructionPtr ptr)
|
|
{
|
|
ptr.Cursor++;
|
|
return ptr;
|
|
}
|
|
|
|
public static InstructionPtr operator --(InstructionPtr ptr)
|
|
{
|
|
ptr.Cursor--;
|
|
return ptr;
|
|
}
|
|
}
|