From: yu.dongliang <18588496441@163.com> Date: Mon, 30 Sep 2024 11:32:45 +0000 (+0800) Subject: struct & array init with '{.key = value, } ok X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=535eb16d3763c4c22aff52eed1aee05583222666;p=scf.git struct & array init with '{.key = value, } ok --- diff --git a/core/scf_lex_word.h b/core/scf_lex_word.h index d8ebde8..f6f460a 100644 --- a/core/scf_lex_word.h +++ b/core/scf_lex_word.h @@ -157,17 +157,18 @@ enum scf_lex_words // const literal value SCF_LEX_WORD_CONST_CHAR, - SCF_LEX_WORD_CONST_STRING, - - SCF_LEX_WORD_CONST_FLOAT, - SCF_LEX_WORD_CONST_DOUBLE, - SCF_LEX_WORD_CONST_COMPLEX, SCF_LEX_WORD_CONST_INT, SCF_LEX_WORD_CONST_U32, SCF_LEX_WORD_CONST_I64, SCF_LEX_WORD_CONST_U64, + SCF_LEX_WORD_CONST_FLOAT, + SCF_LEX_WORD_CONST_DOUBLE, + SCF_LEX_WORD_CONST_COMPLEX, + + SCF_LEX_WORD_CONST_STRING, + SCF_LEX_WORD_ID, // identity, start of _, a-z, A-Z, may include 0-9 }; @@ -221,6 +222,11 @@ static inline int scf_lex_is_operator(scf_lex_word_t* w) } static inline int scf_lex_is_const(scf_lex_word_t* w) +{ + return w->type >= SCF_LEX_WORD_CONST_CHAR && w->type <= SCF_LEX_WORD_CONST_STRING; +} + +static inline int scf_lex_is_const_integer(scf_lex_word_t* w) { return w->type >= SCF_LEX_WORD_CONST_CHAR && w->type <= SCF_LEX_WORD_CONST_U64; } diff --git a/core/scf_scope.c b/core/scf_scope.c index 4a9faf1..8790ebe 100644 --- a/core/scf_scope.c +++ b/core/scf_scope.c @@ -95,13 +95,12 @@ void scf_scope_free(scf_scope_t* scope) scf_type_t* scf_scope_find_type(scf_scope_t* scope, const char* name) { + scf_type_t* t; scf_list_t* l; - for (l = scf_list_head(&scope->type_list_head); - l != scf_list_sentinel(&scope->type_list_head); l = scf_list_next(l)) { + for (l = scf_list_head(&scope->type_list_head); l != scf_list_sentinel(&scope->type_list_head); l = scf_list_next(l)) { + t = scf_list_data(l, scf_type_t, list); - //printf("%s(),%d, scope: %p, name: %s\n", __func__, __LINE__, scope, name); - scf_type_t* t = scf_list_data(l, scf_type_t, list); if (!strcmp(name, t->name->data)) { return t; } @@ -111,12 +110,12 @@ scf_type_t* scf_scope_find_type(scf_scope_t* scope, const char* name) scf_type_t* scf_scope_find_type_type(scf_scope_t* scope, const int type) { + scf_type_t* t; scf_list_t* l; - for (l = scf_list_head(&scope->type_list_head); - l != scf_list_sentinel(&scope->type_list_head); l = scf_list_next(l)) { + for (l = scf_list_head(&scope->type_list_head); l != scf_list_sentinel(&scope->type_list_head); l = scf_list_next(l)) { + t = scf_list_data(l, scf_type_t, list); - scf_type_t* t = scf_list_data(l, scf_type_t, list); if (type == t->type) return t; } @@ -125,13 +124,12 @@ scf_type_t* scf_scope_find_type_type(scf_scope_t* scope, const int type) scf_variable_t* scf_scope_find_variable(scf_scope_t* scope, const char* name) { + scf_variable_t* v; int i; for (i = 0; i < scope->vars->size; i++) { + v = scope->vars->data[i]; - scf_variable_t* v = scope->vars->data[i]; - - //printf("%s(),%d, scope: %p, name: %s, v: %s\n", __func__, __LINE__, scope, name, v->w->text->data); if (v->w && !strcmp(name, v->w->text->data)) return v; } @@ -140,12 +138,11 @@ scf_variable_t* scf_scope_find_variable(scf_scope_t* scope, const char* name) scf_label_t* scf_scope_find_label(scf_scope_t* scope, const char* name) { - scf_list_t* l; + scf_label_t* label; + scf_list_t* l; - for (l = scf_list_head(&scope->label_list_head); - l != scf_list_sentinel(&scope->label_list_head); l = scf_list_next(l)) { - - scf_label_t* label = scf_list_data(l, scf_label_t, list); + for (l = scf_list_head(&scope->label_list_head); l != scf_list_sentinel(&scope->label_list_head); l = scf_list_next(l)) { + label = scf_list_data(l, scf_label_t, list); if (!strcmp(name, label->w->text->data)) return label; @@ -158,10 +155,8 @@ scf_function_t* scf_scope_find_function(scf_scope_t* scope, const char* name) scf_function_t* f; scf_list_t* l; - for (l = scf_list_head(&scope->function_list_head); - l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { - - f = scf_list_data(l, scf_function_t, list); + for (l = scf_list_head(&scope->function_list_head); l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { + f = scf_list_data(l, scf_function_t, list); if (!strcmp(name, f->node.w->text->data)) return f; @@ -171,12 +166,11 @@ scf_function_t* scf_scope_find_function(scf_scope_t* scope, const char* name) scf_function_t* scf_scope_find_same_function(scf_scope_t* scope, scf_function_t* f0) { - scf_list_t* l; - - for (l = scf_list_head(&scope->function_list_head); - l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { + scf_function_t* f1; + scf_list_t* l; - scf_function_t* f1 = scf_list_data(l, scf_function_t, list); + for (l = scf_list_head(&scope->function_list_head); l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { + f1 = scf_list_data(l, scf_function_t, list); if (scf_function_same(f0, f1)) return f1; @@ -186,12 +180,11 @@ scf_function_t* scf_scope_find_same_function(scf_scope_t* scope, scf_function_t* scf_function_t* scf_scope_find_proper_function(scf_scope_t* scope, const char* name, scf_vector_t* argv) { - scf_list_t* l; - - for (l = scf_list_head(&scope->function_list_head); - l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { + scf_function_t* f; + scf_list_t* l; - scf_function_t* f = scf_list_data(l, scf_function_t, list); + for (l = scf_list_head(&scope->function_list_head); l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { + f = scf_list_data(l, scf_function_t, list); if (strcmp(f->node.w->text->data, name)) continue; @@ -204,6 +197,7 @@ scf_function_t* scf_scope_find_proper_function(scf_scope_t* scope, const char* n int scf_scope_find_like_functions(scf_vector_t** pfunctions, scf_scope_t* scope, const char* name, scf_vector_t* argv) { + scf_function_t* f; scf_vector_t* vec; scf_list_t* l; @@ -211,10 +205,8 @@ int scf_scope_find_like_functions(scf_vector_t** pfunctions, scf_scope_t* scope, if (!vec) return -ENOMEM; - for (l = scf_list_head(&scope->function_list_head); - l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { - - scf_function_t* f = scf_list_data(l, scf_function_t, list); + for (l = scf_list_head(&scope->function_list_head); l != scf_list_sentinel(&scope->function_list_head); l = scf_list_next(l)) { + f = scf_list_data(l, scf_function_t, list); if (strcmp(f->node.w->text->data, name)) continue; @@ -240,6 +232,7 @@ int scf_scope_find_like_functions(scf_vector_t** pfunctions, scf_scope_t* scope, int scf_scope_find_overloaded_functions(scf_vector_t** pfunctions, scf_scope_t* scope, const int op_type, scf_vector_t* argv) { + scf_function_t* f; scf_vector_t* vec; scf_list_t* l; @@ -247,10 +240,8 @@ int scf_scope_find_overloaded_functions(scf_vector_t** pfunctions, scf_scope_t* if (!vec) return -ENOMEM; - for (l = scf_list_head(&scope->operator_list_head); - l != scf_list_sentinel(&scope->operator_list_head); l = scf_list_next(l)) { - - scf_function_t* f = scf_list_data(l, scf_function_t, list); + for (l = scf_list_head(&scope->operator_list_head); l != scf_list_sentinel(&scope->operator_list_head); l = scf_list_next(l)) { + f = scf_list_data(l, scf_function_t, list); if (op_type != f->op_type) continue; diff --git a/core/scf_type_cast.c b/core/scf_type_cast.c index e0e7bab..3279738 100644 --- a/core/scf_type_cast.c +++ b/core/scf_type_cast.c @@ -96,8 +96,7 @@ scf_type_cast_t* scf_find_base_type_cast(int src_type, int dst_type) int scf_type_cast_check(scf_ast_t* ast, scf_variable_t* dst, scf_variable_t* src) { if (!dst->const_flag && src->const_flag) { - scf_logw("type cast %s -> %s discard 'const'\n", - src->w->text->data, dst->w->text->data); + scf_logw("type cast %s -> %s discard 'const'\n", src->w->text->data, dst->w->text->data); } scf_string_t* dst_type = NULL; diff --git a/core/scf_variable.h b/core/scf_variable.h index 212c6bc..564ab14 100644 --- a/core/scf_variable.h +++ b/core/scf_variable.h @@ -114,7 +114,7 @@ static inline int scf_variable_const(scf_variable_t* v) return v->const_flag && 0 == v->nb_pointers && 0 == v->nb_dimentions; } -static inline int scf_variable_const_interger(scf_variable_t* v) +static inline int scf_variable_const_integer(scf_variable_t* v) { return scf_type_is_integer(v->type) && v->const_flag && 0 == v->nb_pointers && 0 == v->nb_dimentions; } @@ -133,7 +133,7 @@ static inline int scf_variable_float(scf_variable_t* v) return scf_type_is_float(v->type) && 0 == v->nb_pointers && 0 == v->nb_dimentions; } -static inline int scf_variable_interger(scf_variable_t* v) +static inline int scf_variable_integer(scf_variable_t* v) { return scf_type_is_integer(v->type) || v->nb_pointers > 0 || v->nb_dimentions > 0; } @@ -183,4 +183,3 @@ static inline int scf_variable_may_malloced(scf_variable_t* v) } #endif - diff --git a/examples/init_struct_array.c b/examples/init_struct_array.c new file mode 100644 index 0000000..5aaab2b --- /dev/null +++ b/examples/init_struct_array.c @@ -0,0 +1,33 @@ +int printf(const char* fmt, ...); + +struct S +{ + char* name; + int type; +}; + +S s = +{ + .name = "sss", + .type = 123, +}; + +int a[4] = +{ + .0 = 1, + .1 = 2, + .2 = 3, + .3 = 4, +}; + +int main() +{ + printf("s->name: %s\n", s->name); + printf("s->type: %d\n", s->type); + + printf("a[0]: %d\n", a[0]); + printf("a[1]: %d\n", a[1]); + printf("a[2]: %d\n", a[2]); + printf("a[3]: %d\n", a[3]); + return 0; +} diff --git a/examples/oop.c b/examples/oop.c index ae6d1fb..f6cad9a 100644 --- a/examples/oop.c +++ b/examples/oop.c @@ -21,7 +21,7 @@ int sub(int i, int j) Aops aops = { add, - sub, + .pt1 = sub, }; int main() diff --git a/lex/scf_lex_util.c b/lex/scf_lex_util.c index 67d765b..1379a8e 100644 --- a/lex/scf_lex_util.c +++ b/lex/scf_lex_util.c @@ -550,7 +550,7 @@ int _lex_dot(scf_lex_t* lex, scf_lex_word_t** pword, scf_char_t* c0) return ret; } - if (SCF_LEX_WORD_CONST_INT <= w1->type && w1->type <= SCF_LEX_WORD_CONST_U64) { + if (SCF_LEX_WORD_CONST_CHAR <= w1->type && w1->type <= SCF_LEX_WORD_CONST_U64) { ret = __lex_pop_word(lex, &w2); if (ret < 0) { diff --git a/native/risc/scf_risc_inst.c b/native/risc/scf_risc_inst.c index 79459f6..1f3a94a 100644 --- a/native/risc/scf_risc_inst.c +++ b/native/risc/scf_risc_inst.c @@ -2158,7 +2158,7 @@ static int _risc_inst_div_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s1->color) { - if (!scf_variable_const_interger(s1->var)) { + if (!scf_variable_const_integer(s1->var)) { scf_loge("\n"); return -EINVAL; } @@ -2175,7 +2175,7 @@ static int _risc_inst_div_handler(scf_native_t* ctx, scf_3ac_code_t* c) } else if (0 == s0->color) { - if (!scf_variable_const_interger(s0->var)) { + if (!scf_variable_const_integer(s0->var)) { scf_loge("\n"); return -EINVAL; } @@ -2238,7 +2238,7 @@ static int _risc_inst_mod_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s1->color) { - if (!scf_variable_const_interger(s1->var)) { + if (!scf_variable_const_integer(s1->var)) { scf_loge("\n"); return -EINVAL; } @@ -2255,7 +2255,7 @@ static int _risc_inst_mod_handler(scf_native_t* ctx, scf_3ac_code_t* c) } else if (0 == s0->color) { - if (!scf_variable_const_interger(s0->var)) { + if (!scf_variable_const_integer(s0->var)) { scf_loge("\n"); return -EINVAL; } @@ -2337,7 +2337,7 @@ static int _risc_inst_mul_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s1->color) { - if (!scf_variable_const_interger(s1->var)) { + if (!scf_variable_const_integer(s1->var)) { scf_loge("\n"); return -EINVAL; } @@ -2421,7 +2421,7 @@ static int _risc_inst_mul_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s->color) { - if (!scf_variable_const_interger(s->var)) { + if (!scf_variable_const_integer(s->var)) { scf_loge("\n"); return -EINVAL; } @@ -2496,7 +2496,7 @@ static int _risc_inst_add_handler(scf_native_t* ctx, scf_3ac_code_t* c) return -EINVAL; } - if (!scf_variable_const_interger(s1->var)) { + if (!scf_variable_const_integer(s1->var)) { scf_loge("\n"); return -EINVAL; } @@ -2586,7 +2586,7 @@ static int _risc_inst_add_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c) return -EINVAL; } - if (!scf_variable_const_interger(s->var)) { + if (!scf_variable_const_integer(s->var)) { scf_loge("\n"); return -EINVAL; } @@ -2674,7 +2674,7 @@ static int _risc_inst_sub_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c) return -EINVAL; } - if (!scf_variable_const_interger(s->var)) { + if (!scf_variable_const_integer(s->var)) { scf_loge("\n"); return -EINVAL; } @@ -2753,7 +2753,7 @@ static int _risc_inst_sub_handler(scf_native_t* ctx, scf_3ac_code_t* c) return -EINVAL; } - if (!scf_variable_const_interger(s1->var)) { + if (!scf_variable_const_integer(s1->var)) { scf_loge("\n"); return -EINVAL; } @@ -2824,7 +2824,7 @@ static int _risc_inst_bit_and_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s1->color) { - if (!scf_variable_const_interger(s1->var)) { + if (!scf_variable_const_integer(s1->var)) { scf_loge("\n"); return -EINVAL; } @@ -2888,7 +2888,7 @@ static int _risc_inst_bit_or_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s1->color) { - if (!scf_variable_const_interger(s1->var)) { + if (!scf_variable_const_integer(s1->var)) { scf_loge("\n"); return -EINVAL; } @@ -2963,7 +2963,7 @@ static int _risc_inst_and_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s->color) { - if (!scf_variable_const_interger(s->var)) { + if (!scf_variable_const_integer(s->var)) { scf_loge("\n"); return -EINVAL; } @@ -3038,7 +3038,7 @@ static int _risc_inst_or_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s->color) { - if (!scf_variable_const_interger(s->var)) { + if (!scf_variable_const_integer(s->var)) { scf_loge("\n"); return -EINVAL; } @@ -3359,7 +3359,7 @@ static int _risc_inst_cast_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s->color) { - if (!scf_variable_const_interger(vs)) + if (!scf_variable_const_integer(vs)) return -EINVAL; if (src_size < dst_size) @@ -3392,7 +3392,7 @@ static int _risc_inst_cast_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s->color) { - if (!scf_variable_const_interger(vs)) + if (!scf_variable_const_integer(vs)) return -EINVAL; if (src_size < dst_size) @@ -3474,7 +3474,7 @@ static int _risc_inst_div_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s->color) { - if (!scf_variable_const_interger(s->var)) { + if (!scf_variable_const_integer(s->var)) { scf_loge("\n"); return -EINVAL; } @@ -3549,7 +3549,7 @@ static int _risc_inst_mod_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (0 == s->color) { - if (!scf_variable_const_interger(s->var)) { + if (!scf_variable_const_integer(s->var)) { scf_loge("\n"); return -EINVAL; } @@ -4036,7 +4036,7 @@ static int _risc_inst_assign_handler(scf_native_t* ctx, scf_3ac_code_t* c) if (scf_variable_const_string(v)) return ctx->iops->ISTR2G(c, f, rd, v); - if (!scf_variable_const_interger(v)) { + if (!scf_variable_const_integer(v)) { scf_loge("\n"); return -EINVAL; } diff --git a/parse/scf_dfa_expr.c b/parse/scf_dfa_expr.c index 6bfff2e..a55b24c 100644 --- a/parse/scf_dfa_expr.c +++ b/parse/scf_dfa_expr.c @@ -725,7 +725,7 @@ int _expr_multi_rets(scf_expr_t* e) parent->nodes[i] = e; parent->nb_nodes = i + 1; - scf_loge("parent->nb_nodes: %d\n", parent->nb_nodes); + scf_logd("parent->nb_nodes: %d\n", parent->nb_nodes); return 0; } diff --git a/parse/scf_dfa_init_data.c b/parse/scf_dfa_init_data.c index d9fce44..861ce9f 100644 --- a/parse/scf_dfa_init_data.c +++ b/parse/scf_dfa_init_data.c @@ -10,103 +10,106 @@ int scf_struct_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_ int _expr_add_var(scf_parse_t* parse, dfa_data_t* d); typedef struct { - int nb_lbs; - int nb_rbs; + scf_vector_t* init_exprs; -} data_module_data_t; + dfa_index_t* current_index; + int current_n; + + int current_dim; + + int nb_lbs; + int nb_rbs; +} init_module_data_t; static int _do_data_init(scf_dfa_t* dfa, scf_vector_t* words, dfa_data_t* d) { scf_parse_t* parse = dfa->priv; scf_variable_t* var = d->current_var; scf_lex_word_t* w = words->data[words->size - 1]; - data_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; dfa_init_expr_t* ie; int ret = -1; int i = 0; if (d->current_var->nb_dimentions > 0) - ret = scf_array_init(parse->ast, w, d->current_var, d->init_exprs); + ret = scf_array_init(parse->ast, w, d->current_var, md->init_exprs); else if (d->current_var->type >= SCF_STRUCT) - ret = scf_struct_init(parse->ast, w, d->current_var, d->init_exprs); + ret = scf_struct_init(parse->ast, w, d->current_var, md->init_exprs); if (ret < 0) goto error; - if (d->current_var->global_flag) { - - for (i = 0; i < d->init_exprs->size; i++) { - ie = d->init_exprs->data[i]; + for (i = 0; i < md->init_exprs->size; i++) { + ie = md->init_exprs->data[i]; - assert(!ie->current_index); + if (d->current_var->global_flag) { ret = scf_expr_calculate(parse->ast, ie->expr, NULL); if (ret < 0) goto error; scf_expr_free(ie->expr); - free(ie); - ie = NULL; - } - } else { - for (i = 0; i < d->init_exprs->size; i++) { - ie = d->init_exprs->data[i]; - - assert(!ie->current_index); + ie->expr = NULL; + } else { ret = scf_node_add_child((scf_node_t*)parse->ast->current_block, ie->expr); if (ret < 0) goto error; - - free(ie); - ie = NULL; + ie->expr = NULL; } + + free(ie); + ie = NULL; } error: - for (; i < d->init_exprs->size; i++) { - ie = d->init_exprs->data[i]; - - assert(!ie->current_index); + for (; i < md->init_exprs->size; i++) { + ie = md->init_exprs->data[i]; scf_expr_free(ie->expr); free(ie); ie = NULL; } - scf_vector_free(d->init_exprs); - d->init_exprs = NULL; + scf_vector_free(md->init_exprs); + md->init_exprs = NULL; - scf_vector_free(d->current_index); - d->current_index = NULL; + free(md->current_index); + md->current_index = NULL; - d->current_dim = -1; - md->nb_lbs = 0; - md->nb_rbs = 0; + md->current_dim = -1; + md->nb_lbs = 0; + md->nb_rbs = 0; return ret; } static int _add_data_init_expr(scf_dfa_t* dfa, scf_vector_t* words, dfa_data_t* d) { + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + dfa_init_expr_t* ie; + assert(!d->expr->parent); - assert(d->current_dim >= 0); + assert(md->current_dim >= 0); - scf_logi("current_dim: %d, d->expr: %p\n", d->current_dim, d->expr); + size_t N = sizeof(dfa_index_t) * md->current_n; - dfa_init_expr_t* init_expr = malloc(sizeof(dfa_init_expr_t)); - if (!init_expr) { - scf_loge("alloc init_expr failed\n"); - return SCF_DFA_ERROR; - } + ie = malloc(sizeof(dfa_init_expr_t) + N); + if (!ie) + return -ENOMEM; + + ie->expr = d->expr; + d ->expr = NULL; + ie->n = md->current_n; - init_expr->expr = d->expr; - d->expr = NULL; - init_expr->current_index = scf_vector_clone(d->current_index); + memcpy(ie->index, md->current_index, N); + + int ret = scf_vector_add(md->init_exprs, ie); + if (ret < 0) + return ret; - scf_vector_add(d->init_exprs, init_expr); return SCF_DFA_OK; } @@ -114,10 +117,10 @@ static int _data_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; dfa_data_t* d = data; - data_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; - if (d->current_dim < 0) { - scf_logi("d->current_dim: %d\n", d->current_dim); + if (md->current_dim < 0) { + scf_logi("md->current_dim: %d\n", md->current_dim); return SCF_DFA_NEXT_SYNTAX; } @@ -126,56 +129,104 @@ static int _data_action_comma(scf_dfa_t* dfa, scf_vector_t* words, void* data) return SCF_DFA_ERROR; } - intptr_t index = (intptr_t)d->current_index->data[d->current_dim] + 1; - d->current_index->data[d->current_dim] = (void*)index; + md->current_index[md->current_dim].w = NULL; + md->current_index[md->current_dim].i++; - scf_logi("\033[31m d->current_dim[%d]: %ld\033[0m\n", d->current_dim, index); + intptr_t i = md->current_index[md->current_dim].i; + + scf_logi("\033[31m md->current_dim[%d]: %ld\033[0m\n", md->current_dim, i); SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "init_data_comma"), SCF_DFA_HOOK_POST); - return SCF_DFA_NEXT_WORD; + return SCF_DFA_SWITCH_TO; } - -static int _data_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) +static int _data_action_entry(scf_dfa_t* dfa, scf_vector_t* words, void* data) { - if (words->size < 2) { - scf_loge("\n"); - return SCF_DFA_ERROR; - } - scf_parse_t* parse = dfa->priv; dfa_data_t* d = data; - scf_lex_word_t* w = words->data[words->size - 2]; - data_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + scf_lex_word_t* w = words->data[words->size - 1]; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; - if (SCF_LEX_WORD_ASSIGN == w->type) { - memset(md, 0, sizeof(data_module_data_t)); + md->nb_lbs = 0; + md->nb_rbs = 0; - assert(!d->init_exprs); - d->init_exprs = scf_vector_alloc(); + assert(!md->init_exprs); + assert(!md->current_index); - d->current_dim = -1; - md->nb_lbs = 0; - md->nb_rbs = 0; - d->current_index = scf_vector_alloc(); + md->init_exprs = scf_vector_alloc(); + if (!md->init_exprs) + return -ENOMEM; - d->expr_local_flag = 1; + md->current_dim = -1; + md->current_n = 0; - SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "init_data_comma"), SCF_DFA_HOOK_POST); - } + d->expr_local_flag = 1; + + SCF_DFA_PUSH_HOOK(scf_dfa_find_node(dfa, "init_data_comma"), SCF_DFA_HOOK_POST); + + return SCF_DFA_CONTINUE; +} + +static int _data_action_lb(scf_dfa_t* dfa, scf_vector_t* words, void* data) +{ + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; d->expr = NULL; - d->current_dim++; + md->current_dim++; md->nb_lbs++; - if (d->current_dim >= d->current_index->size) - scf_vector_add(d->current_index, 0); - assert(d->current_dim < d->current_index->size); + if (md->current_dim >= md->current_n) { + + void* p = realloc(md->current_index, sizeof(dfa_index_t) * (md->current_dim + 1)); + if (!p) + return -ENOMEM; + md->current_index = p; + md->current_n = md->current_dim + 1; + } int i; - for (i = d->current_dim + 1; i < d->current_index->size; i++) - d->current_index->data[i] = 0; + for (i = md->current_dim; i < md->current_n; i++) { + + md->current_index[i].w = NULL; + md->current_index[i].i = 0; + } + + return SCF_DFA_NEXT_WORD; +} + +static int _data_action_member(scf_dfa_t* dfa, scf_vector_t* words, void* data) +{ + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + + if (md->current_dim >= md->current_n) { + scf_loge("init data not right, file: %s, line: %d\n", w->file->data, w->line); + return SCF_DFA_ERROR; + } + + md->current_index[md->current_dim].w = w; + + return SCF_DFA_NEXT_WORD; +} + +static int _data_action_index(scf_dfa_t* dfa, scf_vector_t* words, void* data) +{ + scf_parse_t* parse = dfa->priv; + dfa_data_t* d = data; + scf_lex_word_t* w = words->data[words->size - 1]; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + + if (md->current_dim >= md->current_n) { + scf_loge("init data not right, file: %s, line: %d\n", w->file->data, w->line); + return SCF_DFA_ERROR; + } + + md->current_index[md->current_dim].i = w->data.u64; return SCF_DFA_NEXT_WORD; } @@ -184,7 +235,7 @@ static int _data_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) { scf_parse_t* parse = dfa->priv; dfa_data_t* d = data; - data_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; if (d->expr) { dfa_identity_t* id = scf_stack_top(d->current_identities); @@ -199,23 +250,22 @@ static int _data_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) } md->nb_rbs++; - d->current_dim--; + md->current_dim--; int i; - for (i = d->current_dim + 1; i < d->current_index->size; i++) - d->current_index->data[i] = 0; + for (i = md->current_dim + 1; i < md->current_n; i++) { + + md->current_index[i].w = NULL; + md->current_index[i].i = 0; + } if (md->nb_rbs == md->nb_lbs) { d->expr_local_flag = 0; - if (_do_data_init(dfa, words, d) < 0) { - scf_loge("do data init failed\n"); + if (_do_data_init(dfa, words, d) < 0) return SCF_DFA_ERROR; - } scf_dfa_del_hook_by_name(&(dfa->hooks[SCF_DFA_HOOK_POST]), "init_data_comma"); - - scf_logi("\n"); } return SCF_DFA_NEXT_WORD; @@ -223,21 +273,25 @@ static int _data_action_rb(scf_dfa_t* dfa, scf_vector_t* words, void* data) static int _dfa_init_module_init_data(scf_dfa_t* dfa) { - SCF_DFA_MODULE_NODE(dfa, init_data, comma, scf_dfa_is_comma, _data_action_comma); - SCF_DFA_MODULE_NODE(dfa, init_data, lb, scf_dfa_is_lb, _data_action_lb); - SCF_DFA_MODULE_NODE(dfa, init_data, rb, scf_dfa_is_rb, _data_action_rb); + SCF_DFA_MODULE_NODE(dfa, init_data, entry, scf_dfa_is_lb, _data_action_entry); + SCF_DFA_MODULE_NODE(dfa, init_data, comma, scf_dfa_is_comma, _data_action_comma); + SCF_DFA_MODULE_NODE(dfa, init_data, lb, scf_dfa_is_lb, _data_action_lb); + SCF_DFA_MODULE_NODE(dfa, init_data, rb, scf_dfa_is_rb, _data_action_rb); + + SCF_DFA_MODULE_NODE(dfa, init_data, dot, scf_dfa_is_dot, scf_dfa_action_next); + SCF_DFA_MODULE_NODE(dfa, init_data, member, scf_dfa_is_identity, _data_action_member); + SCF_DFA_MODULE_NODE(dfa, init_data, index, scf_dfa_is_const_integer, _data_action_index); + SCF_DFA_MODULE_NODE(dfa, init_data, assign, scf_dfa_is_assign, scf_dfa_action_next); scf_parse_t* parse = dfa->priv; dfa_data_t* d = parse->dfa_data; - data_module_data_t* md = d->module_datas[dfa_module_init_data.index]; + init_module_data_t* md = d->module_datas[dfa_module_init_data.index]; assert(!md); - md = calloc(1, sizeof(data_module_data_t)); - if (!md) { - scf_loge("module data alloc failed\n"); - return SCF_DFA_ERROR; - } + md = calloc(1, sizeof(init_module_data_t)); + if (!md) + return -ENOMEM; d->module_datas[dfa_module_init_data.index] = md; @@ -246,12 +300,20 @@ static int _dfa_init_module_init_data(scf_dfa_t* dfa) static int _dfa_init_syntax_init_data(scf_dfa_t* dfa) { - SCF_DFA_GET_MODULE_NODE(dfa, init_data, comma, comma); - SCF_DFA_GET_MODULE_NODE(dfa, init_data, lb, lb); - SCF_DFA_GET_MODULE_NODE(dfa, init_data, rb, rb); - SCF_DFA_GET_MODULE_NODE(dfa, expr, entry, expr); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, entry, entry); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, comma, comma); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, lb, lb); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, rb, rb); + + SCF_DFA_GET_MODULE_NODE(dfa, init_data, dot, dot); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, member, member); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, index, index); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, assign, assign); + + SCF_DFA_GET_MODULE_NODE(dfa, expr, entry, expr); // empty init, use 0 to fill the data + scf_dfa_node_add_child(entry, lb); scf_dfa_node_add_child(lb, rb); // multi-dimention data init @@ -261,11 +323,21 @@ static int _dfa_init_syntax_init_data(scf_dfa_t* dfa) scf_dfa_node_add_child(comma, lb); // init expr for member of data + scf_dfa_node_add_child(lb, dot); + scf_dfa_node_add_child(comma, dot); + scf_dfa_node_add_child(lb, expr); scf_dfa_node_add_child(expr, comma); scf_dfa_node_add_child(comma, expr); scf_dfa_node_add_child(expr, rb); + scf_dfa_node_add_child(dot, member); + scf_dfa_node_add_child(member, assign); + scf_dfa_node_add_child(assign, expr); + + scf_dfa_node_add_child(dot, index); + scf_dfa_node_add_child(index, assign); + return 0; } diff --git a/parse/scf_dfa_util.h b/parse/scf_dfa_util.h index 693b221..46801e7 100644 --- a/parse/scf_dfa_util.h +++ b/parse/scf_dfa_util.h @@ -67,6 +67,13 @@ static inline int scf_dfa_is_range(scf_dfa_t* dfa, void* word) return SCF_LEX_WORD_RANGE == w->type; } +static inline int scf_dfa_is_dot(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return SCF_LEX_WORD_DOT == w->type; +} + static inline int scf_dfa_is_comma(scf_dfa_t* dfa, void* word) { scf_lex_word_t* w = word; @@ -123,6 +130,13 @@ static inline int scf_dfa_is_identity(scf_dfa_t* dfa, void* word) return scf_lex_is_identity(w); } +static inline int scf_dfa_is_const_integer(scf_dfa_t* dfa, void* word) +{ + scf_lex_word_t* w = word; + + return scf_lex_is_const_integer(w); +} + static inline int scf_dfa_is_base_type(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 b261507..7eb38ce 100644 --- a/parse/scf_dfa_var.c +++ b/parse/scf_dfa_var.c @@ -173,7 +173,7 @@ static int _var_init_expr(scf_dfa_t* dfa, dfa_data_t* d, int semi_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", + scf_logd("d->expr: %p, d->current_var: %p, global_flag: %d\n", d->expr, d->current_var, d->current_var->global_flag); if (scf_expr_calculate(parse->ast, d->expr, NULL) < 0) { @@ -188,7 +188,7 @@ static int _var_init_expr(scf_dfa_t* dfa, dfa_data_t* d, int semi_flag) scf_node_add_child((scf_node_t*)parse->ast->current_block, (scf_node_t*)d->expr); - scf_loge("d->expr->parent->type: %d\n", d->expr->parent->type); + scf_logd("d->expr->parent->type: %d\n", d->expr->parent->type); if (_expr_multi_rets(d->expr) < 0) { scf_loge("\n"); @@ -417,7 +417,7 @@ static int _dfa_init_syntax_var(scf_dfa_t* dfa) SCF_DFA_GET_MODULE_NODE(dfa, expr, entry, expr); - SCF_DFA_GET_MODULE_NODE(dfa, init_data, lb, init_data_lb); + SCF_DFA_GET_MODULE_NODE(dfa, init_data, entry, init_data); SCF_DFA_GET_MODULE_NODE(dfa, init_data, rb, init_data_rb); @@ -443,8 +443,8 @@ static int _dfa_init_syntax_var(scf_dfa_t* dfa) scf_dfa_node_add_child(expr, comma); scf_dfa_node_add_child(expr, semicolon); - // struct or array var init - scf_dfa_node_add_child(assign, init_data_lb); + // struct or array init + scf_dfa_node_add_child(assign, init_data); scf_dfa_node_add_child(init_data_rb, comma); scf_dfa_node_add_child(init_data_rb, semicolon); diff --git a/parse/scf_operator_handler_const.c b/parse/scf_operator_handler_const.c index 3015431..7039425 100644 --- a/parse/scf_operator_handler_const.c +++ b/parse/scf_operator_handler_const.c @@ -572,7 +572,7 @@ static int _scf_op_const_type_cast(scf_ast_t* ast, scf_node_t** nodes, int nb_no } else result->const_flag = 0; - if (scf_variable_interger(src) && scf_variable_interger(dst)) { + if (scf_variable_integer(src) && scf_variable_integer(dst)) { int size; if (src ->nb_dimentions > 0) diff --git a/parse/scf_operator_handler_expr.c b/parse/scf_operator_handler_expr.c index ac2f9eb..1e42350 100644 --- a/parse/scf_operator_handler_expr.c +++ b/parse/scf_operator_handler_expr.c @@ -177,7 +177,7 @@ static int _scf_op_expr_type_cast(scf_ast_t* ast, scf_node_t** nodes, int nb_nod if (parent->w) SCF_XCHG(r->w, parent->w); - scf_loge("parent: %p\n", parent); + scf_logd("parent: %p\n", parent); scf_node_free_data(parent); parent->type = r->type; parent->var = r; @@ -193,7 +193,7 @@ static int _scf_op_expr_type_cast(scf_ast_t* ast, scf_node_t** nodes, int nb_nod scf_node_free_data(parent); - scf_loge("parent->result: %p, parent: %p, v->type: %d\n", parent->result, parent, v->type); + scf_logd("parent->result: %p, parent: %p, v->type: %d\n", parent->result, parent, v->type); parent->type = v->type; parent->var = v; @@ -530,8 +530,7 @@ static int _scf_op_expr_assign(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, scf_member_t* m0 = NULL; scf_member_t* m1 = NULL; - scf_logw("v0->type: %d, v1->type: %d, SCF_VAR_U8: %d\n", - v0->type, v1->type, SCF_VAR_U8); + scf_logd("v0->type: %d, v1->type: %d\n", v0->type, v1->type); if (!scf_variable_const_string(v1)) assert(v0->type == v1->type); diff --git a/parse/scf_operator_handler_semantic.c b/parse/scf_operator_handler_semantic.c index b227849..856030f 100644 --- a/parse/scf_operator_handler_semantic.c +++ b/parse/scf_operator_handler_semantic.c @@ -963,7 +963,7 @@ static int _scf_op_semantic_array_index(scf_ast_t* ast, scf_node_t** nodes, int } } - if (!scf_variable_interger(v1)) { + if (!scf_variable_integer(v1)) { scf_loge("array index should be an interger\n"); return -1; } @@ -1197,7 +1197,7 @@ static int _scf_op_semantic_if(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, return -1; } - if (!r || !scf_variable_interger(r)) { + if (!r || !scf_variable_integer(r)) { scf_loge("\n"); return -1; } @@ -1239,7 +1239,7 @@ static int _scf_op_semantic_do(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, return -1; } - if (!r || !scf_variable_interger(r)) { + if (!r || !scf_variable_integer(r)) { scf_loge("\n"); return -1; } @@ -1265,7 +1265,7 @@ static int _scf_op_semantic_while(scf_ast_t* ast, scf_node_t** nodes, int nb_nod return -1; } - if (!r || !scf_variable_interger(r)) { + if (!r || !scf_variable_integer(r)) { scf_loge("\n"); return -1; } @@ -1300,7 +1300,7 @@ static int _scf_op_semantic_switch(scf_ast_t* ast, scf_node_t** nodes, int nb_no return -1; } - if (!r || !scf_variable_interger(r)) { + if (!r || !scf_variable_integer(r)) { scf_loge("\n"); return -1; } @@ -1331,7 +1331,7 @@ static int _scf_op_semantic_case(scf_ast_t* ast, scf_node_t** nodes, int nb_node return -1; } - if (!r || !scf_variable_interger(r)) { + if (!r || !scf_variable_integer(r)) { scf_loge("\n"); return -1; } @@ -1371,7 +1371,7 @@ static int _scf_op_semantic_for(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes return -1; } - if (!r || !scf_variable_interger(r)) { + if (!r || !scf_variable_integer(r)) { scf_loge("\n"); return -1; } @@ -1539,7 +1539,7 @@ static int _scf_op_semantic_neg(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes } } - if (scf_variable_interger(v0) || scf_variable_float(v0)) { + if (scf_variable_integer(v0) || scf_variable_float(v0)) { scf_type_t* t = NULL; int ret = scf_ast_find_type_type(&t, ast, v0->type); @@ -1586,7 +1586,7 @@ static int _scf_op_semantic_inc(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes } } - if (scf_variable_interger(v0)) { + if (scf_variable_integer(v0)) { scf_type_t* t = NULL; int ret = scf_ast_find_type_type(&t, ast, v0->type); @@ -1838,7 +1838,7 @@ static int _scf_op_semantic_logic_not(scf_ast_t* ast, scf_node_t** nodes, int nb } } - if (scf_variable_interger(v0)) { + if (scf_variable_integer(v0)) { int const_flag = v0->const_flag && 0 == v0->nb_pointers && 0 == v0->nb_dimentions; @@ -1927,9 +1927,9 @@ static int _scf_op_semantic_binary(scf_ast_t* ast, scf_node_t** nodes, int nb_no } } - if (scf_variable_interger(v0) || scf_variable_float(v0)) { + if (scf_variable_integer(v0) || scf_variable_float(v0)) { - if (scf_variable_interger(v1) || scf_variable_float(v1)) { + if (scf_variable_integer(v1) || scf_variable_float(v1)) { scf_function_t* func_ptr = NULL; scf_variable_t* v2 = NULL; @@ -1950,7 +1950,7 @@ static int _scf_op_semantic_binary(scf_ast_t* ast, scf_node_t** nodes, int nb_no return -EINVAL; } - } else if (!scf_variable_interger(v1)) { + } else if (!scf_variable_integer(v1)) { scf_loge("var calculated with a pointer should be a interger\n"); return -EINVAL; } else { @@ -1979,7 +1979,7 @@ static int _scf_op_semantic_binary(scf_ast_t* ast, scf_node_t** nodes, int nb_no } else if (nb_pointers1 > 0) { - if (!scf_variable_interger(v0)) { + if (!scf_variable_integer(v0)) { scf_loge("var calculated with a pointer should be a interger\n"); return -EINVAL; } else { @@ -2106,7 +2106,7 @@ static int _scf_op_semantic_binary_interger(scf_ast_t* ast, scf_node_t** nodes, } } - if (scf_variable_interger(v0) && scf_variable_interger(v1)) { + if (scf_variable_integer(v0) && scf_variable_integer(v1)) { int const_flag = v0->const_flag && v1->const_flag; @@ -2444,9 +2444,9 @@ static int _scf_op_semantic_binary_assign(scf_ast_t* ast, scf_node_t** nodes, in } } - if (scf_variable_interger(v0) || scf_variable_float(v0)) { + if (scf_variable_integer(v0) || scf_variable_float(v0)) { - if (scf_variable_interger(v1) || scf_variable_float(v1)) { + if (scf_variable_integer(v1) || scf_variable_float(v1)) { if (!scf_variable_same_type(v0, v1)) { @@ -2517,7 +2517,7 @@ static int _scf_op_semantic_binary_interger_assign(scf_ast_t* ast, scf_node_t** } } - if (scf_variable_interger(v0) && scf_variable_interger(v1)) { + if (scf_variable_integer(v0) && scf_variable_integer(v1)) { if (!scf_variable_same_type(v0, v1)) { @@ -2622,9 +2622,9 @@ static int _scf_op_semantic_cmp(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes } } - if (scf_variable_interger(v0) || scf_variable_float(v0)) { + if (scf_variable_integer(v0) || scf_variable_float(v0)) { - if (scf_variable_interger(v1) || scf_variable_float(v1)) { + if (scf_variable_integer(v1) || scf_variable_float(v1)) { int const_flag = v0->const_flag && v1->const_flag; diff --git a/parse/scf_parse.h b/parse/scf_parse.h index c04ad9c..7f45738 100644 --- a/parse/scf_parse.h +++ b/parse/scf_parse.h @@ -36,8 +36,15 @@ struct scf_parse_s }; typedef struct { - scf_vector_t* current_index; - scf_expr_t* expr; + scf_lex_word_t* w; + intptr_t i; +} dfa_index_t; + +typedef struct { + scf_expr_t* expr; + + int n; // number of index array + dfa_index_t index[0]; // index array } dfa_init_expr_t; @@ -75,10 +82,6 @@ struct dfa_data_s { scf_lex_word_t* current_async_w; - scf_vector_t* init_exprs; - int current_dim; - scf_vector_t* current_index; - scf_type_t* root_struct; scf_type_t* current_struct; diff --git a/parse/scf_struct_array.c b/parse/scf_struct_array.c index 883e9c0..dd06593 100644 --- a/parse/scf_struct_array.c +++ b/parse/scf_struct_array.c @@ -1,13 +1,10 @@ #include"scf_dfa.h" #include"scf_parse.h" -static int _scf_array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* array, - intptr_t* indexes, int nb_indexes, scf_node_t** pnode) +static int __array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* array, dfa_index_t* index, int n, scf_node_t** pnode) { - if (!pnode) { - scf_loge("\n"); + if (!pnode) return -1; - } scf_type_t* t = scf_block_find_type_type(ast->current_block, SCF_VAR_INT); scf_node_t* root = *pnode; @@ -15,65 +12,72 @@ static int _scf_array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variabl if (!root) root = scf_node_alloc(NULL, array->type, array); - scf_logi("array->nb_dimentions: %d, nb_indexes: %d\n", array->nb_dimentions, nb_indexes); - - if (nb_indexes < array->nb_dimentions) { - scf_loge("\n"); + if (n < array->nb_dimentions) { + scf_loge("number of indexes less than needed, array '%s', file: %s, line: %d\n", + array->w->text->data, w->file->data, w->line); return -1; } int i; for (i = 0; i < array->nb_dimentions; i++) { - int k = indexes[i]; + intptr_t k = index[i].i; if (k >= array->dimentions[i]) { - scf_loge("\n"); + scf_loge("index [%ld] out of size [%d], in dim: %d, file: %s, line: %d\n", + k, array->dimentions[i], i, w->file->data, w->line); return -1; } scf_variable_t* v_index = scf_variable_alloc(NULL, t); v_index->const_flag = 1; v_index->const_literal_flag = 1; - v_index->data.i = k; + v_index->data.i64 = k; - scf_node_t* node_index = scf_node_alloc(NULL, v_index->type, v_index); - scf_node_t* node_op_index = scf_node_alloc(w, SCF_OP_ARRAY_INDEX, NULL); + scf_node_t* node_index = scf_node_alloc(NULL, v_index->type, v_index); + scf_node_t* node_op = scf_node_alloc(w, SCF_OP_ARRAY_INDEX, NULL); - scf_node_add_child(node_op_index, root); - scf_node_add_child(node_op_index, node_index); - root = node_op_index; + scf_node_add_child(node_op, root); + scf_node_add_child(node_op, node_index); + root = node_op; } *pnode = root; return array->nb_dimentions; } -int scf_struct_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* _struct, - intptr_t* indexes, int nb_indexes, scf_node_t** pnode) +int scf_struct_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* _struct, dfa_index_t* index, int n, scf_node_t** pnode) { - if (!pnode) { - scf_loge("\n"); + if (!pnode) return -1; - } - scf_type_t* t = scf_block_find_type_type(ast->current_block, _struct->type); - scf_variable_t* v = NULL; - scf_node_t* root = *pnode; + scf_variable_t* v = NULL; + scf_type_t* t = scf_block_find_type_type(ast->current_block, _struct->type); + scf_node_t* root = *pnode; if (!root) root = scf_node_alloc(NULL, _struct->type, _struct); int j = 0; - while (j < nb_indexes) { - - int k = indexes[j]; + while (j < n) { if (!t->scope) { scf_loge("\n"); return -1; } + int k; + + if (index[j].w) { + for (k = 0; k < t->scope->vars->size; k++) { + v = t->scope->vars->data[k]; + + if (v->w && !strcmp(index[j].w->text->data, v->w->text->data)) + break; + } + } else + k = index[j].i; + if (k >= t->scope->vars->size) { scf_loge("\n"); return -1; @@ -81,23 +85,21 @@ int scf_struct_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* _s v = t->scope->vars->data[k]; - scf_node_t* node_op_pointer = scf_node_alloc(w, SCF_OP_POINTER, NULL); - scf_node_t* node_v = scf_node_alloc(NULL, v->type, v); + scf_node_t* node_op = scf_node_alloc(w, SCF_OP_POINTER, NULL); + scf_node_t* node_v = scf_node_alloc(NULL, v->type, v); - scf_node_add_child(node_op_pointer, root); - scf_node_add_child(node_op_pointer, node_v); - root = node_op_pointer; + scf_node_add_child(node_op, root); + scf_node_add_child(node_op, node_v); + root = node_op; scf_logi("j: %d, k: %d, v: '%s'\n", j, k, v->w->text->data); j++; if (v->nb_dimentions > 0) { - int ret = _scf_array_member_init(ast, w, v, indexes + j, nb_indexes - j, &root); - if (ret < 0) { - scf_loge("\n"); + int ret = __array_member_init(ast, w, v, index + j, n - j, &root); + if (ret < 0) return -1; - } j += ret; scf_logi("struct var member: %s->%s[]\n", _struct->w->text->data, v->w->text->data); @@ -107,20 +109,22 @@ int scf_struct_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* _s // if 'v' is a base type var or a pointer, and of course 'v' isn't an array, // we can't get the member of v !! // the index must be the last one, and its expr is to init v ! - if (j < nb_indexes - 1) { - scf_loge("\n"); + if (j < n - 1) { + scf_loge("number of indexes more than needed, struct member: %s->%s, file: %s, line: %d\n", + _struct->w->text->data, v->w->text->data, w->file->data, w->line); return -1; } scf_logi("struct var member: %s->%s\n", _struct->w->text->data, v->w->text->data); *pnode = root; - return nb_indexes; + return n; } // 'v' is not a base type var or a pointer, it's a struct // first, find the type in this struct scope, then find in global scf_type_t* type_v = NULL; + while (t) { type_v = scf_scope_find_type_type(t->scope, v->type); if (type_v) @@ -143,47 +147,44 @@ int scf_struct_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* _s t = type_v; } - // if goto here, the index->size if less than needed, error - scf_loge("error: struct var member: %s->%s\n", _struct->w->text->data, v->w->text->data); + scf_loge("number of indexes less than needed, struct member: %s->%s, file: %s, line: %d\n", + _struct->w->text->data, v->w->text->data, w->file->data, w->line); return -1; } -int scf_array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* array, intptr_t* indexes, int nb_indexes, scf_node_t** pnode) +int scf_array_member_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* array, dfa_index_t* index, int n, scf_node_t** pnode) { scf_node_t* root = NULL; - int ret = _scf_array_member_init(ast, w, array, indexes, nb_indexes, &root); - if (ret < 0) { - scf_loge("\n"); - return -1; - } + int ret = __array_member_init(ast, w, array, index, n, &root); + if (ret < 0) + return ret; if (array->type < SCF_STRUCT || array->nb_pointers > 0) { - if (ret < nb_indexes - 1) { + + if (ret < n - 1) { scf_loge("\n"); return -1; } *pnode = root; - return nb_indexes; + return n; } - ret = scf_struct_member_init(ast, w, array, indexes + ret, nb_indexes - ret, &root); - if (ret < 0) { - scf_loge("\n"); - return -1; - } + ret = scf_struct_member_init(ast, w, array, index + ret, n - ret, &root); + if (ret < 0) + return ret; *pnode = root; - return nb_indexes; + return n; } int scf_array_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_vector_t* init_exprs) { - dfa_init_expr_t* init_expr; + dfa_init_expr_t* ie; - int nb_unset_dims = 0; - int unset_dims[8]; + int unset = 0; + int unset_dim = -1; int i; int j; @@ -192,72 +193,65 @@ int scf_array_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_v scf_logi("dim[%d]: %d\n", i, var->dimentions[i]); - if (var->dimentions[i] < 0) - unset_dims[nb_unset_dims++] = i; - } + if (var->dimentions[i] < 0) { - if (nb_unset_dims > 1) { - scf_loge("\n"); - return -1; - } + if (unset > 0) { + scf_loge("array '%s' should only unset 1-dimention size, file: %s, line: %d\n", + var->w->text->data, w->file->data, w->line); + return -1; + } - if (1 == nb_unset_dims) { + unset++; + unset_dim = i; + } + } - int unset_dim = unset_dims[0]; - int unset_index_max = -1; + if (unset) { + int unset_max = -1; for (i = 0; i < init_exprs->size; i++) { - init_expr = init_exprs->data[i]; + ie = init_exprs->data[i]; - if ((intptr_t)init_expr->current_index->data[unset_dim] > unset_index_max) - unset_index_max = (intptr_t)init_expr->current_index->data[unset_dim]; + if (unset_max < ie->index[unset_dim].i) + unset_max = ie->index[unset_dim].i; } - var->dimentions[unset_dim] = unset_index_max + 1; + var->dimentions[unset_dim] = unset_max + 1; } for (i = 0; i < init_exprs->size; i++) { - init_expr = init_exprs->data[i]; + ie = init_exprs->data[i]; for (j = 0; j < var->nb_dimentions; j++) { - printf("\033[32m%s(), %d, i: %d, dim: %d, size: %d, index: %d\033[0m\n", - __func__, __LINE__, i, j, var->dimentions[j], - (int)(intptr_t)(init_expr->current_index->data[j])); + intptr_t index = ie->index[j].i; - if ((intptr_t)init_expr->current_index->data[j] >= var->dimentions[j]) { - - scf_loge("index [%d] out of size [%d], in dim: %d\n", - (int)(intptr_t)init_expr->current_index->data[j], var->dimentions[j], j); - return -1; - } + scf_logi("\033[32mi: %d, dim: %d, size: %d, index: %ld\033[0m\n", i, j, var->dimentions[j], index); } } for (i = 0; i < init_exprs->size; i++) { - init_expr = init_exprs->data[i]; + ie = init_exprs->data[i]; - scf_logi("#### data init, i: %d, init_expr->expr: %p\n", i, init_expr->expr); + scf_logi("#### data init, i: %d, init expr: %p\n", i, ie->expr); - scf_expr_t* e = scf_expr_alloc(); - scf_node_t* assign = scf_node_alloc(w, SCF_OP_ASSIGN, NULL); - scf_node_t* node = NULL; - intptr_t* indexes = (intptr_t*)init_expr->current_index->data; - int nb_indexes = init_expr->current_index->size; + scf_expr_t* e; + scf_node_t* assign; + scf_node_t* node = NULL; - if (scf_array_member_init(ast, w, var, indexes, nb_indexes, &node) < 0) { + if (scf_array_member_init(ast, w, var, ie->index, ie->n, &node) < 0) { scf_loge("\n"); return -1; } + e = scf_expr_alloc(); + assign = scf_node_alloc(w, SCF_OP_ASSIGN, NULL); + scf_node_add_child(assign, node); - scf_node_add_child(assign, init_expr->expr); + scf_node_add_child(assign, ie->expr); scf_expr_add_node(e, assign); - scf_vector_free(init_expr->current_index); - init_expr->current_index = NULL; - - init_expr->expr = e; + ie->expr = e; printf("\n"); } @@ -266,34 +260,29 @@ int scf_array_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_v int scf_struct_init(scf_ast_t* ast, scf_lex_word_t* w, scf_variable_t* var, scf_vector_t* init_exprs) { - dfa_init_expr_t* init_expr; + dfa_init_expr_t* ie; int i; for (i = 0; i < init_exprs->size; i++) { - init_expr = init_exprs->data[i]; + ie = init_exprs->data[i]; - scf_logi("#### struct init, i: %d, init_expr->expr: %p\n", i, init_expr->expr); + scf_logi("#### struct init, i: %d, init_expr->expr: %p\n", i, ie->expr); - scf_node_t* node = NULL; - intptr_t* indexes = (intptr_t*)init_expr->current_index->data; - int nb_indexes = init_expr->current_index->size; + scf_expr_t* e; + scf_node_t* assign; + scf_node_t* node = NULL; - if (scf_struct_member_init(ast, w, var, indexes, nb_indexes, &node) < 0) { - scf_loge("\n"); + if (scf_struct_member_init(ast, w, var, ie->index, ie->n, &node) < 0) return -1; - } - scf_expr_t* e = scf_expr_alloc(); - scf_node_t* assign = scf_node_alloc(w, SCF_OP_ASSIGN, NULL); + e = scf_expr_alloc(); + assign = scf_node_alloc(w, SCF_OP_ASSIGN, NULL); scf_node_add_child(assign, node); - scf_node_add_child(assign, init_expr->expr); + scf_node_add_child(assign, ie->expr); scf_expr_add_node(e, assign); - scf_vector_free(init_expr->current_index); - init_expr->current_index = NULL; - - init_expr->expr = e; + ie->expr = e; printf("\n"); }