Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

192 строки
8.6 KiB

  1. #define new(type) new type
  2. #define proc auto
  3. #ifdef _DEBUG
  4. constexpr bool is_debug_build = true;
  5. #else
  6. constexpr bool is_debug_build = false;
  7. #endif
  8. #define if_debug if constexpr (is_debug_build)
  9. #ifdef _MSC_VER
  10. # define if_windows if constexpr (1)
  11. # define if_linux if constexpr (0)
  12. # define debug_break() if_debug __debugbreak()
  13. #else
  14. # include <signal.h>
  15. # define debug_break() if_debug raise(SIGTRAP)
  16. # define if_windows if (0)
  17. # define if_linux if (1)
  18. #endif
  19. #define concat_( a, b) a##b
  20. #define label(prefix, lnum) concat_(prefix,lnum)
  21. #define try \
  22. if (1) \
  23. goto label(body,__LINE__); \
  24. else \
  25. while (1) \
  26. if (1) { \
  27. if(Globals::error) { \
  28. if (log_level == Log_Level::Debug) { \
  29. printf("in %s:%d\n", __FILE__, __LINE__); \
  30. } \
  31. return 0; \
  32. } \
  33. break; \
  34. } \
  35. else label(body,__LINE__):
  36. #define try_void \
  37. if (1) \
  38. goto label(body,__LINE__); \
  39. else \
  40. while (1) \
  41. if (1) { \
  42. if(Globals::error) { \
  43. if (log_level == Log_Level::Debug) { \
  44. printf("in %s:%d\n", __FILE__, __LINE__); \
  45. } \
  46. return; \
  47. } \
  48. break; \
  49. } \
  50. else label(body,__LINE__):
  51. #define define_array_list(type, name) \
  52. struct name##_Array_List { \
  53. type* data; \
  54. int length; \
  55. int next_index; \
  56. }; \
  57. \
  58. \
  59. proc append_to_array_list(name##_Array_List* arraylist, type element) -> void { \
  60. if (arraylist->next_index == arraylist->length) { \
  61. arraylist->length *= 2; \
  62. arraylist->data = \
  63. (type*)realloc(arraylist->data, arraylist->length * sizeof(type)); \
  64. } \
  65. arraylist->data[arraylist->next_index++] = element; \
  66. } \
  67. \
  68. \
  69. proc create_##name##_array_list(int initial_capacity = 16) -> name##_Array_List* { \
  70. name##_Array_List* ret = new(name##_Array_List); \
  71. ret->data = (type*)malloc(initial_capacity * sizeof(type)); \
  72. ret->next_index = 0; \
  73. ret->length = initial_capacity; \
  74. return ret; \
  75. }
  76. template<typename F>
  77. class defer_finalizer {
  78. F f;
  79. bool moved;
  80. public:
  81. template<typename T>
  82. defer_finalizer(T && f_) : f(std::forward<T>(f_)), moved(false) { }
  83. defer_finalizer(const defer_finalizer &) = delete;
  84. defer_finalizer(defer_finalizer && other) : f(std::move(other.f)), moved(other.moved) {
  85. other.moved = true;
  86. }
  87. ~defer_finalizer() {
  88. if (!moved) f();
  89. }
  90. };
  91. struct {
  92. template<typename F>
  93. defer_finalizer<F> operator<<(F && f) {
  94. return defer_finalizer<F>(std::forward<F>(f));
  95. }
  96. } deferrer;
  97. #define TOKENPASTE(x, y) x ## y
  98. #define TOKENPASTE2(x, y) TOKENPASTE(x, y)
  99. #define defer auto TOKENPASTE2(__deferred_lambda_call, __COUNTER__) = deferrer << [&]
  100. /*
  101. Usage of the create_error_macros:
  102. */
  103. #define __create_error(keyword, ...) \
  104. create_error( \
  105. __FILE__, __LINE__, \
  106. Memory::get_or_create_lisp_object_keyword(keyword), \
  107. __VA_ARGS__)
  108. #define create_out_of_memory_error(...) \
  109. __create_error("out-of-memory", __VA_ARGS__)
  110. #define create_generic_error(...) \
  111. __create_error("generic", __VA_ARGS__)
  112. #define create_not_yet_implemented_error() \
  113. __create_error("not-yet-implemented", "This feature has not yet been implemented.")
  114. #define create_parsing_error(...) \
  115. __create_error("parsing-error", __VA_ARGS__)
  116. #define create_symbol_undefined_error(...) \
  117. __create_error("symbol-undefined", __VA_ARGS__)
  118. #define create_type_missmatch_error(expected, actual) \
  119. __create_error("type-missmatch", \
  120. "Type missmatch: expected %s, got %s", \
  121. expected, actual)
  122. #define create_wrong_number_of_arguments_error(expected, actual) \
  123. __create_error("wrong-number-of-arguments", \
  124. "Wrong number of arguments: expected %d, got %d", \
  125. expected, actual)
  126. #define assert_arguments_length(expected, actual) \
  127. do { \
  128. if (expected != actual) { \
  129. create_wrong_number_of_arguments_error(expected, actual); \
  130. } \
  131. } while(0)
  132. #define assert_type(_node, _type) \
  133. do { \
  134. if (Memory::get_type(_node) != _type) { \
  135. create_type_missmatch_error("symbol", Lisp_Object_Type_to_string(Memory::get_type(_node))); \
  136. } \
  137. } while(0)
  138. #define assert(condition) \
  139. do { \
  140. if (!(condition)) { \
  141. create_generic_error("Assertion-error."); \
  142. } \
  143. } while(0)
  144. // #define assert(cond) \
  145. // if_debug { \
  146. // if (!cond) { \
  147. // if (log_level == Log_Level::Debug) { \
  148. // printf("Assertion failed: %s %d", __FILE__, __LINE__); \
  149. // } \
  150. // debug_break(); \
  151. // } \
  152. // } else {} \
  153. #define console_normal "\x1B[0m"
  154. #define console_red "\x1B[31m"
  155. #define console_green "\x1B[32m"
  156. #define console_cyan "\x1B[36m"
  157. // #define console_normal ""
  158. // #define console_red ""
  159. // #define console_green ""
  160. // #define console_cyan ""