From e995bb75fbd52ccaa47b6e0c6a0a0eba1c6b38ac Mon Sep 17 00:00:00 2001 From: CPunch Date: Wed, 13 Jan 2021 14:13:55 -0600 Subject: [PATCH] added tonumber() and tostring() to base lib --- src/cbaselib.c | 24 ++++++++++++++++++++++++ src/cobj.c | 11 +++++++++++ src/cobj.h | 4 +++- src/cvalue.c | 17 +++++++++++++++++ src/cvalue.h | 1 + 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index 1ac1a72..46e8d0c 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -57,6 +57,26 @@ int cosmoB_pcall(CState *state, int nargs, CValue *args) { return 2; } +int cosmoB_tonumber(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "tonumber() expected 1 argument, got %d!", nargs); + return 0; + } + + cosmoV_pushNumber(state, cosmoV_toNumber(state, args[0])); + return 1; +} + +int cosmoB_tostring(CState *state, int nargs, CValue *args) { + if (nargs != 1) { + cosmoV_error(state, "tostring() expected 1 argument, got %d!", nargs); + return 0; + } + + cosmoV_pushValue(state, cosmoV_newObj(cosmoV_toString(state, args[0]))); + return 1; +} + int cosmoB_loadstring(CState *state, int nargs, CValue *args) { if (nargs < 1) { cosmoV_error(state, "loadstring() expected 1 argument, got %d!", nargs); @@ -126,6 +146,8 @@ void cosmoB_loadLibrary(CState *state) { "assert", "type", "pcall", + "tonumber", + "tostring" "loadstring" }; @@ -134,6 +156,8 @@ void cosmoB_loadLibrary(CState *state) { cosmoB_assert, cosmoB_type, cosmoB_pcall, + cosmoB_tonumber, + cosmoB_tostring, cosmoB_loadstring }; diff --git a/src/cobj.c b/src/cobj.c index 76099a4..9d17600 100644 --- a/src/cobj.c +++ b/src/cobj.c @@ -461,6 +461,17 @@ CObjString *cosmoO_toString(CState *state, CObj *obj) { } } +cosmo_Number cosmoO_toNumber(CState *state, CObj *obj) { + switch (obj->type) { + case COBJ_STRING: { + CObjString *str = (CObjString*)obj; + return strtod(str->str, NULL); + } + default: // maybe in the future throw an error? + return 0; + } +} + void printObject(CObj *o) { switch (o->type) { case COBJ_STRING: { diff --git a/src/cobj.h b/src/cobj.h index 2790797..992d14f 100644 --- a/src/cobj.h +++ b/src/cobj.h @@ -146,7 +146,6 @@ CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func); CObjError *cosmoO_newError(CState *state, CValue err); CObjMethod *cosmoO_newMethod(CState *state, CValue func, CObj *obj); CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func); -CObjString *cosmoO_toString(CState *state, CObj *val); CObjUpval *cosmoO_newUpvalue(CState *state, CValue *val); // grabs the base proto of the CObj* (if CObj is a CObjObject, that is returned) @@ -194,4 +193,7 @@ const char *cosmoO_typeStr(CObj* obj); #define cosmoO_readCString(x) ((CObjString*)x)->str +CObjString *cosmoO_toString(CState *state, CObj *obj); +cosmo_Number cosmoO_toNumber(CState *state, CObj *obj); + #endif diff --git a/src/cvalue.c b/src/cvalue.c index 46e8e84..5b89a2f 100644 --- a/src/cvalue.c +++ b/src/cvalue.c @@ -55,6 +55,23 @@ CObjString *cosmoV_toString(CState *state, CValue val) { } } +cosmo_Number cosmoV_toNumber(CState *state, CValue val) { + switch(GET_TYPE(val)) { + case COSMO_TNUMBER: { + return cosmoV_readNumber(val); + } + case COSMO_TBOOLEAN: { + return cosmoV_readBoolean(val) ? 1 : 0; + } + case COSMO_TOBJ: { + return cosmoO_toNumber(state, cosmoV_readObj(val)); + } + case COSMO_TNIL: // fall through + default: + return 0; + } +} + const char *cosmoV_typeStr(CValue val) { switch (GET_TYPE(val)) { case COSMO_TNIL: return ""; diff --git a/src/cvalue.h b/src/cvalue.h index 8ccd4f1..b1bb3fa 100644 --- a/src/cvalue.h +++ b/src/cvalue.h @@ -114,6 +114,7 @@ void appendValArray(CState *state, CValueArray *array, CValue val); void printValue(CValue val); COSMO_API bool cosmoV_equal(CValue valA, CValue valB); COSMO_API CObjString *cosmoV_toString(CState *state, CValue val); +COSMO_API cosmo_Number cosmoV_toNumber(CState *state, CValue val); COSMO_API const char *cosmoV_typeStr(CValue val); // return constant char array for corresponding type #endif