2020-12-04 06:04:14 +00:00
|
|
|
#include "cosmo.h"
|
2020-10-28 05:16:30 +00:00
|
|
|
#include "cmem.h"
|
|
|
|
#include "cvalue.h"
|
|
|
|
#include "cobj.h"
|
|
|
|
|
|
|
|
void initValArray(CState *state, CValueArray *val, size_t startCapacity) {
|
|
|
|
val->count = 0;
|
|
|
|
val->capacity = startCapacity;
|
|
|
|
val->values = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanValArray(CState *state, CValueArray *array) {
|
|
|
|
cosmoM_freearray(state, CValue, array->values, array->capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
void appendValArray(CState *state, CValueArray *array, CValue val) {
|
|
|
|
cosmoM_growarray(state, CValue, array->values, array->count, array->capacity);
|
|
|
|
|
|
|
|
array->values[array->count++] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cosmoV_equal(CValue valA, CValue valB) {
|
2020-12-04 06:04:14 +00:00
|
|
|
if (GET_TYPE(valA) != GET_TYPE(valB)) // are they the same type?
|
2020-10-28 05:16:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// compare
|
2020-12-04 06:04:14 +00:00
|
|
|
switch (GET_TYPE(valA)) {
|
|
|
|
case COSMO_TBOOLEAN: return cosmoV_readBoolean(valA) == cosmoV_readBoolean(valB);
|
|
|
|
case COSMO_TNUMBER: return cosmoV_readNumber(valA) == cosmoV_readNumber(valB);
|
|
|
|
case COSMO_TOBJ: return cosmoO_equal(cosmoV_readObj(valA), cosmoV_readObj(valB));
|
2020-10-28 05:16:30 +00:00
|
|
|
case COSMO_TNIL: return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:32:04 +00:00
|
|
|
CObjString *cosmoV_toString(CState *state, CValue val) {
|
2020-12-04 06:04:14 +00:00
|
|
|
switch (GET_TYPE(val)) {
|
2020-10-28 05:16:30 +00:00
|
|
|
case COSMO_TNUMBER: {
|
|
|
|
char buf[32];
|
2020-12-04 06:04:14 +00:00
|
|
|
int size = snprintf((char*)&buf, 32, "%.14g", cosmoV_readNumber(val));
|
2020-10-28 05:16:30 +00:00
|
|
|
return cosmoO_copyString(state, (char*)&buf, size);
|
|
|
|
}
|
|
|
|
case COSMO_TBOOLEAN: {
|
2020-12-04 06:04:14 +00:00
|
|
|
return cosmoV_readBoolean(val) ? cosmoO_copyString(state, "true", 4) : cosmoO_copyString(state, "false", 5);
|
2020-10-28 05:16:30 +00:00
|
|
|
}
|
|
|
|
case COSMO_TOBJ: {
|
2020-12-04 06:04:14 +00:00
|
|
|
return cosmoO_toString(state, cosmoV_readObj(val));
|
2020-10-28 05:16:30 +00:00
|
|
|
}
|
2020-11-10 01:44:12 +00:00
|
|
|
case COSMO_TNIL: {
|
|
|
|
return cosmoO_copyString(state, "nil", 3);
|
|
|
|
}
|
2020-10-28 05:16:30 +00:00
|
|
|
default:
|
2020-11-10 01:44:12 +00:00
|
|
|
return cosmoO_copyString(state, "<unkn val>", 10);
|
2020-10-28 05:16:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:32:04 +00:00
|
|
|
const char *cosmoV_typeStr(CValue val) {
|
2020-12-04 06:04:14 +00:00
|
|
|
switch (GET_TYPE(val)) {
|
2020-11-30 18:32:04 +00:00
|
|
|
case COSMO_TNIL: return "<nil>";
|
|
|
|
case COSMO_TBOOLEAN: return "<bool>";
|
|
|
|
case COSMO_TNUMBER: return "<number>";
|
2020-12-04 06:04:14 +00:00
|
|
|
case COSMO_TOBJ: return cosmoO_typeStr(cosmoV_readObj(val));
|
2020-11-30 18:32:04 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return "<unkn val>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 05:16:30 +00:00
|
|
|
void printValue(CValue val) {
|
2020-12-04 06:04:14 +00:00
|
|
|
switch (GET_TYPE(val)) {
|
2020-10-28 05:16:30 +00:00
|
|
|
case COSMO_TNUMBER:
|
2020-12-04 06:04:14 +00:00
|
|
|
printf("%g", cosmoV_readNumber(val));
|
2020-10-28 05:16:30 +00:00
|
|
|
break;
|
|
|
|
case COSMO_TBOOLEAN:
|
|
|
|
printf(cosmoV_readBoolean(val) ? "true" : "false");
|
|
|
|
break;
|
|
|
|
case COSMO_TOBJ: {
|
2020-12-04 06:04:14 +00:00
|
|
|
printObject(cosmoV_readObj(val));
|
2020-10-28 05:16:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case COSMO_TNIL:
|
|
|
|
printf("nil");
|
|
|
|
break;
|
|
|
|
default:
|
2020-11-10 01:44:12 +00:00
|
|
|
printf("<unkn val>");
|
2020-10-28 05:16:30 +00:00
|
|
|
}
|
|
|
|
}
|