mirror of
https://github.com/CPunch/Cosmo.git
synced 2025-11-06 23:50:19 +00:00
Initial commit
This commit is contained in:
62
src/cvalue.h
Normal file
62
src/cvalue.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef CVALUE_H
|
||||
#define CVALUE_H
|
||||
|
||||
#include "cosmo.h"
|
||||
|
||||
typedef enum {
|
||||
COSMO_TNIL,
|
||||
COSMO_TBOOLEAN,
|
||||
COSMO_TNUMBER,
|
||||
COSMO_TOBJ,
|
||||
COSMO_TUSERDATA
|
||||
} CosmoType;
|
||||
|
||||
typedef double cosmo_Number;
|
||||
|
||||
/*
|
||||
holds primitive cosmo types
|
||||
*/
|
||||
typedef struct CValue {
|
||||
CosmoType type;
|
||||
union {
|
||||
cosmo_Number num;
|
||||
bool b; // boolean
|
||||
void *ptr; // userdata
|
||||
CObj *obj;
|
||||
} val;
|
||||
} CValue;
|
||||
typedef CValue* StkPtr;
|
||||
|
||||
typedef struct CValueArray {
|
||||
size_t capacity;
|
||||
size_t count;
|
||||
CValue *values;
|
||||
} CValueArray;
|
||||
|
||||
COSMO_API void initValArray(CState *state, CValueArray *val, size_t startCapacity);
|
||||
COSMO_API void cleanValArray(CState *state, CValueArray *array); // cleans array
|
||||
COSMO_API void appendValArray(CState *state, CValueArray *array, CValue val);
|
||||
|
||||
COSMO_API void printValue(CValue val);
|
||||
COSMO_API bool cosmoV_equal(CValue valA, CValue valB);
|
||||
COSMO_API CObjString *cosmoV_toString(CState *state, CValue val);
|
||||
|
||||
#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
|
||||
|
||||
// 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
|
||||
Reference in New Issue
Block a user