~bl4ckb0ne/boson

10c8468dd37146b28d19d6164e4a2d2c32b444c0 — Simon Zeni 4 months ago b2ca4f0
interpreter: eval array
1 files changed, 26 insertions(+), 0 deletions(-)

M src/interpreter.c
M src/interpreter.c => src/interpreter.c +26 -0
@@ 158,6 158,29 @@ eval_identifier(struct context *ctx, struct ast_identifier *identifier)
	return hash_table_get(ctx->env, identifier->data);
}

static struct object *
eval_array(struct context *ctx, struct ast_expression_list *array)
{
	struct object *obj = calloc(1, sizeof(struct object));
	if (!obj) {
		fatal("failed to allocate array");
	}

	obj->type = OBJECT_TYPE_ARRAY;
	obj->array.n = array->n;
	obj->array.objects = calloc(array->n, sizeof(struct object*));

	for (size_t i = 0; i < array->n; ++i) {
		struct object *item = eval_expression(ctx, array->expressions[i]);
		if (!item) {
			fatal("array item at %zu is empty", i);
		}
		obj->array.objects[i] = item;
	}

	return obj;
}

struct object *
eval_expression(struct context *ctx, struct ast_expression *expression)
{


@@ 178,6 201,9 @@ eval_expression(struct context *ctx, struct ast_expression *expression)
	case EXPRESSION_IDENTIFIER:
		obj = eval_identifier(ctx, expression->data.identifier);
		break;
	case EXPRESSION_ARRAY:
		obj = eval_array(ctx, expression->data.array);
		break;
	default:
		fatal("todo handle expression %s",
				ast_expression_to_str(expression));