main.c 8.2 KB

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