main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 handle_breakpoint_option(int argc, char **argv, int i) {
  51. if (0 == strcmp(argv[i], "--break-point")) {
  52. if (i + 2 < argc) {
  53. uint32_t pc =
  54. pika_debug_find_break_point_pc(argv[i + 1], atoi(argv[i + 2]));
  55. // printf("break point pc: %d\r\n", pc);
  56. /* json output */
  57. printf("{\"pc\":%d}\r\n", pc);
  58. return 0;
  59. } else {
  60. printf("Invalid --break-point option usage.\n");
  61. return -1;
  62. }
  63. }
  64. return -1;
  65. }
  66. int isFileExists(const char *filename) {
  67. FILE *file = fopen(filename, "r");
  68. if (file == NULL) {
  69. // Failed to open the file, it may not exist or other error occurred
  70. return 0; // File does not exist or error
  71. }
  72. // File successfully opened, close it and return true
  73. fclose(file);
  74. return 1; // File exists
  75. }
  76. static int _do_main(int argc, char **argv) {
  77. int parc = argc - 1;
  78. PikaMaker *maker = New_PikaMaker();
  79. /* --add-file xxx --add-file yyy */
  80. // __platform_printf("parc: %d\r\n", parc);
  81. for (int i = 1; i < argc; i++) {
  82. // __platform_printf("%s\r\n", argv[i]);
  83. if (0 == strcmp(argv[i], "--add-file")) {
  84. // __platform_printf("add file: %s\r\n", argv[i + 1]);
  85. if (i + 1 < argc) {
  86. pikaMaker_linkRaw(maker, argv[i + 1]);
  87. }
  88. }
  89. }
  90. /* delete --xxx yyy */
  91. for (int i = 1; i < argc; i++) {
  92. int res = handle_breakpoint_option(argc, argv, i);
  93. if (0 == res) {
  94. return 0;
  95. }
  96. if (0 == strcmp(argv[i], "--add-file")) {
  97. // printf("before delete: %d\r\n", parc);
  98. // for (int j = 0; j < parc; j++) {
  99. // printf("%s\r\n", argv[j + 1]);
  100. // }
  101. parc -= 2;
  102. for (int j = i; j < argc - 2; j++) {
  103. argv[j] = argv[j + 2];
  104. }
  105. // printf("after delete: %d\r\n", parc);
  106. // for (int j = 0; j < parc; j++) {
  107. // printf("%s\r\n", argv[j + 1]);
  108. // }
  109. }
  110. }
  111. if (0 == parc) {
  112. /* no input, default to main.py */
  113. /* run pika_binder to bind C modules */
  114. pika_binder();
  115. if (isFileExists("module_list.txt")) {
  116. pikaMaker_compileModuleWithListFile(maker, "../module_list.txt");
  117. } else {
  118. pikaMaker_compileModuleWithListFile(maker, "module_list_default.txt");
  119. }
  120. PIKA_RES res = pikaMaker_linkCompiledModules(maker, "pikaModules.py.a");
  121. pikaMaker_deinit(maker);
  122. return res;
  123. }
  124. /* example: ./rust-msc-latest-linux -h | --help */
  125. if (1 == parc) {
  126. if (0 == strcmp(argv[1], "-h") || 0 == strcmp(argv[1], "--help")) {
  127. help(argv[0]);
  128. return 0;
  129. }
  130. }
  131. /* example: ./rust-msc-latest-linux main.py */
  132. if (1 == parc) {
  133. char *module_entry = argv[1];
  134. /* remove subfix */
  135. char *subfix = strrchr(module_entry, '.');
  136. if (subfix) {
  137. *subfix = '\0';
  138. }
  139. pikaMaker_compileModuleWithDepends(maker, module_entry);
  140. PIKA_RES res = pikaMaker_linkCompiledModules(maker, "pikaModules.py.a");
  141. pikaMaker_deinit(maker);
  142. return res;
  143. }
  144. /* example ./rust-msc-latest-linux main.py -o out.a */
  145. if (3 == parc) {
  146. if (0 == strcmp(argv[2], "-o")) {
  147. char *module_entry = argv[1];
  148. /* remove subfix */
  149. char *subfix = strrchr(module_entry, '.');
  150. if (subfix) {
  151. *subfix = '\0';
  152. }
  153. pikaMaker_compileModuleWithDepends(maker, module_entry);
  154. PIKA_RES res = pikaMaker_linkCompiledModules(maker, argv[3]);
  155. pikaMaker_deinit(maker);
  156. return res;
  157. }
  158. }
  159. /* example ./rust-msc-latest --docgen main.py */
  160. if (2 == parc) {
  161. if (0 == strcmp(argv[1], "--docgen")) {
  162. Args buffs = {0};
  163. char *file_path = argv[2];
  164. char *file_name = strsPathGetFileName(&buffs, file_path);
  165. printf("file_name: %s\r\n", file_name);
  166. char *file_folder = strsPathGetFolder(&buffs, file_path);
  167. char *file_name_no_ext = strsGetFirstToken(&buffs, file_name, '.');
  168. printf("file_name_no_ext: %s\r\n", file_name_no_ext);
  169. char *file_out_name = strsAppend(&buffs, file_name_no_ext, ".md");
  170. printf("file_out_name: %s\r\n", file_out_name);
  171. char *file_out_path = strsPathJoin(&buffs, file_folder, file_out_name);
  172. Parser *parser = parser_create();
  173. printf("generating doc %s: %s\r\n", file_path, file_out_path);
  174. parser_file2DocFile(parser, file_path, file_out_path);
  175. strsDeinit(&buffs);
  176. parser_deinit(parser);
  177. return 0;
  178. }
  179. }
  180. /* example: ./rust-msc-latest-linux -docgen main.py -o main.md */
  181. if (4 == parc) {
  182. if (0 == strcmp(argv[1], "--docgen") && 0 == strcmp(argv[3], "-o")) {
  183. Args buffs = {0};
  184. char *file_path = argv[2];
  185. char *file_out_path = argv[4];
  186. Parser *parser = parser_create();
  187. printf("generating doc %s: %s\r\n", file_path, file_out_path);
  188. parser_file2DocFile(parser, file_path, file_out_path);
  189. strsDeinit(&buffs);
  190. parser_deinit(parser);
  191. return 0;
  192. }
  193. }
  194. /* example: ./rust-msc-latest-linux -c main.py */
  195. if (2 == parc) {
  196. if (0 == strcmp(argv[1], "-c")) {
  197. Args buffs = {0};
  198. /* compile only */
  199. char *module_entry = argv[2];
  200. char *module_out = strsCopy(&buffs, module_entry);
  201. char *subfix = strrchr(module_out, '.');
  202. if (subfix) {
  203. *subfix = '\0';
  204. }
  205. module_out = strsAppend(&buffs, module_out, ".py.o");
  206. printf("compiling %s to %s...\r\n", module_entry, module_out);
  207. PIKA_RES res = pikaCompileFileWithOutputName(module_out, module_entry);
  208. strsDeinit(&buffs);
  209. return res;
  210. }
  211. }
  212. /* example: ./rust-msc-latest-linux -c main.py -o main.py.o */
  213. if (4 == parc) {
  214. if (0 == strcmp(argv[1], "-c") && 0 == strcmp(argv[3], "-o")) {
  215. /* compile only */
  216. char *module_entry = argv[2];
  217. printf("compiling %s to %s...\r\n", module_entry, argv[4]);
  218. PIKA_RES res = pikaCompileFileWithOutputName(argv[4], module_entry);
  219. return res;
  220. }
  221. }
  222. /* no valid input */
  223. printf("Invalid input.\r\n");
  224. help(argv[0]);
  225. return -1;
  226. }
  227. int main(int argc, char **argv) {
  228. char *buf = NULL;
  229. FILE *pika_studio = NULL;
  230. if (argc == 1) {
  231. pika_studio = __platform_fopen("pika.studio", "r");
  232. if (NULL != pika_studio) {
  233. buf = __platform_malloc(1024);
  234. __platform_fread(buf, 1, 1024, pika_studio);
  235. /* find <args> </args> */
  236. char *start = strstr(buf, "<args>");
  237. char *end = strstr(buf, "</args>");
  238. if (start && end) {
  239. start += 6;
  240. *end = '\0';
  241. __platform_printf("args: %s\r\n", start);
  242. /* split args */
  243. char *args[32] = {0};
  244. args[0] = argv[0];
  245. char *p = start;
  246. while (p < end) {
  247. char *q = strchr(p, ' ');
  248. if (q) {
  249. *q = '\0';
  250. args[argc++] = p;
  251. p = q + 1;
  252. } else {
  253. args[argc++] = p;
  254. break;
  255. }
  256. }
  257. argv = args;
  258. // for (int i = 0; i < argc; i++) {
  259. // __platform_printf("argv[%d]: %s\r\n", i, argv[i]);
  260. // }
  261. }
  262. }
  263. }
  264. int ret = _do_main(argc, argv);
  265. if (NULL != buf) {
  266. __platform_free(buf);
  267. }
  268. if (NULL != pika_studio) {
  269. __platform_fclose(pika_studio);
  270. }
  271. return ret;
  272. }