From 85bd567d3c12511149feb3c50e4ae078cee7950a Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 13 Feb 2024 22:13:54 -0500 Subject: Add kwargs as a macro hack --- compile.c | 17 +++++++++++++---- nextlang.c | 1 + nextlang.h | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/compile.c b/compile.c index 3b71d6f2..1dfd3c26 100644 --- a/compile.c +++ b/compile.c @@ -203,22 +203,31 @@ CORD compile(env_t *env, ast_t *ast) } case FunctionDef: { auto fndef = Match(ast, FunctionDef); - CORD_appendf(&env->staticdefs, "static %r %r(", fndef->ret_type ? compile_type(env, fndef->ret_type) : "void", compile(env, fndef->name)); + CORD name = compile(env, fndef->name); + CORD_appendf(&env->staticdefs, "static %r %r_(", fndef->ret_type ? compile_type(env, fndef->ret_type) : "void", name); for (arg_ast_t *arg = fndef->args; arg; arg = arg->next) { CORD_appendf(&env->staticdefs, "%r %s", compile_type(env, arg->type), arg->name); if (arg->next) env->staticdefs = CORD_cat(env->staticdefs, ", "); } env->staticdefs = CORD_cat(env->staticdefs, ");\n"); - CORD_appendf(&env->funcs, "%r %r(", fndef->ret_type ? compile_type(env, fndef->ret_type) : "void", compile(env, fndef->name)); + CORD kwargs = CORD_asprintf("#define %r(...) ({ struct {", name); + CORD passed_args = CORD_EMPTY; + CORD_appendf(&env->funcs, "%r %r_(", fndef->ret_type ? compile_type(env, fndef->ret_type) : "void", name); for (arg_ast_t *arg = fndef->args; arg; arg = arg->next) { - CORD_appendf(&env->funcs, "%r %s", compile_type(env, arg->type), arg->name); + CORD arg_type = compile_type(env, arg->type); + CORD_appendf(&env->funcs, "%r %s", arg_type, arg->name); if (arg->next) env->funcs = CORD_cat(env->funcs, ", "); + CORD_appendf(&kwargs, "%r %s; ", arg_type, arg->name); + CORD_appendf(&passed_args, "__args.%s", arg->name); + if (arg->next) passed_args = CORD_cat(passed_args, ", "); } + CORD_appendf(&kwargs, "} __args = {__VA_ARGS__}; %r_(%r); })\n", name, passed_args); + CORD body = compile(env, fndef->body); if (CORD_fetch(body, 0) != '{') body = CORD_asprintf("{\n%r\n}", body); - CORD_appendf(&env->funcs, ") %r", body); + CORD_appendf(&env->funcs, ") %r\n%r", body, kwargs); return CORD_EMPTY; } case FunctionCall: { diff --git a/nextlang.c b/nextlang.c index 0124f834..cfd86c61 100644 --- a/nextlang.c +++ b/nextlang.c @@ -90,6 +90,7 @@ int main(int argc, char *argv[]) ldlibs = heap_strf("%s %s", ldlibs, getenv("LDLIBS")); const char *run = heap_strf("tcc -run %s %s -", cflags, ldlibs); + // const char *run = heap_strf("gcc -x c %s %s -", cflags, ldlibs); FILE *cc = popen(run, "w"); CORD_put(program, cc); fclose(cc); diff --git a/nextlang.h b/nextlang.h index 3f17e281..d434b3b3 100644 --- a/nextlang.h +++ b/nextlang.h @@ -24,6 +24,7 @@ #define Num_t double #define String_t CORD +#define Str_t CORD #define Bool_t bool #define yes (Bool_t)true -- cgit v1.2.3