mirror of
https://github.com/CPunch/Cosmo.git
synced 2025-07-13 06:10:07 +00:00
Added tostring and partially added tonumber
This commit is contained in:
parent
8cd0112c48
commit
5d25f84699
@ -75,6 +75,38 @@ int cosmoB_loadstring(CState *state, int nargs, CValue *args) {
|
|||||||
return 2; // <boolean>, <closure> or <error>
|
return 2; // <boolean>, <closure> or <error>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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_pushString(state, cosmoV_toString(state, args[0])->str);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cosmoB_tonumber(CState *state, int nargs, CValue *args) {
|
||||||
|
if (nargs > 1) {
|
||||||
|
cosmoV_error(state, "tonumber() expected 1 argument, got %d", nargs);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
CValue arg = args[0];
|
||||||
|
switch(GET_TYPE(arg)) {
|
||||||
|
case COSMO_TNUMBER:
|
||||||
|
cosmoV_pushNumber(state, cosmoV_readNumber(arg));
|
||||||
|
case COSMO_TBOOLEAN:
|
||||||
|
cosmoV_pushNumber(state, cosmoV_readBoolean(arg) ? 1 : 0);
|
||||||
|
case COSMO_TOBJ:
|
||||||
|
cosmoV_pushNumber(state, 0); // pending __tonumber or similar
|
||||||
|
case COSMO_TNIL:
|
||||||
|
cosmoV_pushNumber(state, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// ================================================================ [STRING.*] ================================================================
|
// ================================================================ [STRING.*] ================================================================
|
||||||
|
|
||||||
// string.sub
|
// string.sub
|
||||||
@ -126,7 +158,9 @@ void cosmoB_loadLibrary(CState *state) {
|
|||||||
"assert",
|
"assert",
|
||||||
"type",
|
"type",
|
||||||
"pcall",
|
"pcall",
|
||||||
"loadstring"
|
"loadstring",
|
||||||
|
"tostring",
|
||||||
|
"tonumber"
|
||||||
};
|
};
|
||||||
|
|
||||||
CosmoCFunction baseLib[] = {
|
CosmoCFunction baseLib[] = {
|
||||||
@ -134,7 +168,9 @@ void cosmoB_loadLibrary(CState *state) {
|
|||||||
cosmoB_assert,
|
cosmoB_assert,
|
||||||
cosmoB_type,
|
cosmoB_type,
|
||||||
cosmoB_pcall,
|
cosmoB_pcall,
|
||||||
cosmoB_loadstring
|
cosmoB_loadstring,
|
||||||
|
cosmoB_tostring,
|
||||||
|
cosmoB_tonumber
|
||||||
};
|
};
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
@ -9,6 +9,8 @@ COSMO_API int cosmoB_print(CState *state, int nargs, CValue *args);
|
|||||||
COSMO_API int cosmoB_assert(CState *state, int nargs, CValue *args);
|
COSMO_API int cosmoB_assert(CState *state, int nargs, CValue *args);
|
||||||
COSMO_API int cosmoB_type(CState *state, int nargs, CValue *args);
|
COSMO_API int cosmoB_type(CState *state, int nargs, CValue *args);
|
||||||
COSMO_API int cosmoB_pcall(CState *state, int nargs, CValue *args);
|
COSMO_API int cosmoB_pcall(CState *state, int nargs, CValue *args);
|
||||||
|
COSMO_API int cosmoB_tostring(CState *state, int nargs, CValue *args);
|
||||||
|
COSMO_API int cosmoB_tonumber(CState *state, int nargs, CValue *args);
|
||||||
|
|
||||||
#define cosmoV_typeError(state, name, expectedTypes, formatStr, ...) \
|
#define cosmoV_typeError(state, name, expectedTypes, formatStr, ...) \
|
||||||
cosmoV_error(state, name " expected (" expectedTypes "), got (" formatStr ")!", __VA_ARGS__);
|
cosmoV_error(state, name " expected (" expectedTypes "), got (" formatStr ")!", __VA_ARGS__);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user