main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "BaseObj.h"
  2. #include "PikaCompiler.h"
  3. #include "PikaObj.h"
  4. #include "PikaParser.h"
  5. #include "dataStrs.h"
  6. #include "libpikabinder.h"
  7. #include "pikascript/pikascript-core/dataStrs.h"
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. void help(char *argv0) {
  12. Args buffs = {0};
  13. char *exe = argv0;
  14. printf("Usage:\r\n"
  15. " %s"
  16. " - [Binding C modules and compile all from main.py]\r\n"
  17. " %s test.py"
  18. " - [Compile all from test.py]\r\n"
  19. " %s test.py -o out.a"
  20. " - [Compile all from test.py and link to out.a]\r\n"
  21. " %s -c test.py"
  22. " - [Only compile test.py to test.py.o]\r\n"
  23. " %s -c test.py -o out.o"
  24. " - [Only compile test.py to out.o]\r\n",
  25. exe, exe, exe, exe, exe);
  26. strsDeinit(&buffs);
  27. }
  28. /* fake implement */
  29. PikaObj *__pikaMain;
  30. void New_PikaStdData_List(void) {}
  31. void New_PikaStdData_Dict(void) {}
  32. void New_PikaStdData_Tuple(void) {}
  33. void New_PikaStdData_String(void) {}
  34. void New_PikaStdData_ByteArray(void) {}
  35. int strGetSizeUtf8(char *str) { return 0; }
  36. void PikaStdData_Tuple___init__(PikaObj *self) {}
  37. void PikaStdData_List___init__(PikaObj *self) {}
  38. void PikaStdData_List_append(PikaObj *self, Arg *arg) {}
  39. void PikaStdData_Dict___init__(PikaObj *self) {}
  40. void PikaStdData_Dict_set(PikaObj *self, char *key, Arg *val) {}
  41. PikaObj *New_builtins_object(Args *args){return NULL;};
  42. PikaObj *New_PikaStdLib_SysObj(Args *args) { return NULL; };
  43. PikaObj *New_builtins(Args *args) { return NULL; };
  44. PikaObj *New_builtins_RangeObj(Args *args) { return NULL; }
  45. PikaObj *New_PikaStdData_FILEIO(Args *args) { return NULL; }
  46. char *string_slice(Args *outBuffs, char *str, int start, int end) {
  47. return NULL;
  48. }
  49. int PikaStdData_FILEIO_init(PikaObj *self, char *path, char *mode) { return 0; }
  50. static int _do_main(int argc, char **argv) {
  51. int parc = argc - 1;
  52. PikaMaker *maker = New_PikaMaker();
  53. /* --add-file xxx --add-file yyy */
  54. // __platform_printf("parc: %d\r\n", parc);
  55. for (int i = 1; i < argc; i++) {
  56. // __platform_printf("%s\r\n", argv[i]);
  57. if (0 == strcmp(argv[i], "--add-file")) {
  58. // __platform_printf("add file: %s\r\n", argv[i + 1]);
  59. if (i + 1 < argc) {
  60. pikaMaker_linkRaw(maker, argv[i + 1]);
  61. }
  62. }
  63. }
  64. /* delete --xxx yyy */
  65. for (int i = 1; i < argc; i++) {
  66. if (0 == strcmp(argv[i], "--add-file")) {
  67. // printf("before delete: %d\r\n", parc);
  68. // for (int j = 0; j < parc; j++) {
  69. // printf("%s\r\n", argv[j + 1]);
  70. // }
  71. parc -= 2;
  72. for (int j = i; j < argc - 2; j++) {
  73. argv[j] = argv[j + 2];
  74. }
  75. // printf("after delete: %d\r\n", parc);
  76. // for (int j = 0; j < parc; j++) {
  77. // printf("%s\r\n", argv[j + 1]);
  78. // }
  79. }
  80. }
  81. if (0 == parc) {
  82. /* no input, default to main.py */
  83. /* run pika_binder to bind C modules */
  84. pika_binder();
  85. pikaMaker_compileModuleWithDepends(maker, "main");
  86. PIKA_RES res = pikaMaker_linkCompiledModules(maker, "pikaModules.py.a");
  87. pikaMaker_deinit(maker);
  88. return res;
  89. }
  90. /* example: ./rust-msc-latest-linux -h | --help */
  91. if (1 == parc) {
  92. if (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help")) {
  93. help(argv[0]);
  94. return 0;
  95. }
  96. }
  97. /* example: ./rust-msc-latest-linux main.py */
  98. if (1 == parc) {
  99. char *module_entry = argv[1];
  100. /* remove subfix */
  101. char *subfix = strrchr(module_entry, '.');
  102. if (subfix) {
  103. *subfix = '\0';
  104. }
  105. pikaMaker_compileModuleWithDepends(maker, module_entry);
  106. PIKA_RES res = pikaMaker_linkCompiledModules(maker, "pikaModules.py.a");
  107. pikaMaker_deinit(maker);
  108. return res;
  109. }
  110. /* example ./rust-msc-latest-linux main.py -o out.a */
  111. if (3 == parc) {
  112. if (0 == strcmp(argv[2], "-o")) {
  113. char *module_entry = argv[1];
  114. /* remove subfix */
  115. char *subfix = strrchr(module_entry, '.');
  116. if (subfix) {
  117. *subfix = '\0';
  118. }
  119. pikaMaker_compileModuleWithDepends(maker, module_entry);
  120. PIKA_RES res = pikaMaker_linkCompiledModules(maker, argv[3]);
  121. pikaMaker_deinit(maker);
  122. return res;
  123. }
  124. }
  125. /* example ./rust-msc-latest --docgen main.py */
  126. if (2 == parc) {
  127. if (0 == strcmp(argv[1], "--docgen")) {
  128. Args buffs = {0};
  129. char *file_path = argv[2];
  130. char *file_name = strsPathGetFileName(&buffs, file_path);
  131. printf("file_name: %s\r\n", file_name);
  132. char *file_folder = strsPathGetFolder(&buffs, file_path);
  133. char *file_name_no_ext = strsGetFirstToken(&buffs, file_name, '.');
  134. printf("file_name_no_ext: %s\r\n", file_name_no_ext);
  135. char *file_out_name = strsAppend(&buffs, file_name_no_ext, ".md");
  136. printf("file_out_name: %s\r\n", file_out_name);
  137. char *file_out_path = strsPathJoin(&buffs, file_folder, file_out_name);
  138. Parser *parser = New_parser();
  139. printf("generating doc %s: %s\r\n", file_path, file_out_path);
  140. parser_file2DocFile(parser, file_path, file_out_path);
  141. strsDeinit(&buffs);
  142. parser_deinit(parser);
  143. return 0;
  144. }
  145. }
  146. /* example: ./rust-msc-latest-linux -docgen main.py -o main.md */
  147. if (4 == parc) {
  148. if (0 == strcmp(argv[1], "--docgen") && 0 == strcmp(argv[3], "-o")) {
  149. Args buffs = {0};
  150. char *file_path = argv[2];
  151. char *file_out_path = argv[4];
  152. Parser *parser = New_parser();
  153. printf("generating doc %s: %s\r\n", file_path, file_out_path);
  154. parser_file2DocFile(parser, file_path, file_out_path);
  155. strsDeinit(&buffs);
  156. parser_deinit(parser);
  157. return 0;
  158. }
  159. }
  160. /* example: ./rust-msc-latest-linux -c main.py */
  161. if (2 == parc) {
  162. if (0 == strcmp(argv[1], "-c")) {
  163. Args buffs = {0};
  164. /* compile only */
  165. char *module_entry = argv[2];
  166. char *module_out = strsCopy(&buffs, module_entry);
  167. char *subfix = strrchr(module_out, '.');
  168. if (subfix) {
  169. *subfix = '\0';
  170. }
  171. module_out = strsAppend(&buffs, module_out, ".py.o");
  172. printf("compiling %s to %s...\r\n", module_entry, module_out);
  173. PIKA_RES res = pikaCompileFileWithOutputName(module_out, module_entry);
  174. strsDeinit(&buffs);
  175. return res;
  176. }
  177. }
  178. /* example: ./rust-msc-latest-linux -c main.py -o main.py.o */
  179. if (4 == parc) {
  180. if (0 == strcmp(argv[1], "-c") && 0 == strcmp(argv[3], "-o")) {
  181. /* compile only */
  182. char *module_entry = argv[2];
  183. printf("compiling %s to %s...\r\n", module_entry, argv[4]);
  184. PIKA_RES res = pikaCompileFileWithOutputName(argv[4], module_entry);
  185. return res;
  186. }
  187. }
  188. /* no valid input */
  189. printf("Invalid input.\r\n");
  190. help(argv[0]);
  191. return -1;
  192. }
  193. int main(int argc, char **argv) {
  194. char *buf = NULL;
  195. FILE *pika_studio = NULL;
  196. if (argc == 1) {
  197. pika_studio = __platform_fopen("pika.studio", "r");
  198. if (NULL != pika_studio) {
  199. buf = __platform_malloc(1024);
  200. __platform_fread(buf, 1, 1024, pika_studio);
  201. /* find <args> </args> */
  202. char *start = strstr(buf, "<args>");
  203. char *end = strstr(buf, "</args>");
  204. if (start && end) {
  205. start += 6;
  206. *end = '\0';
  207. __platform_printf("args: %s\r\n", start);
  208. /* split args */
  209. char *args[32] = {0};
  210. args[0] = argv[0];
  211. char *p = start;
  212. while (p < end) {
  213. char *q = strchr(p, ' ');
  214. if (q) {
  215. *q = '\0';
  216. args[argc++] = p;
  217. p = q + 1;
  218. } else {
  219. args[argc++] = p;
  220. break;
  221. }
  222. }
  223. argv = args;
  224. // for (int i = 0; i < argc; i++) {
  225. // __platform_printf("argv[%d]: %s\r\n", i, argv[i]);
  226. // }
  227. }
  228. }
  229. }
  230. int ret = _do_main(argc, argv);
  231. if (NULL != buf) {
  232. __platform_free(buf);
  233. }
  234. if (NULL != pika_studio) {
  235. __platform_fclose(pika_studio);
  236. }
  237. return ret;
  238. }