Tools made in assistance of the Metacall Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.8 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* prefix,
  8. tstring* bucket_count,
  9. tstring* K_size, tstring* K_comp_pred, tstring* K_hash_func,
  10. tstring* V_size
  11. ) {
  12. tstring* key_type = tstring_n_compose("tc",
  13. prefix, "_key_t"
  14. );
  15. tstring* key_type_decl = tstring_n_compose("ctctc",
  16. "struct ", key_type, "{\n"
  17. "char* data[",K_size,"];\n"
  18. "};\n"
  19. );
  20. tstring* value_type = tstring_n_compose("tc",
  21. prefix, "_value_t"
  22. );
  23. tstring* value_type_decl = tstring_n_compose("ctctc",
  24. "struct ", value_type, "{\n"
  25. "char* data[",V_size,"];\n"
  26. "};\n"
  27. );
  28. tstring* accessor_type = tstring_n_compose("tc",
  29. prefix, "_accessor_t"
  30. );
  31. tstring* accessor_type_decl = tstring_n_compose("ctc",
  32. "struct ", accessor_type, "{\n"
  33. "void* value;\n"
  34. "};\n"
  35. );
  36. tstring* hash_decl = tstring_n_compose("ctctctctc",
  37. "size_t ",K_hash_func,"(void*);\n"
  38. "struct ",prefix,"_hashtype{\n"
  39. "size_t operator()(",key_type," key) {\n"
  40. "return ", K_hash_func, "(key);\n"
  41. "}\n"
  42. "}\n"
  43. );
  44. tstring* hashmap_ptr_type = tstring_n_compose("tc",
  45. prefix,"_hashmap_ptr"
  46. );
  47. tstring* hashmap_decl = tstring_n_compose("ctc",
  48. "typedef void* ",hashmap_ptr_type,";\n"
  49. );
  50. tstring* create_hashmap_decl = tstring_n_compose("tctc",
  51. hashmap_ptr_type," ",prefix,"_hm_create();\n"
  52. );
  53. tstring* destroy_hashmap_decl = tstring_n_compose("ctctc",
  54. "void ", prefix, "_hm_destroy(",hashmap_ptr_type,");\n"
  55. );
  56. tstring* hashmap_push_decl = tstring_n_compose("ctctctc",
  57. "void ", prefix, "_hm_push(",key_type,"* k, ",value_type,"* v);\n"
  58. );
  59. tstring* hashmap_get_decl = tstring_n_compose("tctctc",
  60. accessor_type, " ", prefix, "_hm_get(",key_type,"* k);\n"
  61. );
  62. tstring* hashmap_endget_decl = tstring_n_compose("ctctc",
  63. "void ", prefix, "_hm_endget(",accessor_type," acc);\n"
  64. );
  65. tstring* hashmap_remove_decl = tstring_n_compose("ctctc",
  66. "void ", prefix, "_hm_remove(",key_type,"* k);\n"
  67. );
  68. tstring* top_guard = tstring_n_compose("ctctc",
  69. "#ifndef GUARD_", prefix,
  70. "\n#define GUARD_", prefix,"\n\n"
  71. );
  72. tstring* bottom_guard = tstring_n_compose("ctc",
  73. "\n#endif // GUARD_", prefix, "\n"
  74. );
  75. FILE* header;
  76. if(
  77. !(header = fopen(header_filename->data, "w"))
  78. ) {
  79. char* m = header_filename->data;
  80. fprintf(stderr, "Couldn't open header file: %s\n", header_filename->data);
  81. exit(EXIT_FAILURE);
  82. }
  83. if(
  84. tstring_n_write(
  85. header,12,
  86. top_guard,
  87. key_type_decl,
  88. value_type_decl,
  89. accessor_type_decl,
  90. hashmap_decl,
  91. create_hashmap_decl,
  92. destroy_hashmap_decl,
  93. hashmap_get_decl,
  94. hashmap_endget_decl,
  95. hashmap_push_decl,
  96. hashmap_remove_decl,
  97. bottom_guard
  98. ) != 0
  99. ) {
  100. fprintf(stderr, "Couldn't generate header file: error writing to file: %s\n", header_filename->data);
  101. exit(EXIT_FAILURE);
  102. }
  103. }