~yerinalexey/sushi

cffbc42b03cca7c05be1da67c6f49fcea6e68806 — Alexey Yerin 4 months ago 138cf54 main
env: implement string-len
1 files changed, 19 insertions(+), 0 deletions(-)

M src/env.c
M src/env.c => src/env.c +19 -0
@@ 458,6 458,24 @@ env_string_sub(struct location loc, struct list_expression *list,
}

static struct expression *
env_string_len(struct location loc, struct list_expression *list,
	struct binding **bindings)
{
	if (!list) {
		error(loc, "Not enough arguments");
	}
	struct expression *str = eval(list->expr, bindings);
	if (str->kind != EXPR_STRING) {
		error(loc, "Expected argument to be a string");
	}

	struct expression *expr = mkexpression();
	expr->kind = EXPR_INTEGER;
	expr->integer = strlen(str->string);
	return expr;
}

static struct expression *
env_display(struct location loc, struct list_expression *list,
	struct binding **bindings)
{


@@ 511,6 529,7 @@ environment[] = {

	{ .name = "string-concat", .func = &env_string_concat },
	{ .name = "string-sub",    .func = &env_string_sub },
	{ .name = "string-len",    .func = &env_string_len },

	{ .name = "display", .func = &env_display },
	{ .name = "newline", .func = &env_newline },