diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-12 23:41:32 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-12 23:41:32 -0400 |
| commit | da9cc93c466c65e0294b4b29bec6603b2d4552eb (patch) | |
| tree | bfd266d559365366802eaf1a285d7983b426f9c8 | |
| parent | 46a2aa2ffc71820767f0cdaead84c26dc240c893 (diff) | |
Make functions print with `func name(...)->... [file:line]` info
| -rw-r--r-- | builtins/functions.c | 12 | ||||
| -rw-r--r-- | builtins/functions.h | 6 | ||||
| -rw-r--r-- | builtins/types.c | 6 | ||||
| -rw-r--r-- | compile.c | 26 | ||||
| -rw-r--r-- | environment.h | 1 | ||||
| -rw-r--r-- | test/optionals.tm | 4 |
6 files changed, 48 insertions, 7 deletions
diff --git a/builtins/functions.c b/builtins/functions.c index a2b17807..edbea33b 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -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: diff --git a/builtins/functions.h b/builtins/functions.h index b79c596a..16e1cd22 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -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); diff --git a/builtins/types.c b/builtins/types.c index 512fdfc3..307b4756 100644 --- a/builtins/types.c +++ b/builtins/types.c @@ -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; @@ -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) diff --git a/environment.h b/environment.h index 2dfd4bfb..dac3065a 100644 --- a/environment.h +++ b/environment.h @@ -13,6 +13,7 @@ typedef struct { CORD funcs; CORD typeinfos; CORD variable_initializers; + CORD function_naming; } compilation_unit_t; typedef struct fn_ctx_s { diff --git a/test/optionals.tm b/test/optionals.tm index a3f33d6c..928587f7 100644 --- a/test/optionals.tm +++ b/test/optionals.tm @@ -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") |
