parser.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "parser.h"
  2. #include "stdlib.h"
  3. #include "stdio.h"
  4. #include "yacc.tab.h"
  5. #include "symtable.h"
  6. #include "structtable.h"
  7. #include <string.h>
  8. // file descriptor for global data
  9. FILE *fd_global;
  10. int tabcount;
  11. void print_node(FILE *fd, struct ast_node *n) {
  12. // parse node
  13. struct entry *e;
  14. struct ast_node *child;
  15. struct struct_entry *se;
  16. int i;
  17. printf("parsing: %d : %d\n", n->node_type, AST_FUNCTION_DEFINITION);
  18. switch (n->node_type) {
  19. /* the game itself, parent of all nodes
  20. * contains statements, just print them all one by one
  21. */
  22. case AST_GROUP:
  23. case AST_GAME:
  24. tabcount++;
  25. for (int i = 0; i < n->children.elements; i++) {
  26. print_node(fd, (struct ast_node *) dd_da_get(&n->children, i));
  27. }
  28. tabcount--;
  29. break;
  30. /* identifier, just print it
  31. */
  32. case AST_IDENTIFIER:
  33. e = symtable_entryat(n->value);
  34. // internal variable - width
  35. if (e->token == DD_INTERNAL_WIDTH) {
  36. fprintf(fd, "DD_GAME_WIDTH", e->lexptr);
  37. }
  38. else
  39. // internal variable - height
  40. if (e->token == DD_INTERNAL_HEIGHT) {
  41. fprintf(fd, "DD_GAME_HEIGHT", e->lexptr);
  42. }
  43. // normal variable
  44. else {
  45. fprintf(fd, "%s", e->lexptr);
  46. }
  47. for (int i = 0; i < n->children.elements; i++) {
  48. // some variables print less of the children
  49. if (e->value == 1 && i+1 >= n->children.elements) break;
  50. child = dd_da_get(&n->children, i);
  51. fprintf(fd, "->");
  52. print_node(fd, child);
  53. }
  54. break;
  55. case AST_ARGUMENT:
  56. e = symtable_entryat(n->value);
  57. char *type;
  58. switch (e->token) {
  59. case DD_KEYWORD_INT: type = "int"; break;
  60. case DD_KEYWORD_FLOAT: type = "float"; break;
  61. case DD_KEYWORD_STRUCT: type = "struct"; break;
  62. case DD_KEYWORD_VOID:
  63. default:
  64. type = "void"; break;
  65. }
  66. fprintf(fd, "%s %s", type, e->lexptr);
  67. break;
  68. /* number
  69. */
  70. case AST_NUMBER:
  71. fprintf(fd, "%d", n->value);
  72. break;
  73. case AST_STRING:
  74. e = symtable_entryat(n->value);
  75. fprintf(fd, "%s", e->lexptr);
  76. break;
  77. /* definition
  78. * <type> child1;
  79. */
  80. case AST_DEFINITION:
  81. child = dd_da_get(&n->children, 0);
  82. e = symtable_entryat(child->value);
  83. if (e->token == DD_KEYWORD_VOID) fprintf(fd, "void ");
  84. else if (e->token == DD_KEYWORD_INT) fprintf(fd, "int ");
  85. else if (e->token == DD_KEYWORD_FLOAT) fprintf(fd, "float ");
  86. else if (e->token == DD_KEYWORD_STRUCT) fprintf(fd, " struct %s *", struct_entryat(e->value)->name);
  87. else fprintf(fd, "%d ", e->token);
  88. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  89. fprintf(fd, ";\n");
  90. break;
  91. /* function definition
  92. */
  93. case AST_FUNCTION_DEFINITION:
  94. e = symtable_entryat(n->value);
  95. if (e->token == DD_KEYWORD_VOID) fprintf(fd, "void ");
  96. else if (e->token == DD_KEYWORD_INT) fprintf(fd, "int ");
  97. else fprintf(fd, "void ");
  98. fprintf(fd, "%s () {\n", e->lexptr);
  99. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  100. fprintf(fd, "};\n");
  101. break;
  102. /* function definition
  103. */
  104. case AST_FUNCTION_CALL:
  105. n = dd_da_get(&n->children, 0);
  106. e = symtable_entryat(n->value);
  107. print_node(fd, n);
  108. fprintf(fd, "(");
  109. e->value = 1;
  110. print_node(fd, n);
  111. e->value = 0;
  112. fprintf(fd, ");\n");
  113. break;
  114. /* equality
  115. * child1 = child2;
  116. */
  117. case AST_ASSIGNMENT:
  118. e = symtable_entryat(n->value);
  119. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  120. fprintf(fd, " = ");
  121. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  122. fprintf(fd, ";\n");
  123. break;
  124. case AST_ADDITION:
  125. fprintf(fd, "(");
  126. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  127. fprintf(fd, " + ");
  128. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  129. fprintf(fd, ")");
  130. break;
  131. case AST_SUBSTRACTION:
  132. fprintf(fd, "(");
  133. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  134. fprintf(fd, " - ");
  135. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  136. fprintf(fd, ")");
  137. break;
  138. case AST_MULTIPLICATION:
  139. fprintf(fd, "(");
  140. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  141. fprintf(fd, " * ");
  142. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  143. fprintf(fd, ")");
  144. break;
  145. case AST_DIVISION:
  146. fprintf(fd, "(");
  147. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  148. fprintf(fd, " / ");
  149. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  150. fprintf(fd, ")");
  151. break;
  152. case AST_LESS_THAN:
  153. fprintf(fd, "(");
  154. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  155. fprintf(fd, " < ");
  156. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  157. fprintf(fd, ")");
  158. break;
  159. case AST_MORE_THAN:
  160. fprintf(fd, "(");
  161. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  162. fprintf(fd, " > ");
  163. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  164. fprintf(fd, ")");
  165. break;
  166. case AST_EQUAL:
  167. fprintf(fd, "(");
  168. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  169. fprintf(fd, " == ");
  170. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  171. fprintf(fd, ")");
  172. break;
  173. case AST_LESS_EQUAL:
  174. fprintf(fd, "(");
  175. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  176. fprintf(fd, " <= ");
  177. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  178. fprintf(fd, ")");
  179. break;
  180. case AST_MORE_EQUAL:
  181. fprintf(fd, "(");
  182. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  183. fprintf(fd, " >= ");
  184. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 1));
  185. fprintf(fd, ")");
  186. break;
  187. case AST_GROUP_EXPRESSIONS:
  188. fprintf(fd, "(");
  189. print_node(fd, (struct ast_node *) dd_da_get(&n->children, 0));
  190. fprintf(fd, ")");
  191. break;
  192. case AST_ARGUMENTS:
  193. for (int i = 0; i < n->children.elements; i++) {
  194. if (i != 0) fprintf(fd, ", ");
  195. print_node(fd, (struct ast_node *) dd_da_get(&n->children, i));
  196. }
  197. break;
  198. case AST_STRUCT:
  199. se = struct_entryat(n->value);
  200. // init struct's appearence
  201. fprintf(fd, "struct %s {\n", se->name);
  202. if (se->parent >= 0) {
  203. fprintf(fd, " struct %s parent;\n", struct_entryat(se->parent)->name);
  204. }
  205. // parse struct's children
  206. struct ast_node *group = dd_da_get(&n->children, 0);
  207. for (int i = 0; i < group->children.elements; i++) {
  208. struct ast_node *child = dd_da_get(&group->children, i);
  209. // parse functions as signatures
  210. if (child->node_type == AST_FUNCTION_DEFINITION) {
  211. e = symtable_entryat(child->value);
  212. // is not override function
  213. if (e->value == 0) {
  214. if (e->token == DD_KEYWORD_VOID) fprintf(fd, " void ");
  215. else if (e->token == DD_KEYWORD_INT) fprintf(fd, " int ");
  216. else fprintf(fd, " void ");
  217. fprintf(fd, "(*%s)(struct %s*", e->lexptr, se->name);
  218. if (child->children.elements > 0) fprintf(fd, ", ");
  219. print_node(fd, (struct ast_node *) dd_da_get(&child->children, 0));
  220. fprintf(fd, ");\n", e->lexptr, se->name);
  221. }
  222. }
  223. // parse everything else as normal
  224. else {
  225. print_node(fd, child);
  226. }
  227. }
  228. fprintf(fd, "};\n");
  229. // struct's functions
  230. for (int i = 0; i < group->children.elements; i++) {
  231. struct ast_node *child = dd_da_get(&group->children, i);
  232. if (child->node_type == AST_FUNCTION_DEFINITION) {
  233. e = symtable_entryat(child->value);
  234. if (strcmp(e->lexptr, "init") == 0) continue;
  235. fprintf(fd, "void %s_%s(struct %s *this) {\n", se->name, e->lexptr, se->name);
  236. print_node(fd, (struct ast_node *) dd_da_get(&child->children, 1));
  237. fprintf(fd, "}\n");
  238. }
  239. }
  240. // struct's initialiser
  241. fprintf(fd, "void %s_init(struct %s *this) {\n", se->name, se->name);
  242. // there is parent, so initialise that
  243. if (se->parent >= 0) {
  244. fprintf(fd, " %s_init(&this->parent);\n", struct_entryat(se->parent)->name);
  245. }
  246. // init all structs
  247. // init based on ast children?
  248. for (int i = 0; i < group->children.elements; i++) {
  249. struct ast_node *child = dd_da_get(&group->children, i);
  250. if (child->node_type == AST_FUNCTION_DEFINITION) {
  251. e = symtable_entryat(child->value);
  252. // override
  253. if (e->value != 0) {
  254. fprintf(fd, " this->parent.%s = %s_%s;\n", e->lexptr, se->name, e->lexptr);
  255. }
  256. else
  257. // not `init`
  258. if (strcmp(e->lexptr, "init") != 0) {
  259. fprintf(fd, " this->%s = %s_%s;\n", e->lexptr, se->name, e->lexptr);
  260. }
  261. // `init` function has special rules
  262. else {
  263. struct ast_node *func_child = dd_da_get(&child->children, 1);
  264. print_node(fd, func_child);
  265. }
  266. } else
  267. if (child->node_type == AST_DEFINITION) {
  268. struct ast_node *child2 = dd_da_get(&child->children, 0);
  269. e = symtable_entryat(child2->value);
  270. struct struct_entry *target_struct = struct_entryat(e->value);
  271. if (e->token == DD_KEYWORD_STRUCT) {
  272. fprintf(fd, " this->%s = %s_create();\n", e->lexptr, target_struct->name);
  273. }
  274. }
  275. }
  276. // close structs init
  277. fprintf(fd, "}\n");
  278. // struct's constructor
  279. fprintf(fd, "struct %s *%s_create() {\n", se->name, se->name);
  280. fprintf(fd, " struct %s *temp = malloc(sizeof(struct %s));\n", se->name, se->name);
  281. fprintf(fd, " %s_init(temp);\n", se->name, se->name);
  282. fprintf(fd, " return temp;\n");
  283. fprintf(fd, "}\n");
  284. break;
  285. /* if statement */
  286. case AST_IF:
  287. fprintf(fd, "if (");
  288. print_node(fd, dd_da_get(&n->children, 0));
  289. fprintf(fd, ") {\n");
  290. print_node(fd, dd_da_get(&n->children, 1));
  291. fprintf(fd, "}\n");
  292. break;
  293. } // switch
  294. }
  295. // responsible for creating a file and translating ast to target language
  296. void parse(const char *filename, struct ast_node *n) {
  297. tabcount = 0;
  298. fd_global = fopen(filename, "w");
  299. fprintf(fd_global, "#include <stdlib.h>\n");
  300. fprintf(fd_global, "#include \"world.h\"\n");
  301. fprintf(fd_global, "#include \"sprite.h\"\n");
  302. fprintf(fd_global, "#include \"vector2d.h\"\n");
  303. print_node(fd_global, n);
  304. fclose(fd_global);
  305. }