Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

280 Zeilen
17 KiB

  1. #define new(type) new type
  2. #define proc auto
  3. #ifdef _DEBUG
  4. # define if_debug if constexpr (true)
  5. #else
  6. # define if_debug if constexpr (false)
  7. #endif
  8. #ifdef _MSC_VER
  9. # define debug_break() if_debug __debugbreak()
  10. # define if_windows if constexpr (true)
  11. # define if_linux if constexpr (false)
  12. #else
  13. # define debug_break() if_debug raise(SIGTRAP)
  14. # define if_windows if constexpr (false)
  15. # define if_linux if constexpr (true)
  16. #endif
  17. #define concat_( a, b) a##b
  18. #define label(prefix, lnum) concat_(prefix,lnum)
  19. #define try_or_else_return(val) \
  20. if (1) \
  21. goto label(body,__LINE__); \
  22. else \
  23. while (1) \
  24. if (1) { \
  25. if (Globals::error) { \
  26. if (Globals::log_level == Log_Level::Debug) { \
  27. printf("in %s:%d\n", __FILE__, __LINE__); \
  28. } \
  29. return val; \
  30. } \
  31. break; \
  32. } \
  33. else label(body,__LINE__):
  34. ;
  35. #define try_struct try_or_else_return({})
  36. #define try_void try_or_else_return()
  37. #define try try_or_else_return(0)
  38. #define dont_break_on_errors fluid_let(Globals::breaking_on_errors, false)
  39. #define ignore_logging fluid_let(Globals::log_level, Log_Level::None)
  40. #define define_array_list(type, name) \
  41. \
  42. struct name##_Array_List { \
  43. type* data; \
  44. int length; \
  45. int next_index; \
  46. }; \
  47. \
  48. proc remove_index_from_array_list(name##_Array_List* arraylist, int index) -> void { \
  49. arraylist->data[index] = \
  50. arraylist->data[--(arraylist->next_index)]; \
  51. } \
  52. \
  53. proc append_to_array_list(name##_Array_List* arraylist, type element) -> void { \
  54. if (arraylist->next_index == arraylist->length) { \
  55. arraylist->length *= 2; \
  56. arraylist->data = \
  57. (type*)realloc(arraylist->data, arraylist->length * sizeof(type)); \
  58. } \
  59. arraylist->data[arraylist->next_index] = element; \
  60. arraylist->next_index++; \
  61. } \
  62. \
  63. proc _merge_array_lists(name##_Array_List* arr, int start, int mid, int end) -> void { \
  64. int start2 = mid + 1; \
  65. \
  66. /* If the direct merge is already sorted */ \
  67. if ((size_t)arr->data[mid] <= (size_t)arr->data[start2]) { \
  68. return; \
  69. } \
  70. \
  71. /* Two pointers to maintain start of both arrays to merge */ \
  72. while (start <= mid && start2 <= end) { \
  73. if ((size_t)arr->data[start] <= (size_t)arr->data[start2]) { \
  74. start++; \
  75. } \
  76. else { \
  77. type value = arr->data[start2]; \
  78. int index = start2; \
  79. \
  80. /* Shift all the elements between element 1; element 2, right by 1. */ \
  81. while (index != start) { \
  82. arr->data[index] = arr->data[index - 1]; \
  83. index--; \
  84. } \
  85. arr->data[start] = value; \
  86. \
  87. /* Update all the pointers */ \
  88. start++; \
  89. mid++; \
  90. start2++; \
  91. } \
  92. } \
  93. } \
  94. \
  95. proc sort_array_list(name##_Array_List* arraylist, int left=-1, int right=-1) -> void { \
  96. if (left == -1) { \
  97. sort_array_list(arraylist, 0, arraylist->next_index - 1); \
  98. return; \
  99. } else if (left == right) { \
  100. return; \
  101. } \
  102. \
  103. int middle = left + (right-left) / 2; \
  104. \
  105. sort_array_list(arraylist, left, middle); \
  106. sort_array_list(arraylist, middle+1, right); \
  107. \
  108. _merge_array_lists(arraylist, left, middle, right); \
  109. } \
  110. \
  111. proc sorted_array_list_find(name##_Array_List* arraylist, type elem, int left=-1, int right=-1) -> int { \
  112. if (left == -1) { \
  113. return sorted_array_list_find(arraylist, elem, 0, arraylist->next_index - 1); \
  114. } else if (left == right) { \
  115. if ((size_t)arraylist->data[left] == (size_t)elem) \
  116. return left; \
  117. return -1; \
  118. } else if (right < left) \
  119. return -1; \
  120. \
  121. int middle = left + (right-left) / 2; \
  122. \
  123. if ((size_t)arraylist->data[middle] < (size_t)elem) \
  124. return sorted_array_list_find(arraylist, elem, middle+1, right); \
  125. if ((size_t)arraylist->data[middle] > (size_t)elem) \
  126. return sorted_array_list_find(arraylist, elem, left, middle-1); \
  127. return middle; \
  128. } \
  129. \
  130. proc create_##name##_array_list(int initial_capacity = 16) -> name##_Array_List { \
  131. name##_Array_List ret; \
  132. ret.data = (type*)malloc(initial_capacity * sizeof(type)); \
  133. ret.next_index = 0; \
  134. ret.length = initial_capacity; \
  135. return ret; \
  136. }
  137. /*
  138. * iterate over array lists
  139. */
  140. #define for_array_list(l) \
  141. if (int it_index = 0); else \
  142. for (auto it = (l).data[0]; \
  143. it_index < (l).next_index; \
  144. it=(l).data[++it_index])
  145. /*
  146. * iterate over lisp vectors
  147. */
  148. #define for_lisp_vector(v) \
  149. if (!v); else \
  150. if (int it_index = 0); else \
  151. for (auto it = v->value.vector.data; \
  152. it_index < v->value.vector.length; \
  153. it=v->value.vector.data+(++it_index))
  154. /*
  155. * iterate over lisp lists
  156. */
  157. #define for_lisp_list(l) \
  158. if (!l); else \
  159. if (int it_index = 0); else \
  160. for (Lisp_Object* head = l, *it; \
  161. Memory::get_type(head) == Lisp_Object_Type::Pair && (it = head->value.pair.first); \
  162. head = head->value.pair.rest, ++it_index)
  163. /**
  164. Usage of the create_error_macros:
  165. */
  166. #define __create_error(keyword, ...) \
  167. create_error( \
  168. __FILE__, __LINE__, \
  169. Memory::get_or_create_lisp_object_keyword(keyword), \
  170. __VA_ARGS__)
  171. #define create_out_of_memory_error(...) \
  172. __create_error("out-of-memory", __VA_ARGS__)
  173. #define create_generic_error(...) \
  174. __create_error("generic", __VA_ARGS__)
  175. #define create_not_yet_implemented_error() \
  176. __create_error("not-yet-implemented", "This feature has not yet been implemented.")
  177. #define create_parsing_error(...) \
  178. __create_error("parsing-error", __VA_ARGS__)
  179. #define create_symbol_undefined_error(...) \
  180. __create_error("symbol-undefined", __VA_ARGS__)
  181. #define create_type_missmatch_error(expected, actual) \
  182. __create_error("type-missmatch", \
  183. "Type missmatch: expected %s, got %s", \
  184. expected, actual)
  185. #define create_wrong_number_of_arguments_error(expected, actual) \
  186. __create_error("wrong-number-of-arguments", \
  187. "Wrong number of arguments: expected %d, got %d", \
  188. expected, actual)
  189. #define create_too_many_arguments_error(expected, actual) \
  190. __create_error("wrong-number-of-arguments", \
  191. "Wrong number of arguments: expected less or equal to %d, got %d", \
  192. expected, actual)
  193. #define create_too_few_arguments_error(expected, actual) \
  194. __create_error("wrong-number-of-arguments", \
  195. "Wrong number of arguments: expected greater or equal to %d, got %d", \
  196. expected, actual)
  197. #define assert_arguments_length(expected, actual) \
  198. do { \
  199. if (expected != actual) { \
  200. create_wrong_number_of_arguments_error(expected, actual); \
  201. } \
  202. } while(0)
  203. #define assert_arguments_length_less_equal(expected, actual) \
  204. do { \
  205. if (expected < actual) { \
  206. create_too_many_arguments_error(expected, actual); \
  207. } \
  208. } while(0)
  209. #define assert_arguments_length_greater_equal(expected, actual) \
  210. do { \
  211. if (expected > actual) { \
  212. create_too_few_arguments_error(expected, actual); \
  213. } \
  214. } while(0)
  215. #define assert_type(_node, _type) \
  216. do { \
  217. if (Memory::get_type(_node) != _type) { \
  218. create_type_missmatch_error( \
  219. Lisp_Object_Type_to_string(_type), \
  220. Lisp_Object_Type_to_string(Memory::get_type(_node))); \
  221. } \
  222. } while(0)
  223. #define assert(condition) \
  224. do { \
  225. if (!(condition)) { \
  226. create_generic_error("Assertion-error."); \
  227. } \
  228. } while(0)
  229. /*
  230. #define assert(cond) \
  231. if_debug { \
  232. if (!cond) { \
  233. if (log_level == Log_Level::Debug) { \
  234. printf("Assertion failed: %s %d", __FILE__, __LINE__); \
  235. } \
  236. debug_break(); \
  237. } \
  238. } else {} \
  239. */
  240. #define console_normal "\x1B[0m"
  241. #define console_red "\x1B[31m"
  242. #define console_green "\x1B[32m"
  243. #define console_cyan "\x1B[36m"
  244. // #define console_normal ""
  245. // #define console_red ""
  246. // #define console_green ""
  247. // #define console_cyan ""