aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-23 12:42:10 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-23 12:42:10 -0400
commita1978752141835c386012fff15ceb36261f37997 (patch)
tree988084e503a9118ceb48b99b8c8cb0b3453f172b
parentdceb9255736c69538293ee551272cda1b03a9fd3 (diff)
Bugfix for say() when length is >512, and added back the `newline`
optional parameter (default=yes)
-rw-r--r--builtins/functions.c6
-rw-r--r--builtins/functions.h2
-rw-r--r--compile.c2
-rw-r--r--environment.c3
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)}},
};