Abstract away stdout
Instead of using stdout directly, we're now using a configurable apfl_format_writer.
This commit is contained in:
parent
136a6da06b
commit
fa4ed7bf83
6 changed files with 31 additions and 9 deletions
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue