From fa4ed7bf836b8d48b5c8cf4db043744c4f35d435 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 28 Oct 2022 21:32:17 +0200 Subject: [PATCH] Abstract away stdout Instead of using stdout directly, we're now using a configurable apfl_format_writer. --- src/apfl.h | 7 ++++++- src/context.c | 13 ++++++++++--- src/context.h | 4 ++++ src/globals.c | 6 +++--- src/main.c | 5 ++++- webpage/playground/playground.c | 5 ++++- 6 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/apfl.h b/src/apfl.h index f1e0ba4..4e359c9 100644 --- a/src/apfl.h +++ b/src/apfl.h @@ -616,7 +616,12 @@ enum apfl_result { APFL_RESULT_ERR_FATAL, }; -apfl_ctx apfl_ctx_new(struct apfl_allocator); +struct apfl_config { + struct apfl_allocator allocator; + struct apfl_format_writer output_writer; +}; + +apfl_ctx apfl_ctx_new(struct apfl_config); void apfl_ctx_destroy(apfl_ctx); diff --git a/src/context.c b/src/context.c index f28c200..70a2592 100644 --- a/src/context.c +++ b/src/context.c @@ -611,9 +611,9 @@ error: } apfl_ctx -apfl_ctx_new(struct apfl_allocator base_allocator) +apfl_ctx_new(struct apfl_config config) { - apfl_ctx ctx = ALLOC_OBJ(base_allocator, struct apfl_ctx_data); + apfl_ctx ctx = ALLOC_OBJ(config.allocator, struct apfl_ctx_data); if (ctx == NULL) { return NULL; } @@ -628,12 +628,14 @@ apfl_ctx_new(struct apfl_allocator base_allocator) ctx->globals = NULL; ctx->iterative_runners = iterative_runners_list_new(); - apfl_gc_init(&ctx->gc, base_allocator, get_roots, ctx); + apfl_gc_init(&ctx->gc, config.allocator, get_roots, ctx); if ((ctx->globals = apfl_scope_new(&ctx->gc)) == NULL) { goto error; } + ctx->output_writer = config.output_writer; + if (!init_globals(ctx)) { goto error; } @@ -1301,3 +1303,8 @@ void *apfl_get_userdata(apfl_ctx ctx, apfl_stackidx index) } return value.userdata; } + +struct apfl_format_writer apfl_get_output_writer(apfl_ctx ctx) +{ + return ctx->output_writer; +} diff --git a/src/context.h b/src/context.h index 7329bbf..73ac501 100644 --- a/src/context.h +++ b/src/context.h @@ -123,6 +123,8 @@ struct apfl_ctx_data { struct error_handler *error_handler; struct iterative_runners_list iterative_runners; + + struct apfl_format_writer output_writer; }; void apfl_call_stack_entry_deinit(struct apfl_allocator, struct call_stack_entry *); @@ -147,6 +149,8 @@ struct call_stack_entry *apfl_call_stack_cur_entry(apfl_ctx); struct scope *apfl_closure_scope_for_func(apfl_ctx); +struct apfl_format_writer apfl_get_output_writer(apfl_ctx); + bool apfl_ctx_register_iterative_runner(apfl_ctx, apfl_iterative_runner); void apfl_ctx_unregister_iterative_runner(apfl_ctx, apfl_iterative_runner); diff --git a/src/globals.c b/src/globals.c index aad8b94..99480cc 100644 --- a/src/globals.c +++ b/src/globals.c @@ -146,7 +146,7 @@ IMPL_MATH_OP_FUNC(impl_div, "/", single_identity, op_div) static void print(apfl_ctx ctx) { - struct apfl_format_writer w = apfl_format_file_writer(stdout); + struct apfl_format_writer w = apfl_get_output_writer(ctx); size_t len = apfl_len(ctx, 0); for (size_t i = 0; i < len; i++) { @@ -170,7 +170,7 @@ print(apfl_ctx ctx) static void dump(apfl_ctx ctx) { - struct apfl_format_writer w = apfl_format_file_writer(stdout); + struct apfl_format_writer w = apfl_get_output_writer(ctx); apfl_get_list_member_by_index(ctx, 0, 0); apfl_drop(ctx, 0); TRY_FORMAT(ctx, apfl_debug_print_val(ctx, -1, w)); @@ -179,7 +179,7 @@ dump(apfl_ctx ctx) static void disasm(apfl_ctx ctx) { - struct apfl_format_writer w = apfl_format_file_writer(stdout); + struct apfl_format_writer w = apfl_get_output_writer(ctx); apfl_get_list_member_by_index(ctx, 0, 0); apfl_drop(ctx, 0); struct apfl_value value = apfl_stack_must_get(ctx, -1); diff --git a/src/main.c b/src/main.c index 9263a6a..865a0a8 100644 --- a/src/main.c +++ b/src/main.c @@ -151,7 +151,10 @@ repl_eval(void) { int rv = 0; - apfl_ctx ctx = apfl_ctx_new(apfl_allocator_default()); + apfl_ctx ctx = apfl_ctx_new((struct apfl_config) { + .allocator = apfl_allocator_default(), + .output_writer = apfl_format_file_writer(stdout), + }); if (ctx == NULL) { fprintf(stderr, "Failed to init the context\n"); return 1; diff --git a/webpage/playground/playground.c b/webpage/playground/playground.c index f4b86d5..29e2286 100644 --- a/webpage/playground/playground.c +++ b/webpage/playground/playground.c @@ -78,7 +78,10 @@ main(void) struct apfl_format_writer w_ok = {.write = web_fmt_write, .opaque = (void *)0}; struct apfl_format_writer w_err = {.write = web_fmt_write, .opaque = (void *)1}; - apfl_ctx ctx = apfl_ctx_new(apfl_allocator_default()); + apfl_ctx ctx = apfl_ctx_new((struct apfl_config) { + .allocator = apfl_allocator_default(), + .output_writer = w_ok, + }); if (ctx == NULL) { assert(apfl_format_put_string(w_err, "Failed to init the context\n")); return 1;