object.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 "value.h"
  13. #define OBJ_TYPE(value) (AS_OBJ(value)->type)
  14. #define IS_STRING(value) isObjType(value, OBJ_STRING)
  15. #define AS_STRING(value) ((ObjString *) AS_OBJ(value))
  16. #define AS_CSTRING(value) (((ObjString *) AS_OBJ(value))->chars)
  17. typedef enum {
  18. OBJ_STRING,
  19. } ObjType;
  20. struct Obj {
  21. ObjType type;
  22. struct Obj *next;
  23. };
  24. struct ObjString {
  25. struct Obj obj;
  26. int length;
  27. char *chars;
  28. };
  29. ObjString *takeString(char *chars, int length);
  30. ObjString *copyString(const char *chars, int length);
  31. void printObject(Value value);
  32. static inline bool isObjType(Value value, ObjType type) {
  33. return IS_OBJ(value) && AS_OBJ(value)->type == type;
  34. }
  35. #endif//CLOX_OBJECT_H