Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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