Minor refactoring, fixed 'or' and 'and' logical operators

This commit is contained in:
CPunch 2021-01-24 12:17:46 -06:00
parent e699005a74
commit c82a01c968
3 changed files with 9 additions and 10 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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;
}