parser.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "parser.h"
  2. #include "stdlib.h"
  3. #include "stdio.h"
  4. #include "yacc.tab.h"
  5. #include "symtable.h"
  6. #include <string.h>
  7. #include "file_op.h"
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #define DD_BUFFER_SIZE 1000
  14. static char buffer[DD_BUFFER_SIZE];
  15. // flags
  16. static int has_semicolon = 1;
  17. void print_node(FILE *fd, struct ast_node *n);
  18. // file descriptor for global data
  19. FILE *fd_global;
  20. struct command_translation {
  21. char *command;
  22. char *translation;
  23. };
  24. void print_command(FILE *fd, struct ast_node *command);
  25. void print_echo(FILE *fd, struct ast_node *command);
  26. void print_definition(FILE *fd, struct ast_node *command);
  27. void print_operator_binary(FILE *fd, struct ast_node *command);
  28. void print_number(FILE *fd, struct ast_node *command);
  29. void print_function(FILE *fd, struct ast_node *command);
  30. void print_class(FILE *fd, struct ast_node *command);
  31. void print_return(FILE *fd, struct ast_node *command);
  32. void print_array(FILE *fd, struct ast_node *command);
  33. void print_function_call(FILE *fd, struct ast_node *command);
  34. void print_identifier(FILE *fd, struct ast_node *command);
  35. void print_new(FILE *fd, struct ast_node *command);
  36. void print_if(FILE *fd, struct ast_node *command);
  37. void print_if(FILE *fd, struct ast_node *command) {
  38. int cchild = 0;
  39. while (cchild < command->children.elements) {
  40. if (cchild != 0) {
  41. fprintf(fd, "else ");
  42. }
  43. if (command->children.elements -cchild >= 2) {
  44. fprintf(fd, "if (");
  45. has_semicolon = 0;
  46. print_node(fd, dd_da_get(&command->children, cchild));
  47. has_semicolon = 1;
  48. fprintf(fd, ")");
  49. cchild++;
  50. }
  51. fprintf(fd, " {\n");
  52. print_node(fd, dd_da_get(&command->children, cchild));
  53. fprintf(fd, "}\n");
  54. cchild++;
  55. }
  56. }
  57. void print_new(FILE *fd, struct ast_node *command) {
  58. fprintf(fd, "new ");
  59. print_node(fd, dd_da_get(&command->children, 0));
  60. fprintf(fd, "(");
  61. for (int i = 1; i < command->children.elements; i++) {
  62. if (i != 1) {
  63. fprintf(fd, ", ");
  64. }
  65. print_node(fd, dd_da_get(&command->children, i));
  66. }
  67. fprintf(fd, ")");
  68. }
  69. void print_function_call(FILE *fd, struct ast_node *command) {
  70. print_node(fd, dd_da_get(&command->children, 0));
  71. fprintf(fd, "(");
  72. for (int i = 1; i < command->children.elements; i++) {
  73. if (i != 1) {
  74. fprintf(fd, ", ");
  75. }
  76. struct ast_node *child = dd_da_get(&command->children, i);
  77. print_node(fd, child);
  78. }
  79. fprintf(fd, ")");
  80. if (has_semicolon) {
  81. fprintf(fd, ";\n");
  82. }
  83. }
  84. void print_number(FILE *fd, struct ast_node *command) {
  85. fprintf(fd, "%d", command->value);
  86. }
  87. void print_operator_binary(FILE *fd, struct ast_node *command) {
  88. struct entry *e = symtable_entryat(command->value);
  89. struct ast_node *child1 = dd_da_get(&command->children, 0);
  90. if (strcmp(e->lexptr, "=") != 0) {
  91. fprintf(fd, "(");
  92. }
  93. print_node(fd, child1);
  94. for (int i = 1; i < command->children.elements; i++) {
  95. fprintf(fd, " %s ", e->lexptr);
  96. struct ast_node *child2 = dd_da_get(&command->children, i);
  97. print_node(fd, child2);
  98. }
  99. if (strcmp(e->lexptr, "=") != 0) {
  100. fprintf(fd, ")");
  101. }
  102. }
  103. void print_definition(FILE *fd, struct ast_node *command) {
  104. struct ast_node *varname = dd_da_get(&command->children, 1);
  105. // if definition has children (like `this.x`) don't use `var`
  106. if (varname->children.elements == 0) {
  107. fprintf(fd, "var ");
  108. }
  109. print_node(fd, varname);
  110. if (command->children.elements >= 3) {
  111. fprintf(fd, " = ");
  112. print_node(fd, dd_da_get(&command->children, 2));
  113. }
  114. fprintf(fd, ";\n");
  115. }
  116. /* find out which command it is, and call the right function
  117. */
  118. void print_command(FILE *fd, struct ast_node *command) {
  119. struct entry *e = symtable_entryat(command->value);
  120. if (strcmp(e->lexptr, "echo") == 0) {
  121. print_echo(fd, command);
  122. }
  123. else
  124. if (strcmp(e->lexptr, "def") == 0) {
  125. print_definition(fd, command);
  126. }
  127. else
  128. if (strcmp(e->lexptr, "new") == 0) {
  129. print_new(fd, command);
  130. }
  131. else
  132. if (strcmp(e->lexptr, "=") == 0) {
  133. print_operator_binary(fd, command);
  134. fprintf(fd, ";\n");
  135. }
  136. else
  137. if (strcmp(e->lexptr, "+") == 0
  138. || strcmp(e->lexptr, "-") == 0
  139. || strcmp(e->lexptr, "*") == 0
  140. || strcmp(e->lexptr, "/") == 0
  141. || strcmp(e->lexptr, ">=") == 0
  142. || strcmp(e->lexptr, "==") == 0
  143. || strcmp(e->lexptr, "<=") == 0
  144. || strcmp(e->lexptr, "&&") == 0
  145. || strcmp(e->lexptr, "||") == 0
  146. || strcmp(e->lexptr, "<") == 0
  147. || strcmp(e->lexptr, ">") == 0) {
  148. print_operator_binary(fd, command);
  149. }
  150. else
  151. if (strcmp(e->lexptr, "function") == 0) {
  152. print_function(fd, command);
  153. }
  154. else
  155. if (strcmp(e->lexptr, "class") == 0) {
  156. print_class(fd, command);
  157. }
  158. else
  159. if (strcmp(e->lexptr, "return") == 0) {
  160. print_return(fd, command);
  161. }
  162. else
  163. if (strcmp(e->lexptr, "array") == 0) {
  164. print_array(fd, command);
  165. }
  166. else
  167. if (strcmp(e->lexptr, "if") == 0) {
  168. print_if(fd, command);
  169. }
  170. }
  171. void print_echo(FILE *fd, struct ast_node *command) {
  172. fprintf(fd, "console.log(");
  173. for (int i = 0; i < command->children.elements; i++) {
  174. if (i > 0) {
  175. fprintf(fd, " +");
  176. }
  177. print_node(fd, dd_da_get(&command->children, i));
  178. }
  179. fprintf(fd, ");\n");
  180. }
  181. void print_function(FILE *fd, struct ast_node *command) {
  182. struct entry *e = symtable_entryat(command->value);
  183. fprintf(fd, "this.");
  184. //name
  185. struct ast_node *name = dd_da_get(&command->children, 0);
  186. print_node(fd, name);
  187. fprintf(fd, " = function(");
  188. //arguments
  189. struct ast_node *arg = dd_da_get(&command->children, 1);
  190. print_node(fd, arg);
  191. fprintf(fd, ") {\n");
  192. //commands
  193. struct ast_node *cmd = dd_da_get(&command->children, 2);
  194. print_node(fd, cmd);
  195. fprintf(fd, "};\n");
  196. }
  197. void print_class(FILE *fd, struct ast_node *command) {
  198. fprintf(fd, "var ");
  199. // name
  200. struct ast_node *name = dd_da_get(&command->children, 0);
  201. print_node(fd, name);
  202. fprintf(fd, " = function() {\n");
  203. int cchild = 1;
  204. // subclass
  205. struct ast_node *subclass = dd_da_get(&command->children, 1);
  206. if (subclass->node_type == AST_IDENTIFIER) {
  207. struct entry *subentry = symtable_entryat(subclass->value);
  208. fprintf(fd, "%s.call(this);\n", subentry->lexptr);
  209. cchild++;
  210. }
  211. //components
  212. struct ast_node *cmn = dd_da_get(&command->children, cchild);
  213. print_node(fd, cmn);
  214. fprintf(fd, "}\n");
  215. }
  216. void print_return(FILE *fd, struct ast_node *command) {
  217. fprintf(fd, "return ");
  218. for (int i = 0; i < command->children.elements; i++) {
  219. print_node(fd, dd_da_get(&command->children, i));
  220. }
  221. fprintf(fd, ";\n");
  222. }
  223. void print_array(FILE *fd, struct ast_node *command) {
  224. fprintf(fd, "[\n");
  225. for (int i = 0; i < command->children.elements; i++) {
  226. if (i != 0) {
  227. fprintf(fd, ",\n");
  228. }
  229. print_node(fd, dd_da_get(&command->children, i));
  230. }
  231. fprintf(fd, "]");
  232. }
  233. void print_identifier(FILE *fd, struct ast_node *command) {
  234. struct entry *e = symtable_entryat(command->value);
  235. fprintf(fd, "%s", e->lexptr);
  236. for (int i = 0; i < command->children.elements; i++) {
  237. fprintf(fd, ".");
  238. print_node(fd, dd_da_get(&command->children, i));
  239. }
  240. }
  241. void print_node(FILE *fd, struct ast_node *n) {
  242. switch (n->node_type) {
  243. case AST_GAME:
  244. case AST_GROUP:
  245. for (int i = 0; i < n->children.elements; i++) {
  246. print_node(fd, dd_da_get(&n->children, i));
  247. }
  248. break;
  249. case AST_COMMAND_NATIVE: {
  250. struct entry *e = symtable_entryat(n->value);
  251. print_command(fd, n);
  252. break;
  253. }
  254. case AST_COMMAND_CUSTOM: {
  255. print_function_call(fd, n);
  256. break;
  257. }
  258. case AST_NUMBER: {
  259. print_number(fd, n);
  260. break;
  261. }
  262. case AST_STRING: {
  263. struct entry *e = symtable_entryat(n->value);
  264. fprintf(fd, "'%s'", e->lexptr);
  265. break;
  266. }
  267. case AST_IDENTIFIER: {
  268. print_identifier(fd, n);
  269. break;
  270. }
  271. }
  272. }
  273. // responsible for creating a file and translating ast to target language
  274. void parse_javascript(const char *filename, struct ast_node *n) {
  275. dir_create("build");
  276. fd_global = fopen(filename, "w");
  277. if (!fd_global) {
  278. printf("unable to open '%s': %s\n", filename, strerror(errno));
  279. return;
  280. }
  281. fprintf(fd_global, "var eng = new engine();\n");
  282. print_node(fd_global, n);
  283. fprintf(fd_global, "eng.setWorld(world_main);\n");
  284. fclose(fd_global);
  285. int dir = open("build", O_DIRECTORY);
  286. if (!dir) {
  287. printf("error opening `build/`: %s\n", strerror(errno));
  288. }
  289. file_copy_at(0, "/usr/share/ddlang/dd_pixi_engine.js", dir, "dd_pixi_engine.js", 0);
  290. file_copy_at(0, "/usr/share/ddlang/index.html", dir, "index.html", 0);
  291. file_copy_at(0, "/usr/share/ddlang/pixi.min.js", dir, "pixi.min.js", 0);
  292. dir_create("build/images");
  293. dir_copy_recursive(0, "images", dir, "images");
  294. close(dir);
  295. }