From d1b2ba7a53242ecfeeb7bb80adb54af323f3119d Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 22 Apr 2022 23:13:01 +0200 Subject: [PATCH] Use formatter for printing values --- src/Makefile.am | 1 - src/apfl.h | 2 +- src/eval.c | 6 ++-- src/internal.c | 17 ------------ src/internal.h | 2 -- src/main.c | 4 +-- src/value.c | 74 +++++++++++++++++++++++++++---------------------- src/value.h | 2 +- 8 files changed, 48 insertions(+), 60 deletions(-) delete mode 100644 src/internal.c diff --git a/src/Makefile.am b/src/Makefile.am index 7901177..789b5e7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,7 +13,6 @@ libapfl_a_SOURCES += expr.c libapfl_a_SOURCES += format.c libapfl_a_SOURCES += gc.c libapfl_a_SOURCES += hashmap.c -libapfl_a_SOURCES += internal.c libapfl_a_SOURCES += parser.c libapfl_a_SOURCES += position.c libapfl_a_SOURCES += resizable.c diff --git a/src/apfl.h b/src/apfl.h index 4c54e56..22722e2 100644 --- a/src/apfl.h +++ b/src/apfl.h @@ -622,7 +622,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); -void apfl_debug_print_val(apfl_ctx, apfl_stackidx, FILE *); +bool apfl_debug_print_val(apfl_ctx, apfl_stackidx, FILE *); #ifdef __cplusplus } diff --git a/src/eval.c b/src/eval.c index 84013a5..3b4f007 100644 --- a/src/eval.c +++ b/src/eval.c @@ -180,16 +180,16 @@ eval_expr(apfl_ctx ctx, struct apfl_expr expr) return result; } -void +bool apfl_debug_print_val(apfl_ctx ctx, apfl_stackidx index, FILE *f) { struct apfl_value value; if (!apfl_stack_pop(ctx, &value, index)) { fprintf(f, "apfl_debug_print_val: Invalid stack index %d\n", index); - return; + return true; } - apfl_value_print(value, f); + return apfl_value_print(value, f); } struct apfl_iterative_runner_data { diff --git a/src/internal.c b/src/internal.c deleted file mode 100644 index 043c9ef..0000000 --- a/src/internal.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - -#include "apfl.h" -#include "internal.h" - -void -apfl_print_indented(unsigned indent, FILE *f, const char* fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - while (indent--) { - fputs(" ", f); - } - vfprintf(f, fmt, ap); - va_end(ap); -} diff --git a/src/internal.h b/src/internal.h index dbdd2eb..96c527c 100644 --- a/src/internal.h +++ b/src/internal.h @@ -12,8 +12,6 @@ extern "C" { // Internal use only functions -void apfl_print_indented(unsigned indent, FILE *, const char* fmt, ...); - struct apfl_string *apfl_string_move_into_new_gc_string(struct gc *gc, struct apfl_string *in); #ifdef __cplusplus diff --git a/src/main.c b/src/main.c index 0129899..e7693bb 100644 --- a/src/main.c +++ b/src/main.c @@ -159,10 +159,10 @@ repl_eval(void) while (apfl_iterative_runner_next(runner)) { switch (apfl_iterative_runner_get_result(runner)) { case APFL_RESULT_OK : - apfl_debug_print_val(ctx, -1, stdout); + assert(apfl_debug_print_val(ctx, -1, stdout)); break; case APFL_RESULT_ERR: - apfl_debug_print_val(ctx, -1, stderr); + assert(apfl_debug_print_val(ctx, -1, stderr)); fprintf(stderr, "Error occurred during evaluation.\n"); break; case APFL_RESULT_ERR_FATAL: diff --git a/src/value.c b/src/value.c index 8f23873..8e88bea 100644 --- a/src/value.c +++ b/src/value.c @@ -4,11 +4,14 @@ #include "alloc.h" #include "context.h" +#include "format.h" #include "internal.h" #include "resizable.h" #include "hashmap.h" #include "value.h" +#define TRY(x) do { if (!(x)) return false; } while (0) + size_t apfl_list_len(struct list_header *list) { @@ -73,45 +76,44 @@ apfl_dict_deinit(struct dict_header *header) apfl_hashmap_deinit(&header->map); } -static void -print(unsigned indent, FILE *out, struct apfl_value value, bool skip_first_indent) +static bool +format(unsigned indent, struct apfl_format_writer w, struct apfl_value value, bool skip_first_indent) { - struct apfl_string_view sv; - unsigned first_indent = skip_first_indent ? 0 : indent; + TRY(apfl_format_put_indent(w, skip_first_indent ? 0 : indent)); switch (value.type) { case VALUE_NIL: - apfl_print_indented(first_indent, out, "nil"); - return; + TRY(apfl_format_put_string(w, "nil")); + return true; case VALUE_BOOLEAN: - apfl_print_indented(first_indent, out, value.boolean ? "true" : "false"); - return; + TRY(apfl_format_put_string(w, value.boolean ? "true" : "false")); + return true; case VALUE_NUMBER: - apfl_print_indented(first_indent, out, "%f", value.number); - return; + TRY(apfl_format_put_number(w, value.number)); + return true; case VALUE_STRING: - sv = apfl_string_view_from(*value.string); - apfl_print_indented(first_indent, out, "\"" APFL_STR_FMT "\"", APFL_STR_FMT_ARGS(sv)); - return; + TRY(apfl_format_put_string(w, "\"")); + TRY(apfl_format_put_string(w, *value.string)); + TRY(apfl_format_put_string(w, "\"")); + return true; case VALUE_LIST: if (value.list->len == 0) { - apfl_print_indented(first_indent, out, "[]"); - return; + return apfl_format_put_string(w, "[]"); } - apfl_print_indented(first_indent, out, "[\n"); + TRY(apfl_format_put_string(w, "[\n")); for (size_t i = 0; i < value.list->len; i++) { - print(indent+1, out, value.list->items[i], false); - apfl_print_indented(indent, out, "\n"); + TRY(format(indent+1, w, value.list->items[i], false)); + TRY(apfl_format_put_string(w, "\n")); } - apfl_print_indented(indent, out, "]"); - return; + TRY(apfl_format_put_indent(w, indent)); + TRY(apfl_format_put_string(w, "]")); + return true; case VALUE_DICT: if (apfl_hashmap_count(value.dict->map) == 0) { - apfl_print_indented(first_indent, out, "[->]"); - return; + return apfl_format_put_string(w, "[->]"); } - apfl_print_indented(first_indent, out, "[\n"); + TRY(apfl_format_put_string(w, "[\n")); HASHMAP_EACH(&value.dict->map, cur) { struct apfl_value *k = apfl_hashmap_cursor_peek_key(cur); @@ -119,16 +121,20 @@ print(unsigned indent, FILE *out, struct apfl_value value, bool skip_first_inden struct apfl_value *v = apfl_hashmap_cursor_peek_value(cur); assert(v != NULL); - print(indent+1, out, *k, false); - fprintf(out, " -> "); - print(indent+1, out, *v, true); - fprintf(out, "\n"); + TRY(format(indent+1, w, *k, false)); + TRY(apfl_format_put_string(w, " -> ")); + TRY(format(indent+1, w, *v, true)); + TRY(apfl_format_put_string(w, "\n")); } - apfl_print_indented(indent, out, "]"); - return; + TRY(apfl_format_put_indent(w, indent)); + TRY(apfl_format_put_string(w, "]")); + return true; } - fprintf(out, "Unknown value? (%d)\n", (int)value.type); + TRY(apfl_format_put_string(w, "Unknown value ? (")); + TRY(apfl_format_put_number(w, (int)value.type)); + TRY(apfl_format_put_string(w, ")")); + return true; } struct apfl_value @@ -139,11 +145,13 @@ apfl_value_move(struct apfl_value *src) return out; } -void +bool apfl_value_print(struct apfl_value value, FILE *out) { - print(0, out, value, false); - fprintf(out, "\n"); + 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; } static bool diff --git a/src/value.h b/src/value.h index c097639..1effec0 100644 --- a/src/value.h +++ b/src/value.h @@ -56,7 +56,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); -void apfl_value_print(struct apfl_value, FILE *); +bool apfl_value_print(struct apfl_value, FILE *); apfl_hash apfl_value_hash(const struct apfl_value); enum get_item_result {