clox/object.h

61 lines
1.2 KiB
C
Raw Permalink Normal View History

2019-06-02 14:58:15 -05:00
#ifndef clox_object_h
#define clox_object_h
#include "common.h"
#include "table.h"
#include "value.h"
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
#define IS_STRING(value) isObjType(value, OBJ_STRING)
#define IS_ARRAY(value) isObjType(value, OBJ_ARRAY)
#define AS_STRING(value) ((ObjString*)AS_OBJ(value))
#define AS_CSTRING(value) (((ObjString*)AS_OBJ(value))->chars)
#define AS_ARRAY(value) ((ObjArray*)AS_OBJ(value))
#define AS_VARRAY(value) (((ObjArray*)AS_OBJ(value))->array)
#define AS_HASH(value) (((ObjHash*)AS_OBJ(value))->hashTable)
typedef enum {
OBJ_STRING,
OBJ_ARRAY,
OBJ_HASH
} ObjType;
struct sObj {
ObjType type;
struct sObj* next;
};
struct sObjString {
Obj obj;
int length;
char* chars;
uint32_t hash;
};
struct sObjArray {
Obj obj;
ValueArray* array;
};
struct sObjHash {
Obj obj;
Table* hashTable;
};
ObjArray* takeArray(ValueArray* valArray);
ObjHash* takeHash(Table* hash);
ObjString* takeString(char* chars, int length);
ObjString* copyString(const char* chars, int length);
void printObject(Value value);
static inline bool isObjType(Value value, ObjType type) {
return IS_OBJ(value) && AS_OBJ(value)->type == type;
}
#endif