added tonumber() and tostring() to base lib

This commit is contained in:
CPunch 2021-01-13 14:13:55 -06:00
parent 8cd0112c48
commit e995bb75fb
5 changed files with 56 additions and 1 deletions

View File

@ -57,6 +57,26 @@ int cosmoB_pcall(CState *state, int nargs, CValue *args) {
return 2; 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) { int cosmoB_loadstring(CState *state, int nargs, CValue *args) {
if (nargs < 1) { if (nargs < 1) {
cosmoV_error(state, "loadstring() expected 1 argument, got %d!", nargs); cosmoV_error(state, "loadstring() expected 1 argument, got %d!", nargs);
@ -126,6 +146,8 @@ void cosmoB_loadLibrary(CState *state) {
"assert", "assert",
"type", "type",
"pcall", "pcall",
"tonumber",
"tostring"
"loadstring" "loadstring"
}; };
@ -134,6 +156,8 @@ void cosmoB_loadLibrary(CState *state) {
cosmoB_assert, cosmoB_assert,
cosmoB_type, cosmoB_type,
cosmoB_pcall, cosmoB_pcall,
cosmoB_tonumber,
cosmoB_tostring,
cosmoB_loadstring cosmoB_loadstring
}; };

View File

@ -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) { void printObject(CObj *o) {
switch (o->type) { switch (o->type) {
case COBJ_STRING: { case COBJ_STRING: {

View File

@ -146,7 +146,6 @@ CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func);
CObjError *cosmoO_newError(CState *state, CValue err); CObjError *cosmoO_newError(CState *state, CValue err);
CObjMethod *cosmoO_newMethod(CState *state, CValue func, CObj *obj); CObjMethod *cosmoO_newMethod(CState *state, CValue func, CObj *obj);
CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func); CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func);
CObjString *cosmoO_toString(CState *state, CObj *val);
CObjUpval *cosmoO_newUpvalue(CState *state, CValue *val); CObjUpval *cosmoO_newUpvalue(CState *state, CValue *val);
// grabs the base proto of the CObj* (if CObj is a CObjObject, that is returned) // 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 #define cosmoO_readCString(x) ((CObjString*)x)->str
CObjString *cosmoO_toString(CState *state, CObj *obj);
cosmo_Number cosmoO_toNumber(CState *state, CObj *obj);
#endif #endif

View File

@ -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) { const char *cosmoV_typeStr(CValue val) {
switch (GET_TYPE(val)) { switch (GET_TYPE(val)) {
case COSMO_TNIL: return "<nil>"; case COSMO_TNIL: return "<nil>";

View File

@ -114,6 +114,7 @@ void appendValArray(CState *state, CValueArray *array, CValue val);
void printValue(CValue val); void printValue(CValue val);
COSMO_API bool cosmoV_equal(CValue valA, CValue valB); COSMO_API bool cosmoV_equal(CValue valA, CValue valB);
COSMO_API CObjString *cosmoV_toString(CState *state, CValue val); 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 COSMO_API const char *cosmoV_typeStr(CValue val); // return constant char array for corresponding type
#endif #endif