add handlers and routing
This commit is contained in:
parent
bb13c0ae2b
commit
bdd1322a82
|
|
@ -7,7 +7,7 @@ namespace Qrakhen.Qamp.Core.Collections;
|
|||
/// <summary>
|
||||
/// Similar to a register with the caveat that the memory can free up or reserve blocks within itself, returning the next writeable address when storing data.
|
||||
/// </summary>
|
||||
public class Memory<TValue> :
|
||||
public class Addreg<TValue> :
|
||||
IGetSet<Address, TValue>,
|
||||
IToArray<TValue>,
|
||||
IEnumerable<KeyValuePair<Address, TValue>>
|
||||
|
|
@ -27,7 +27,7 @@ public class Memory<TValue> :
|
|||
set => _data[index] = value;
|
||||
}
|
||||
|
||||
public Memory(int size = 4096)
|
||||
public Addreg(int size = 4096)
|
||||
{
|
||||
Size = size;
|
||||
_data = new TValue[size];
|
||||
|
|
@ -53,7 +53,7 @@ public class Memory<TValue> :
|
|||
if (mask < BLOCK_FULL)
|
||||
{
|
||||
int index = 0;
|
||||
while ((mask & (1 << index)) > 0)
|
||||
while ((mask & (1 << index)) > 0)
|
||||
index++;
|
||||
|
||||
return (Address)((long)block * BLOCK_SIZE + index);
|
||||
|
|
@ -8,11 +8,13 @@ public readonly record struct Operation(OpCode OpCode, Value Left, Value Right);
|
|||
|
||||
public delegate Value OperationHandler(Operation operation);
|
||||
|
||||
public static class ValueOperation
|
||||
public class AlgorithmicHandler : IOperationResolver
|
||||
{
|
||||
private static readonly ILogger _logger = LoggerService.Get(nameof(ValueOperation));
|
||||
private readonly ILogger _logger = LoggerService.Get(nameof(AlgorithmicHandler));
|
||||
|
||||
private static readonly Register<OpCode, OperationHandler> _operations;
|
||||
private readonly Register<OpCode, OperationHandler> _operations;
|
||||
|
||||
public bool CanResolve(OpCode opCode) => (opCode & OpCode.F_Operation) > 0;
|
||||
|
||||
public static Value Resolve(Operation operation)
|
||||
{
|
||||
|
|
@ -41,7 +43,7 @@ public static class ValueOperation
|
|||
};
|
||||
}
|
||||
|
||||
static ValueOperation()
|
||||
static AlgorithmicHandler()
|
||||
{
|
||||
_operations = new Register<OpCode, OperationHandler> {
|
||||
{ OpCode.Equal, Equal },
|
||||
|
|
@ -43,6 +43,7 @@ public enum OpCode
|
|||
BitwiseXor = 0x67,
|
||||
BitwiseLeft = 0x68,
|
||||
BitwiseRight = 0x69,
|
||||
|
||||
F_Unary = 0x0a,
|
||||
Not = 0x6a,
|
||||
Negate = 0x6b,
|
||||
|
|
|
|||
|
|
@ -25,6 +25,56 @@ public class Call
|
|||
}
|
||||
}
|
||||
|
||||
public interface IOperationResolver
|
||||
{
|
||||
bool CanResolve(OpCode code);
|
||||
OperationResult Resolve(OpCode code);
|
||||
}
|
||||
|
||||
public struct OperationResult
|
||||
{
|
||||
public bool Success;
|
||||
public Value Value;
|
||||
public RuntimeException? Error;
|
||||
|
||||
public static OperationResult MakeSuccess(Value value)
|
||||
{
|
||||
return new OperationResult { Success = true, Value = value };
|
||||
}
|
||||
|
||||
public static OperationResult MakeError(RuntimeException exception)
|
||||
{
|
||||
return new OperationResult { Success = false, Value = Value.Void, Error = exception };
|
||||
}
|
||||
}
|
||||
|
||||
public class OperationRouter
|
||||
{
|
||||
private readonly List<IOperationResolver> _handlers = [];
|
||||
private readonly Dictionary<OpCode, IOperationResolver> _cache = [];
|
||||
|
||||
public void Register(IOperationResolver resolver)
|
||||
{
|
||||
_handlers.Add(resolver);
|
||||
}
|
||||
|
||||
public OperationResult Route(OpCode opCode)
|
||||
{
|
||||
if (!_cache.TryGetValue(opCode, out IOperationResolver? resolver)) {
|
||||
resolver = _handlers.FirstOrDefault(h => h.CanResolve(opCode));
|
||||
if (resolver == null)
|
||||
return OperationResult.MakeError(
|
||||
new RuntimeException(
|
||||
$"Could not find an IOperationHandler for OpCode {opCode}.",
|
||||
opCode));
|
||||
|
||||
_cache[opCode] = resolver;
|
||||
}
|
||||
|
||||
return resolver.Resolve(opCode);
|
||||
}
|
||||
}
|
||||
|
||||
public class Runner : IDisposable
|
||||
{
|
||||
private readonly ILogger _logger = LoggerService.Get<Runner>();
|
||||
|
|
@ -96,13 +146,13 @@ public class Runner : IDisposable
|
|||
//IO.Console.Write($"{opCode}: \n - {string.Join("\n - ", Stack.ToArray().Subset(0, Stack.Count))}");
|
||||
|
||||
if (TestFlag(opCode, Op.F_Operation)) {
|
||||
Value result = ValueOperation.Resolve(PopOperation(opCode));
|
||||
Value result = AlgorithmicHandler.Resolve(PopOperation(opCode));
|
||||
Push(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (TestFlag(opCode, Op.F_Compare)) {
|
||||
Value result = ValueOperation.Resolve(PopOperation(opCode));
|
||||
Value result = AlgorithmicHandler.Resolve(PopOperation(opCode));
|
||||
Push(result);
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,11 @@
|
|||
namespace Qrakhen.Qamp.Core.Values.Objects;
|
||||
using Qrakhen.Qamp.Core.Collections;
|
||||
|
||||
namespace Qrakhen.Qamp.Core.Values.Objects;
|
||||
|
||||
public static class ObjRegister
|
||||
{
|
||||
private static readonly Addreg<Obj> _register = [];
|
||||
}
|
||||
|
||||
public class Obj(ValueType type) : ITypedValue
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ public readonly struct Ptr
|
|||
{
|
||||
private static readonly PushStack<Obj> _register = new();
|
||||
|
||||
private static readonly Collections.Memory<Obj> _memory = [];
|
||||
private static readonly Collections.Addreg<Obj> _memory = [];
|
||||
|
||||
[Serialized] public readonly Address Address;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue