if (f->visited_flag)
continue;
- scf_loge("f: %p, %s\n", f, f->node.w->text->data);
+ scf_logd("f: %p, %s\n", f, f->node.w->text->data);
f->visited_flag = 1;
total1 += ret;
}
- scf_loge("total0: %d, total1: %d\n", total0, total1);
+ scf_logi("total0: %d, total1: %d\n", total0, total1);
} while (total0 != total1);
--- /dev/null
+#include"../lib/scf_capi.c"
+
+struct mat
+{
+ int refs;
+ int n;
+ int data[0];
+};
+
+int main()
+{
+ mat* m = calloc(1, sizeof(mat) + 4 * sizeof(int));
+
+ m->data[0] = 1;
+ m->data[1] = 2;
+ m->data[2] = 3;
+ m->data[3] = 4;
+
+ int i;
+ for (i = 0; i < 4; i++)
+ printf("%d\n", m->data[i]);
+
+ return 0;
+}
lex->fp = fopen(path, "r");
if (!lex->fp) {
+
char cwd[4096];
getcwd(cwd, 4095);
- scf_loge("path: %s, errno: %d, pwd: %s\n", path, errno, cwd);
+ scf_loge("open file '%s' failed, errno: %d, default path dir: %s\n", path, errno, cwd);
free(lex);
return -1;
assert(words->size >= 1);
scf_lex_word_t* w = words->data[words->size - 1];
+
scf_loge("ret: %d, w->type: %d, '%s', line: %d\n\n", ret, w->type, w->text->data, w->line);
ret = SCF_DFA_ERROR;
static int _class_calculate_size(scf_dfa_t* dfa, scf_type_t* s)
{
- scf_parse_t* parse = dfa->priv;
+ scf_variable_t* v;
int size = 0;
int i;
- for (i = 0; i < s->scope->vars->size; i++) {
-
- scf_variable_t* v = s->scope->vars->data[i];
-
- if (v->size < 0) {
- scf_loge("error: sizeof var: '%s'\n", v->w->text->data);
- return SCF_DFA_ERROR;
- }
+ int j;
- int align;
- if (1 == v->size)
- align = 1;
- else if (2 == v->size)
- align = 2;
- else if (v->size <= 4)
- align = 4;
- else
- align = 8;
-
- v->offset = (size + align - 1) / align * align;
+ for (i = 0; i < s->scope->vars->size; i++) {
+ v = s->scope->vars->data[i];
+
+ assert(v->size >= 0);
+
+ switch (v->size) {
+ case 1:
+ v->offset = size;
+ break;
+ case 2:
+ v->offset = (size + 1) & ~0x1;
+ break;
+ case 3:
+ case 4:
+ v->offset = (size + 3) & ~0x3;
+ break;
+ default:
+ v->offset = (size + 7) & ~0x7;
+ break;
+ };
if (v->nb_dimentions > 0) {
- int j;
- int capacity = 1;
+ v->capacity = 1;
for (j = 0; j < v->nb_dimentions; j++) {
+
if (v->dimentions[j] < 0) {
- scf_loge("v: '%s'\n", v->w->text->data);
+ scf_loge("number of %d-dimention for array '%s' is less than 0, size: %d, file: %s, line: %d\n",
+ j, v->w->text->data, v->dimentions[j], v->w->file->data, v->w->line);
return SCF_DFA_ERROR;
}
- capacity *= v->dimentions[j];
+ v->capacity *= v->dimentions[j];
}
- v->capacity = capacity;
size = v->offset + v->size * v->capacity;
- } else {
+ } else
size = v->offset + v->size;
- }
- scf_logi("class '%s', member: '%s', offset: %d, size: %d, v->dim: %d, v->capacity: %d\n",
- s->name->data, v->w->text->data, v->offset, v->size, v->nb_dimentions, v->capacity);
+ scf_logi("class '%s', member: '%s', member_flag: %d, offset: %d, size: %d, v->dim: %d, v->capacity: %d\n",
+ s->name->data, v->w->text->data, v->member_flag, v->offset, v->size, v->nb_dimentions, v->capacity);
}
s->size = size;
s->node.define_flag = 1;
var->const_literal_flag = 1;
}
+ scf_loge("var: %s, member_flag: %d, line: %d\n", var->w->text->data, var->member_flag, var->w->line);
+
node = scf_node_alloc(w, var->type, var);
if (!node) {
scf_loge("var node '%s' alloc failed\n", w->text->data);
scf_parse_t* parse = dfa->priv;
dfa_data_t* d = data;
scf_lex_word_t* w = words->data[words->size - 1];
+ dfa_for_data_t* fd = NULL;
scf_stack_t* s = d->module_datas[dfa_module_for.index];
- scf_node_t* _for = scf_node_alloc(w, SCF_OP_FOR, NULL);
+ scf_node_t* _for = scf_node_alloc(w, SCF_OP_FOR, NULL);
- if (!_for) {
- scf_loge("node alloc failed\n");
- return SCF_DFA_ERROR;
- }
+ if (!_for)
+ return -ENOMEM;
- dfa_for_data_t* fd = calloc(1, sizeof(dfa_for_data_t));
- if (!fd) {
- scf_loge("module data alloc failed\n");
- return SCF_DFA_ERROR;
- }
+ fd = calloc(1, sizeof(dfa_for_data_t));
+ if (!fd)
+ return -ENOMEM;
if (d->current_node)
scf_node_add_child(d->current_node, _for);
- else {
+ else
scf_node_add_child((scf_node_t*)parse->ast->current_block, _for);
- scf_loge("current_block: %p\n", parse->ast->current_block);
- }
fd->parent_block = parse->ast->current_block;
fd->parent_node = d->current_node;
dfa_for_data_t* fd = scf_stack_top(s);
if (!d->expr) {
- scf_loge("need expr before ','\n");
+ scf_loge("need expr before ',' in file: %s, line: %d\n", w->file->data, w->line);
return SCF_DFA_ERROR;
}
scf_vector_add(fd->update_exprs, d->expr);
d->expr = NULL;
} else {
- scf_loge("too many ';' in for\n");
+ scf_loge("too many ';' in for, file: %s, line: %d\n", w->file->data, w->line);
return SCF_DFA_ERROR;
}
static int _for_action_semicolon(scf_dfa_t* dfa, scf_vector_t* words, void* data)
{
- if (!data) {
- scf_loge("\n");
+ if (!data)
return SCF_DFA_ERROR;
- }
scf_parse_t* parse = dfa->priv;
dfa_data_t* d = data;
d->expr = NULL;
}
} else {
- scf_loge("too many ';' in for\n");
+ scf_loge("too many ';' in for, file: %s, line: %d\n", w->file->data, w->line);
return SCF_DFA_ERROR;
}
scf_vector_add(fd->update_exprs, d->expr);
d->expr = NULL;
} else {
- scf_loge("too many ';' in for\n");
+ scf_loge("too many ';' in for, file: %s, line: %d\n", w->file->data, w->line);
return SCF_DFA_ERROR;
}
dfa_data_t* d = data;
scf_lex_word_t* w = words->data[words->size - 1];
- scf_loge("include '%s', line %d\n", w->text->data, w->line);
return SCF_DFA_NEXT_WORD;
}
scf_block_t* cur = parse->ast->current_block;
assert(w->data.s);
- scf_loge("include '%s', line %d\n", w->data.s->data, w->line);
+ scf_logd("include '%s', line %d\n", w->data.s->data, w->line);
parse->lex = NULL;
parse->ast->current_block = parse->ast->root_block;
int ret = scf_parse_file(parse, w->data.s->data);
if (ret < 0) {
- scf_loge("\n");
- return SCF_DFA_ERROR;
+ scf_loge("parse file '%s' failed, 'include' line: %d\n", w->data.s->data, w->line);
+ goto error;
}
if (parse->lex != lex && parse->lex->macros) { // copy macros
if (!lex->macros) {
lex->macros = scf_vector_clone(parse->lex->macros);
- if (!lex->macros)
- return -ENOMEM;
+
+ if (!lex->macros) {
+ ret = -ENOMEM;
+ goto error;
+ }
} else {
ret = scf_vector_cat(lex->macros, parse->lex->macros);
if (ret < 0)
- return ret;
+ goto error;
}
scf_macro_t* m;
}
}
+ ret = SCF_DFA_NEXT_WORD;
+error:
parse->lex = lex;
parse->ast->current_block = cur;
-
- return SCF_DFA_NEXT_WORD;
+ return ret;
}
static int _include_action_LF(scf_dfa_t* dfa, scf_vector_t* words, void* data)
id = scf_stack_pop(d->current_identities);
assert(id && id->type);
- scf_loge("\n");
-
if (id->nb_pointers > 0) {
t = scf_block_find_type_type(parse->ast->current_block, SCF_VAR_INTPTR);
static int _union_calculate_size(scf_dfa_t* dfa, scf_type_t* s)
{
- scf_parse_t* parse = dfa->priv;
+ scf_variable_t* v;
int max_size = 0;
int i;
int j;
+
for (i = 0; i < s->scope->vars->size; i++) {
+ v = s->scope->vars->data[i];
- scf_variable_t* v = s->scope->vars->data[i];
assert(v->size >= 0);
int size = 0;
if (v->nb_dimentions > 0) {
- int capacity = 1;
+ v->capacity = 1;
+
for (j = 0; j < v->nb_dimentions; j++) {
+
if (v->dimentions[j] < 0) {
- scf_loge("v: '%s'\n", v->w->text->data);
+ scf_loge("number of %d-dimention for array '%s' is less than 0, number: %d, file: %s, line: %d\n",
+ j, v->w->text->data, v->dimentions[j], v->w->file->data, v->w->line);
return SCF_DFA_ERROR;
+ }
- } else if (0 == v->dimentions[j]) {
- if (j < v->nb_dimentions - 1) {
- scf_loge("v: '%s'\n", v->w->text->data);
- return SCF_DFA_ERROR;
- }
+ if (0 == v->dimentions[j] && j < v->nb_dimentions - 1) {
+
+ scf_loge("only the number of array's last dimention can be 0, array '%s', dimention: %d, file: %s, line: %d\n",
+ v->w->text->data, j, v->w->file->data, v->w->line);
+ return SCF_DFA_ERROR;
}
- capacity *= v->dimentions[j];
+ v->capacity *= v->dimentions[j];
}
- v->capacity = capacity;
size = v->size * v->capacity;
- } else {
+ } else
size = v->size;
- }
if (max_size < size)
max_size = size;
if (!r)
return -ENOMEM;
+ r->member_flag = v1->member_flag;
+
int i;
for (i = 0; i < v1->nb_dimentions; i++)
scf_variable_add_array_dimention(r, v1->dimentions[i]);
if (scf_variable_const(v1)) {
if (v1->data.i < 0) {
- scf_loge("error: index %d < 0\n", v1->data.i);
+ scf_loge("array index '%s' < 0, real: %d, file: %s, line: %d\n",
+ v1->w->text->data, v1->data.i, v1->w->file->data, v1->w->line);
return -1;
}
if (v1->data.i >= v0->dimentions[0]) {
- scf_loge("index %d >= size %d\n", v1->data.i, v0->dimentions[0]);
- return -1;
+
+ if (!v0->member_flag) {
+ scf_loge("array index '%s' >= size %d, real: %d, file: %s, line: %d\n",
+ v1->w->text->data, v0->dimentions[0], v1->data.i, v1->w->file->data, v1->w->line);
+ return -1;
+ }
+
+ scf_logw("array index '%s' >= size %d, real: %d, confirm it for a zero-array end of a struct? file: %s, line: %d\n",
+ v1->w->text->data, v0->dimentions[0], v1->data.i, v1->w->file->data, v1->w->line);
}
}
} else if (0 == v0->nb_dimentions && v0->nb_pointers > 0) {
if (!r)
return -ENOMEM;
+ r->member_flag = v0->member_flag;
+
int i;
for (i = 1; i < v0->nb_dimentions; i++)
scf_variable_add_array_dimention(r, v0->dimentions[i]);
if (f->vargs_flag) {
if (f->argv->size > nb_nodes - 1) {
- scf_loge("\n");
+ scf_loge("number of args pass to '%s()' at least needs %d, real: %d, file: %s, line: %d\n",
+ f->node.w->text->data, f->argv->size, nb_nodes - 1, parent->w->file->data, parent->w->line);
return -1;
}
} else if (f->argv->size != nb_nodes - 1) {
- scf_loge("\n");
+ scf_loge("number of args pass to '%s()' needs %d, real: %d, file: %s, line: %d\n",
+ f->node.w->text->data, f->argv->size, nb_nodes - 1, parent->w->file->data, parent->w->line);
return -1;
}
return 0;
}
- if (scf_lex_open(&parse->lex, path) < 0) {
- scf_loge("\n");
+ if (scf_lex_open(&parse->lex, path) < 0)
return -1;
- }
+
scf_ast_add_file_block(parse->ast, path);
parse->lex->next = parse->lex_list;