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