// 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
};
}
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;
}
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;
}
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;
}
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;
}
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
}
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;
}
}
#endif
-
--- /dev/null
+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;
+}
Aops aops =
{
add,
- sub,
+ .pt1 = sub,
};
int main()
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) {
if (0 == s1->color) {
- if (!scf_variable_const_interger(s1->var)) {
+ if (!scf_variable_const_integer(s1->var)) {
scf_loge("\n");
return -EINVAL;
}
} else if (0 == s0->color) {
- if (!scf_variable_const_interger(s0->var)) {
+ if (!scf_variable_const_integer(s0->var)) {
scf_loge("\n");
return -EINVAL;
}
if (0 == s1->color) {
- if (!scf_variable_const_interger(s1->var)) {
+ if (!scf_variable_const_integer(s1->var)) {
scf_loge("\n");
return -EINVAL;
}
} else if (0 == s0->color) {
- if (!scf_variable_const_interger(s0->var)) {
+ if (!scf_variable_const_integer(s0->var)) {
scf_loge("\n");
return -EINVAL;
}
if (0 == s1->color) {
- if (!scf_variable_const_interger(s1->var)) {
+ if (!scf_variable_const_integer(s1->var)) {
scf_loge("\n");
return -EINVAL;
}
if (0 == s->color) {
- if (!scf_variable_const_interger(s->var)) {
+ if (!scf_variable_const_integer(s->var)) {
scf_loge("\n");
return -EINVAL;
}
return -EINVAL;
}
- if (!scf_variable_const_interger(s1->var)) {
+ if (!scf_variable_const_integer(s1->var)) {
scf_loge("\n");
return -EINVAL;
}
return -EINVAL;
}
- if (!scf_variable_const_interger(s->var)) {
+ if (!scf_variable_const_integer(s->var)) {
scf_loge("\n");
return -EINVAL;
}
return -EINVAL;
}
- if (!scf_variable_const_interger(s->var)) {
+ if (!scf_variable_const_integer(s->var)) {
scf_loge("\n");
return -EINVAL;
}
return -EINVAL;
}
- if (!scf_variable_const_interger(s1->var)) {
+ if (!scf_variable_const_integer(s1->var)) {
scf_loge("\n");
return -EINVAL;
}
if (0 == s1->color) {
- if (!scf_variable_const_interger(s1->var)) {
+ if (!scf_variable_const_integer(s1->var)) {
scf_loge("\n");
return -EINVAL;
}
if (0 == s1->color) {
- if (!scf_variable_const_interger(s1->var)) {
+ if (!scf_variable_const_integer(s1->var)) {
scf_loge("\n");
return -EINVAL;
}
if (0 == s->color) {
- if (!scf_variable_const_interger(s->var)) {
+ if (!scf_variable_const_integer(s->var)) {
scf_loge("\n");
return -EINVAL;
}
if (0 == s->color) {
- if (!scf_variable_const_interger(s->var)) {
+ if (!scf_variable_const_integer(s->var)) {
scf_loge("\n");
return -EINVAL;
}
if (0 == s->color) {
- if (!scf_variable_const_interger(vs))
+ if (!scf_variable_const_integer(vs))
return -EINVAL;
if (src_size < dst_size)
if (0 == s->color) {
- if (!scf_variable_const_interger(vs))
+ if (!scf_variable_const_integer(vs))
return -EINVAL;
if (src_size < dst_size)
if (0 == s->color) {
- if (!scf_variable_const_interger(s->var)) {
+ if (!scf_variable_const_integer(s->var)) {
scf_loge("\n");
return -EINVAL;
}
if (0 == s->color) {
- if (!scf_variable_const_interger(s->var)) {
+ if (!scf_variable_const_integer(s->var)) {
scf_loge("\n");
return -EINVAL;
}
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;
}
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;
}
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;
}
{
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;
}
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;
}
{
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);
}
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;
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;
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
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;
}
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;
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;
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) {
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");
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);
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);
} 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)
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;
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;
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);
}
}
- if (!scf_variable_interger(v1)) {
+ if (!scf_variable_integer(v1)) {
scf_loge("array index should be an interger\n");
return -1;
}
return -1;
}
- if (!r || !scf_variable_interger(r)) {
+ if (!r || !scf_variable_integer(r)) {
scf_loge("\n");
return -1;
}
return -1;
}
- if (!r || !scf_variable_interger(r)) {
+ if (!r || !scf_variable_integer(r)) {
scf_loge("\n");
return -1;
}
return -1;
}
- if (!r || !scf_variable_interger(r)) {
+ if (!r || !scf_variable_integer(r)) {
scf_loge("\n");
return -1;
}
return -1;
}
- if (!r || !scf_variable_interger(r)) {
+ if (!r || !scf_variable_integer(r)) {
scf_loge("\n");
return -1;
}
return -1;
}
- if (!r || !scf_variable_interger(r)) {
+ if (!r || !scf_variable_integer(r)) {
scf_loge("\n");
return -1;
}
return -1;
}
- if (!r || !scf_variable_interger(r)) {
+ if (!r || !scf_variable_integer(r)) {
scf_loge("\n");
return -1;
}
}
}
- 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);
}
}
- 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);
}
}
- if (scf_variable_interger(v0)) {
+ if (scf_variable_integer(v0)) {
int const_flag = v0->const_flag && 0 == v0->nb_pointers && 0 == v0->nb_dimentions;
}
}
- 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;
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 {
} 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 {
}
}
- 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;
}
}
- 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)) {
}
}
- 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)) {
}
}
- 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;
};
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;
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;
#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;
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;
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);
// 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)
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;
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");
}
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");
}