common.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // Created by 李晓明 on 2023/6/14.
  3. //
  4. #ifndef CLOX__COMMON_H_
  5. #define CLOX__COMMON_H_
  6. #include <stdbool.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define DEBUG_PRINT_CODE
  13. #define DEBUG_TRACE_EXECUTION
  14. #define DEBUG_STACK_INFO
  15. //#define DEBUG_STRESS_GC
  16. #define DEBUG_LOG_GC
  17. #define PRINT_METADATA
  18. #define UINT8_COUNT (UINT8_MAX + 1)
  19. #ifdef PRINT_METADATA
  20. #define PRINTLNF(format, ...) printf("("__FILE__ \
  21. ":%d) %s: " format "\n", \
  22. __LINE__, __FUNCTION__, ##__VA_ARGS__)
  23. #else
  24. #define PRINTLNF(format, ...) printf(format "\n", ##__VA_ARGS__)
  25. #endif
  26. #define PRINT_CHAR(char_value) PRINTLNF(#char_value ": %c", char_value)
  27. #define PRINT_WCHAR(char_value) PRINTLNF(#char_value ": %lc", char_value)
  28. #define PRINT_INT(int_value) PRINTLNF(#int_value " : %d", int_value)
  29. #define PRINT_LONG(long_value) PRINTLNF(#long_value ": %ld", long_value)
  30. #define PRINT_LLONG(long_value) PRINTLNF(#long_value ": %lld", long_value)
  31. #define PRINT_BINARY(int_value) PrintBinary((unsigned int) int_value);
  32. #define PRINT_HEX(int_value) PRINTLNF(#int_value ": %#x", int_value)
  33. #define PRINT_BOOL(bool_value) PRINTLNF(#bool_value ": %s", bool_value ? "true" : "false")
  34. #define PRINT_DOUBLE(double_value) PRINTLNF(#double_value ": %g", double_value)
  35. #define PRINT_STRING(string_value) PRINTLNF(#string_value ": %s", string_value)
  36. #define PRINT_ARRAY(format, array, length) \
  37. { \
  38. int array_index; \
  39. for (array_index = 0; array_index < length; ++array_index) { \
  40. printf(format, array[array_index]); \
  41. }; \
  42. printf("\n"); \
  43. }
  44. #define PRINT_INT_ARRAY_LN(array, length) \
  45. { \
  46. int i; \
  47. for (i = 0; i < length; ++i) { \
  48. PRINTLNF(#array "[%d]: %d", i, array[i]); \
  49. } \
  50. }
  51. #define PRINT_INT_ARRAY(array, length) PRINT_ARRAY("%d, ", array, length)
  52. #define PRINT_CHAR_ARRAY(array, length) PRINT_ARRAY("%c, ", array, length)
  53. #define PRINT_DOUBLE_ARRAY(array, length) PRINT_ARRAY("%g, ", array, length)
  54. #define PRINT_IF_ERROR(format, ...) \
  55. if (errno != 0) { \
  56. fprintf(stderr, format, ##__VA_ARGS__); \
  57. fprintf(stderr, ": %s\n", strerror(errno)); \
  58. }
  59. #endif//CLOX__COMMON_H_