From 67e0e63fa9ddeebcbfaef89b43d64cde512735f1 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Sat, 10 Jun 2023 16:53:56 +0800 Subject: [PATCH] fix: 1, const local var print error, 2, break / continue in do while. --- core/scf_operator_handler_3ac.c | 16 +++++++++------ examples/const_local.c | 10 ++++++++++ parse/scf_dfa_util.h | 28 +++++++++++++++++++++++++++ parse/scf_dfa_var.c | 3 ++- parse/scf_operator_handler_semantic.c | 9 ++++++--- 5 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 examples/const_local.c diff --git a/core/scf_operator_handler_3ac.c b/core/scf_operator_handler_3ac.c index e102e6d..034b105 100644 --- a/core/scf_operator_handler_3ac.c +++ b/core/scf_operator_handler_3ac.c @@ -591,15 +591,17 @@ static int _scf_op_break(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* scf_node_t* n = (scf_node_t*)ast->current_block; - while (n && (SCF_OP_WHILE != n->type && SCF_OP_FOR != n->type)) { + while (n + && SCF_OP_WHILE != n->type + && SCF_OP_REPEAT != n->type + && SCF_OP_FOR != n->type) n = n->parent; - } if (!n) { scf_loge("\n"); return -1; } - assert(SCF_OP_WHILE == n->type || SCF_OP_FOR == n->type); + assert(SCF_OP_WHILE == n->type || SCF_OP_FOR == n->type || SCF_OP_REPEAT == n->type); scf_node_t* parent = n->parent; assert(parent); @@ -618,15 +620,17 @@ static int _scf_op_continue(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, vo scf_node_t* n = (scf_node_t*)ast->current_block; - while (n && (SCF_OP_WHILE != n->type && SCF_OP_FOR != n->type)) { + while (n + && SCF_OP_WHILE != n->type + && SCF_OP_REPEAT != n->type + && SCF_OP_FOR != n->type) n = n->parent; - } if (!n) { scf_loge("\n"); return -1; } - assert(SCF_OP_WHILE == n->type || SCF_OP_FOR == n->type); + assert(SCF_OP_WHILE == n->type || SCF_OP_FOR == n->type || SCF_OP_REPEAT == n->type); scf_3ac_code_t* branch = scf_branch_ops_code(SCF_OP_GOTO, NULL, NULL); diff --git a/examples/const_local.c b/examples/const_local.c new file mode 100644 index 0000000..6ddf9e4 --- /dev/null +++ b/examples/const_local.c @@ -0,0 +1,10 @@ +int printf(const char* fmt, ...); + +int main() +{ + const int i = 123; + + printf("%d\n", i); + + return 0; +} diff --git a/parse/scf_dfa_util.h b/parse/scf_dfa_util.h index 9149960..b4868a6 100644 --- a/parse/scf_dfa_util.h +++ b/parse/scf_dfa_util.h @@ -235,6 +235,34 @@ static inline int scf_dfa_is_end_if(scf_dfa_t* dfa, void* word) return SCF_LEX_WORD_KEY_END_IF == w->type; } +static inline int scf_dfa_is_continue(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_KEY_CONTINUE == w->type; +} + +static inline int scf_dfa_is_break(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_KEY_BREAK == w->type; +} + +static inline int scf_dfa_is_return(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_KEY_RETURN == w->type; +} + +static inline int scf_dfa_is_exit(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_KEY_EXIT == w->type; +} + static inline int scf_dfa_is_while(scf_dfa_t* dfa, void* word) { scf_lex_word_t* w = word; diff --git a/parse/scf_dfa_var.c b/parse/scf_dfa_var.c index f979ed7..c7d634c 100644 --- a/parse/scf_dfa_var.c +++ b/parse/scf_dfa_var.c @@ -178,7 +178,8 @@ static int _var_init_expr(scf_dfa_t* dfa, dfa_parse_data_t* d, int semi_flag) d->expr_local_flag--; - if (d->current_var->global_flag) { + if (d->current_var->global_flag + || (d->current_var->const_flag && 0 == d->current_var->nb_pointers + d->current_var->nb_dimentions)) { scf_logw("d->expr: %p, d->current_var: %p, global_flag: %d\n", d->expr, d->current_var, d->current_var->global_flag); diff --git a/parse/scf_operator_handler_semantic.c b/parse/scf_operator_handler_semantic.c index d39748c..9635177 100644 --- a/parse/scf_operator_handler_semantic.c +++ b/parse/scf_operator_handler_semantic.c @@ -1169,15 +1169,18 @@ static int _scf_op_semantic_break(scf_ast_t* ast, scf_node_t** nodes, int nb_nod scf_node_t* n = (scf_node_t*)ast->current_block; - while (n && (SCF_OP_WHILE != n->type && SCF_OP_FOR != n->type)) { + while (n + && SCF_OP_WHILE != n->type + && SCF_OP_REPEAT != n->type + && SCF_OP_FOR != n->type) n = n->parent; - } if (!n) { scf_loge("\n"); return -1; } - assert(SCF_OP_WHILE == n->type || SCF_OP_FOR == n->type); + + assert(SCF_OP_WHILE == n->type || SCF_OP_FOR == n->type || SCF_OP_REPEAT == n->type); if (!n->parent) { scf_loge("\n"); -- 2.25.1