From a1978752141835c386012fff15ceb36261f37997 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 23 Aug 2024 12:42:10 -0400 Subject: Bugfix for say() when length is >512, and added back the `newline` optional parameter (default=yes) --- builtins/functions.c | 6 ++++-- builtins/functions.h | 2 +- compile.c | 2 +- environment.c | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/builtins/functions.c b/builtins/functions.c index d71587b9..3eea3c89 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -233,14 +233,16 @@ public void end_test(void *expr, const TypeInfo *type, CORD expected, const char } } -public void say(CORD text) +public void say(CORD text, bool newline) { uint8_t buf[512] = {0}; size_t buf_len = sizeof(buf)-1; const char *str = CORD_to_const_char_star(text); uint8_t *normalized = u8_normalize(UNINORM_NFD, (uint8_t*)str, strlen(str), buf, &buf_len); if (normalized) { - puts((char*)normalized); + write(STDOUT_FILENO, normalized, buf_len); + if (newline) + write(STDOUT_FILENO, "\n", 1); if (normalized != buf) free(normalized); } diff --git a/builtins/functions.h b/builtins/functions.h index ac4fbf81..70266ba6 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -21,7 +21,7 @@ void end_test(void *expr, const TypeInfo *type, CORD expected, const char *filen #define test(expr, type, expected, filename, start, end) {\ start_test(filename, start, end); \ end_test(expr, type, expected, filename, start, end); } -void say(CORD text); +void say(CORD text, bool newline); uint32_t generic_hash(const void *obj, const TypeInfo *type); int32_t generic_compare(const void *x, const void *y, const TypeInfo *type); diff --git a/compile.c b/compile.c index 15045d6d..1dfe54d9 100644 --- a/compile.c +++ b/compile.c @@ -832,7 +832,7 @@ CORD compile_statement(env_t *env, ast_t *ast) } if (chunk->next) code = CORD_cat(code, ", "); } - return CORD_cat(code, "));"); + return CORD_cat(code, "), yes);"); } case Return: { if (!env->fn_ctx) code_err(ast, "This return statement is not inside any function"); diff --git a/environment.c b/environment.c index f1d2a042..b650004c 100644 --- a/environment.c +++ b/environment.c @@ -31,7 +31,8 @@ env_t *new_compilation_unit(CORD *libname) const char *name; binding_t binding; } global_vars[] = { - {"say", {.code="say", .type=Type(FunctionType, .args=new(arg_t, .name="text", .type=TEXT_TYPE), .ret=Type(VoidType))}}, + {"say", {.code="say", .type=Type(FunctionType, .args=new(arg_t, .name="text", .type=TEXT_TYPE, + .next=new(arg_t, .name="newline", .type=Type(BoolType), .default_val=FakeAST(Bool, true))), .ret=Type(VoidType))}}, {"fail", {.code="fail", .type=Type(FunctionType, .args=new(arg_t, .name="message", .type=TEXT_TYPE), .ret=Type(AbortType))}}, {"USE_COLOR", {.code="USE_COLOR", .type=Type(BoolType)}}, }; -- cgit v1.2.3