struct_table.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include "struct_table.h"
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. struct struct_table_entry struct_table[DD_STRUCT_TABLE_TOTAL];
  6. int struct_table_current = 0;
  7. // initialise the struct table index to no tables
  8. void struct_table_init() {
  9. struct_table_current = -1;
  10. // Engine's initial structs and their members
  11. struct_table_push("dd_world", 0);
  12. struct_table_push_member("create", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  13. struct_table_push_member("onload", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  14. struct_table_push_member("update", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  15. struct_table_push_member("resize", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  16. struct_table_push_member("draw", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  17. struct_table_push_member("key_input", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  18. struct_table_push_member("mouse_input", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  19. struct_table_push_member("clean", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  20. struct_table_push("dd_matrix", 0);
  21. struct_table_push("dd_mesh", 0);
  22. struct_table_push_member("draw", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  23. struct_table_push_member("clean", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  24. struct_table_push_member("set_primitive", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  25. struct_table_push_member("load", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  26. struct_table_push_member("copy", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  27. struct_table_push("dd_meshColour", "dd_mesh");
  28. struct_table_push_member("set_colour", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  29. struct_table_push("dd_meshRising", "dd_meshColour");
  30. struct_table_push_member("set_animation_max", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  31. struct_table_push_member("set_animation_current", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  32. struct_table_push("dd_meshTexture", "dd_meshColour");
  33. struct_table_push_member("preloadTexture", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  34. struct_table_push_member("applyTexture", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  35. struct_table_push_member("loadTexture", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  36. struct_table_push_member("set_primitive_texcoords", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  37. struct_table_push_member("copyTexture", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  38. struct_table_push("dd_sound", 0);
  39. struct_table_push_member("load", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  40. struct_table_push_member("clean", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  41. struct_table_push_member("play", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  42. struct_table_push_member("playLoop", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  43. struct_table_push_member("stop", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  44. struct_table_push_member("setVolume", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  45. struct_table_push("dd_vec3", 0);
  46. struct_table_push_member("set", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  47. struct_table_push_member("getX", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  48. struct_table_push_member("getY", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  49. struct_table_push_member("getZ", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  50. struct_table_push("dd_string3d", 0);
  51. struct_table_push_member("setAlign", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  52. struct_table_push_member("setColorFront", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  53. struct_table_push_member("setColorBack", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  54. struct_table_push_member("draw", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  55. struct_table_push_member("drawInt", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  56. struct_table_push_member("drawLimit", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  57. struct_table_push_member("clean", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  58. struct_table_push("avdl_program", 0);
  59. struct_table_push_member("clean", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  60. struct_table_push_member("setVertexShader", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  61. struct_table_push_member("setFragmentShader", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  62. struct_table_push_member("useProgram", DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  63. //struct_table_push("dd_gamejolt_response_struct", 0);
  64. }
  65. // push a new struct to the table
  66. int struct_table_push(const char *structname, const char *parentname) {
  67. // check for max number of structs
  68. if (struct_table_current+1 > DD_STRUCT_TABLE_TOTAL) {
  69. printf("struct_table_add: max number of struct tables reached\n");
  70. exit(-1);
  71. }
  72. // increment counter
  73. struct_table_current++;
  74. // set the values of the new struct
  75. struct struct_table_entry *newStruct = &struct_table[struct_table_current];
  76. strncpy(newStruct->name, structname, DD_STRUCT_TABLE_NAME_SIZE -1);
  77. newStruct->name[DD_STRUCT_TABLE_NAME_SIZE-1] = '\0';
  78. newStruct->member_total = 0;
  79. newStruct->parent = -1;
  80. // if a parent name was given, check if it exists, and assign as parent of the new struct
  81. if (parentname) {
  82. for (int i = 0; i < struct_table_current; i++) {
  83. if (strcmp(struct_table[i].name, parentname) == 0) {
  84. newStruct->parent = i;
  85. break;
  86. }
  87. }
  88. // no parent was found, this is an error!
  89. if (newStruct->parent < 0) {
  90. printf("error: struct_table_push: no parent found with name '%s'\n", parentname);
  91. exit(-1);
  92. }
  93. }
  94. // return the index of the new struct
  95. return struct_table_current;
  96. }
  97. /* push a new member to the last pushed struct
  98. * get a reference to the last pushed struct
  99. * check if there is space in the struct
  100. * add a new member to it
  101. */
  102. struct struct_table_entry_member *struct_table_push_member(const char *name, enum dd_variable_type type, const char *nametype, int isRef) {
  103. struct struct_table_entry *currentStruct = &struct_table[struct_table_current];
  104. if (currentStruct->member_total+1 >= DD_STRUCT_TABLE_MEMBER_TOTAL) {
  105. printf("struct_table_push_member: struct '%s': maximum number of members reached\n", currentStruct->name);
  106. exit(-1);
  107. }
  108. struct struct_table_entry_member *newMember = &currentStruct->members[currentStruct->member_total];
  109. newMember->type = type;
  110. strncpy(newMember->name, name, DD_STRUCT_TABLE_NAME_SIZE -1);
  111. newMember->name[DD_STRUCT_TABLE_NAME_SIZE-1] = '\0';
  112. newMember->arrayCount = 1;
  113. newMember->isRef = isRef;
  114. if (nametype) {
  115. strncpy(newMember->nametype, nametype, DD_STRUCT_TABLE_NAME_SIZE -1);
  116. newMember->nametype[DD_STRUCT_TABLE_NAME_SIZE-1] = '\0';
  117. }
  118. else {
  119. newMember->nametype[0] = '\0';
  120. }
  121. currentStruct->member_total++;
  122. return &currentStruct->members[currentStruct->member_total -1];
  123. }
  124. void struct_table_push_member_array(const char *name, enum dd_variable_type type, const char *nametype, int arrayCount, int isRef) {
  125. struct struct_table_entry_member *newMember = struct_table_push_member(name, type, nametype, isRef);
  126. newMember->arrayCount = arrayCount;
  127. }
  128. void struct_table_pop() {
  129. struct struct_table_entry *currentTable = &struct_table[struct_table_current];
  130. if (currentTable->member_total > 0) {
  131. currentTable->member_total--;
  132. }
  133. }
  134. /* print all structs and their members
  135. * this is meant for debug only
  136. */
  137. void struct_table_print() {
  138. printf("struct table start\n");
  139. for (int i = 0; i <= struct_table_current; i++) {
  140. struct struct_table_entry *s = &struct_table[i];
  141. printf("struct: %s", s->name);
  142. if (s->parent >= 0) {
  143. printf(":%s", struct_table_get_name(s->parent));
  144. }
  145. printf("\n");
  146. for (int j = 0; j < s->member_total; j++) {
  147. struct struct_table_entry_member *m = &s->members[j];
  148. printf(" member: %s\n", m->name);
  149. }
  150. }
  151. printf("struct table end\n");
  152. }
  153. // return the name of the struct on index, make sure index is in bounds
  154. const char *struct_table_get_name(int index) {
  155. if (index < 0 || index > struct_table_current) {
  156. printf("error: struct_table_get_name: index out of bounds: %d\n", index);
  157. exit(-1);
  158. }
  159. return struct_table[index].name;
  160. }
  161. const char *struct_table_get_member_name(int structIndex, int memberIndex) {
  162. return struct_table[structIndex].members[memberIndex].name;
  163. }
  164. int struct_table_getMemberArrayCount(int structIndex, int memberIndex) {
  165. return struct_table[structIndex].members[memberIndex].arrayCount;
  166. }
  167. int struct_table_getMemberIsRef(int structIndex, int memberIndex) {
  168. return struct_table[structIndex].members[memberIndex].isRef;
  169. }
  170. enum dd_variable_type struct_table_get_member_type(int structIndex, int memberIndex) {
  171. return struct_table[structIndex].members[memberIndex].type;
  172. }
  173. int struct_table_get_member(int structIndex, const char *membername) {
  174. for (int i = 0; i < struct_table[structIndex].member_total; i++) {
  175. struct struct_table_entry_member *m = &struct_table[structIndex].members[i];
  176. if (strcmp(m->name, membername) == 0) {
  177. return i;
  178. }
  179. }
  180. return -1;
  181. }
  182. int struct_table_is_member_primitive(int structIndex, int memberIndex) {
  183. struct struct_table_entry_member *m = &struct_table[structIndex].members[memberIndex];
  184. return m->type != DD_VARIABLE_TYPE_STRUCT;
  185. }
  186. int struct_table_is_member_primitive_string(int structIndex, const char *membername) {
  187. if (struct_table_has_member(structIndex, membername)) {
  188. int memberId = struct_table_get_member(structIndex, membername);
  189. struct struct_table_entry_member *m = &struct_table[structIndex].members[memberId];
  190. return m->type != DD_VARIABLE_TYPE_STRUCT;
  191. }
  192. else
  193. if (struct_table[structIndex].parent >= 0) {
  194. return struct_table_is_member_primitive_string(struct_table[structIndex].parent, membername);
  195. }
  196. return 0;
  197. }
  198. int struct_table_has_member(int structIndex, const char *membername) {
  199. if (structIndex < 0 || structIndex > struct_table_current) {
  200. printf("error: struct_table_has_member: index out of bounds: %d for member '%s'\n", structIndex, membername);
  201. exit(-1);
  202. }
  203. struct struct_table_entry *e = &struct_table[structIndex];
  204. for (int i = 0; i < e->member_total; i++) {
  205. struct struct_table_entry_member *m = &e->members[i];
  206. if (strcmp(m->name, membername) == 0) {
  207. return 1;
  208. }
  209. }
  210. return 0;
  211. }
  212. int struct_table_has_member_parent(int structIndex, const char *membername) {
  213. if (structIndex < 0 || structIndex > struct_table_current) {
  214. printf("error: struct_table_has_member: index out of bounds: %d for member '%s'\n", structIndex, membername);
  215. exit(-1);
  216. }
  217. // has member
  218. if (struct_table_has_member(structIndex, membername)) {
  219. return 1;
  220. }
  221. // maybe parent has member ?
  222. if (struct_table[structIndex].parent >= 0) {
  223. return struct_table_has_member_parent(struct_table[structIndex].parent, membername);
  224. }
  225. // doesn't have member
  226. return 0;
  227. }
  228. static int parent_level;
  229. static int parent_level_current;
  230. static int struct_table_is_member_parent_search(int structIndex, const char *membername) {
  231. if (structIndex < 0 || structIndex > struct_table_current) {
  232. printf("error: struct_table_is_member_parent: index out of bounds: %d\n", structIndex);
  233. exit(-1);
  234. }
  235. parent_level++;
  236. struct struct_table_entry *t = &struct_table[structIndex];
  237. // is member of this struct
  238. if (struct_table_has_member(structIndex, membername)) {
  239. parent_level_current = parent_level;
  240. }
  241. if (t->parent == -1) {
  242. return parent_level_current;
  243. }
  244. //return -1;
  245. return struct_table_is_member_parent_search(t->parent, membername);
  246. }
  247. int struct_table_is_member_parent(int structIndex, const char *membername) {
  248. parent_level_current = -1;
  249. parent_level = -1;
  250. return struct_table_is_member_parent_search(structIndex, membername);
  251. }
  252. int struct_table_get_index(const char *structname) {
  253. if (!structname) {
  254. return -1;
  255. }
  256. for (int i = 0; i <= struct_table_current; i++) {
  257. if (strcmp(struct_table[i].name, structname) == 0) {
  258. return i;
  259. }
  260. }
  261. return -1;
  262. }
  263. int struct_table_get_member_scope(int structIndex, int memberIndex) {
  264. if (structIndex < 0 || structIndex > struct_table_current) {
  265. printf("error: struct_table_get_member_scope: index out of bounds: %d\n", structIndex);
  266. exit(-1);
  267. }
  268. struct struct_table_entry_member *m = &struct_table[structIndex].members[memberIndex];
  269. if (m->type == DD_VARIABLE_TYPE_STRUCT) {
  270. return struct_table_get_index(m->nametype);
  271. }
  272. printf("struct_table_get_member_scope: error: '%s' of '%s' is not struct\n", m->name, struct_table_get_name(structIndex));
  273. exit(-1);
  274. }
  275. int struct_table_get_member_scope_string(int structIndex, const char *membername) {
  276. if (structIndex < 0 || structIndex > struct_table_current) {
  277. printf("error: struct_table_get_member_scope_string: index out of bounds: %d\n", structIndex);
  278. exit(-1);
  279. }
  280. if (struct_table_has_member(structIndex, membername)) {
  281. int memberIndex = struct_table_get_member(structIndex, membername);
  282. struct struct_table_entry_member *m = &struct_table[structIndex].members[memberIndex];
  283. if (m->type == DD_VARIABLE_TYPE_STRUCT) {
  284. return struct_table_get_index(m->nametype);
  285. }
  286. printf("struct_table_get_member_scope: error: '%s' of '%s' is not struct\n", m->name, struct_table_get_name(structIndex));
  287. exit(-1);
  288. }
  289. else
  290. if (struct_table[structIndex].parent) {
  291. return struct_table_get_member_scope_string(struct_table[structIndex].parent, membername);
  292. }
  293. printf("failed searching for %d %s\n", structIndex, membername);
  294. struct_table_print();
  295. printf("struct_table_get_member_scope: error: \n");
  296. exit(-1);
  297. }
  298. int struct_table_get_parent(int structIndex) {
  299. return struct_table[structIndex].parent;
  300. }
  301. unsigned int struct_table_get_member_total(int structIndex) {
  302. return struct_table[structIndex].member_total;
  303. }
  304. char *struct_table_get_member_nametype(int structIndex, int memberIndex) {
  305. return struct_table[structIndex].members[memberIndex].nametype;
  306. }
  307. int struct_table_count() {
  308. return struct_table_current+1;
  309. }
  310. int struct_table_exists(const char *structname) {
  311. if (!structname) {
  312. printf("struct_table_exists: no struct given\n");
  313. exit(-1);
  314. }
  315. for (int i = 0; i <= struct_table_current; i++) {
  316. if (strcmp(struct_table[i].name, structname) == 0) {
  317. return 1;
  318. }
  319. }
  320. return 0;
  321. }