spacepaste

  1.  
  2. #include "../Shell/builtins/printfmacro.h"
  3. #include "mpc/mpc.h"
  4. #include "mpc/mpc.c"
  5. #include <stdbool.h>
  6. #include "../Shell/builtins/libstring.h"
  7. #include "../Shell/builtins/regex_str.h"
  8. #include "../Shell/builtins/env.h"
  9. char * outfile = "tmpfile.c";
  10. static void AP(mpc_ast_t *a, int d, FILE *fp) {
  11. int i;
  12. if (a == NULL) {
  13. fprintf(fp, "NULL\n");
  14. return;
  15. }
  16. for (i = 0; i < d; i++) { fprintf(fp, " "); }
  17. if (strlen(a->contents)) {
  18. fprintf(fp, "tag: '%s', contents: '%s'\n", a->tag,
  19. a->contents);
  20. } else {
  21. fprintf(fp, "tag: '%s' \n", a->tag);
  22. }
  23. for (i = 0; i < a->children_num; i++) {
  24. AP(a->children[i], d+1, fp);
  25. }
  26. }
  27. bool find_tree_string(mpc_ast_t *a, char * string) {
  28. int i;
  29. if (a == NULL) {
  30. printf("NULL\n");
  31. return false;
  32. }
  33. if (strcmp(a->contents, string) == 0) {
  34. str_new(tmp);
  35. str_insert_string(tmp, " puts(\"");
  36. str_insert_string(tmp, a->contents);
  37. str_insert_string(tmp, " \");\n");
  38. str_output_append(tmp, outfile);
  39. str_free(tmp);
  40. return true;
  41. }
  42. return false;
  43. }
  44. bool find_tree_string2(mpc_ast_t *a) {
  45. int i;
  46. if (a == NULL) {
  47. printf("NULL\n");
  48. return false;
  49. }
  50. if (strcmp(a->tag, "ISA_DEFINE|word|regex") == 0 || strcmp(a->tag, "word|regex") == 0 ) {
  51. str_new(tmp);
  52. str_insert_string(tmp, " puts(\"");
  53. str_insert_string(tmp, a->contents);
  54. str_insert_string(tmp, " \");\n");
  55. str_output_append(tmp, outfile);
  56. str_free(tmp);
  57. return true;
  58. } else if (strcmp(a->tag, "ISA_DEFINE|>") == 0) {
  59. for (i = 0; i < a->children_num; i++) {
  60. if (find_tree_string2(a->children[i]) == true) return true;
  61. }
  62. }
  63. return false;
  64. }
  65. int find_definition_strings_called = 0;
  66. bool find_definition_strings(mpc_ast_t *a, bool omit_else) {
  67. if (a == NULL) {
  68. printf("NULL\n");
  69. return false;
  70. }
  71. if (strcmp(a->tag, "word|regex") == 0) {
  72. find_definition_strings_called++;
  73. printf("%s\n", a->contents);
  74. str_new(tmp);
  75. if (omit_else == false) if (find_definition_strings_called >1) str_insert_string(tmp, "else ");
  76. str_insert_string(tmp, "if (!");
  77. str_insert_string(tmp, "regexEngineb(");
  78. str_insert_int(tmp, strlen(a->contents));
  79. str_insert_string(tmp, ", ");
  80. str_insert_int(tmp, strlen(a->contents));
  81. str_insert_string(tmp, ", \"");
  82. if (strlen(a->contents) != 1) {
  83. str_insert_int(tmp, strlen(a->contents)-1);
  84. str_insert_string(tmp, "-");
  85. }
  86. str_insert_string(tmp, "0,");
  87. str_insert_string(tmp, a->contents);
  88. str_insert_string(tmp, "\", binstr.data, ");
  89. if (find_definition_strings_called >1) { str_insert_string(tmp, "true"); }
  90. else { str_insert_string(tmp, "false"); }
  91. str_insert_string(tmp, ")");
  92. str_insert_string(tmp, ") {\n");
  93. str_insert_string(tmp, " printf(\"a->contents = ");
  94. str_insert_string(tmp, a->contents);
  95. str_insert_string(tmp, ", regexEngineb_bits = %s\\n\", regexEngineb_bits);\n");
  96. str_insert_string(tmp, "}\n");
  97. str_output_append(tmp, outfile);
  98. str_free(tmp);
  99. }
  100. return false;
  101. }
  102. bool find_definition_items(mpc_ast_t *a) {
  103. int i;
  104. if (a == NULL) {
  105. printf("NULL\n");
  106. return false;
  107. }
  108. if (strcmp(a->tag, "item|bracketed_item|>") == 0) {
  109. for (i = 0; i < a->children_num; i++) {
  110. find_definition_strings(a->children[i], true);
  111. }
  112. }
  113. else if (strcmp(a->tag, "item|normal_item|>") == 0) {
  114. for (i = 0; i < a->children_num; i++) {
  115. find_definition_strings(a->children[i], false);
  116. }
  117. }
  118. else {
  119. for (i = 0; i < a->children_num; i++) {
  120. find_definition_items(a->children[i]);
  121. }
  122. }
  123. return false;
  124. }
  125. bool find_definition_statements(mpc_ast_t *a) {
  126. int i;
  127. if (a == NULL) {
  128. printf("NULL\n");
  129. return false;
  130. }
  131. if (strcmp(a->tag, "definition_statement|>") == 0) {
  132. for (i = 0; i < a->children_num; i++) {
  133. find_definition_items(a->children[i]);
  134. }
  135. }
  136. else {
  137. for (i = 0; i < a->children_num; i++) {
  138. find_definition_statements(a->children[i]);
  139. }
  140. }
  141. return false;
  142. }
  143. bool find_definition_root(mpc_ast_t *a, char * string) {
  144. int i;
  145. if (a == NULL) {
  146. printf("NULL\n");
  147. return false;
  148. }
  149. if (strcmp(a->tag, "root|definition|>") == 0) {
  150. find_definition_strings_called = 0;
  151. for (i = 0; i < a->children_num; i++) {
  152. if (find_definition_root(a->children[i], string) == true) find_definition_statements(a);
  153. }
  154. find_definition_strings_called = 0;
  155. }
  156. else if (strcmp(a->tag, "definition_start|>") == 0) {
  157. for (i = 0; i < a->children_num; i++) {
  158. if (find_tree_string(a->children[i], string) == true) return true;
  159. }
  160. }
  161. else {
  162. for (i = 0; i < a->children_num; i++) {
  163. find_definition_root(a->children[i], string);
  164. }
  165. }
  166. return false;
  167. }
  168. bool find_instruction_root(mpc_ast_t *a) {
  169. int i;
  170. if (a == NULL) {
  171. printf("NULL\n");
  172. return false;
  173. }
  174. if (strcmp(a->tag, "root|instruction|>") == 0) {
  175. for (i = 0; i < a->children_num; i++) {
  176. find_instruction_root(a->children[i]);
  177. }
  178. }
  179. else if (strcmp(a->tag, "instruction_start|>") == 0) {
  180. for (i = 0; i < a->children_num; i++) {
  181. if (find_tree_string2(a->children[i]) == true) return true;
  182. }
  183. }
  184. else {
  185. for (i = 0; i < a->children_num; i++) {
  186. find_instruction_root(a->children[i]);
  187. }
  188. }
  189. return false;
  190. }
  191. void aquire_action_code(mpc_ast_t *a, struct regex_string * r) {
  192. if (a->children_num) {
  193. str_reg(a->children[0]->tag);
  194. if (str_is("name|>") || str_is("num|>") || str_is("indexedname|>") || str_is("name_segmented|>") || str_is("num_segmented|>") || str_is("indexedname_segmented|>")) return;
  195. }
  196. str_insert_string(*r, "/*ACTION CODE BEGIN*/");
  197. int i = 0;
  198. for (i = 0; i < a->children_num; i++) {
  199. str_reg(a->children[i]->tag);
  200. if (str_is("regex")) {
  201. if (a->children[i]->contents != NULL) {
  202. str_insert_string(*r, a->children[i]->contents);
  203. }
  204. }
  205. }
  206. str_insert_string(*r, "/*ACTION CODE END*/");
  207. }
  208. struct action_code_builder_object_references {
  209. int argv_id; // argv index
  210. int * content_id; // content index indexes
  211. int content_amount;
  212. int occurence_number;
  213. int instances;
  214. };
  215. struct action_code_builder_references {
  216. int argc;
  217. env_t argv;
  218. struct action_code_builder_object_references * ref;
  219. struct action_code_builder_content * content;
  220. int instances;
  221. };
  222. struct action_code_builder_content {
  223. struct regex_string * content; // content of action code
  224. int instances;
  225. };
  226. struct action_code_builder {
  227. struct action_code_builder_references * references;
  228. int instances;
  229. } * action_code_builder_struct;
  230. int action_code_builder_argument_index = 0;
  231. void Build_Action_Code_Structure_Quick_Scan(mpc_ast_t *a, int * Build_Action_Code_Structure_Quick_Scan_Matches) {
  232. int i;
  233. if (a == NULL) {
  234. return;
  235. }
  236. bool is_an_action_structure = false;
  237. bool is_an_action_segmented_structure = false;
  238. bool has_tag = false;
  239. if (strlen(a->tag)) has_tag = true;
  240. if (has_tag) {
  241. if (strcmp(a->tag, "action_code|>") == 0) is_an_action_structure = true;
  242. else if (strcmp(a->tag, "action_code_segmented|>") == 0) is_an_action_segmented_structure = true;
  243. }
  244. if (!is_an_action_structure && !is_an_action_segmented_structure) for (i = 0; i < a->children_num; i++) {
  245. Build_Action_Code_Structure_Quick_Scan(a->children[i], Build_Action_Code_Structure_Quick_Scan_Matches);
  246. }
  247. else (*Build_Action_Code_Structure_Quick_Scan_Matches)++;
  248. }
  249. struct binder {
  250. int bind_index;
  251. bool bind_segmented;
  252. int bind_type;
  253. char * target_content_name;
  254. int instances;
  255. } * action_code_binder;
  256. int action_code_binder_skip_next = 0;
  257. #define BIND_NONE -1
  258. #define BIND_NAME 1
  259. #define BIND_NUM 2
  260. #define BIND_FUSED 3
  261. void Build_Action_Code_Structure(mpc_ast_t *a, int d, bool is_sub_contents_part_of_action, bool renew) {
  262. if (renew) {
  263. if (action_code_builder_struct == NULL) {
  264. action_code_builder_struct = malloc(sizeof(struct action_code_builder)*1);
  265. action_code_builder_struct->instances = 1;
  266. action_code_builder_struct[action_code_builder_struct->instances-1].references = malloc(sizeof(struct action_code_builder_references)*1);
  267. action_code_builder_struct[action_code_builder_struct->instances-1].references->instances = 1;
  268. action_code_builder_struct[action_code_builder_struct->instances-1].references->argc = 0;
  269. action_code_builder_struct[action_code_builder_struct->instances-1].references->argv = NULL;
  270. action_code_builder_struct[action_code_builder_struct->instances-1].references->content = malloc(sizeof(struct action_code_builder_content)*1);
  271. action_code_builder_struct[action_code_builder_struct->instances-1].references->content->instances = 1;
  272. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref = malloc(sizeof(struct action_code_builder_object_references)*1);
  273. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances = 1;
  274. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->argv_id = -1;
  275. }
  276. else {
  277. action_code_builder_struct = realloc(action_code_builder_struct, sizeof(struct action_code_builder)*(action_code_builder_struct->instances+1));
  278. action_code_builder_struct->instances++;
  279. action_code_builder_struct[action_code_builder_struct->instances-1].references = malloc(sizeof(struct action_code_builder_references)*1);
  280. action_code_builder_struct[action_code_builder_struct->instances-1].references->instances = 1;
  281. action_code_builder_struct[action_code_builder_struct->instances-1].references->argc = 0;
  282. action_code_builder_struct[action_code_builder_struct->instances-1].references->argv = NULL;
  283. action_code_builder_struct[action_code_builder_struct->instances-1].references->content = malloc(sizeof(struct action_code_builder_content)*1);
  284. action_code_builder_struct[action_code_builder_struct->instances-1].references->content->instances = 1;
  285. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref = malloc(sizeof(struct action_code_builder_object_references)*1);
  286. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances = 1;
  287. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->argv_id = -1;
  288. }
  289. }
  290. int i;
  291. if (a == NULL) {
  292. return;
  293. }
  294. bool is_an_action = false;
  295. bool is_an_action_structure = false;
  296. bool is_an_action_segmented_structure = false;
  297. bool has_tag = false;
  298. bool has_contents = false;
  299. if (strlen(a->tag)) has_tag = true;
  300. if (strlen(a->contents)) has_contents = true;
  301. if (has_tag) {
  302. if (strcmp(a->tag, "action|>") == 0) is_an_action = true;
  303. else if (strcmp(a->tag, "action_code|>") == 0) is_an_action_structure = true;
  304. else if (strcmp(a->tag, "action_code_segmented|>") == 0) is_an_action_segmented_structure = true;
  305. if (is_an_action_segmented_structure == true || is_an_action_structure == true) is_sub_contents_part_of_action = true;
  306. if (is_sub_contents_part_of_action == false && (strcmp(a->tag, "ISA_DEFINE|word|regex") == 0 || strcmp(a->tag, "ISA_RULE|word|regex") == 0 || strcmp(a->tag, "word|regex") == 0 || strcmp(a->tag, "binary|regex") == 0)) {
  307. if (action_code_builder_struct[action_code_builder_struct->instances-1].references->ref != NULL && action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->argv_id != -1) {
  308. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref = realloc(
  309. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref,
  310. sizeof(
  311. struct action_code_builder_object_references
  312. )
  313. *
  314. (
  315. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances+1
  316. )
  317. );
  318. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances++;
  319. }
  320. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances-1].content_id = NULL;
  321. action_code_builder_struct[action_code_builder_struct->instances-1].references->argc++;
  322. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances-1].argv_id = action_code_builder_struct[action_code_builder_struct->instances-1].references->argc-1;
  323. action_code_builder_struct[action_code_builder_struct->instances-1].references->argv = env__add2(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv, a->contents);
  324. int occurence = 0, tag_index = action_code_builder_struct[action_code_builder_struct->instances-1].references->argc-1;
  325. for (int i = 0; i < tag_index; i++) if (strcmp(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[i], action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[tag_index]) == 0) occurence++;
  326. pi(occurence)
  327. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances-1].occurence_number = occurence;
  328. ps(a->contents)
  329. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->argc-1)
  330. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances-1].argv_id)
  331. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[0].argv_id)
  332. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances-1)
  333. }
  334. }
  335. if (is_sub_contents_part_of_action) {
  336. // how to build the structure of a reference...
  337. if (has_tag) {
  338. // try to get tag type
  339. str_reg(a->tag);
  340. if (str_is("name|>")) {
  341. if (action_code_binder_skip_next != 0) {
  342. action_code_binder_skip_next--;
  343. } else {
  344. puts("BINDING");
  345. if (action_code_binder == NULL) {
  346. action_code_binder = malloc(sizeof(struct binder)*1);
  347. action_code_binder->instances = 1;
  348. action_code_binder_skip_next = 0;
  349. }
  350. else {
  351. action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1));
  352. action_code_binder->instances++;
  353. }
  354. action_code_binder[action_code_binder->instances-1].target_content_name = a->children[1]->contents;
  355. action_code_binder[action_code_binder->instances-1].bind_index = 0;
  356. action_code_binder[action_code_binder->instances-1].bind_type = BIND_NAME;
  357. }
  358. }
  359. else if (str_is("num|>")) {
  360. if (action_code_binder_skip_next != 0) {
  361. action_code_binder_skip_next--;
  362. } else {
  363. puts("BINDING");
  364. if (action_code_binder == NULL) {
  365. action_code_binder = malloc(sizeof(struct binder)*1);
  366. action_code_binder->instances = 1;
  367. action_code_binder_skip_next = 0;
  368. }
  369. else {
  370. action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1));
  371. action_code_binder->instances++;
  372. }
  373. action_code_binder[action_code_binder->instances-1].bind_index= atoi(a->children[1]->contents);
  374. action_code_binder[action_code_binder->instances-1].bind_type = BIND_NUM;
  375. }
  376. }
  377. else if (str_is("indexedname|>")) {
  378. if (action_code_binder_skip_next != 0) {
  379. action_code_binder_skip_next--;
  380. } else {
  381. puts("BINDING");
  382. if (action_code_binder == NULL) {
  383. action_code_binder = malloc(sizeof(struct binder)*1);
  384. action_code_binder->instances = 1;
  385. action_code_binder_skip_next = 0;
  386. }
  387. else {
  388. action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1));
  389. action_code_binder->instances++;
  390. }
  391. action_code_binder[action_code_binder->instances-1].target_content_name = a->children[1]->contents;
  392. action_code_binder[action_code_binder->instances-1].bind_index= atoi(a->children[2]->children[1]->contents);
  393. action_code_binder[action_code_binder->instances-1].bind_type = BIND_FUSED;
  394. action_code_binder_skip_next = 1;
  395. }
  396. }
  397. else if (str_is("name_segmented|>")) {
  398. puts("BINDING");
  399. if (action_code_binder == NULL) {
  400. action_code_binder = malloc(sizeof(struct binder)*1);
  401. action_code_binder->instances = 1;
  402. }
  403. else {
  404. action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1));
  405. action_code_binder->instances++;
  406. }
  407. }
  408. else if (str_is("num_segmented|>")) {
  409. puts("BINDING");
  410. if (action_code_binder == NULL) {
  411. action_code_binder = malloc(sizeof(struct binder)*1);
  412. action_code_binder->instances = 1;
  413. }
  414. else {
  415. action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1));
  416. action_code_binder->instances++;
  417. }
  418. }
  419. else if (str_is("indexedname_segmented|>")) {
  420. puts("BINDING");
  421. if (action_code_binder == NULL) {
  422. action_code_binder = malloc(sizeof(struct binder)*1);
  423. action_code_binder->instances = 1;
  424. }
  425. else {
  426. action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1));
  427. action_code_binder->instances++;
  428. }
  429. }
  430. if (is_an_action && action_code_binder != NULL) {
  431. for (int ii = 0; ii < action_code_binder->instances; ii++) {
  432. if (action_code_binder[ii].bind_type == BIND_NAME) {
  433. ps(action_code_binder[ii].target_content_name);
  434. int tag_index = action_code_builder_struct[action_code_builder_struct->instances-1].references->argc-1;
  435. for (int i = 0; i < action_code_builder_struct[action_code_builder_struct->instances-1].references->argc; i++) {
  436. if (strcmp(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[i], action_code_binder[ii].target_content_name) == 0) {
  437. ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[i].argv_id]);
  438. if (action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[i].occurence_number == action_code_binder[ii].bind_index) {
  439. action_code_binder[ii].bind_index = i;
  440. break;
  441. }
  442. }
  443. }
  444. pi(action_code_binder[ii].bind_index)
  445. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].argv_id);
  446. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].occurence_number);
  447. ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].argv_id]);
  448. }
  449. else if (action_code_binder[ii].bind_type == BIND_NUM) {
  450. pi(action_code_binder[ii].bind_index)
  451. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].argv_id);
  452. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].occurence_number);
  453. ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].argv_id]);
  454. }
  455. else if (action_code_binder[ii].bind_type == BIND_FUSED) {
  456. ps(action_code_binder[ii].target_content_name);
  457. int tag_index = action_code_builder_struct[action_code_builder_struct->instances-1].references->argc-1;
  458. for (int i = 0; i < action_code_builder_struct[action_code_builder_struct->instances-1].references->argc; i++) {
  459. if (strcmp(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[i], action_code_binder[ii].target_content_name) == 0) {
  460. ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[i].argv_id]);
  461. if (action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[i].occurence_number == action_code_binder[ii].bind_index) {
  462. action_code_binder[ii].bind_index = i;
  463. break;
  464. }
  465. }
  466. }
  467. pi(action_code_binder[ii].bind_index)
  468. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].argv_id);
  469. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].occurence_number);
  470. ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].argv_id]);
  471. }
  472. }
  473. // get action code contents
  474. str_new(actioncode);
  475. aquire_action_code(a, &actioncode);
  476. action_code_builder_struct[action_code_builder_struct->instances-1].references->content->content = &actioncode;
  477. ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->content->content->string);
  478. /*
  479. *
  480. * unsure about exact operation mode
  481. *
  482. * #0${NULL#0}$NULL$1 {/*NULL1*\/} {/*NULL2*\/}
  483. *
  484. * >>>
  485. *
  486. * #0 > arg id 0, first occurence
  487. * #0 > condent id 0 > {/*NULL1*\/}
  488. * #0 > condent id 1 > {/*NULL2*\/}
  489. *
  490. * ${NULL#0} > arg id 0, first occurence
  491. * ${NULL#0} > condent id 0 > {/*NULL1*\/}
  492. * ${NULL#0} > condent id 1 > {/*NULL2*\/}
  493. *
  494. * $NULL > arg id 0, first occurence
  495. * $NULL > condent id 0 > {/*NULL1*\/}
  496. * $NULL > condent id 1 > {/*NULL2*\/}
  497. *
  498. * #1 > arg id 1, first occurence
  499. * #1 > condent id 0 > {/*NULL1*\/}
  500. * #1 > condent id 1 > {/*NULL2*\/}
  501. *
  502. * each paramater is assigned a reference containing an argv id and a content id array
  503. *
  504. * */
  505. // implements multi action code storage (operation of the content id array), probably does not function correctly
  506. for (int ii = 0; ii < action_code_binder->instances; ii++) {
  507. pi(action_code_binder[ii].bind_index)
  508. if (action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_id == NULL) {
  509. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_id = malloc(sizeof(int)*1);
  510. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_amount = 1;
  511. }
  512. else {
  513. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_id = realloc(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_id, sizeof(int)*(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_amount+1));
  514. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_amount++;
  515. }
  516. action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_id[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_amount-1] = action_code_binder[ii].bind_index;
  517. }
  518. for (int ii = 0; ii < action_code_binder->instances; ii++) {
  519. for (int iii = 0; iii < action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_amount; iii++) {
  520. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_amount)
  521. pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_id[iii])
  522. }
  523. }
  524. if (action_code_binder != NULL) {
  525. free(action_code_binder);
  526. action_code_binder = NULL;
  527. }
  528. };
  529. if (has_contents) {
  530. for (i = 0; i < d; i++) { printf(" "); }
  531. printf("tag: '%s', contents: '%s'\n", a->tag, a->contents);
  532. }
  533. else {
  534. for (i = 0; i < d; i++) { printf(" "); }
  535. printf("tag: '%s'\n", a->tag);
  536. }
  537. }
  538. else if (has_contents) {
  539. for (i = 0; i < d; i++) { printf(" "); }
  540. printf("contents: '%s'\n", a->contents);
  541. }
  542. }
  543. for (i = 0; i < a->children_num; i++) {
  544. Build_Action_Code_Structure(a->children[i], d+1, is_sub_contents_part_of_action, false);
  545. }
  546. }
  547. int number_ifs = 0;
  548. mpc_ast_t *ra = NULL;
  549. int instructions = 0;
  550. mpc_ast_t *prev;
  551. bool isimportant = false;
  552. int tag_index = 0;
  553. bool has_action_structures = false;
  554. void find_instructions(mpc_ast_t *a, mpc_ast_t *orig, int d, bool is_sub_contents_part_of_instruction, bool is_sub_contents_part_of_sub_rule, bool is_sub_contents_part_of_action) {
  555. int i;
  556. if (a == NULL) {
  557. printf("NULL\n");
  558. return;
  559. }
  560. bool is_end = false;
  561. if (strcmp(a->tag, "instruction_end|>") == 0 || strcmp(a->tag, "instruction_end|EOL|string") == 0) is_end = true;
  562. bool is_an_action = false;
  563. bool is_an_action_structure = false;
  564. bool is_an_action_segmented_structure = false;
  565. if (strcmp(a->tag, "action|>") == 0) is_an_action = true;
  566. else if (strcmp(a->tag, "action_code|>") == 0) is_an_action_structure = true;
  567. else if (strcmp(a->tag, "action_code_segmented|>") == 0) is_an_action_segmented_structure = true;
  568. if (is_an_action_segmented_structure == true || is_an_action_structure == true) is_sub_contents_part_of_action = true;
  569. if (strcmp(a->tag, "root|instruction|>") == 0) {
  570. tag_index = 0;
  571. is_sub_contents_part_of_instruction = true;
  572. ra = a;
  573. int matches = 0;
  574. has_action_structures = false;
  575. puts("initiating quick search for code structures");
  576. Build_Action_Code_Structure_Quick_Scan(a, &matches);
  577. puts("quick search COMPLETE");
  578. if (matches != 0) {
  579. has_action_structures = true;
  580. puts("code structures detected\ninitiating full search for code structures");
  581. Build_Action_Code_Structure(a, 0, false, true);
  582. puts("full search complete");
  583. puts("PRINTING ARGUMENT LIST");
  584. printf("action_code_builder_struct[%d].references->argc = %d\n", action_code_builder_struct->instances, action_code_builder_struct[action_code_builder_struct->instances-1].references->argc);
  585. for(int i = 0; i < action_code_builder_struct[action_code_builder_struct->instances-1].references->argc; i++) {
  586. printf("action_code_builder_struct[%d].references->argv[%d] = %s\n", action_code_builder_struct->instances, i, action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[i]);
  587. }
  588. puts("PRINTED ARGUMENT LIST");
  589. } else puts("no code structures detected");
  590. } else if (strcmp(a->tag, "root|definition|>") == 0 || strcmp(a->tag, "root|>") == 0) {
  591. is_sub_contents_part_of_instruction = false;
  592. }
  593. if (is_sub_contents_part_of_instruction == true) {
  594. for (i = 0; i < d; i++) printf(" ");
  595. if (is_end == true) {
  596. printf("tag: '%s', contents: '%s'\n", a->tag, a->contents);
  597. pi(number_ifs)
  598. while(number_ifs != 0) {
  599. number_ifs--;
  600. str_new(tmp);
  601. pi(number_ifs)
  602. if (number_ifs != 0) { str_insert_string(tmp, "} else read__string_undo(binstr.data);\n"); }
  603. else str_insert_string(tmp, "}");
  604. str_output_append(tmp, outfile);
  605. str_free(tmp);
  606. }
  607. }
  608. else {
  609. if (strlen(a->contents)) {
  610. printf("tag: '%s', contents: '%s'\n", a->tag, a->contents);
  611. if (strcmp(a->tag, "binary|regex") == 0) {
  612. if (number_ifs == 0) instructions++;
  613. number_ifs++;
  614. str_new(tmp);
  615. if (instructions > 1 && number_ifs == 1) str_insert_string(tmp, " else ");
  616. str_insert_string(tmp, "if (!");
  617. str_insert_string(tmp, "regexEngineb(");
  618. str_insert_int(tmp, strlen(a->contents));
  619. str_insert_string(tmp, ", ");
  620. str_insert_int(tmp, strlen(a->contents));
  621. str_insert_string(tmp, ", \"");
  622. if (strlen(a->contents) != 1) {
  623. str_insert_int(tmp, strlen(a->contents)-1);
  624. str_insert_string(tmp, "-");
  625. }
  626. str_insert_string(tmp, "0,");
  627. str_insert_string(tmp, a->contents);
  628. str_insert_string(tmp, "\", binstr.data, ");
  629. if (instructions >1 && number_ifs == 1) { str_insert_string(tmp, "true"); }
  630. else { str_insert_string(tmp, "false"); }
  631. str_insert_string(tmp, ")");
  632. str_insert_string(tmp, ") {\n");
  633. str_insert_string(tmp, " printf(\"a->contents = ");
  634. str_insert_string(tmp, a->contents);
  635. str_insert_string(tmp, ", regexEngineb_bits = %s\\n\", regexEngineb_bits);\n");
  636. str_output_append(tmp, outfile);
  637. str_free(tmp);
  638. if (number_ifs == 1) find_instruction_root(ra);
  639. }
  640. if (strcmp(a->tag, "ISA_RULE|word|regex") == 0 || (strcmp(a->tag, "word|regex") == 0 && is_sub_contents_part_of_sub_rule == true)) {
  641. puts("FINDING");
  642. find_definition_root(orig, a->contents);
  643. puts("FOUND");
  644. }
  645. if (strcmp(a->tag, "ISA_DEFINE|word|regex") == 0 || strcmp(a->tag, "ISA_RULE|word|regex") == 0 || strcmp(a->tag, "word|regex") == 0 || strcmp(a->tag, "binary|regex") == 0) {
  646. if (is_sub_contents_part_of_action == false && has_action_structures) {
  647. // ps(a->contents); // contains current argv string
  648. // pi(tag_index);
  649. // pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances);
  650. // ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[tag_index]);
  651. // pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].argv_id);
  652. // pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->argc);
  653. // ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].argv_id]);
  654. // pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].occurence_number);
  655. // if (strcmp(a->contents, action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[tag_index]) != 0) {
  656. // puts("INCONSISTANCY DETECTED");
  657. // abort();
  658. // };
  659. // tag_index++;
  660. }
  661. }
  662. } else {
  663. printf("tag: '%s'\n", a->tag);
  664. if (strcmp(a->tag, "ISA_RULE|>") == 0) is_sub_contents_part_of_sub_rule = true;
  665. if (is_an_action == true && is_sub_contents_part_of_action == false) {
  666. str_new(actioncode);
  667. aquire_action_code(a, &actioncode);
  668. ps(actioncode.string);
  669. str_output_append(actioncode, outfile);
  670. str_free(actioncode);
  671. }
  672. if (is_an_action_segmented_structure == true || is_an_action_structure == true) {
  673. }
  674. }
  675. }
  676. }
  677. prev = a;
  678. if (
  679. is_an_action == false &&
  680. is_an_action_structure == false &&
  681. is_an_action_segmented_structure == false
  682. ) for (i = 0; i < a->children_num; i++) {
  683. find_instructions(a->children[i], orig, d+1, is_sub_contents_part_of_instruction, is_sub_contents_part_of_sub_rule, is_sub_contents_part_of_action);
  684. }
  685. }
  686. void evalfile(char * string, mpc_parser_t *p, mpc_result_t * r) {
  687. if (mpc_parse_contents(string, p, r)) { // parse file specified in string with parser p and store resulting AST into r->output
  688. AP(r->output, 0, stdout); // prints full AST
  689. mpc_ast_delete(r->output);
  690. } else {
  691. mpc_err_print(r->error);
  692. mpc_err_delete(r->error);
  693. }
  694. }
  695. void evalfileI(char * string, mpc_parser_t *p, mpc_result_t * r) {
  696. if (mpc_parse_contents(string, p, r)) { // parse file specified in string with parser p and store resulting AST into r->output
  697. instructions = 0;
  698. find_instructions(r->output, r->output, 0, false, false, false); // attempts to print instruction part of ast with definition replacement done in instructions
  699. mpc_ast_delete(r->output);
  700. } else {
  701. mpc_err_print(r->error);
  702. mpc_err_delete(r->error);
  703. }
  704. }
  705. void eval(char * string, mpc_parser_t *p, mpc_result_t * r) {
  706. if (mpc_parse("test", string, p, r)) {
  707. mpc_ast_print(r->output);
  708. mpc_ast_delete(r->output);
  709. } else {
  710. mpc_err_print(r->error);
  711. mpc_err_delete(r->error);
  712. }
  713. }
  714. #define mpcnew(x) mpc_parser_t * x = mpc_new(#x)
  715. int main(int argc, char **argv) {
  716. mpcnew(parser_root);
  717. mpcnew(root);
  718. mpcnew(definition);
  719. mpcnew(instruction);
  720. mpcnew(definition_start);
  721. mpcnew(definition_statement);
  722. mpcnew(definition_item);
  723. mpcnew(instruction_start);
  724. mpcnew(instruction_statement);
  725. mpcnew(instruction_item);
  726. mpcnew(instruction_end);
  727. mpcnew(EOL);
  728. mpcnew(hash);
  729. mpcnew(ISA_DEFINE);
  730. mpcnew(colon);
  731. mpcnew(ha_statement);
  732. mpcnew(item);
  733. mpcnew(bracketed_item);
  734. mpcnew(normal_item);
  735. mpcnew(pair);
  736. mpcnew(single);
  737. mpcnew(S);
  738. mpcnew(space);
  739. mpcnew(tab);
  740. mpcnew(word);
  741. mpcnew(binary);
  742. mpcnew(ISA_RULE);
  743. mpcnew(comma);
  744. mpcnew(comment);
  745. mpcnew(single_line_comment);
  746. mpcnew(multi_line_comment);
  747. mpcnew(action_code);
  748. mpcnew(name);
  749. mpcnew(num);
  750. mpcnew(indexedname);
  751. mpcnew(number);
  752. mpcnew(word_segmented);
  753. mpcnew(number_segmented);
  754. mpcnew(action_code_segmented);
  755. mpcnew(name_segmented);
  756. mpcnew(num_segmented);
  757. mpcnew(indexedname_segmented);
  758. mpcnew(SS);
  759. mpcnew(action);
  760. mpc_err_t* err = mpca_lang_contents(MPCA_LANG_WHITESPACE_SENSITIVE, "grammer_parser.parse", parser_root, root, definition, instruction, definition_start, definition_statement, definition_item, instruction_start, instruction_statement, instruction_item, instruction_end, item, bracketed_item, normal_item, single, pair, hash, colon, word, binary, ISA_RULE, ISA_DEFINE, EOL, S, space, tab, comma, comment, single_line_comment, multi_line_comment, action_code, name, num, indexedname, number, action_code_segmented, name_segmented, num_segmented, indexedname_segmented, number_segmented, word_segmented, SS, action);
  761. if (err != NULL) {
  762. mpc_err_print(err);
  763. mpc_err_delete(err);
  764. exit(1);
  765. }
  766. mpc_result_t r;
  767. str_new(ff);
  768. str_include(ff, "ISA_test_PRE.c");
  769. str_output(ff, outfile);
  770. // evalfile("Intel4004TEST.isa", parser_root, &r);
  771. // evalfile("Intel4004.isa", parser_root, &r);
  772. evalfileI("Intel4004.isa", parser_root, &r);
  773. str_reset(ff);
  774. str_insert_string(ff, "}\n");
  775. str_output_append(ff, outfile);
  776. str_free(ff);
  777. // mpc_parser_t * re = mpc_new("re");
  778. // mpc_define(re, mpc_re("(ab(cd))+"));
  779. // mpc_result_t rr;
  780. // if (mpc_parse("test", "abcd", re, &rr)) {
  781. // puts("passed");
  782. // ps(rr.output)
  783. // } else {
  784. // puts("failed");
  785. // mpc_err_print(rr.error);
  786. // mpc_err_delete(rr.error);
  787. // }
  788. return 0;
  789. }
  790.