struct & array init with '{.key = value, } ok
authoryu.dongliang <18588496441@163.com>
Mon, 30 Sep 2024 11:32:45 +0000 (19:32 +0800)
committeryu.dongliang <18588496441@163.com>
Mon, 30 Sep 2024 11:32:45 +0000 (19:32 +0800)
17 files changed:
core/scf_lex_word.h
core/scf_scope.c
core/scf_type_cast.c
core/scf_variable.h
examples/init_struct_array.c [new file with mode: 0644]
examples/oop.c
lex/scf_lex_util.c
native/risc/scf_risc_inst.c
parse/scf_dfa_expr.c
parse/scf_dfa_init_data.c
parse/scf_dfa_util.h
parse/scf_dfa_var.c
parse/scf_operator_handler_const.c
parse/scf_operator_handler_expr.c
parse/scf_operator_handler_semantic.c
parse/scf_parse.h
parse/scf_struct_array.c

index d8ebde8774211d824447f1d81a46247db56e9a14..f6f460aa970080838eda9991315cbc752020617d 100644 (file)
@@ -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;
 }
index 4a9faf1a060523f825f5a783844e82337c45fe07..8790ebe13acbba2b79b48f0bbc14da283f3fc905 100644 (file)
@@ -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;
index e0e7bab78cbcfcf4bc84fb475d8f36930762fedb..32797389445f7b82e0fa9a6149831e9a47bdadfa 100644 (file)
@@ -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;
index 212c6bc8619b2abe59404dc8d3be67241e82369a..564ab14ccd5ff1dbce67156237c2c4c906d5ed78 100644 (file)
@@ -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 (file)
index 0000000..5aaab2b
--- /dev/null
@@ -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;
+}
index ae6d1fb740d4c7ad124d6f706931af9bf011db2b..f6cad9a97c4ca799475a2808dc01470370aece9f 100644 (file)
@@ -21,7 +21,7 @@ int sub(int i, int j)
 Aops aops =
 {
        add,
-       sub,
+       .pt1 = sub,
 };
 
 int main()
index 67d765b567caac23e3f9162948b4dcd022097258..1379a8eba39f3453478537c0bce6ac9ef10f6fe0 100644 (file)
@@ -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) {
index 79459f6387d71326bba72b93d731b55dcfb361bd..1f3a94aff7fc2c4c50ebc88f758a8ec0fb24dd0f 100644 (file)
@@ -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;
                }
index 6bfff2e2b46a7231a7c1863e0afd522582ea7c21..a55b24c77eeb1436a154922ebe73a26063b7fb51 100644 (file)
@@ -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;
 }
index d9fce44653b84f3fca9a62f373b1f40d58fa2229..861ce9f82a9ff55292012990c4ec1556f4442c84 100644 (file)
@@ -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;
 }
 
index 693b2217347d7bc9636a57cd6701ad3fcc47d045..46801e739ce6d29ec370a4b3c705e9b91f6111a7 100644 (file)
@@ -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;
index b2615072c6ffe5867ac0186f7de16013397d1deb..7eb38ceeebfad5153a0f60e92e2d1f2fe78c8f21 100644 (file)
@@ -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);
 
index 30154310d14213c3d252b9f51fc3ac9e2a18fe25..7039425c4817b592bc8cb31e22217ba809d1e6e5 100644 (file)
@@ -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)
index ac2f9eb28f809258960db24bb98f4db4b843f2e1..1e42350a8e2186ca1f9e323927adeed717937a5a 100644 (file)
@@ -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);
index b2278493963a0cce71d19c60d8f104e2588c23be..856030faa682dcc5b4c33d6cc0719e5f4511e435 100644 (file)
@@ -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; 
 
index c04ad9c52ab005fcf7576fceccc572f9e18f9035..7f457380d6c2675044bc421fb645c11775bb98ff 100644 (file)
@@ -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;
 
index 883e9c0522a8abd8fe398312d8c48ea84b228312..dd065934849fe945b51121ac9d5444735aa48807 100644 (file)
@@ -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");
        }