Bugfix for say() when length is >512, and added back the newline

optional parameter (default=yes)
This commit is contained in:
Bruce Hill 2024-08-23 12:42:10 -04:00
parent dceb925573
commit a197875214
4 changed files with 8 additions and 5 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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");

View File

@ -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)}},
};