Tools made in assistance of the Metacall Project
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.

230 Zeilen
6.3 KiB

  1. #include "tstring.h"
  2. #include "string.h"
  3. #include "stdlib.h"
  4. #include "stdio.h"
  5. void generator(
  6. tstring* header_filename,
  7. tstring* source_filename,
  8. tstring* prefix,
  9. tstring* bucket_count,
  10. tstring* K_size, tstring* K_comp_pred, tstring* K_hash_func,
  11. tstring* V_size
  12. ) {
  13. tstring* key_type = tstring_n_compose("tc",
  14. prefix, "_key_t"
  15. );
  16. tstring* key_type_decl = tstring_n_compose("ctctc",
  17. "struct ", key_type, "{\n"
  18. "char* data[",K_size,"];\n"
  19. "};\n"
  20. );
  21. tstring* value_type = tstring_n_compose("tc",
  22. prefix, "_value_t"
  23. );
  24. tstring* value_type_decl = tstring_n_compose("ctctc",
  25. "struct ", value_type, "{\n"
  26. "char* data[",V_size,"];\n"
  27. "};\n"
  28. );
  29. tstring* accessor_type = tstring_n_compose("tc",
  30. prefix, "_accessor_t"
  31. );
  32. tstring* accessor_type_decl = tstring_n_compose("ctc",
  33. "struct ", accessor_type, "{\n"
  34. "void* value;\n"
  35. "};\n"
  36. );
  37. tstring* hash_decl = tstring_n_compose("ctctctctc",
  38. "size_t ",K_hash_func,"(void*);\n"
  39. "struct ",prefix,"_hashtype{\n"
  40. "size_t operator()(const ",key_type,"& key) {\n"
  41. "return ", K_hash_func, "((void*)&key);\n"
  42. "}\n"
  43. "};\n"
  44. );
  45. tstring* hash_type = tstring_n_compose("tc",
  46. prefix,"_hashtype"
  47. );
  48. tstring* hashmap_ptr_type = tstring_n_compose("tc",
  49. prefix,"_hashmap_ptr"
  50. );
  51. tstring* hashmap_decl = tstring_n_compose("ctc",
  52. "typedef void* ",hashmap_ptr_type,";\n"
  53. );
  54. tstring* cpp_hashmap_type = tstring_n_compose("ctctctctc",
  55. "mct20::lfhmap<",key_type,",",value_type,",",bucket_count,",",hash_type,">"
  56. );
  57. tstring* cpp_hashmap_ptr_type = tstring_n_compose("tc",
  58. cpp_hashmap_type,"*"
  59. );
  60. tstring* create_hashmap_decl = tstring_n_compose("tctc",
  61. hashmap_ptr_type," ",prefix,"_hm_create();\n"
  62. );
  63. tstring* destroy_hashmap_decl = tstring_n_compose("ctctc",
  64. "void ", prefix, "_hm_destroy(",hashmap_ptr_type,");\n"
  65. );
  66. tstring* hashmap_push_decl = tstring_n_compose("ctctctctc",
  67. "void ", prefix, "_hm_push(",hashmap_ptr_type," ptr, ",key_type,"* k, ",value_type,"* v);\n"
  68. );
  69. tstring* hashmap_get_decl = tstring_n_compose("tctctctc",
  70. accessor_type, " ", prefix, "_hm_get(",hashmap_ptr_type," ptr, ",key_type,"* k);\n"
  71. );
  72. tstring* hashmap_endget_decl = tstring_n_compose("ctctc",
  73. "void ", prefix, "_hm_endget(",accessor_type," acc);\n"
  74. );
  75. tstring* hashmap_remove_decl = tstring_n_compose("ctctctc",
  76. "void ", prefix, "_hm_remove(",hashmap_ptr_type," ptr, ",key_type,"* k);\n"
  77. );
  78. tstring* top_guard = tstring_n_compose("ctctc",
  79. "#ifndef GUARD_", prefix,
  80. "\n#define GUARD_", prefix,"\n\n"
  81. );
  82. tstring* bottom_guard = tstring_n_compose("ctc",
  83. "\n#endif // GUARD_", prefix, "\n"
  84. );
  85. tstring* extc_top_guard = cstring_to_tstring(
  86. "#ifdef __cplusplus\n"
  87. "extern \"C\" {\n"
  88. "#endif\n"
  89. );
  90. tstring* extc_bottom_guard = cstring_to_tstring(
  91. "#ifdef __cplusplus\n"
  92. "}\n"
  93. "#endif\n"
  94. );
  95. tstring* cpp_header_include = cstring_to_tstring(
  96. "#include \"lfhmap.hpp\"\n"
  97. );
  98. tstring* key_equality_predicate_handler = tstring_n_compose("ctctctctc",
  99. "bool operator==(",key_type,"& lhs,",key_type,"& rhs) {\n"
  100. " return K_comp_pred((void*)&lhs,(void*)&rhs);\n"
  101. "}\n"
  102. "bool operator!=(",key_type,"& lhs,",key_type,"& rhs) {\n"
  103. " return !K_comp_pred((void*)&lhs,(void*)&rhs);\n"
  104. "}\n"
  105. );
  106. tstring* to_cpp_hashmap_cast = tstring_n_compose("ctc",
  107. "(", cpp_hashmap_ptr_type, ")"
  108. );
  109. tstring* from_cpp_hashmap_cast = tstring_n_compose("ctc",
  110. "(", hashmap_ptr_type, ")"
  111. );
  112. tstring* create_hashmap_impl = tstring_n_compose("tctctctc",
  113. hashmap_ptr_type," ",prefix,"_hm_create(){\n"
  114. " return ",from_cpp_hashmap_cast,"new ",cpp_hashmap_type,"();\n"
  115. "}\n"
  116. );
  117. tstring* destroy_hashmap_impl = tstring_n_compose("ctctctc",
  118. "void ", prefix, "_hm_destroy(",hashmap_ptr_type," value){\n"
  119. " delete ",to_cpp_hashmap_cast,"value;\n"
  120. "}\n"
  121. );
  122. tstring* hashmap_push_impl = tstring_n_compose("ctctctctctc",
  123. "void ", prefix, "_hm_push(",hashmap_ptr_type," ptr, ",key_type,"* k, ",value_type,"* v){\n"
  124. " (",to_cpp_hashmap_cast,"ptr)->set(*k, *v);\n"
  125. "}\n"
  126. );
  127. tstring* hashmap_get_impl = tstring_n_compose("tctctctctctctc",
  128. accessor_type, " ", prefix, "_hm_get(",hashmap_ptr_type," ptr, ",key_type,"* k){\n",
  129. accessor_type, " acc;\n"
  130. " auto obtained = (",to_cpp_hashmap_cast,"ptr)->get(*k);\n"
  131. " if(obtained) {\n"
  132. " acc.value = (void*)new mct20::accessor<",value_type,">(obtained.value());\n"
  133. " } else {\n"
  134. " acc.value = nullptr;\n"
  135. " }\n"
  136. "}\n"
  137. );
  138. tstring* hashmap_endget_impl = tstring_n_compose("ctctctc",
  139. "void ", prefix, "_hm_endget(",accessor_type," acc) {\n"
  140. " delete (mct20::accessor<",value_type,">*)acc.value;\n"
  141. "}\n"
  142. );
  143. tstring* hashmap_remove_impl = tstring_n_compose("ctctctctc",
  144. "void ", prefix, "_hm_remove(",hashmap_ptr_type," ptr, ",key_type,"* k) {\n"
  145. " (",to_cpp_hashmap_cast,"ptr)->remove(*k);\n"
  146. "}\n"
  147. );
  148. {
  149. FILE* header;
  150. if(
  151. !(header = fopen(header_filename->data, "w"))
  152. ) {
  153. char* m = header_filename->data;
  154. fprintf(stderr, "Couldn't open header file: %s\n", header_filename->data);
  155. exit(EXIT_FAILURE);
  156. }
  157. if(
  158. tstring_n_write(
  159. header,14,
  160. top_guard,
  161. extc_top_guard,
  162. key_type_decl,
  163. value_type_decl,
  164. accessor_type_decl,
  165. hashmap_decl,
  166. create_hashmap_decl,
  167. destroy_hashmap_decl,
  168. hashmap_get_decl,
  169. hashmap_endget_decl,
  170. hashmap_push_decl,
  171. hashmap_remove_decl,
  172. extc_bottom_guard,
  173. bottom_guard
  174. ) != 0
  175. ) {
  176. fprintf(stderr, "Couldn't generate header file: error writing to file: %s\n", header_filename->data);
  177. exit(EXIT_FAILURE);
  178. }
  179. fclose(header);
  180. }
  181. {
  182. FILE* source;
  183. if(
  184. !(source = fopen(source_filename->data, "w"))
  185. ) {
  186. char* m = source_filename->data;
  187. fprintf(stderr, "Couldn't open source file: %s\n", source_filename->data);
  188. exit(EXIT_FAILURE);
  189. }
  190. if(
  191. tstring_n_write(
  192. source,22,
  193. top_guard,
  194. cpp_header_include,
  195. extc_top_guard,
  196. key_type_decl,
  197. value_type_decl,
  198. accessor_type_decl,
  199. hash_decl,
  200. hashmap_decl,
  201. create_hashmap_decl,
  202. destroy_hashmap_decl,
  203. hashmap_get_decl,
  204. hashmap_endget_decl,
  205. hashmap_push_decl,
  206. hashmap_remove_decl,
  207. create_hashmap_impl,
  208. destroy_hashmap_impl,
  209. hashmap_get_impl,
  210. hashmap_endget_impl,
  211. hashmap_push_impl,
  212. hashmap_remove_impl,
  213. extc_bottom_guard,
  214. bottom_guard
  215. ) != 0
  216. ) {
  217. fprintf(stderr, "Couldn't generate header file: error writing to file: %s\n", source_filename->data);
  218. exit(EXIT_FAILURE);
  219. }
  220. fclose(source);
  221. }
  222. }