Cosmo/src/cvalue.h

61 lines
1.6 KiB
C
Raw Normal View History

2020-10-28 05:16:30 +00:00
#ifndef CVALUE_H
#define CVALUE_H
#include "cosmo.h"
typedef enum {
COSMO_TNIL,
COSMO_TBOOLEAN,
COSMO_TNUMBER,
COSMO_TOBJ
2020-10-28 05:16:30 +00:00
} CosmoType;
typedef double cosmo_Number;
/*
holds primitive cosmo types
*/
typedef struct CValue {
CosmoType type;
union {
cosmo_Number num;
bool b; // boolean
CObj *obj;
} val;
} CValue;
typedef CValue* StkPtr;
typedef struct CValueArray {
size_t capacity;
size_t count;
CValue *values;
} CValueArray;
2020-11-13 02:06:38 +00:00
void initValArray(CState *state, CValueArray *val, size_t startCapacity);
void cleanValArray(CState *state, CValueArray *array); // cleans array
void appendValArray(CState *state, CValueArray *array, CValue val);
2020-10-28 05:16:30 +00:00
2020-11-13 02:06:38 +00:00
void printValue(CValue val);
2020-10-28 05:16:30 +00:00
COSMO_API bool cosmoV_equal(CValue valA, CValue valB);
COSMO_API CObjString *cosmoV_toString(CState *state, CValue val);
2020-11-30 18:50:55 +00:00
COSMO_API const char *cosmoV_typeStr(CValue val); // return constant char array for corresponding type
2020-10-28 05:16:30 +00:00
2020-11-17 09:36:56 +00:00
#define IS_NUMBER(x) (x.type == COSMO_TNUMBER)
#define IS_BOOLEAN(x) (x.type == COSMO_TBOOLEAN)
#define IS_NIL(x) (x.type == COSMO_TNIL)
#define IS_OBJ(x) (x.type == COSMO_TOBJ)
2020-10-28 05:16:30 +00:00
// create CValues
#define cosmoV_newNumber(x) ((CValue){COSMO_TNUMBER, {.num = x}})
#define cosmoV_newBoolean(x) ((CValue){COSMO_TBOOLEAN, {.b = x}})
#define cosmoV_newObj(x) ((CValue){COSMO_TOBJ, {.obj = (CObj*)x}})
#define cosmoV_newNil() ((CValue){COSMO_TNIL, {.num = 0}})
// read CValues
#define cosmoV_readNumber(x) ((cosmo_Number)x.val.num)
#define cosmoV_readBoolean(x) ((bool)x.val.b)
#define cosmoV_readObj(x) ((CObj*)x.val.obj)
#endif