optimize some code for lex & DFA module
authoryu.dongliang <18588496441@163.com>
Fri, 30 Aug 2024 07:44:20 +0000 (15:44 +0800)
committeryu.dongliang <18588496441@163.com>
Fri, 30 Aug 2024 07:44:20 +0000 (15:44 +0800)
core/scf_lex_word.h
lex/scf_lex.c
lex/scf_lex.h
parse/scf_dfa.c
parse/scf_dfa_container.c
parse/scf_dfa_create.c
parse/scf_dfa_expr.c
parse/scf_dfa_util.h

index c8c978be944ddc829f1ea78b21c370ebb7d743ed..c10952b9d41ae977d3764be9095807909b6b8a88 100644 (file)
@@ -247,7 +247,7 @@ typedef struct {
 
 static inline int scf_lex_is_identity(scf_lex_word_t* w)
 {
-       return w->type >= SCF_LEX_WORD_ID;
+       return SCF_LEX_WORD_ID == w->type;
 }
 
 static inline int scf_lex_is_operator(scf_lex_word_t* w)
index ff80b9e0eb1da32cff01e51e821e11b81125d1ec..6b953b0bedbeafecd90533cc37608ba230e765e2 100644 (file)
@@ -456,10 +456,9 @@ static int _lex_identity(scf_lex_t* lex, scf_lex_word_t** pword, scf_char_t* c0)
                        } else {
                                int type = _find_key_word(s->data);
 
-                               if (-1 == type) {
-                                       type = SCF_LEX_WORD_ID + lex->nb_identities++;
-                                       w = scf_lex_word_alloc(lex->file, lex->nb_lines, lex->pos, type);
-                               } else
+                               if (-1 == type)
+                                       w = scf_lex_word_alloc(lex->file, lex->nb_lines, lex->pos, SCF_LEX_WORD_ID);
+                               else
                                        w = scf_lex_word_alloc(lex->file, lex->nb_lines, lex->pos, type);
                        }
 
index cdc606fb60240bd07346bcb858b02128e203e298..9d42928bf06c57f0af931fe190064b0bb3275649 100644 (file)
@@ -31,8 +31,6 @@ typedef struct {
 
        FILE*           fp; // file pointer to the code
 
-       int             nb_identities;
-
        scf_string_t*   file; // original code file name
        int             nb_lines;
        int             pos;
index 210f41422ceabddd1b529dd7ac581191b8eb6f8c..96b773c70052e49456f877286af8d1896a01488b 100644 (file)
@@ -112,15 +112,7 @@ void scf_dfa_node_free(scf_dfa_node_t* node)
        assert(0 == node->refs);
 
        if (node->childs) {
-               int i;
-
-               for (i = 0; i < node->childs->size; i++) {
-                       scf_dfa_node_t* child = node->childs->data[i];
-
-                       scf_dfa_node_free(child);
-                       child = NULL;
-               }
-
+               scf_vector_clear(node->childs, (void (*)(void*) )scf_dfa_node_free);
                scf_vector_free(node->childs);
                node->childs = NULL;
        }
@@ -137,25 +129,30 @@ int scf_dfa_node_add_child(scf_dfa_node_t* parent, scf_dfa_node_t* child)
                return -1;
        }
 
-       if (!parent->childs) {
-               parent->childs = scf_vector_alloc();
-               scf_vector_add(parent->childs, child);
-               child->refs++;
-               return 0;
-       }
-
+       scf_dfa_node_t* node;
        int i;
 
-       for (i = 0; i < parent->childs->size; i++) {
-               scf_dfa_node_t* node = parent->childs->data[i];
+       if (parent->childs) {
+
+               for (i = 0; i < parent->childs->size; i++) {
+                       node      = parent->childs->data[i];
 
-               if (!strcmp(child->name, node->name)) {
-                       scf_logi("repeated: node->name: %s\n", node->name);
-                       return SCF_DFA_REPEATED;
+                       if (!strcmp(child->name, node->name)) {
+                               scf_logi("repeated: child: %s, parent: %s\n", child->name, parent->name);
+                               return SCF_DFA_REPEATED;
+                       }
                }
+
+       } else {
+               parent->childs = scf_vector_alloc();
+               if (!parent->childs)
+                       return -ENOMEM;
        }
 
-       scf_vector_add(parent->childs, child);
+       int ret = scf_vector_add(parent->childs, child);
+       if (ret < 0)
+               return ret;
+
        child->refs++;
        return 0;
 }
@@ -171,10 +168,11 @@ int scf_dfa_open(scf_dfa_t** pdfa, const char* name, void* priv)
 
        int i;
        for (i = 0; dfa_ops_array[i]; i++) {
-               if (!strcmp(name, dfa_ops_array[i]->name)) {
-                       ops = dfa_ops_array[i];
+               ops =   dfa_ops_array[i];
+
+               if (!strcmp(name, ops->name))
                        break;
-               }
+               ops = NULL;
        }
 
        if (!ops) {
@@ -204,15 +202,7 @@ void scf_dfa_close(scf_dfa_t* dfa)
                return;
 
        if (dfa->nodes) {
-               int i;
-
-               for (i = 0; i < dfa->nodes->size; i++) {
-                       scf_dfa_node_t* node = dfa->nodes->data[i];
-
-                       scf_dfa_node_free(node);
-                       node = NULL;
-               }
-
+               scf_vector_clear(dfa->nodes, (void (*)(void*) )scf_dfa_node_free);
                scf_vector_free(dfa->nodes);
                dfa->nodes = NULL;
        }
@@ -229,13 +219,15 @@ void scf_dfa_close(scf_dfa_t* dfa)
 int scf_dfa_add_node(scf_dfa_t* dfa, scf_dfa_node_t* node)
 {
        if (!dfa || !node)
-               return -1;
+               return -EINVAL;
 
-       if (!dfa->nodes)
+       if (!dfa->nodes) {
                dfa->nodes = scf_vector_alloc();
+               if (!dfa->nodes)
+                       return -ENOMEM;
+       }
 
-       scf_vector_add(dfa->nodes, node);
-       return 0;
+       return scf_vector_add(dfa->nodes, node);
 }
 
 scf_dfa_node_t* scf_dfa_find_node(scf_dfa_t* dfa, const char* name)
@@ -246,10 +238,11 @@ scf_dfa_node_t* scf_dfa_find_node(scf_dfa_t* dfa, const char* name)
        if (!dfa->nodes)
                return NULL;
 
+       scf_dfa_node_t* node;
        int i;
 
        for (i = 0; i < dfa->nodes->size; i++) {
-               scf_dfa_node_t* node = dfa->nodes->data[i];
+               node      = dfa->nodes->data[i];
 
                if (!strcmp(name, node->name))
                        return node;
@@ -262,20 +255,19 @@ static int _scf_dfa_childs_parse_word(scf_dfa_t* dfa, scf_dfa_node_t** childs, i
 {
        assert(words->size > 0);
 
-       int i = 0;
-       while (i < nb_childs) {
+       int i;
+       for (i = 0; i < nb_childs; i++) {
+
                scf_dfa_node_t* child = childs[i];
                scf_lex_word_t* w     = words->data[words->size - 1];
 
-               scf_logd("nb_childs: %d, child: %s, w: %s\n", nb_childs, child->name, w->text->data);
+               scf_logd("i: %d, nb_childs: %d, child: %s, w: %s\n", i, nb_childs, child->name, w->text->data);
 
                scf_dfa_hook_t* hook = scf_dfa_find_hook(dfa, &(dfa->hooks[SCF_DFA_HOOK_PRE]), w);
                if (hook) {
                        // if pre hook is set, deliver the word to the proper hook node.
-                       if (hook->node != child) {
-                               i++;
+                       if (hook->node != child)
                                continue;
-                       }
 
                        scf_logi("\033[32mpre hook: %s\033[0m\n", hook->node->name);
 
@@ -285,21 +277,17 @@ static int _scf_dfa_childs_parse_word(scf_dfa_t* dfa, scf_dfa_node_t** childs, i
 
                } else {
                        assert(child->is);
-                       if (!child->is(dfa, w)) {
-                               i++;
+                       if (!child->is(dfa, w))
                                continue;
-                       }
                }
 
                int ret = _scf_dfa_node_parse_word(dfa, child, words, data);
 
-               if (SCF_DFA_OK == ret) {
+               if (SCF_DFA_OK == ret)
                        return SCF_DFA_OK;
 
-               else if (SCF_DFA_ERROR == ret)
+               else if (SCF_DFA_ERROR == ret)
                        return SCF_DFA_ERROR;
-
-               i++;
        }
 
        scf_logd("SCF_DFA_NEXT_SYNTAX\n\n");
@@ -433,58 +421,37 @@ _continue:
 
 int scf_dfa_parse_word(scf_dfa_t* dfa, void* word, void* data)
 {
-       if (!dfa || !word) {
-               scf_loge("\n");
-               return -1;
-       }
+       if (!dfa || !word)
+               return -EINVAL;
 
-       if (!dfa->syntaxes || dfa->syntaxes->size <= 0) {
-               scf_loge("\n");
-               return -1;
-       }
+       if (!dfa->syntaxes || dfa->syntaxes->size <= 0)
+               return -EINVAL;
 
-       if (!dfa->ops || !dfa->ops->pop_word) {
-               scf_loge("\n");
-               return -1;
-       }
-
-       scf_vector_t*   words = scf_vector_alloc();
-       scf_lex_word_t* w     = word;
-
-       int ret;
-       int i;
+       if (!dfa->ops || !dfa->ops->pop_word)
+               return -EINVAL;
 
-//     assert(!dfa->words);
-//     dfa->words = scf_vector_alloc();
+       scf_vector_t* words = scf_vector_alloc();
+       if (!words)
+               return -ENOMEM;
 
-       scf_vector_add(words, word);
-//     scf_vector_add(dfa->words, word);
+       int ret = scf_vector_add(words, word);
+       if (ret < 0)
+               return ret;
 
        ret = _scf_dfa_childs_parse_word(dfa, (scf_dfa_node_t**)dfa->syntaxes->data, dfa->syntaxes->size, words, data);
 
-
        if (SCF_DFA_OK != ret) {
                assert(words->size >= 1);
 
-               w = words->data[words->size - 1];
+               scf_lex_word_t* w = words->data[words->size - 1];
                scf_loge("ret: %d, w->type: %d, '%s', line: %d\n\n", ret, w->type, w->text->data, w->line);
 
-               scf_vector_free(words);
-               words = NULL;
-               return SCF_DFA_ERROR;
-       }
-
-       for (i = 0; i < words->size; i++) {
-               scf_lex_word_t* w = words->data[i];
-               scf_logd("##free w: %p, w->type: %d, '%s'\n", w, w->type, w->text->data);
-
-               dfa->ops->free_word(words->data[i]);
-               words->data[i] = NULL;
+               ret = SCF_DFA_ERROR;
        }
 
+       scf_vector_clear(words, (void (*)(void*) )dfa->ops->free_word);
        scf_vector_free(words);
        words = NULL;
 
-       return SCF_DFA_OK;
+       return ret;
 }
-
index cc8d6c7b15cf721cca0eb7f3c3a567e160f2374f..d7583fbd4a7902d6a0c6f8ac83c77f9d81144e29 100644 (file)
@@ -313,4 +313,3 @@ scf_dfa_module_t dfa_module_container =
 
        .fini_module = _dfa_fini_module_container,
 };
-
index dec420d1ccafe812f81b60deeb44d946390ed71e..cf71e8a63438386c51349e8b88296f89f08a61f8 100644 (file)
@@ -262,4 +262,3 @@ scf_dfa_module_t dfa_module_create =
 
        .fini_module = _dfa_fini_module_create,
 };
-
index 85ce9aa05838c113879f5b8463210974f7a6a334..412bd0773a71288d203ba17918dec52af9019b5e 100644 (file)
@@ -943,11 +943,11 @@ static int _dfa_init_syntax_expr(scf_dfa_t* dfa)
        scf_dfa_node_add_child(expr,       semicolon);
 
        // container(ptr, type, member)
-       scf_dfa_node_add_child(expr,       container);
-       scf_dfa_node_add_child(create_rp,  rp);
-       scf_dfa_node_add_child(create_rp,  binary_op);
-       scf_dfa_node_add_child(create_rp,  comma);
-       scf_dfa_node_add_child(create_rp,  semicolon);
+       scf_dfa_node_add_child(expr,         container);
+       scf_dfa_node_add_child(container_rp, rp);
+       scf_dfa_node_add_child(container_rp, binary_op);
+       scf_dfa_node_add_child(container_rp, comma);
+       scf_dfa_node_add_child(container_rp, semicolon);
 
        // create class object
        scf_dfa_node_add_child(expr,       create);
@@ -1028,9 +1028,6 @@ static int _dfa_init_syntax_expr(scf_dfa_t* dfa)
 
        // create class object
        scf_dfa_node_add_child(binary_op,  create);
-       scf_dfa_node_add_child(create_id,  semicolon);
-       scf_dfa_node_add_child(create_rp,  semicolon);
-
        scf_dfa_node_add_child(binary_op,  expr);
 
        scf_dfa_node_add_child(unary_post, rp);
@@ -1063,4 +1060,3 @@ scf_dfa_module_t dfa_module_expr =
 
        .fini_module = _dfa_fini_module_expr,
 };
-
index 4db002cd150da1a410595e3434792c0d1966c6e2..181e5bd0e999ee62d3e5421b6b920185b987f9e8 100644 (file)
@@ -453,4 +453,3 @@ static inline int scf_dfa_is_end_var(scf_dfa_t* dfa, void* word)
 }
 
 #endif
-