Skip to content

Commit

Permalink
towards empty arrays (#59)
Browse files Browse the repository at this point in the history
* empty arrays
  • Loading branch information
smaludzi authored Sep 2, 2023
1 parent 441b940 commit 839708c
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 34 deletions.
10 changes: 7 additions & 3 deletions back/functab.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions back/functab.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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__ */
3 changes: 2 additions & 1 deletion back/nev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 12 additions & 13 deletions front/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions front/emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions front/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions front/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions front/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@ array: ARR_DIM_BEG expr_list ARR_DIM_END ':' param
$$->line_no = $<line_no>1;
};

array: '[' ']' ':' param
{
$$ = array_new(NULL, $4);
$$->line_no = $<line_no>1;
};

array: '[' expr_list ']' ':' param
{
$$ = array_new($2, $5);
Expand All @@ -480,6 +486,12 @@ array: '[' array_sub_list ']' ':' param
$$->line_no = $<line_no>1;
};

array_sub: '[' ']'
{
$$ = array_new_sub(NULL);
$$->line_no = $<line_no>1;
};

array_sub: '[' expr_list ']'
{
$$ = array_new_sub($2);
Expand Down
43 changes: 33 additions & 10 deletions front/tcheckarr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
{
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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));
}
}
Expand All @@ -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;
Expand All @@ -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)
{
Expand Down
19 changes: 19 additions & 0 deletions sample/sample122.nev
Original file line number Diff line number Diff line change
@@ -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)
}

13 changes: 10 additions & 3 deletions sample/sample_main_strarr.nev
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

func test(argv[argc] : string) -> int
func test1(argv[argc] : string) -> int
{
for (s in argv)
{
Expand All @@ -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
}

0 comments on commit 839708c

Please sign in to comment.