Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopodl committed Mar 5, 2024
1 parent 78d6da0 commit 59390c9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
37 changes: 2 additions & 35 deletions argon/vm/areval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())); \
} \
Expand Down
37 changes: 35 additions & 2 deletions argon/vm/datatype/arobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -739,15 +772,15 @@ 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)) {
Release(output);
return nullptr;
}

ListRemove(head_list, (ArSSize)0);
ListRemove(head_list, (ArSSize) 0);
bases_idx = 0;
}

Expand Down
4 changes: 4 additions & 0 deletions argon/vm/datatype/arobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 59390c9

Please sign in to comment.