parser.c 11 KB

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