From 59390c95e417fd30c61f852653e3dfb29bafe7bc Mon Sep 17 00:00:00 2001 From: jacopodl Date: Tue, 5 Mar 2024 17:13:05 +0100 Subject: [PATCH] refactor --- argon/vm/areval.cpp | 37 ++-------------------------------- argon/vm/datatype/arobject.cpp | 37 ++++++++++++++++++++++++++++++++-- argon/vm/datatype/arobject.h | 4 ++++ 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/argon/vm/areval.cpp b/argon/vm/areval.cpp index d24321e2..1656c19a 100644 --- a/argon/vm/areval.cpp +++ b/argon/vm/areval.cpp @@ -24,39 +24,6 @@ using namespace argon::vm; using namespace argon::vm::datatype; -ArObject *Binary(ArObject *l, ArObject *r, int offset) { - BinaryOp lop = nullptr; - BinaryOp rop = nullptr; - ArObject *result = nullptr; - - if (AR_GET_TYPE(l)->ops != nullptr) - lop = AR_GET_BINARY_OP(AR_GET_TYPE(l)->ops, offset); - - if (AR_GET_TYPE(r)->ops != nullptr) - rop = AR_GET_BINARY_OP(AR_GET_TYPE(r)->ops, offset); - - if (lop != nullptr) - result = lop(l, r); - - if (rop != nullptr && result == nullptr && !IsPanickingFrame()) - result = rop(l, r); - - return result; -} - -ArObject *BinaryOriented(ArObject *l, ArObject *r, int offset) { - ArObject *result = nullptr; - BinaryOp lop = nullptr; - - if (AR_GET_TYPE(l)->ops != nullptr) - lop = AR_GET_BINARY_OP(AR_GET_TYPE(l)->ops, offset); - - if (lop != nullptr) - result = lop(l, r); - - return result; -} - ArObject *GetCallableFromType(ArObject **type) { String *key; ArObject *ret; @@ -597,14 +564,14 @@ ArObject *argon::vm::Eval(Fiber *fiber) { } while(0) #define BINARY_OP4(first, second, op, opchar) \ - if ((ret = BinaryOriented(first, second, offsetof(OpSlots, op))) == nullptr) { \ + if ((ret = ExecBinaryOpOriented(first, second, offsetof(OpSlots, op))) == nullptr) { \ if (!IsPanickingFrame()) { \ ErrorFormat(kRuntimeError[0], kRuntimeError[2], #opchar, AR_TYPE_NAME(first), AR_TYPE_NAME(second)); \ } \ break; } \ #define BINARY_OP(op, opchar) \ - if ((ret = Binary(PEEK1(), TOP(), offsetof(OpSlots, op))) == nullptr) { \ + if ((ret = ExecBinaryOp(PEEK1(), TOP(), offsetof(OpSlots, op))) == nullptr) { \ if (!IsPanickingFrame()) { \ ErrorFormat(kRuntimeError[0], kRuntimeError[2], #opchar, AR_TYPE_NAME(PEEK1()), AR_TYPE_NAME(TOP())); \ } \ diff --git a/argon/vm/datatype/arobject.cpp b/argon/vm/datatype/arobject.cpp index 20a91d94..40e167a2 100644 --- a/argon/vm/datatype/arobject.cpp +++ b/argon/vm/datatype/arobject.cpp @@ -396,6 +396,39 @@ ArObject *argon::vm::datatype::Compare(const ArObject *self, const ArObject *oth return result; } +ArObject *argon::vm::datatype::ExecBinaryOp(ArObject *left, ArObject *right, int offset) { + BinaryOp lop = nullptr; + BinaryOp rop = nullptr; + ArObject *result = nullptr; + + if (AR_GET_TYPE(left)->ops != nullptr) + lop = AR_GET_BINARY_OP(AR_GET_TYPE(left)->ops, offset); + + if (AR_GET_TYPE(right)->ops != nullptr) + rop = AR_GET_BINARY_OP(AR_GET_TYPE(right)->ops, offset); + + if (lop != nullptr) + result = lop(left, right); + + if (rop != nullptr && result == nullptr && !IsPanickingFrame()) + result = rop(left, right); + + return result; +} + +ArObject *argon::vm::datatype::ExecBinaryOpOriented(ArObject *left, ArObject *right, int offset) { + ArObject *result = nullptr; + BinaryOp lop = nullptr; + + if (AR_GET_TYPE(left)->ops != nullptr) + lop = AR_GET_BINARY_OP(AR_GET_TYPE(left)->ops, offset); + + if (lop != nullptr) + result = lop(left, right); + + return result; +} + ArObject *HashWrapper(ArObject *func, ArObject *self, ArObject **args, ArObject *kwargs, ArSize argc) { return (ArObject *) UIntNew(AR_GET_TYPE(self)->hash(self)); } @@ -739,7 +772,7 @@ Tuple *CalculateMRO(const List *bases) { auto tail_list = ((List *) bases->objects[i]); if (bases_idx != i && head == tail_list->objects[0]) - ListRemove(tail_list, (ArSSize)0); + ListRemove(tail_list, (ArSSize) 0); } if (!ListAppend(output, head)) { @@ -747,7 +780,7 @@ Tuple *CalculateMRO(const List *bases) { return nullptr; } - ListRemove(head_list, (ArSSize)0); + ListRemove(head_list, (ArSSize) 0); bases_idx = 0; } diff --git a/argon/vm/datatype/arobject.h b/argon/vm/datatype/arobject.h index 12a3c13e..a38b8758 100644 --- a/argon/vm/datatype/arobject.h +++ b/argon/vm/datatype/arobject.h @@ -24,6 +24,10 @@ namespace argon::vm::datatype { ArObject *Compare(const ArObject *self, const ArObject *other, CompareMode mode); + ArObject *ExecBinaryOp(ArObject *left, ArObject *right, int offset); + + ArObject *ExecBinaryOpOriented(ArObject *left, ArObject *right, int offset); + ArObject *IteratorGet(ArObject *object, bool reversed); ArObject *IteratorNext(ArObject *iterator);