main.c 8.3 KB

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