2021-09-06 02:24:56 -07:00
|
|
|
#ifndef OBJECT_H
|
|
|
|
#define OBJECT_H
|
|
|
|
|
2022-01-21 21:24:26 -08:00
|
|
|
#include "chunk.h"
|
2021-09-06 02:24:56 -07:00
|
|
|
#include "common.h"
|
|
|
|
#include "value.h"
|
|
|
|
|
|
|
|
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
|
|
|
|
|
2022-01-21 21:24:26 -08:00
|
|
|
#define IS_FUNCTION(value) isObjType(value, OBJ_FUNCTION)
|
2021-09-06 02:24:56 -07:00
|
|
|
#define IS_STRING(value) isObjType(value, OBJ_STRING)
|
|
|
|
|
2022-01-21 21:24:26 -08:00
|
|
|
#define AS_FUNCTION(value) ((ObjFunction *)AS_OBJ(value))
|
2021-09-06 02:24:56 -07:00
|
|
|
#define AS_STRING(value) ((ObjString *)AS_OBJ(value))
|
|
|
|
#define AS_CSTRING(value) (((ObjString *)AS_OBJ(value))->chars)
|
|
|
|
|
|
|
|
typedef enum {
|
2022-01-21 21:24:26 -08:00
|
|
|
OBJ_FUNCTION,
|
2021-09-06 02:24:56 -07:00
|
|
|
OBJ_STRING,
|
|
|
|
} ObjType;
|
|
|
|
|
|
|
|
struct Obj {
|
|
|
|
ObjType type;
|
2021-09-09 22:57:03 -07:00
|
|
|
struct Obj *next;
|
2021-09-06 02:24:56 -07:00
|
|
|
};
|
|
|
|
|
2022-01-21 21:24:26 -08:00
|
|
|
typedef struct {
|
|
|
|
Obj obj;
|
|
|
|
int arity;
|
|
|
|
Chunk chunk;
|
|
|
|
ObjString *name;
|
|
|
|
} ObjFunction;
|
|
|
|
|
2021-09-06 02:24:56 -07:00
|
|
|
struct ObjString {
|
|
|
|
Obj obj;
|
|
|
|
int length;
|
|
|
|
char *chars;
|
2021-09-18 11:10:11 -07:00
|
|
|
uint32_t hash;
|
2021-09-06 02:24:56 -07:00
|
|
|
};
|
|
|
|
|
2022-01-21 21:24:26 -08:00
|
|
|
ObjFunction *newFunction();
|
2021-09-07 22:54:12 -07:00
|
|
|
ObjString *takeString(char *chars, int length);
|
2021-09-07 22:34:21 -07:00
|
|
|
ObjString *copyString(const char *chars, int length);
|
2021-09-07 22:54:12 -07:00
|
|
|
void printObject(Value value);
|
2021-09-07 22:34:21 -07:00
|
|
|
|
2021-09-06 02:24:56 -07:00
|
|
|
static inline bool isObjType(Value value, ObjType type) {
|
|
|
|
return IS_OBJ(value) && AS_OBJ(value)->type == type;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|