diff --git a/back/functab.c b/back/functab.c index 4d1fdd8..853835c 100644 --- a/back/functab.c +++ b/back/functab.c @@ -56,7 +56,8 @@ void functab_entry_delete(functab_entry * entries, unsigned int size) } void functab_entry_add_func(functab_entry * entries, unsigned int size, - func * func_value, object * params, unsigned int params_count) + func * func_value, int entry_type, + object * params, unsigned int params_count) { unsigned int times = 0; unsigned int index = 0; @@ -75,6 +76,7 @@ void functab_entry_add_func(functab_entry * entries, unsigned int size, entries[index].id = func_value->decl->id; entries[index].func_addr = 0; entries[index].func_value = func_value; + entries[index].entry_type = entry_type; entries[index].params = params; entries[index].params_count = params_count; } @@ -114,6 +116,7 @@ void functab_entry_resize(functab_entry * entries, unsigned int size, { functab_entry_add_func(entries_new, size_new, entries[i].func_value, + entries[i].entry_type, entries[i].params, entries[i].params_count); } @@ -169,10 +172,11 @@ void functab_close(functab * tab) } } -void functab_add_func(functab * tab, func * func_value, +void functab_add_func(functab * tab, func * func_value, int entry_type, object * params, unsigned int params_count) { - functab_entry_add_func(tab->entries, tab->size, func_value, params, params_count); + functab_entry_add_func(tab->entries, tab->size, func_value, entry_type, + params, params_count); tab->count++; functab_resize(tab); diff --git a/back/functab.h b/back/functab.h index 4ebd8fa..2d5b3d7 100644 --- a/back/functab.h +++ b/back/functab.h @@ -37,6 +37,7 @@ typedef struct functab_entry func * func_value; char * id; unsigned int func_addr; + int entry_type; object * params; unsigned int params_count; } functab_entry; @@ -52,7 +53,8 @@ functab_entry * functab_entry_new(unsigned int size); void functab_entry_delete(functab_entry * entries, unsigned int size); void functab_entry_add_func(functab_entry * entries, unsigned int size, - func * func_value, object * params, unsigned int params_count); + func * func_value, int entry_type, + object * params, unsigned int params_count); functab_entry * functab_entry_lookup(functab_entry * entries, unsigned int size, const char * id); void functab_entry_resize(functab_entry * entries, unsigned int size, @@ -63,7 +65,8 @@ void functab_delete(functab * tab); void functab_resize(functab * tab); void functab_close(functab * tab); -void functab_add_func(functab * tab, func * func_value, object * params, unsigned int params_count); +void functab_add_func(functab * tab, func * func_value, int entry_type, + object * params, unsigned int params_count); functab_entry * functab_lookup(functab * tab, const char * id); #endif /* __FUNCTAB_H__ */ diff --git a/back/nev.c b/back/nev.c index b49cd61..2c547c3 100644 --- a/back/nev.c +++ b/back/nev.c @@ -229,7 +229,8 @@ int nev_prepare_argc_argv(program * prog, const char * entry_name, unsigned int prog->params = entry->params; prog->entry_addr = entry->func_addr; - if (prog->params_count > argc) + if (entry->entry_type == FUNC_ENTRY_TYPE_PARAM_LIST && + argc < prog->params_count) { fprintf(stderr, "too few parameters, expected %d got %d\n", prog->params_count, argc); diff --git a/front/array.c b/front/array.c index 40d6da6..a15550e 100644 --- a/front/array.c +++ b/front/array.c @@ -95,30 +95,29 @@ int elements_to_depth_list(expr_list * elements, expr_list_weak * bfs_list, return 0; } -int array_to_depth_list(expr * value, expr_list_weak * depth_list) +int array_to_depth_list(expr * value, expr_list_weak * bfs_list) { - expr_list_weak * bfs_list = expr_list_weak_new(); - expr_list_weak_add(bfs_list, value, 0); - elements_to_depth_list(value->array.array_value->elements, bfs_list, 1); + if (value->array.array_value->elements) + { + elements_to_depth_list(value->array.array_value->elements, bfs_list, 1); + } - while (bfs_list->count > 0) + expr_list_weak_node * node = bfs_list->head; + while (node != NULL) { - expr_list_weak_node * head = expr_list_weak_pop(bfs_list); - expr * value = head->value; + expr * value = node->value; if (value->type == EXPR_ARRAY && - value->array.array_value->type == ARRAY_SUB) + value->array.array_value->type == ARRAY_SUB && + value->array.array_value->elements != NULL) { elements_to_depth_list(value->array.array_value->elements, - bfs_list, head->distance + 1); + bfs_list, node->distance + 1); } - expr_list_weak_add(depth_list, value, head->distance + 1); - expr_list_weak_node_delete(head); + node = node->prev; } - expr_list_weak_delete(bfs_list); - return 0; } diff --git a/front/emit.c b/front/emit.c index 3c8504a..2696f85 100644 --- a/front/emit.c +++ b/front/emit.c @@ -4328,7 +4328,9 @@ int array_init_elements_emit(expr_list_weak * depth_list, int * elements_count, while (node != NULL) { expr * value = node->value; - if (value != NULL && node->distance == elem_dist) + if (value != NULL && + !expr_is_empty_array(value) && + node->distance == elem_dist) { expr_emit(value, stack_level + (*elements_count)++, module_value, list_weak, result); @@ -5083,7 +5085,8 @@ int func_entry_params(func * func_value, module * module_value, int * result) } } - functab_add_func(module_value->functab_value, func_value, params, params_count); + functab_add_func(module_value->functab_value, func_value, func_value->entry, + params, params_count); } return 0; diff --git a/front/expr.c b/front/expr.c index e470925..8aacb09 100644 --- a/front/expr.c +++ b/front/expr.c @@ -559,6 +559,18 @@ expr * expr_conv(expr * expr_value, conv_type conv) return ret; } +int expr_is_empty_array(expr * value) +{ + if (value->type == EXPR_ARRAY && + (value->array.array_value->type == ARRAY_INIT || + value->array.array_value->type == ARRAY_SUB) && + value->array.array_value->elements == NULL) + { + return 1; + } + return 0; +} + int comb_type_is_basic(comb_type comb) { switch(comb) diff --git a/front/expr.h b/front/expr.h index 08cd735..c0f3062 100644 --- a/front/expr.h +++ b/front/expr.h @@ -392,6 +392,7 @@ expr * expr_new_touple(touple * touple_value); comb_type conv_to_comb_type(conv_type conv); expr * expr_conv(expr * expr_value, conv_type conv); +int expr_is_empty_array(expr * value); int comb_type_is_basic(comb_type comb); void expr_delete(expr * value); diff --git a/front/parser.y b/front/parser.y index 40ef408..0940ecb 100644 --- a/front/parser.y +++ b/front/parser.y @@ -468,6 +468,12 @@ array: ARR_DIM_BEG expr_list ARR_DIM_END ':' param $$->line_no = $1; }; +array: '[' ']' ':' param +{ + $$ = array_new(NULL, $4); + $$->line_no = $1; +}; + array: '[' expr_list ']' ':' param { $$ = array_new($2, $5); @@ -480,6 +486,12 @@ array: '[' array_sub_list ']' ':' param $$->line_no = $1; }; +array_sub: '[' ']' +{ + $$ = array_new_sub(NULL); + $$->line_no = $1; +}; + array_sub: '[' expr_list ']' { $$ = array_new_sub($2); diff --git a/front/tcheckarr.c b/front/tcheckarr.c index 576a46a..dc6be74 100644 --- a/front/tcheckarr.c +++ b/front/tcheckarr.c @@ -41,7 +41,8 @@ int array_depth_list_well_formed(array * array_value, expr_list_weak * depth_lis expr * value = node->value; if (value != NULL) { - if (node->distance == first_distance) + if (!expr_is_empty_array(value) && + node->distance == first_distance) { if (param_expr_cmp(ret, value, false) == TYPECHECK_FAIL) { @@ -61,8 +62,15 @@ int array_depth_list_well_formed(array * array_value, expr_list_weak * depth_lis (value->array.array_value->type == ARRAY_SUB || value->array.array_value->type == ARRAY_INIT)) { - first_comb_elems = - value->array.array_value->elements->count; + if (value->array.array_value->elements) + { + first_comb_elems = + value->array.array_value->elements->count; + } + else + { + first_comb_elems = 0; + } } else { @@ -79,8 +87,11 @@ int array_depth_list_well_formed(array * array_value, expr_list_weak * depth_lis (value->array.array_value->type == ARRAY_SUB || value->array.array_value->type == ARRAY_INIT)) { - if (value->array.array_value->elements->count != - first_comb_elems) + if ((value->array.array_value->elements == NULL && + first_comb_elems != 0) || + (value->array.array_value->elements != NULL && + value->array.array_value->elements->count != + first_comb_elems)) { *result = TYPECHECK_FAIL; print_error_msg( @@ -109,7 +120,6 @@ int array_depth_list_well_formed(array * array_value, expr_list_weak * depth_lis int array_set_dims(expr_list_weak * depth_list) { - int dim = -1; int elems = 0; int distance = 0; expr_list_weak_node * node = NULL; @@ -123,13 +133,19 @@ int array_set_dims(expr_list_weak * depth_list) { if (node->distance != distance) { - dim++; distance = node->distance; if (value->type == EXPR_ARRAY && value->array.array_value->type == ARRAY_SUB) { - elems = value->array.array_value->elements->count; + if (value->array.array_value->elements) + { + elems = value->array.array_value->elements->count; + } + else + { + elems = 0; + } expr_list_add_beg(dims, expr_new_int(elems)); } } @@ -142,7 +158,14 @@ int array_set_dims(expr_list_weak * depth_list) assert(node->value->type == EXPR_ARRAY && node->value->array.array_value->type == ARRAY_INIT); - elems = node->value->array.array_value->elements->count; + if (node->value->array.array_value->elements != NULL) + { + elems = node->value->array.array_value->elements->count; + } + else + { + elems = 0; + } expr_list_add_beg(dims, expr_new_int(elems)); node->value->array.array_value->dims = dims; @@ -153,8 +176,8 @@ int array_set_dims(expr_list_weak * depth_list) int array_well_formed(expr * value, int * result) { expr_list_weak * depth_list = expr_list_weak_new(); - array_to_depth_list(value, depth_list); + array_to_depth_list(value, depth_list); array_depth_list_well_formed(value->array.array_value, depth_list, result); if (*result == TYPECHECK_SUCC) { diff --git a/sample/sample122.nev b/sample/sample122.nev new file mode 100644 index 0000000..9ddec31 --- /dev/null +++ b/sample/sample122.nev @@ -0,0 +1,19 @@ + +func call(tab[D1, D2] : int) -> int +{ + tab[0, 0] +} + +func f1(a : int) -> int +{ + var t = [ {[10, 15]} : int, + {[20, 25]} : int ] : [D, D] : int; + t[1][0,0] = 122; + call( t[1] ) +} + +func main() -> bool +{ + assert(f1(8) == 122) +} + diff --git a/sample/sample_main_strarr.nev b/sample/sample_main_strarr.nev index 8049205..024dfd6 100644 --- a/sample/sample_main_strarr.nev +++ b/sample/sample_main_strarr.nev @@ -1,5 +1,5 @@ -func test(argv[argc] : string) -> int +func test1(argv[argc] : string) -> int { for (s in argv) { @@ -8,9 +8,16 @@ func test(argv[argc] : string) -> int 0 } -func main(argv[argc] : string) -> int +func test2(argv[D1, D2] : string) -> int { - test(argv); + prints(argv[1,0] + "\n"); 0 } +func main(argv[argc] : string) -> int +{ + test1( [] : string ); + test1( argv ); + test2( [ [ "aa" ], [ "bbb" ] ] : string ); + 0 +}