Use formatter for printing values

This commit is contained in:
Laria 2022-04-22 23:13:01 +02:00
parent a4fd3acfac
commit d1b2ba7a53
8 changed files with 48 additions and 60 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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 {

View file

@ -1,17 +0,0 @@
#include <stdarg.h>
#include <stdio.h>
#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);
}

View file

@ -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

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 :
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:

View file

@ -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

View file

@ -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 {