apfl_debug_print_val: Use format writer abstraction

This commit is contained in:
Laria 2022-06-05 22:06:33 +02:00
parent a773e929bf
commit 09f7df79bb
7 changed files with 23 additions and 19 deletions

View file

@ -108,6 +108,15 @@ struct apfl_string apfl_string_builder_move_string(struct apfl_string_builder *)
#define apfl_string_builder_append_cstr(builder, cstr) (apfl_string_builder_append((builder), apfl_string_view_from_cstr((cstr))))
struct apfl_format_writer {
bool (*write)(void *, const char *buf, size_t len);
void *opaque;
};
struct apfl_format_writer apfl_format_file_writer(FILE *f);
struct apfl_format_writer apfl_format_string_writer(struct apfl_string_builder *sb);
// Tokens
enum apfl_token_type {
@ -627,7 +636,7 @@ enum apfl_result apfl_dict_set(apfl_ctx, apfl_stackidx dict, apfl_stackidx k, ap
// Get a value from a container (list or dict) and push it on the stack. container and k will be dropped.
enum apfl_result apfl_get_member(apfl_ctx, apfl_stackidx container, apfl_stackidx k);
bool apfl_debug_print_val(apfl_ctx, apfl_stackidx, FILE *);
bool apfl_debug_print_val(apfl_ctx, apfl_stackidx, struct apfl_format_writer);
struct apfl_messages {
const char *could_not_alloc_mem;

View file

@ -6,6 +6,7 @@
#include "bytecode.h"
#include "compile.h"
#include "context.h"
#include "format.h"
#include "hashmap.h"
#include "strings.h"
#include "value.h"
@ -181,15 +182,17 @@ eval_expr(apfl_ctx ctx, struct apfl_expr expr)
}
bool
apfl_debug_print_val(apfl_ctx ctx, apfl_stackidx index, FILE *f)
apfl_debug_print_val(apfl_ctx ctx, apfl_stackidx index, struct apfl_format_writer w)
{
struct apfl_value value;
if (!apfl_stack_pop(ctx, &value, index)) {
fprintf(f, "apfl_debug_print_val: Invalid stack index %d\n", index);
FMT_TRY(apfl_format_put_string(w, "apfl_debug_print_val: Invalid stack index "));
FMT_TRY(apfl_format_put_int(w, (int)index));
FMT_TRY(apfl_format_put_string(w, "\n"));
return true;
}
return apfl_value_print(value, f);
return apfl_value_print(value, w);
}
struct apfl_iterative_runner_data {

View file

@ -8,7 +8,7 @@
#include "format.h"
#define WRITE(w, buf, len) w.write(w.opaque, buf, len)
#define TRY(x) if (!(x)) return false;
#define TRY FMT_TRY
static bool
write_file(void *opaque, const char *buf, size_t len)

View file

@ -11,14 +11,7 @@ extern "C" {
#include "apfl.h"
struct apfl_format_writer {
bool (*write)(void *, const char *buf, size_t len);
void *opaque;
};
struct apfl_format_writer apfl_format_file_writer(FILE *f);
struct apfl_format_writer apfl_format_string_writer(struct apfl_string_builder *sb);
#define FMT_TRY(x) do { if (!(x)) return false; } while (0)
bool apfl_format_put_string_view(struct apfl_format_writer, struct apfl_string_view);
#define apfl_format_put_string(w, s) apfl_format_put_string_view((w), apfl_string_view_from(s))

View file

@ -159,10 +159,10 @@ repl_eval(void)
while (apfl_iterative_runner_next(runner)) {
switch (apfl_iterative_runner_get_result(runner)) {
case APFL_RESULT_OK :
assert(apfl_debug_print_val(ctx, -1, stdout));
assert(apfl_debug_print_val(ctx, -1, apfl_format_file_writer(stdout)));
break;
case APFL_RESULT_ERR:
assert(apfl_debug_print_val(ctx, -1, stderr));
assert(apfl_debug_print_val(ctx, -1, apfl_format_file_writer(stderr)));
fprintf(stderr, "Error occurred during evaluation.\n");
break;
case APFL_RESULT_ERR_FATAL:

View file

@ -9,7 +9,7 @@
#include "hashmap.h"
#include "value.h"
#define TRY(x) do { if (!(x)) return false; } while (0)
#define TRY FMT_TRY
size_t
apfl_list_len(struct list_header *list)
@ -159,9 +159,8 @@ apfl_value_move(struct apfl_value *src)
}
bool
apfl_value_print(struct apfl_value value, FILE *out)
apfl_value_print(struct apfl_value value, struct apfl_format_writer w)
{
struct apfl_format_writer w = apfl_format_file_writer(out);
TRY(format(0, w, value, false));
TRY(apfl_format_put_string(w, "\n"));
return true;

View file

@ -58,7 +58,7 @@ enum apfl_value_type apfl_value_type_to_abstract_type(enum value_type);
bool apfl_value_eq(const struct apfl_value, const struct apfl_value);
struct apfl_value apfl_value_move(struct apfl_value *src);
bool apfl_value_print(struct apfl_value, FILE *);
bool apfl_value_print(struct apfl_value, struct apfl_format_writer);
apfl_hash apfl_value_hash(const struct apfl_value);
enum get_item_result {