From 6c72787d28d10e8a14159e541f8737de523bd28e Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Fri, 30 Aug 2024 15:44:20 +0800 Subject: [PATCH] optimize some code for lex & DFA module --- core/scf_lex_word.h | 2 +- lex/scf_lex.c | 7 +- lex/scf_lex.h | 2 - parse/scf_dfa.c | 145 +++++++++++++++----------------------- parse/scf_dfa_container.c | 1 - parse/scf_dfa_create.c | 1 - parse/scf_dfa_expr.c | 14 ++-- parse/scf_dfa_util.h | 1 - 8 files changed, 65 insertions(+), 108 deletions(-) diff --git a/core/scf_lex_word.h b/core/scf_lex_word.h index c8c978b..c10952b 100644 --- a/core/scf_lex_word.h +++ b/core/scf_lex_word.h @@ -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) diff --git a/lex/scf_lex.c b/lex/scf_lex.c index ff80b9e..6b953b0 100644 --- a/lex/scf_lex.c +++ b/lex/scf_lex.c @@ -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); } diff --git a/lex/scf_lex.h b/lex/scf_lex.h index cdc606f..9d42928 100644 --- a/lex/scf_lex.h +++ b/lex/scf_lex.h @@ -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; diff --git a/parse/scf_dfa.c b/parse/scf_dfa.c index 210f414..96b773c 100644 --- a/parse/scf_dfa.c +++ b/parse/scf_dfa.c @@ -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; } - diff --git a/parse/scf_dfa_container.c b/parse/scf_dfa_container.c index cc8d6c7..d7583fb 100644 --- a/parse/scf_dfa_container.c +++ b/parse/scf_dfa_container.c @@ -313,4 +313,3 @@ scf_dfa_module_t dfa_module_container = .fini_module = _dfa_fini_module_container, }; - diff --git a/parse/scf_dfa_create.c b/parse/scf_dfa_create.c index dec420d..cf71e8a 100644 --- a/parse/scf_dfa_create.c +++ b/parse/scf_dfa_create.c @@ -262,4 +262,3 @@ scf_dfa_module_t dfa_module_create = .fini_module = _dfa_fini_module_create, }; - diff --git a/parse/scf_dfa_expr.c b/parse/scf_dfa_expr.c index 85ce9aa..412bd07 100644 --- a/parse/scf_dfa_expr.c +++ b/parse/scf_dfa_expr.c @@ -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, }; - diff --git a/parse/scf_dfa_util.h b/parse/scf_dfa_util.h index 4db002c..181e5bd 100644 --- a/parse/scf_dfa_util.h +++ b/parse/scf_dfa_util.h @@ -453,4 +453,3 @@ static inline int scf_dfa_is_end_var(scf_dfa_t* dfa, void* word) } #endif - -- 2.25.1