object.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. ******************************************************************************
  3. * @file : object.h
  4. * @author : simon
  5. * @brief : None
  6. * @attention : None
  7. * @date : 2023/8/23
  8. ******************************************************************************
  9. */
  10. #ifndef CLOX_OBJECT_H
  11. #define CLOX_OBJECT_H
  12. #include "chunk.h"
  13. #include "common.h"
  14. #include "value.h"
  15. #define OBJ_TYPE(value) (AS_OBJ(value)->type)
  16. #define IS_FUNCTION(value) isObjType(value, OBJ_FUNCTION);
  17. #define IS_STRING(value) isObjType(value, OBJ_STRING)
  18. #define AS_FUNCTION(value) ((ObjFunction *) AS_OBJ(value))
  19. #define AS_STRING(value) ((ObjString *) AS_OBJ(value))
  20. #define AS_CSTRING(value) (((ObjString *) AS_OBJ(value))->chars)
  21. typedef enum {
  22. OBJ_STRING,
  23. OBJ_FUNCTION,
  24. } ObjType;
  25. struct Obj {
  26. ObjType type;
  27. struct Obj *next;
  28. };
  29. typedef struct {
  30. struct Obj obj;
  31. int arity;// stores the number of parameters the function expects
  32. Chunk chunk;
  33. ObjString *name;
  34. } ObjFunction;
  35. struct ObjString {
  36. struct Obj obj;
  37. int length;
  38. char *chars;
  39. uint32_t hash;// 缓存 hash 值
  40. };
  41. ObjFunction *newFunction();
  42. ObjString *takeString(char *chars, int length);
  43. ObjString *copyString(const char *chars, int length);
  44. void printObject(Value value);
  45. static inline bool isObjType(Value value, ObjType type) {
  46. return IS_OBJ(value) && AS_OBJ(value)->type == type;
  47. }
  48. #endif//CLOX_OBJECT_H