#include "../Shell/builtins/printfmacro.h" #include "mpc/mpc.h" #include "mpc/mpc.c" #include #include "../Shell/builtins/libstring.h" #include "../Shell/builtins/regex_str.h" #include "../Shell/builtins/env.h" char * outfile = "tmpfile.c"; static void AP(mpc_ast_t *a, int d, FILE *fp) { int i; if (a == NULL) { fprintf(fp, "NULL\n"); return; } for (i = 0; i < d; i++) { fprintf(fp, " "); } if (strlen(a->contents)) { fprintf(fp, "tag: '%s', contents: '%s'\n", a->tag, a->contents); } else { fprintf(fp, "tag: '%s' \n", a->tag); } for (i = 0; i < a->children_num; i++) { AP(a->children[i], d+1, fp); } } bool find_tree_string(mpc_ast_t *a, char * string) { int i; if (a == NULL) { printf("NULL\n"); return false; } if (strcmp(a->contents, string) == 0) { str_new(tmp); str_insert_string(tmp, " puts(\""); str_insert_string(tmp, a->contents); str_insert_string(tmp, " \");\n"); str_output_append(tmp, outfile); str_free(tmp); return true; } return false; } bool find_tree_string2(mpc_ast_t *a) { int i; if (a == NULL) { printf("NULL\n"); return false; } if (strcmp(a->tag, "ISA_DEFINE|word|regex") == 0 || strcmp(a->tag, "word|regex") == 0 ) { str_new(tmp); str_insert_string(tmp, " puts(\""); str_insert_string(tmp, a->contents); str_insert_string(tmp, " \");\n"); str_output_append(tmp, outfile); str_free(tmp); return true; } else if (strcmp(a->tag, "ISA_DEFINE|>") == 0) { for (i = 0; i < a->children_num; i++) { if (find_tree_string2(a->children[i]) == true) return true; } } return false; } int find_definition_strings_called = 0; bool find_definition_strings(mpc_ast_t *a, bool omit_else) { if (a == NULL) { printf("NULL\n"); return false; } if (strcmp(a->tag, "word|regex") == 0) { find_definition_strings_called++; printf("%s\n", a->contents); str_new(tmp); if (omit_else == false) if (find_definition_strings_called >1) str_insert_string(tmp, "else "); str_insert_string(tmp, "if (!"); str_insert_string(tmp, "regexEngineb("); str_insert_int(tmp, strlen(a->contents)); str_insert_string(tmp, ", "); str_insert_int(tmp, strlen(a->contents)); str_insert_string(tmp, ", \""); if (strlen(a->contents) != 1) { str_insert_int(tmp, strlen(a->contents)-1); str_insert_string(tmp, "-"); } str_insert_string(tmp, "0,"); str_insert_string(tmp, a->contents); str_insert_string(tmp, "\", binstr.data, "); if (find_definition_strings_called >1) { str_insert_string(tmp, "true"); } else { str_insert_string(tmp, "false"); } str_insert_string(tmp, ")"); str_insert_string(tmp, ") {\n"); str_insert_string(tmp, " printf(\"a->contents = "); str_insert_string(tmp, a->contents); str_insert_string(tmp, ", regexEngineb_bits = %s\\n\", regexEngineb_bits);\n"); str_insert_string(tmp, "}\n"); str_output_append(tmp, outfile); str_free(tmp); } return false; } bool find_definition_items(mpc_ast_t *a) { int i; if (a == NULL) { printf("NULL\n"); return false; } if (strcmp(a->tag, "item|bracketed_item|>") == 0) { for (i = 0; i < a->children_num; i++) { find_definition_strings(a->children[i], true); } } else if (strcmp(a->tag, "item|normal_item|>") == 0) { for (i = 0; i < a->children_num; i++) { find_definition_strings(a->children[i], false); } } else { for (i = 0; i < a->children_num; i++) { find_definition_items(a->children[i]); } } return false; } bool find_definition_statements(mpc_ast_t *a) { int i; if (a == NULL) { printf("NULL\n"); return false; } if (strcmp(a->tag, "definition_statement|>") == 0) { for (i = 0; i < a->children_num; i++) { find_definition_items(a->children[i]); } } else { for (i = 0; i < a->children_num; i++) { find_definition_statements(a->children[i]); } } return false; } bool find_definition_root(mpc_ast_t *a, char * string) { int i; if (a == NULL) { printf("NULL\n"); return false; } if (strcmp(a->tag, "root|definition|>") == 0) { find_definition_strings_called = 0; for (i = 0; i < a->children_num; i++) { if (find_definition_root(a->children[i], string) == true) find_definition_statements(a); } find_definition_strings_called = 0; } else if (strcmp(a->tag, "definition_start|>") == 0) { for (i = 0; i < a->children_num; i++) { if (find_tree_string(a->children[i], string) == true) return true; } } else { for (i = 0; i < a->children_num; i++) { find_definition_root(a->children[i], string); } } return false; } bool find_instruction_root(mpc_ast_t *a) { int i; if (a == NULL) { printf("NULL\n"); return false; } if (strcmp(a->tag, "root|instruction|>") == 0) { for (i = 0; i < a->children_num; i++) { find_instruction_root(a->children[i]); } } else if (strcmp(a->tag, "instruction_start|>") == 0) { for (i = 0; i < a->children_num; i++) { if (find_tree_string2(a->children[i]) == true) return true; } } else { for (i = 0; i < a->children_num; i++) { find_instruction_root(a->children[i]); } } return false; } void aquire_action_code(mpc_ast_t *a, struct regex_string * r) { if (a->children_num) { str_reg(a->children[0]->tag); if (str_is("name|>") || str_is("num|>") || str_is("indexedname|>") || str_is("name_segmented|>") || str_is("num_segmented|>") || str_is("indexedname_segmented|>")) return; } str_insert_string(*r, "/*ACTION CODE BEGIN*/"); int i = 0; for (i = 0; i < a->children_num; i++) { str_reg(a->children[i]->tag); if (str_is("regex")) { if (a->children[i]->contents != NULL) { str_insert_string(*r, a->children[i]->contents); } } } str_insert_string(*r, "/*ACTION CODE END*/"); } struct action_code_builder_object_references { int argv_id; // argv index int * content_id; // content index indexes int content_amount; int occurence_number; int instances; }; struct action_code_builder_references { int argc; env_t argv; struct action_code_builder_object_references * ref; struct action_code_builder_content * content; int instances; }; struct action_code_builder_content { struct regex_string * content; // content of action code int instances; }; struct action_code_builder { struct action_code_builder_references * references; int instances; } * action_code_builder_struct; int action_code_builder_argument_index = 0; void Build_Action_Code_Structure_Quick_Scan(mpc_ast_t *a, int * Build_Action_Code_Structure_Quick_Scan_Matches) { int i; if (a == NULL) { return; } bool is_an_action_structure = false; bool is_an_action_segmented_structure = false; bool has_tag = false; if (strlen(a->tag)) has_tag = true; if (has_tag) { if (strcmp(a->tag, "action_code|>") == 0) is_an_action_structure = true; else if (strcmp(a->tag, "action_code_segmented|>") == 0) is_an_action_segmented_structure = true; } if (!is_an_action_structure && !is_an_action_segmented_structure) for (i = 0; i < a->children_num; i++) { Build_Action_Code_Structure_Quick_Scan(a->children[i], Build_Action_Code_Structure_Quick_Scan_Matches); } else (*Build_Action_Code_Structure_Quick_Scan_Matches)++; } struct binder { int bind_index; bool bind_segmented; int bind_type; char * target_content_name; int instances; } * action_code_binder; int action_code_binder_skip_next = 0; #define BIND_NONE -1 #define BIND_NAME 1 #define BIND_NUM 2 #define BIND_FUSED 3 void Build_Action_Code_Structure(mpc_ast_t *a, int d, bool is_sub_contents_part_of_action, bool renew) { if (renew) { if (action_code_builder_struct == NULL) { action_code_builder_struct = malloc(sizeof(struct action_code_builder)*1); action_code_builder_struct->instances = 1; action_code_builder_struct[action_code_builder_struct->instances-1].references = malloc(sizeof(struct action_code_builder_references)*1); action_code_builder_struct[action_code_builder_struct->instances-1].references->instances = 1; action_code_builder_struct[action_code_builder_struct->instances-1].references->argc = 0; action_code_builder_struct[action_code_builder_struct->instances-1].references->argv = NULL; action_code_builder_struct[action_code_builder_struct->instances-1].references->content = malloc(sizeof(struct action_code_builder_content)*1); 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->ref = malloc(sizeof(struct action_code_builder_object_references)*1); action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances = 1; action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->argv_id = -1; } else { action_code_builder_struct = realloc(action_code_builder_struct, sizeof(struct action_code_builder)*(action_code_builder_struct->instances+1)); action_code_builder_struct->instances++; action_code_builder_struct[action_code_builder_struct->instances-1].references = malloc(sizeof(struct action_code_builder_references)*1); action_code_builder_struct[action_code_builder_struct->instances-1].references->instances = 1; action_code_builder_struct[action_code_builder_struct->instances-1].references->argc = 0; action_code_builder_struct[action_code_builder_struct->instances-1].references->argv = NULL; action_code_builder_struct[action_code_builder_struct->instances-1].references->content = malloc(sizeof(struct action_code_builder_content)*1); 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->ref = malloc(sizeof(struct action_code_builder_object_references)*1); action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances = 1; action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->argv_id = -1; } } int i; if (a == NULL) { return; } bool is_an_action = false; bool is_an_action_structure = false; bool is_an_action_segmented_structure = false; bool has_tag = false; bool has_contents = false; if (strlen(a->tag)) has_tag = true; if (strlen(a->contents)) has_contents = true; if (has_tag) { if (strcmp(a->tag, "action|>") == 0) is_an_action = true; else if (strcmp(a->tag, "action_code|>") == 0) is_an_action_structure = true; else if (strcmp(a->tag, "action_code_segmented|>") == 0) is_an_action_segmented_structure = true; if (is_an_action_segmented_structure == true || is_an_action_structure == true) is_sub_contents_part_of_action = true; 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)) { 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) { action_code_builder_struct[action_code_builder_struct->instances-1].references->ref = realloc( action_code_builder_struct[action_code_builder_struct->instances-1].references->ref, sizeof( struct action_code_builder_object_references ) * ( action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances+1 ) ); action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances++; } 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; action_code_builder_struct[action_code_builder_struct->instances-1].references->argc++; 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; 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); int occurence = 0, tag_index = action_code_builder_struct[action_code_builder_struct->instances-1].references->argc-1; 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++; pi(occurence) 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; ps(a->contents) pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->argc-1) 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) pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[0].argv_id) pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances-1) } } if (is_sub_contents_part_of_action) { // how to build the structure of a reference... if (has_tag) { // try to get tag type str_reg(a->tag); if (str_is("name|>")) { if (action_code_binder_skip_next != 0) { action_code_binder_skip_next--; } else { puts("BINDING"); if (action_code_binder == NULL) { action_code_binder = malloc(sizeof(struct binder)*1); action_code_binder->instances = 1; action_code_binder_skip_next = 0; } else { action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1)); action_code_binder->instances++; } action_code_binder[action_code_binder->instances-1].target_content_name = a->children[1]->contents; action_code_binder[action_code_binder->instances-1].bind_index = 0; action_code_binder[action_code_binder->instances-1].bind_type = BIND_NAME; } } else if (str_is("num|>")) { if (action_code_binder_skip_next != 0) { action_code_binder_skip_next--; } else { puts("BINDING"); if (action_code_binder == NULL) { action_code_binder = malloc(sizeof(struct binder)*1); action_code_binder->instances = 1; action_code_binder_skip_next = 0; } else { action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1)); action_code_binder->instances++; } action_code_binder[action_code_binder->instances-1].bind_index= atoi(a->children[1]->contents); action_code_binder[action_code_binder->instances-1].bind_type = BIND_NUM; } } else if (str_is("indexedname|>")) { if (action_code_binder_skip_next != 0) { action_code_binder_skip_next--; } else { puts("BINDING"); if (action_code_binder == NULL) { action_code_binder = malloc(sizeof(struct binder)*1); action_code_binder->instances = 1; action_code_binder_skip_next = 0; } else { action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1)); action_code_binder->instances++; } action_code_binder[action_code_binder->instances-1].target_content_name = a->children[1]->contents; action_code_binder[action_code_binder->instances-1].bind_index= atoi(a->children[2]->children[1]->contents); action_code_binder[action_code_binder->instances-1].bind_type = BIND_FUSED; action_code_binder_skip_next = 1; } } else if (str_is("name_segmented|>")) { puts("BINDING"); if (action_code_binder == NULL) { action_code_binder = malloc(sizeof(struct binder)*1); action_code_binder->instances = 1; } else { action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1)); action_code_binder->instances++; } } else if (str_is("num_segmented|>")) { puts("BINDING"); if (action_code_binder == NULL) { action_code_binder = malloc(sizeof(struct binder)*1); action_code_binder->instances = 1; } else { action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1)); action_code_binder->instances++; } } else if (str_is("indexedname_segmented|>")) { puts("BINDING"); if (action_code_binder == NULL) { action_code_binder = malloc(sizeof(struct binder)*1); action_code_binder->instances = 1; } else { action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1)); action_code_binder->instances++; } } if (is_an_action && action_code_binder != NULL) { for (int ii = 0; ii < action_code_binder->instances; ii++) { if (action_code_binder[ii].bind_type == BIND_NAME) { ps(action_code_binder[ii].target_content_name); int tag_index = action_code_builder_struct[action_code_builder_struct->instances-1].references->argc-1; for (int i = 0; i < action_code_builder_struct[action_code_builder_struct->instances-1].references->argc; i++) { if (strcmp(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[i], action_code_binder[ii].target_content_name) == 0) { 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]); if (action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[i].occurence_number == action_code_binder[ii].bind_index) { action_code_binder[ii].bind_index = i; break; } } } pi(action_code_binder[ii].bind_index) pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].argv_id); pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].occurence_number); 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]); } else if (action_code_binder[ii].bind_type == BIND_NUM) { pi(action_code_binder[ii].bind_index) pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].argv_id); pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].occurence_number); 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]); } else if (action_code_binder[ii].bind_type == BIND_FUSED) { ps(action_code_binder[ii].target_content_name); int tag_index = action_code_builder_struct[action_code_builder_struct->instances-1].references->argc-1; for (int i = 0; i < action_code_builder_struct[action_code_builder_struct->instances-1].references->argc; i++) { if (strcmp(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[i], action_code_binder[ii].target_content_name) == 0) { 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]); if (action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[i].occurence_number == action_code_binder[ii].bind_index) { action_code_binder[ii].bind_index = i; break; } } } pi(action_code_binder[ii].bind_index) pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].argv_id); pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].occurence_number); 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]); } } // get action code contents str_new(actioncode); aquire_action_code(a, &actioncode); action_code_builder_struct[action_code_builder_struct->instances-1].references->content->content = &actioncode; ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->content->content->string); /* * * unsure about exact operation mode * * #0${NULL#0}$NULL$1 {/*NULL1*\/} {/*NULL2*\/} * * >>> * * #0 > arg id 0, first occurence * #0 > condent id 0 > {/*NULL1*\/} * #0 > condent id 1 > {/*NULL2*\/} * * ${NULL#0} > arg id 0, first occurence * ${NULL#0} > condent id 0 > {/*NULL1*\/} * ${NULL#0} > condent id 1 > {/*NULL2*\/} * * $NULL > arg id 0, first occurence * $NULL > condent id 0 > {/*NULL1*\/} * $NULL > condent id 1 > {/*NULL2*\/} * * #1 > arg id 1, first occurence * #1 > condent id 0 > {/*NULL1*\/} * #1 > condent id 1 > {/*NULL2*\/} * * each paramater is assigned a reference containing an argv id and a content id array * * */ // implements multi action code storage (operation of the content id array), probably does not function correctly for (int ii = 0; ii < action_code_binder->instances; ii++) { pi(action_code_binder[ii].bind_index) if (action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_id == NULL) { action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_id = malloc(sizeof(int)*1); action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_amount = 1; } else { 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)); action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_amount++; } 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; } for (int ii = 0; ii < action_code_binder->instances; ii++) { 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++) { pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_amount) pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[ii].bind_index].content_id[iii]) } } if (action_code_binder != NULL) { free(action_code_binder); action_code_binder = NULL; } }; if (has_contents) { for (i = 0; i < d; i++) { printf(" "); } printf("tag: '%s', contents: '%s'\n", a->tag, a->contents); } else { for (i = 0; i < d; i++) { printf(" "); } printf("tag: '%s'\n", a->tag); } } else if (has_contents) { for (i = 0; i < d; i++) { printf(" "); } printf("contents: '%s'\n", a->contents); } } for (i = 0; i < a->children_num; i++) { Build_Action_Code_Structure(a->children[i], d+1, is_sub_contents_part_of_action, false); } } int number_ifs = 0; mpc_ast_t *ra = NULL; int instructions = 0; mpc_ast_t *prev; bool isimportant = false; int tag_index = 0; bool has_action_structures = false; 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) { int i; if (a == NULL) { printf("NULL\n"); return; } bool is_end = false; if (strcmp(a->tag, "instruction_end|>") == 0 || strcmp(a->tag, "instruction_end|EOL|string") == 0) is_end = true; bool is_an_action = false; bool is_an_action_structure = false; bool is_an_action_segmented_structure = false; if (strcmp(a->tag, "action|>") == 0) is_an_action = true; else if (strcmp(a->tag, "action_code|>") == 0) is_an_action_structure = true; else if (strcmp(a->tag, "action_code_segmented|>") == 0) is_an_action_segmented_structure = true; if (is_an_action_segmented_structure == true || is_an_action_structure == true) is_sub_contents_part_of_action = true; if (strcmp(a->tag, "root|instruction|>") == 0) { tag_index = 0; is_sub_contents_part_of_instruction = true; ra = a; int matches = 0; has_action_structures = false; puts("initiating quick search for code structures"); Build_Action_Code_Structure_Quick_Scan(a, &matches); puts("quick search COMPLETE"); if (matches != 0) { has_action_structures = true; puts("code structures detected\ninitiating full search for code structures"); Build_Action_Code_Structure(a, 0, false, true); puts("full search complete"); puts("PRINTING ARGUMENT LIST"); 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); for(int i = 0; i < action_code_builder_struct[action_code_builder_struct->instances-1].references->argc; i++) { 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]); } puts("PRINTED ARGUMENT LIST"); } else puts("no code structures detected"); } else if (strcmp(a->tag, "root|definition|>") == 0 || strcmp(a->tag, "root|>") == 0) { is_sub_contents_part_of_instruction = false; } if (is_sub_contents_part_of_instruction == true) { for (i = 0; i < d; i++) printf(" "); if (is_end == true) { printf("tag: '%s', contents: '%s'\n", a->tag, a->contents); pi(number_ifs) while(number_ifs != 0) { number_ifs--; str_new(tmp); pi(number_ifs) if (number_ifs != 0) { str_insert_string(tmp, "} else read__string_undo(binstr.data);\n"); } else str_insert_string(tmp, "}"); str_output_append(tmp, outfile); str_free(tmp); } } else { if (strlen(a->contents)) { printf("tag: '%s', contents: '%s'\n", a->tag, a->contents); if (strcmp(a->tag, "binary|regex") == 0) { if (number_ifs == 0) instructions++; number_ifs++; str_new(tmp); if (instructions > 1 && number_ifs == 1) str_insert_string(tmp, " else "); str_insert_string(tmp, "if (!"); str_insert_string(tmp, "regexEngineb("); str_insert_int(tmp, strlen(a->contents)); str_insert_string(tmp, ", "); str_insert_int(tmp, strlen(a->contents)); str_insert_string(tmp, ", \""); if (strlen(a->contents) != 1) { str_insert_int(tmp, strlen(a->contents)-1); str_insert_string(tmp, "-"); } str_insert_string(tmp, "0,"); str_insert_string(tmp, a->contents); str_insert_string(tmp, "\", binstr.data, "); if (instructions >1 && number_ifs == 1) { str_insert_string(tmp, "true"); } else { str_insert_string(tmp, "false"); } str_insert_string(tmp, ")"); str_insert_string(tmp, ") {\n"); str_insert_string(tmp, " printf(\"a->contents = "); str_insert_string(tmp, a->contents); str_insert_string(tmp, ", regexEngineb_bits = %s\\n\", regexEngineb_bits);\n"); str_output_append(tmp, outfile); str_free(tmp); if (number_ifs == 1) find_instruction_root(ra); } if (strcmp(a->tag, "ISA_RULE|word|regex") == 0 || (strcmp(a->tag, "word|regex") == 0 && is_sub_contents_part_of_sub_rule == true)) { puts("FINDING"); find_definition_root(orig, a->contents); puts("FOUND"); } 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) { if (is_sub_contents_part_of_action == false && has_action_structures) { // ps(a->contents); // contains current argv string // pi(tag_index); // pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances); // ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[tag_index]); // pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].argv_id); // pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->argc); // 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]); // pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].occurence_number); // if (strcmp(a->contents, action_code_builder_struct[action_code_builder_struct->instances-1].references->argv[tag_index]) != 0) { // puts("INCONSISTANCY DETECTED"); // abort(); // }; // tag_index++; } } } else { printf("tag: '%s'\n", a->tag); if (strcmp(a->tag, "ISA_RULE|>") == 0) is_sub_contents_part_of_sub_rule = true; if (is_an_action == true && is_sub_contents_part_of_action == false) { str_new(actioncode); aquire_action_code(a, &actioncode); ps(actioncode.string); str_output_append(actioncode, outfile); str_free(actioncode); } if (is_an_action_segmented_structure == true || is_an_action_structure == true) { } } } } prev = a; if ( is_an_action == false && is_an_action_structure == false && is_an_action_segmented_structure == false ) for (i = 0; i < a->children_num; i++) { 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); } } void evalfile(char * string, mpc_parser_t *p, mpc_result_t * r) { if (mpc_parse_contents(string, p, r)) { // parse file specified in string with parser p and store resulting AST into r->output AP(r->output, 0, stdout); // prints full AST mpc_ast_delete(r->output); } else { mpc_err_print(r->error); mpc_err_delete(r->error); } } void evalfileI(char * string, mpc_parser_t *p, mpc_result_t * r) { if (mpc_parse_contents(string, p, r)) { // parse file specified in string with parser p and store resulting AST into r->output instructions = 0; find_instructions(r->output, r->output, 0, false, false, false); // attempts to print instruction part of ast with definition replacement done in instructions mpc_ast_delete(r->output); } else { mpc_err_print(r->error); mpc_err_delete(r->error); } } void eval(char * string, mpc_parser_t *p, mpc_result_t * r) { if (mpc_parse("test", string, p, r)) { mpc_ast_print(r->output); mpc_ast_delete(r->output); } else { mpc_err_print(r->error); mpc_err_delete(r->error); } } #define mpcnew(x) mpc_parser_t * x = mpc_new(#x) int main(int argc, char **argv) { mpcnew(parser_root); mpcnew(root); mpcnew(definition); mpcnew(instruction); mpcnew(definition_start); mpcnew(definition_statement); mpcnew(definition_item); mpcnew(instruction_start); mpcnew(instruction_statement); mpcnew(instruction_item); mpcnew(instruction_end); mpcnew(EOL); mpcnew(hash); mpcnew(ISA_DEFINE); mpcnew(colon); mpcnew(ha_statement); mpcnew(item); mpcnew(bracketed_item); mpcnew(normal_item); mpcnew(pair); mpcnew(single); mpcnew(S); mpcnew(space); mpcnew(tab); mpcnew(word); mpcnew(binary); mpcnew(ISA_RULE); mpcnew(comma); mpcnew(comment); mpcnew(single_line_comment); mpcnew(multi_line_comment); mpcnew(action_code); mpcnew(name); mpcnew(num); mpcnew(indexedname); mpcnew(number); mpcnew(word_segmented); mpcnew(number_segmented); mpcnew(action_code_segmented); mpcnew(name_segmented); mpcnew(num_segmented); mpcnew(indexedname_segmented); mpcnew(SS); mpcnew(action); 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); if (err != NULL) { mpc_err_print(err); mpc_err_delete(err); exit(1); } mpc_result_t r; str_new(ff); str_include(ff, "ISA_test_PRE.c"); str_output(ff, outfile); // evalfile("Intel4004TEST.isa", parser_root, &r); // evalfile("Intel4004.isa", parser_root, &r); evalfileI("Intel4004.isa", parser_root, &r); str_reset(ff); str_insert_string(ff, "}\n"); str_output_append(ff, outfile); str_free(ff); // mpc_parser_t * re = mpc_new("re"); // mpc_define(re, mpc_re("(ab(cd))+")); // mpc_result_t rr; // if (mpc_parse("test", "abcd", re, &rr)) { // puts("passed"); // ps(rr.output) // } else { // puts("failed"); // mpc_err_print(rr.error); // mpc_err_delete(rr.error); // } return 0; }