-
- #include "../Shell/builtins/printfmacro.h"
- #include "mpc/mpc.h"
- #include "mpc/mpc.c"
- #include <stdbool.h>
- #include "../Shell/builtins/libstring.h"
- #include "../Shell/builtins/regex_str.h"
- #include "../Shell/builtins/env.h"
-
- char * outfile = "tmpfile.c";
-
- void piece(mpc_ast_t *a, bool is_segmented, bool is_word, bool is_number, struct regex_string * b1, struct regex_string * b2) {
- str_reg(a->tag);
- // if (is_segmented) ps(a->tag)
- // if ((((str_is("word|regex") || str_is("word_segmented|word|regex"))) || ((str_is("char") || str_is("number|regex") || str_is("number_segmented|number|regex"))))) {
- // puts("START");
- // pb(is_word)
- // pb(is_number)
- // pb(is_segmented)
- // ps(a->tag)
- // puts("END");
- // }
- if(str_is("name_segmented|>") || str_is("indexedname_segmented|>")) {
- puts("attempting to piece together segmented name");
- int i = 0;
- for (i = 0; i < a->children_num; i++) {
- piece(a->children[i], true, true, false, b1, b2);
- }
- puts("attempted");
- }
- 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) {
- if (a->contents != NULL) {
- ps(a->contents);
- if (is_number) {
- if (b1 != NULL) {
- if (a->contents[0] != '#') str_insert_string(*b1, a->contents)
- }
- }
- else if(is_word) {
- if (b2 != NULL) {
- str_insert_string(*b2, a->contents)
- }
- }
- }
- }
- else if (str_is("word_segmented|>") && is_segmented) {
- puts("attempting to piece together segmented word");
- int i = 0;
- for (i = 0; i < a->children_num; i++) {
- piece(a->children[i], is_segmented, true, false, b1, b2);
- }
- puts("attempted");
- }
- else if (str_is("num_segmented|>")) {
- puts("attempting to piece together segmented number");
- int i = 0;
- for (i = 0; i < a->children_num; i++) {
- piece(a->children[i], true, false, true, b1, b2);
- }
- puts("attempted");
- }
- else if (str_is("number_segmented|>") && is_segmented) {
- puts("attempting to piece together segmented number");
- int i = 0;
- for (i = 0; i < a->children_num; i++) {
- piece(a->children[i], is_segmented, false, true, b1, b2);
- }
- puts("attempted");
- }
- }
-
- 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.indented, 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.indented, 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, ") {");
- // 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, "}");
- str_output_append(tmp.indented, 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, "\n/*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*/\n");
- }
-
- 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;
- int bind_index_old;
- bool bind_index_updated;
- 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 = NULL;
- 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 = NULL;
- 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 || is_an_action_structure) is_sub_contents_part_of_action = true;
-
- if (!is_sub_contents_part_of_action || is_an_action_structure || is_an_action_segmented_structure) {
- if (action_code_binder != NULL) {
- puts("FREEING BINDER");
- free(action_code_binder);
- action_code_binder = NULL;
- }
- }
- pb(is_sub_contents_part_of_action);
-
- 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->ref[action_code_builder_struct[action_code_builder_struct->instances-1].references->ref->instances-1].content_amount = 0;
-
- 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_index_updated = false;
- 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].target_content_name = NULL;
- action_code_binder[action_code_binder->instances-1].bind_index = atoi(a->children[1]->contents);
- action_code_binder[action_code_binder->instances-1].bind_index_updated = false;
- action_code_binder[action_code_binder->instances-1].bind_type = BIND_NUM;
- pp(action_code_binder)
- }
- }
- 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_index_updated = false;
- action_code_binder[action_code_binder->instances-1].bind_type = BIND_FUSED;
- action_code_binder_skip_next = 1;
- }
- }
- else if (str_is("name_segmented|>")) {
- 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;
- }
- else {
- action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1));
- action_code_binder->instances++;
- }
- str_new(name__)
- piece(a, false, false, false, NULL, &name__);
- action_code_binder[action_code_binder->instances-1].target_content_name = strdup(name__.string);
- ps(name__.string)
- str_free(name__);
- action_code_binder[action_code_binder->instances-1].bind_index = 0;
- action_code_binder[action_code_binder->instances-1].bind_index_updated = false;
- action_code_binder[action_code_binder->instances-1].bind_type = BIND_NAME;
- }
- }
- else if (str_is("num_segmented|>")) {
- 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;
- }
- else {
- action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1));
- action_code_binder->instances++;
- }
- str_new(index__)
- piece(a, false, false, false, &index__, NULL);
- ps(index__.string)
- action_code_binder[action_code_binder->instances-1].target_content_name = NULL;
- action_code_binder[action_code_binder->instances-1].bind_index= atoi(strdup(index__.string));
- action_code_binder[action_code_binder->instances-1].bind_index_updated = false;
- str_free(index__);
- action_code_binder[action_code_binder->instances-1].bind_type = BIND_NUM;
- }
- }
- else if (str_is("indexedname_segmented|>")) {
- 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;
- }
- else {
- action_code_binder = realloc(action_code_binder, sizeof(struct binder)*(action_code_binder->instances+1));
- action_code_binder->instances++;
- }
- str_new(index__);
- str_new(name__);
- piece(a, false, false, false, &index__, &name__);
- ps(index__.string)
- ps(name__.string)
- action_code_binder[action_code_binder->instances-1].target_content_name = strdup(name__.string);
- str_free(name__);
- action_code_binder[action_code_binder->instances-1].bind_index= atoi(strdup(index__.string));
- action_code_binder[action_code_binder->instances-1].bind_index_updated = false;
- pi(action_code_binder[action_code_binder->instances-1].bind_index)
- str_free(index__);
- action_code_binder[action_code_binder->instances-1].bind_type = BIND_FUSED;
- action_code_binder_skip_next = 1;
- }
- }
- pp(action_code_binder)
- if (is_an_action) pp(action_code_binder)
- if (is_an_action && action_code_binder != NULL) {
- pp(action_code_binder)
- for (int ii = 0; ii < action_code_binder->instances; ii++) {
- printf("action_code_binder[%d].target_content_name = %s\n", ii, action_code_binder[ii].target_content_name);
- printf("action_code_binder[%d].bind_index = %d\n", ii, action_code_binder[ii].bind_index);
- printf("action_code_binder[%d].bind_index_updated = %s\n", ii, action_code_binder[ii].bind_index_updated==true?"true":"false");
- }
- for (int ii = 0; ii < action_code_binder->instances; ii++) {
- if (action_code_binder[action_code_binder->instances-1].bind_index_updated) {
- 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);
- continue;
- }
- if (action_code_binder[ii].bind_type == BIND_NAME || action_code_binder[ii].bind_type == BIND_FUSED) {
- ps(action_code_binder[ii].target_content_name);
- pi(action_code_binder[action_code_binder->instances-1].bind_index)
- printf("target occurence number is %d\n", action_code_binder[ii].bind_index);
- 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]);
- 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);
- if (action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[i].occurence_number == action_code_binder[ii].bind_index) {
- printf("occurence number %d of target %s found\n", action_code_binder[ii].bind_index, action_code_binder[ii].target_content_name);
- action_code_binder[ii].bind_index_old = action_code_binder[ii].bind_index;
- action_code_binder[ii].bind_index = i;
- action_code_binder[ii].bind_index_updated = true;
- break;
- }
- }
- }
- if (action_code_binder[ii].bind_index_updated == false) {
- 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);
- abort();
- }
- pb(action_code_binder[ii].bind_index_updated)
- 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]);
- }
- }
- /*
- *
- * 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
-
- /*
- * current result:
-
- action_code_binder[ii].bind_index = 0
-
- action_code_binder[ii].bind_index = 0
-
- action_code_binder[ii].bind_index = 0
-
- action_code_binder[ii].bind_index = 1
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[0].bind_index (0)].content_amount = 3
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[0].bind_index (0)].content_id[0] = 0
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[0].bind_index (0)].content_id[1] = 0
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[0].bind_index (0)].content_id[2] = 0
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[1].bind_index (0)].content_amount = 3
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[1].bind_index (0)].content_id[0] = 0
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[1].bind_index (0)].content_id[1] = 0
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[1].bind_index (0)].content_id[2] = 0
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[2].bind_index (0)].content_amount = 3
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[2].bind_index (0)].content_id[0] = 0
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[2].bind_index (0)].content_id[1] = 0
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[2].bind_index (0)].content_id[2] = 0
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[3].bind_index (1)].content_amount = 1
- action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[action_code_binder[3].bind_index (1)].content_id[0] = 1
- */
- if (is_an_action) {
- pp(action_code_builder_struct[action_code_builder_struct->instances-1].references->content)
- // get action code contents
- if (action_code_builder_struct[action_code_builder_struct->instances-1].references->content == 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;
- }
- else {
- 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));
- action_code_builder_struct[action_code_builder_struct->instances-1].references->content->instances++;
- }
- pi(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 = &actioncode;
-
- puts("MALLOCING NEW STRING");
-
- 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);
-
- puts("ASSIGNING NEW STRING");
-
- 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);
-
- puts("PRINTING NEW STRING");
-
- 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);
-
- ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->content->content.string);
-
- 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);
-
- pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->content->instances)
- env_t index_check = env__new();
- for (int ii = 0; ii < action_code_binder->instances; ii++) {
- // itterate through the bind indexes
- str_int2string(idx, action_code_binder[ii].bind_index);
- if (env__get_name(index_check, idx) != NULL) continue;
- ps(idx)
- index_check = env__add2(index_check, idx);
- env__print(index_check, idx);
- puts("");
- pi(action_code_binder[ii].bind_index)
- // creates an instance of content_id for each bind reference, this should not happen
- // instead create an instance of content_id for each action code
- 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++;
- }
- // content id is assigned the bind index, this should not be the case as the bind index is symbolic to the argv_id index
- // instead content_id is assigned to the current action code index reference
-
- 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_builder_struct[action_code_builder_struct->instances-1].references->content->instances-1
-
- ;
-
- 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);
-
- 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++) {
-
- 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]);
- }
- }
- env__free(index_check);
- index_check = 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 || strcmp(a->tag, "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.indented, 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 ");
- }
- else {
- str_insert_string(tmp, "\n");
- }
- 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, ") {");
- // 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.indented, 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) {
- pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].content_amount)
- if (action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].content_amount) {
- for (int i = 0; i < action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].content_amount; i++) {
- 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);
- 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);
- }
- /* 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);
- pp(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[0].content_id)
- // pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[0].content_id[0]);
- pp(action_code_builder_struct[action_code_builder_struct->instances-1].references->content->content.string);
- ps(action_code_builder_struct[action_code_builder_struct->instances-1].references->content->content.string);
- pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].content_id[0]);
- 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]])
- pi(action_code_builder_struct[action_code_builder_struct->instances-1].references->ref[tag_index].content_amount);
- 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);
- 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) {
- str_new(tmp);
- aquire_action_code(a, &tmp);
- ps(tmp.string);
- str_output_append(tmp.indented, outfile);
- str_free(tmp);
- }
- }
- }
- }
- 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_INDENT_LEVEL = 0;
- STR_INDENT_SIZE = 8;
- 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, "}");
- str_output_append(ff.indented, 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;
- }
-