Cosmo/src/cvalue.c

124 lines
3.0 KiB
C
Raw Normal View History

2020-10-28 05:16:30 +00:00
#include "cvalue.h"
2023-02-09 18:32:48 +00:00
#include "cmem.h"
2020-10-28 05:16:30 +00:00
#include "cobj.h"
2023-02-09 18:32:48 +00:00
#include "cosmo.h"
2020-10-28 05:16:30 +00:00
2023-02-09 18:32:48 +00:00
void initValArray(CState *state, CValueArray *val, size_t startCapacity)
{
2020-10-28 05:16:30 +00:00
val->count = 0;
val->capacity = startCapacity;
val->values = NULL;
}
2023-02-09 18:32:48 +00:00
void cleanValArray(CState *state, CValueArray *array)
{
2023-08-30 04:21:52 +00:00
cosmoM_freeArray(state, CValue, array->values, array->capacity);
2020-10-28 05:16:30 +00:00
}
2023-02-09 18:32:48 +00:00
void appendValArray(CState *state, CValueArray *array, CValue val)
{
2023-08-30 04:21:52 +00:00
cosmoM_growArray(state, CValue, array->values, array->count, array->capacity);
2020-10-28 05:16:30 +00:00
array->values[array->count++] = val;
}
2023-02-09 18:32:48 +00:00
bool cosmoV_equal(CState *state, CValue valA, CValue valB)
{
if (GET_TYPE(valA) != GET_TYPE(valB)) // are they the same type?
2020-10-28 05:16:30 +00:00
return false;
// compare
switch (GET_TYPE(valA)) {
2023-02-09 18:32:48 +00:00
case COSMO_TBOOLEAN:
return cosmoV_readBoolean(valA) == cosmoV_readBoolean(valB);
case COSMO_TNUMBER:
return cosmoV_readNumber(valA) == cosmoV_readNumber(valB);
case COSMO_TREF:
return cosmoO_equal(state, cosmoV_readRef(valA), cosmoV_readRef(valB));
case COSMO_TNIL:
return true;
default:
return false;
2020-10-28 05:16:30 +00:00
}
}
2023-02-09 18:32:48 +00:00
CObjString *cosmoV_toString(CState *state, CValue val)
{
switch (GET_TYPE(val)) {
2023-02-09 18:32:48 +00:00
case COSMO_TNUMBER: {
char buf[32];
int size = snprintf((char *)&buf, 32, "%.14g", cosmoV_readNumber(val));
return cosmoO_copyString(state, (char *)&buf, size);
}
case COSMO_TBOOLEAN: {
return cosmoV_readBoolean(val) ? cosmoO_copyString(state, "true", 4)
: cosmoO_copyString(state, "false", 5);
}
case COSMO_TREF: {
return cosmoO_toString(state, cosmoV_readRef(val));
}
case COSMO_TNIL: {
return cosmoO_copyString(state, "nil", 3);
}
default:
return cosmoO_copyString(state, "<unkn val>", 10);
2020-10-28 05:16:30 +00:00
}
}
2023-02-09 18:32:48 +00:00
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_TREF: {
return cosmoO_toNumber(state, cosmoV_readRef(val));
}
case COSMO_TNIL: // fall through
default:
return 0;
}
}
2023-02-09 18:32:48 +00:00
const char *cosmoV_typeStr(CValue val)
{
switch (GET_TYPE(val)) {
2023-02-09 18:32:48 +00:00
case COSMO_TNIL:
return "<nil>";
case COSMO_TBOOLEAN:
return "<bool>";
case COSMO_TNUMBER:
return "<number>";
case COSMO_TREF:
return cosmoO_typeStr(cosmoV_readRef(val));
default:
return "<unkn val>";
}
}
2023-02-09 18:32:48 +00:00
void printValue(CValue val)
{
switch (GET_TYPE(val)) {
2023-02-09 18:32:48 +00:00
case COSMO_TNUMBER:
printf("%g", cosmoV_readNumber(val));
break;
case COSMO_TBOOLEAN:
printf(cosmoV_readBoolean(val) ? "true" : "false");
break;
case COSMO_TREF: {
printObject(cosmoV_readRef(val));
break;
}
case COSMO_TNIL:
printf("nil");
break;
default:
printf("<unkn val>");
2020-10-28 05:16:30 +00:00
}
2021-01-02 05:06:24 +00:00
}