<commit_before>#include <ofc/sema.h>



ofc_sema_array_t* ofc_sema_array(
	ofc_sema_scope_t*              scope,
	const ofc_parse_array_index_t* index)
{
	if (!index || (index->count == 0))
		return NULL;

	unsigned i;
	for (i = 0; i < index->count; i++)
	{
		if (!index->range[i])
			return NULL;

		if (!index->range[i]->first
			|| index->range[i]->stride)
			return NULL;
	}

	ofc_sema_array_t* array
		= (ofc_sema_array_t*)malloc(sizeof(ofc_sema_array_t)
			+ (index->count * sizeof(ofc_sema_array_dims_t)));
	if (!array) return NULL;

	array->dimensions = index->count;

	for (i = 0; i < index->count; i++)
	{
		array->segment[i].base = 1;

		ofc_sema_expr_t* expr = ofc_sema_expr(
			scope, index->range[i]->first);

		const ofc_sema_typeval_t* ctv
			= ofc_sema_expr_constant(expr);

		int64_t d;
		bool got = ofc_sema_typeval_get_integer(ctv, &d);
		ofc_sema_expr_delete(expr);

		if (!got)
		{
			ofc_sema_scope_error(scope,
				index->range[i]->first->src,
				"Failed to resolve array dimension");
			ofc_sema_array_delete(array);
			return NULL;
		}

		if (index->range[i]->last)
		{
			array->segment[i].base = d;
			if ((int64_t)array->segment[i].base != d)
			{
				ofc_sema_scope_error(scope,
					index->range[i]->first->src,
					"Array base out-of-range");
				ofc_sema_array_delete(array);
				return NULL;
			}

			expr = ofc_sema_expr(
				scope, index->range[i]->last);

			ctv = ofc_sema_expr_constant(expr);

			got = ofc_sema_typeval_get_integer(ctv, &d);
			ofc_sema_expr_delete(expr);

			if (!got)
			{
				ofc_sema_scope_error(scope,
					index->range[i]->last->src,
					"Failed to resolve last array index");
				ofc_sema_array_delete(array);
				return NULL;
			}

			if (d <= array->segment[i].base)
			{
				ofc_sema_scope_error(scope,
					index->range[i]->first->src,
					"Last array index must be greater than base");
				ofc_sema_array_delete(array);
				return NULL;
			}

			d -= array->segment[i].base;

			array->segment[i].count = d;
			if ((int64_t)array->segment[i].count != d)
			{
				ofc_sema_scope_error(scope,
					index->range[i]->first->src,
					"Last array index out-of-range");
				ofc_sema_array_delete(array);
				return NULL;
			}
		}
		else
		{<commit_msg>Fixed error with array declarations being inclusive<commit_after>#include <ofc/sema.h>



ofc_sema_array_t* ofc_sema_array(
	ofc_sema_scope_t*              scope,
	const ofc_parse_array_index_t* index)
{
	if (!index || (index->count == 0))
		return NULL;

	unsigned i;
	for (i = 0; i < index->count; i++)
	{
		if (!index->range[i])
			return NULL;

		if (!index->range[i]->first
			|| index->range[i]->stride)
			return NULL;
	}

	ofc_sema_array_t* array
		= (ofc_sema_array_t*)malloc(sizeof(ofc_sema_array_t)
			+ (index->count * sizeof(ofc_sema_array_dims_t)));
	if (!array) return NULL;

	array->dimensions = index->count;

	for (i = 0; i < index->count; i++)
	{
		array->segment[i].base = 1;

		ofc_sema_expr_t* expr = ofc_sema_expr(
			scope, index->range[i]->first);

		const ofc_sema_typeval_t* ctv
			= ofc_sema_expr_constant(expr);

		int64_t d;
		bool got = ofc_sema_typeval_get_integer(ctv, &d);
		ofc_sema_expr_delete(expr);

		if (!got)
		{
			ofc_sema_scope_error(scope,
				index->range[i]->first->src,
				"Failed to resolve array dimension");
			ofc_sema_array_delete(array);
			return NULL;
		}

		if (index->range[i]->last)
		{
			array->segment[i].base = d;
			if ((int64_t)array->segment[i].base != d)
			{
				ofc_sema_scope_error(scope,
					index->range[i]->first->src,
					"Array base out-of-range");
				ofc_sema_array_delete(array);
				return NULL;
			}

			expr = ofc_sema_expr(
				scope, index->range[i]->last);

			ctv = ofc_sema_expr_constant(expr);

			got = ofc_sema_typeval_get_integer(ctv, &d);
			ofc_sema_expr_delete(expr);

			if (!got)
			{
				ofc_sema_scope_error(scope,
					index->range[i]->last->src,
					"Failed to resolve last array index");
				ofc_sema_array_delete(array);
				return NULL;
			}

			if (d <= array->segment[i].base)
			{
				ofc_sema_scope_error(scope,
					index->range[i]->first->src,
					"Last array index must be greater than base");
				ofc_sema_array_delete(array);
				return NULL;
			}

			d -= array->segment[i].base;

			/* Count is inclusive. */
			if ((d + 1) == 0)
			{
				ofc_sema_array_delete(array);
				return NULL;
			}
			d += 1;

			array->segment[i].count = d;
			if ((int64_t)array->segment[i].count != d)
			{
				ofc_sema_scope_error(scope,
					index->range[i]->first->src,
					"Last array index out-of-range");
				ofc_sema_array_delete(array);
				return NULL;
			}
		}
		else
		{
{'fraction_non_alphanumeric': 0.15336088753534913, 'fraction_numerical': 0.005003262997607135, 'mean_word_length': 3.3012160898035545, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4845917', 'n_tokens_mistral': 2222, 'n_tokens_neox': 1848, 'n_words': 318}