Make functions print with func name(...)->... [file:line] info

This commit is contained in:
Bruce Hill 2024-09-12 23:41:32 -04:00
parent 46a2aa2ffc
commit da9cc93c46
6 changed files with 48 additions and 7 deletions

View File

@ -43,6 +43,18 @@ public void tomo_init(void)
errx(1, "Couldn't set printf specifier");
}
static Table_t function_names = {};
public void register_function(void *fn, Text_t name)
{
Table$set(&function_names, &fn, &name, Table$info(Function$info("???"), &Text$info));
}
public Text_t *get_function_name(void *fn)
{
return Table$get(function_names, &fn, Table$info(Function$info("???"), &Text$info));
}
void print_stack_trace(FILE *out, int start, int stop)
{
// Print stack trace:

View File

@ -11,6 +11,9 @@
#include "util.h"
void tomo_init(void);
void register_function(void *fn, Text_t name);
Text_t *get_function_name(void *fn);
__attribute__((format(printf, 1, 2)))
_Noreturn void fail(const char *fmt, ...);
__attribute__((format(printf, 4, 5)))
@ -20,7 +23,8 @@ void start_test(const char *filename, int64_t start, int64_t end);
void end_test(const void *expr, const TypeInfo *type, const char *expected, const char *filename, int64_t start, int64_t end);
#define test(expr, typeinfo, expected, start, end) {\
start_test(__SOURCE_FILE__, start, end); \
end_test((__typeof__(expr)[1]){expr}, typeinfo, expected, __SOURCE_FILE__, start, end); }
auto _expr = expr; \
end_test(&_expr, typeinfo, expected, __SOURCE_FILE__, start, end); }
void say(Text_t text, bool newline);
Text_t ask(Text_t prompt, bool bold, bool force_tty);
_Noreturn void tomo_exit(Text_t text, int32_t status);

View File

@ -39,7 +39,11 @@ public Text_t Func$as_text(const void *fn, bool colorize, const TypeInfo *type)
{
(void)fn;
Text_t text = Text$from_str(type->FunctionInfo.type_str);
if (fn) text = Text$concat(text, Text(": ..."));
if (fn) {
Text_t *name = get_function_name(*(void**)fn);
if (name)
text = *name;
}
if (fn && colorize)
text = Text$concat(Text("\x1b[32;1m"), text, Text("\x1b[m"));
return text;

View File

@ -842,6 +842,20 @@ CORD compile_statement(env_t *env, ast_t *ast)
env->code->funcs = CORD_cat(env->code->funcs, wrapper);
}
CORD text = CORD_all("func ", Match(fndef->name, Var)->name, "(");
for (arg_ast_t *arg = fndef->args; arg; arg = arg->next) {
text = CORD_cat(text, type_to_cord(get_arg_ast_type(env, arg)));
if (arg->next) text = CORD_cat(text, ", ");
}
if (ret_t && ret_t->tag != VoidType)
text = CORD_all(text, ")->", type_to_cord(ret_t));
else
text = CORD_all(text, ")");
env->code->function_naming = CORD_all(
env->code->function_naming,
CORD_asprintf("register_function(%r, Text(\"%r [%s.tm:%ld]\"));\n",
name, text, file_base_name(ast->file->filename), get_line_number(ast->file, ast->start)));
return CORD_EMPTY;
}
case Skip: {
@ -2391,6 +2405,11 @@ CORD compile(env_t *env, ast_t *ast)
auto lambda = Match(ast, Lambda);
CORD name = CORD_asprintf("%rlambda$%ld", namespace_prefix(env->libname, env->namespace), lambda->id);
env->code->function_naming = CORD_all(
env->code->function_naming,
CORD_asprintf("register_function(%r, Text(\"%r [%s.tm:%ld]\"));\n",
name, type_to_cord(get_type(env, ast)), file_base_name(ast->file->filename), get_line_number(ast->file, ast->start)));
env_t *body_scope = fresh_scope(env);
for (arg_ast_t *arg = lambda->args; arg; arg = arg->next) {
type_t *arg_type = get_arg_ast_type(env, arg);
@ -3670,14 +3689,15 @@ CORD compile_file(env_t *env, ast_t *ast)
"#include \"", name, ".tm.h\"\n\n",
env->code->local_typedefs, "\n",
env->code->staticdefs, "\n",
env->code->funcs, "\n",
env->code->typeinfos, "\n",
"public void ", env->namespace->name, "$$initialize(void) {\n",
"static bool initialized = false;\n",
"if (initialized) return;\n",
"initialized = true;\n",
env->code->variable_initializers,
"}\n",
env->code->funcs, "\n",
env->code->typeinfos, "\n");
env->code->function_naming,
"}\n");
}
CORD compile_statement_imports(env_t *env, ast_t *ast)

View File

@ -13,6 +13,7 @@ typedef struct {
CORD funcs;
CORD typeinfos;
CORD variable_initializers;
CORD function_naming;
} compilation_unit_t;
typedef struct fn_ctx_s {

View File

@ -185,12 +185,12 @@ func main():
!! ...
!! Lambdas:
>> yep := maybe_lambda(yes)
= func(): ...?
= func() [optionals.tm:54]?
>> nope := maybe_lambda(no)
= !func()
>> if yep:
>> yep
= func(): ...
= func() [optionals.tm:54]
else: fail("Falsey: $yep")
>> if nope:
fail("Truthy: $nope")