main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "symtable.h"
  4. #include "ast_node.h"
  5. #include "parser.h"
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <sys/stat.h>
  9. #include <time.h>
  10. #include "struct_table.h"
  11. #include "agc_commands.h"
  12. #include <errno.h>
  13. #include "file_op.h"
  14. #include "lexer.h"
  15. #include "semantic_analyser.h"
  16. extern float parsing_float;
  17. char included_files[10][100];
  18. int included_files_num = 0;
  19. // buffer for general use
  20. #define DD_BUFFER_SIZE 1000
  21. char buffer[DD_BUFFER_SIZE];
  22. // game node, parent of all nodes
  23. struct ast_node *game_node;
  24. #define MAX_INPUT_FILES 10
  25. enum AVDL_PLATFORM avdl_platform = AVDL_PLATFORM_NATIVE;
  26. const char *dependencies[] = {
  27. "GLEW",
  28. "m",
  29. "glut",
  30. "SDL2-2.0",
  31. "SDL2_mixer-2.0",
  32. };
  33. unsigned int dependencies_count = sizeof(dependencies) /sizeof(char *);
  34. char *includePath = 0;
  35. // init data, parse, exit
  36. int main(int argc, char *argv[])
  37. {
  38. /* tweakable data
  39. */
  40. int show_ast = 0;
  41. int show_struct_table = 0;
  42. char filename[MAX_INPUT_FILES][100];
  43. int input_file_total = 0;
  44. char *outname = 0;
  45. int getDependencies = 0;
  46. /*
  47. * phases
  48. *
  49. * translate: translate `.dd` files to `.c`
  50. * compile: compile `.c` files to `.o`
  51. * link: link all `.o` files to an executable
  52. *
  53. */
  54. int translate = 1;
  55. int compile = 1;
  56. int link = 1;
  57. // parse arguments
  58. for (int i = 1; i < argc; i++) {
  59. if (strcmp(argv[i], "--dependencies") == 0) {
  60. getDependencies = 1;
  61. }
  62. else
  63. // print abstract syntax tree
  64. if (strcmp(argv[i], "--print-ast") == 0) {
  65. show_ast = 1;
  66. }
  67. else
  68. // print struct table
  69. if (strcmp(argv[i], "--print-struct-table") == 0) {
  70. show_struct_table = 1;
  71. }
  72. else
  73. /* phase arguments
  74. * -t: translate only
  75. * -c: skip linking
  76. */
  77. if (strcmp(argv[i], "-t") == 0) {
  78. compile = 0;
  79. link = 0;
  80. }
  81. else
  82. if (strcmp(argv[i], "-c") == 0) {
  83. link = 0;
  84. }
  85. else
  86. // output filename
  87. if (strcmp(argv[i], "-o") == 0) {
  88. if (argc > i+1) {
  89. outname = argv[i+1];
  90. i++;
  91. }
  92. else {
  93. printf("avdl error: name is expected after `-o`\n");
  94. return -1;
  95. }
  96. }
  97. else
  98. // add include path
  99. if (strcmp(argv[i], "-I") == 0) {
  100. if (argc > i+1) {
  101. includePath = argv[i+1];
  102. i++;
  103. }
  104. else {
  105. printf("avdl error: include path is expected after `-I`\n");
  106. return -1;
  107. }
  108. }
  109. else
  110. // compiling for android
  111. if (strcmp(argv[i], "--android") == 0) {
  112. avdl_platform = AVDL_PLATFORM_ANDROID;
  113. link = 0;
  114. }
  115. else
  116. // input file
  117. if (input_file_total < MAX_INPUT_FILES) {
  118. strncpy(filename[input_file_total], argv[i], 100);
  119. filename[input_file_total][99] = '\0';
  120. input_file_total++;
  121. }
  122. // error argument
  123. else {
  124. printf("avdl error: '%s': Only %d input files can be provided at a time\n", argv[i], MAX_INPUT_FILES);
  125. return -1;
  126. }
  127. }
  128. // make sure the minimum to parse exists
  129. if (input_file_total <= 0) {
  130. printf("avdl error: No filename given\n");
  131. return -1;
  132. }
  133. if (getDependencies) {
  134. for (unsigned int i = 0; i < dependencies_count; i++) {
  135. strcpy(buffer, "ldd ");
  136. strcat(buffer, filename[0]);
  137. strcat(buffer, " | grep 'lib");
  138. strcat(buffer, dependencies[i]);
  139. strcat(buffer, "\\.' | sed 's/^.*=> \\(.*\\) .*$/\\1/'");
  140. //printf("command: %s\n", buffer);
  141. system(buffer);
  142. }
  143. return 0;
  144. }
  145. if (outname && !link && input_file_total > 1) {
  146. printf("avdl error: cannot supply `-o` with `-c` and multiple input files\n");
  147. return -1;
  148. }
  149. if (translate) {
  150. //printf("~~~ translation phase ~~~\n");
  151. }
  152. // translate all given input files
  153. for (int i = 0; i < input_file_total && translate; i++) {
  154. // check if file is meant to be compiled, or is already compiled
  155. if (strcmp(filename[i] +strlen(filename[i]) -3, ".dd") != 0) {
  156. continue;
  157. }
  158. included_files_num = 0;
  159. // initialise the parent node
  160. game_node = ast_create(AST_GAME, 0);
  161. semanticAnalyser_convertToAst(game_node, filename[i]);
  162. /*
  163. * if only transpiling, check output file
  164. */
  165. //printf("transpiling: %s", filename[i]);
  166. // parse resulting ast tree to a file
  167. if (!compile && !link && outname) {
  168. strcpy(buffer, outname);
  169. }
  170. else {
  171. filename[i][strlen(filename[i]) -2] = 'c';
  172. filename[i][strlen(filename[i]) -1] = '\0';
  173. strcpy(buffer, filename[i]);
  174. }
  175. //printf(" to %s\n", buffer);
  176. if (transpile_cglut(buffer, game_node) != 0) {
  177. printf("avdl: transpilation failed to file: %s\n", buffer);
  178. return -1;
  179. }
  180. // print debug data
  181. if (show_ast) {
  182. ast_print(game_node);
  183. }
  184. if (show_struct_table) {
  185. struct_table_print();
  186. }
  187. }
  188. if (compile) {
  189. //printf("~~~ compilation phase ~~~\n");
  190. }
  191. // compile all given input files
  192. for (int i = 0; i < input_file_total && compile; i++) {
  193. // check if file is meant to be compiled, or is already compiled
  194. if (strcmp(filename[i] +strlen(filename[i]) -2, ".c") != 0) {
  195. continue;
  196. }
  197. //printf("compiling: %s\n", filename[i]);
  198. // compile
  199. strcpy(buffer, "gcc -DDD_PLATFORM_NATIVE -c -w ");
  200. strcat(buffer, filename[i]);
  201. strcat(buffer, " -o ");
  202. if (outname && !link) {
  203. strcat(buffer, outname);
  204. }
  205. else {
  206. filename[i][strlen(filename[i]) -1] = 'o';
  207. strcat(buffer, filename[i]);
  208. }
  209. strcat(buffer, " -O3 -lGL -lGLU -lGLEW -lglut -lavdl-cengine -lm -w -lSDL2 -lSDL2_mixer");
  210. if (includePath) {
  211. strcat(buffer, " -I ");
  212. strcat(buffer, includePath);
  213. }
  214. //printf("command: %s\n", buffer);
  215. if (system(buffer)) {
  216. printf("avdl: error compiling file: %s\n", filename[i]);
  217. exit(-1);
  218. }
  219. }
  220. if (1) {
  221. //printf("~~~ asset phase ~~~\n");
  222. }
  223. // compile all given input files
  224. for (int i = 0; i < input_file_total && compile; i++) {
  225. // check if file is asset
  226. if (strcmp(filename[i] +strlen(filename[i]) -4, ".ply") != 0
  227. && strcmp(filename[i] +strlen(filename[i]) -4, ".bmp") != 0
  228. && strcmp(filename[i] +strlen(filename[i]) -4, ".wav") != 0
  229. && strcmp(filename[i] +strlen(filename[i]) -4, ".ogg") != 0) {
  230. continue;
  231. }
  232. if (outname) {
  233. strcpy(buffer, outname);
  234. }
  235. else {
  236. strcpy(buffer, filename[i]);
  237. buffer[strlen(buffer) -4] = '\0';
  238. strcat(buffer, ".asset");
  239. }
  240. //printf("compiling asset: %s to %s\n", filename[i], buffer);
  241. file_copy(filename[i], buffer, 0);
  242. }
  243. // compile the final executable
  244. if (link) {
  245. //printf("~~~ link phase ~~~\n");
  246. buffer[0] = '\0';
  247. sprintf(buffer, "gcc -DDD_PLATFORM_NATIVE ");
  248. for (int i = 0; i < input_file_total; i++) {
  249. strcat(buffer, filename[i]);
  250. strcat(buffer, " ");
  251. }
  252. strcat(buffer, "-O3 -lGL -lGLU -lGLEW -lglut -lavdl-cengine -lm -w -lSDL2 -lSDL2_mixer -lpthread -o ");
  253. if (outname) {
  254. strcat(buffer, outname);
  255. }
  256. else {
  257. strcat(buffer, "game");
  258. }
  259. //printf("command: %s\n", buffer);
  260. if (system(buffer)) {
  261. printf("avdl: error linking files\n");
  262. exit(-1);
  263. }
  264. }
  265. // success!
  266. return 0;
  267. }