From c82a01c96833395d36e6d9ef24c12c5a30c757d1 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sun, 24 Jan 2021 12:17:46 -0600 Subject: [PATCH] Minor refactoring, fixed 'or' and 'and' logical operators --- src/cobj.h | 9 ++------- src/cparse.c | 2 ++ src/cvm.c | 8 +++++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/cobj.h b/src/cobj.h index f3d80ca..3cfe3ce 100644 --- a/src/cobj.h +++ b/src/cobj.h @@ -9,8 +9,8 @@ typedef enum { COBJ_TABLE, COBJ_FUNCTION, COBJ_CFUNCTION, - COBJ_ERROR, // internal use + COBJ_ERROR, COBJ_METHOD, COBJ_CLOSURE, COBJ_UPVALUE, @@ -152,12 +152,7 @@ CObjUpval *cosmoO_newUpvalue(CState *state, CValue *val); // grabs the base proto of the CObj* (if CObj is a CObjObject, that is returned) static inline CObjObject *cosmoO_grabProto(CObj *obj) { - CObjObject *object = obj->proto; - - if (obj->type == COBJ_OBJECT) - object = (CObjObject*)obj; - - return object; + return obj->type == COBJ_OBJECT ? (CObjObject*)obj : obj->proto; } bool cosmoO_getRawObject(CState *state, CObjObject *proto, CValue key, CValue *val, CObj *obj); diff --git a/src/cparse.c b/src/cparse.c index dc63485..61f10c9 100644 --- a/src/cparse.c +++ b/src/cparse.c @@ -573,6 +573,7 @@ static void and_(CParseState *pstate, bool canAssign, Precedence prec) { int jump = writeJmp(pstate, OP_EJMP); // conditional jump without popping writePop(pstate, 1); + valuePopped(pstate, 1); expressionPrecedence(pstate, 1, PREC_AND, true); patchJmp(pstate, jump); @@ -584,6 +585,7 @@ static void or_(CParseState *pstate, bool canAssign, Precedence prec) { patchJmp(pstate, elseJump); writePop(pstate, 1); + valuePopped(pstate, 1); expressionPrecedence(pstate, 1, PREC_OR, true); diff --git a/src/cvm.c b/src/cvm.c index 4bebd10..1fbda98 100644 --- a/src/cvm.c +++ b/src/cvm.c @@ -286,7 +286,9 @@ static bool rawCall(CState *state, CObjClosure *closure, int args, int nresults, return false; // push the return values back onto the stack - memmove(state->top, results, sizeof(CValue) * nres); // copies the return values to the top of the stack + for (int i = 0; i < nres; i++) { + state->top[i] = results[i]; + } state->top += nres; // and make sure to move state->top to match // now, if the caller function expected more return values, push nils onto the stack @@ -304,7 +306,7 @@ bool callCValue(CState *state, CValue func, int args, int nresults, int offset) printf("()\n"); #endif - if (GET_TYPE(func) != COSMO_TOBJ) { + if (!IS_OBJ(func)) { cosmoV_error(state, "Cannot call non-function type %s!", cosmoV_typeStr(func)); return false; } @@ -348,7 +350,7 @@ bool callCValue(CState *state, CValue func, int args, int nresults, int offset) break; } default: - cosmoV_error(state, "Cannot call non-function value %s!", cosmoV_typeStr(func)); + cosmoV_error(state, "Cannot call non-function type %s!", cosmoV_typeStr(func)); return false; }