Rename format => io_writer

To make it clearer that this can be used for writing binary data too.
This commit is contained in:
Laria 2023-02-10 21:38:54 +01:00
parent 0b44878fcd
commit 55c95f99ad
18 changed files with 306 additions and 305 deletions

View file

@ -126,24 +126,25 @@ struct apfl_string_view apfl_string_view_ltrim(struct apfl_string_view sv);
struct apfl_string_view apfl_string_view_rtrim(struct apfl_string_view sv);
struct apfl_string_view apfl_string_view_trim(struct apfl_string_view sv);
struct apfl_format_writer {
struct apfl_io_writer {
bool (*write)(void *, const char *buf, size_t len);
void *opaque;
};
struct apfl_format_writer apfl_format_file_writer(FILE *f);
struct apfl_io_writer apfl_io_file_writer(FILE *f);
struct apfl_format_writer apfl_format_string_writer(struct apfl_string_builder *sb);
struct apfl_io_writer apfl_io_string_writer(struct apfl_string_builder *sb);
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))
bool apfl_format_put_int(struct apfl_format_writer, int);
bool apfl_format_put_char(struct apfl_format_writer, char);
bool apfl_format_put_hexbyte(struct apfl_format_writer, unsigned char);
bool apfl_format_put_pos(struct apfl_format_writer, struct apfl_position);
bool apfl_format_put_indent(struct apfl_format_writer, unsigned);
bool apfl_format_put_number(struct apfl_format_writer, apfl_number);
bool apfl_format_put_poiner(struct apfl_format_writer, void *);
bool apfl_io_write_string_view(struct apfl_io_writer, struct apfl_string_view);
#define apfl_io_write_string(w, s) apfl_io_write_string_view((w), apfl_string_view_from(s))
bool apfl_io_write_byte(struct apfl_io_writer, char);
bool apfl_format_put_int(struct apfl_io_writer, int);
bool apfl_format_put_hexbyte(struct apfl_io_writer, unsigned char);
bool apfl_format_put_pos(struct apfl_io_writer, struct apfl_position);
bool apfl_format_put_indent(struct apfl_io_writer, unsigned);
bool apfl_format_put_number(struct apfl_io_writer, apfl_number);
bool apfl_format_put_poiner(struct apfl_io_writer, void *);
// Tokens
@ -628,7 +629,7 @@ enum apfl_result {
struct apfl_config {
struct apfl_allocator allocator;
struct apfl_format_writer output_writer;
struct apfl_io_writer output_writer;
};
apfl_ctx apfl_ctx_new(struct apfl_config);
@ -647,8 +648,8 @@ enum apfl_result apfl_iterative_runner_get_result(apfl_iterative_runner);
bool apfl_iterative_runner_stopped_because_of_error(apfl_iterative_runner);
bool apfl_iterative_runner_run_repl(
apfl_iterative_runner,
struct apfl_format_writer w_out,
struct apfl_format_writer w_err
struct apfl_io_writer w_out,
struct apfl_io_writer w_err
);
void apfl_iterative_runner_destroy(apfl_iterative_runner);
@ -761,9 +762,9 @@ struct apfl_call_stack_entry_info {
size_t apfl_call_stack_depth(apfl_ctx);
// Get information about the n-th call stack entry, where n=0 is the current entry.
struct apfl_call_stack_entry_info apfl_call_stack_inspect(apfl_ctx, size_t n);
bool apfl_call_stack_entry_info_format(struct apfl_format_writer, struct apfl_call_stack_entry_info);
bool apfl_call_stack_entry_info_format(struct apfl_io_writer, struct apfl_call_stack_entry_info);
bool apfl_debug_print_val(apfl_ctx, apfl_stackidx, struct apfl_format_writer);
bool apfl_debug_print_val(apfl_ctx, apfl_stackidx, struct apfl_io_writer);
// Raise an error with a value from the stack as the message.
APFL_NORETURN void apfl_raise_error(apfl_ctx, apfl_stackidx);

View file

@ -251,20 +251,20 @@ apfl_matcher_instructions_deinit(struct apfl_allocator allocator, struct matcher
#define GET_ARGUMENT_FOR_DUMP(w, ilist, i, arg) \
do { \
if (i >= ilist->len) { \
FMT_TRY(apfl_format_put_string(w, "Bytecode corrupted")); \
FMT_TRY(apfl_io_write_string(w, "Bytecode corrupted")); \
return false; \
} \
arg = ilist->instructions[++i]; \
} while (0)
bool
apfl_bytecode_dump_matcher(unsigned indent, struct apfl_format_writer w, struct matcher_instruction_list *milist)
apfl_bytecode_dump_matcher(unsigned indent, struct apfl_io_writer w, struct matcher_instruction_list *milist)
{
for (size_t i = 0; i < milist->len; i++) {
union matcher_instruction_or_arg arg;
FMT_TRY(apfl_format_put_indent(w, indent));
FMT_TRY(apfl_format_put_string(w, apfl_matcher_instruction_to_string(milist->instructions[i].instruction)));
FMT_TRY(apfl_io_write_string(w, apfl_matcher_instruction_to_string(milist->instructions[i].instruction)));
switch (milist->instructions[i].instruction) {
case MATCHER_IGNORE:
@ -276,43 +276,43 @@ apfl_bytecode_dump_matcher(unsigned indent, struct apfl_format_writer w, struct
case MATCHER_CAPTURE_TO_VAR:
case MATCHER_CAPTURE_TO_VAR_LOCAL:
GET_ARGUMENT_FOR_DUMP(w, milist, i, arg);
FMT_TRY(apfl_format_put_string(w, " "));
FMT_TRY(apfl_format_put_string(w, *arg.string));
FMT_TRY(apfl_io_write_string(w, " "));
FMT_TRY(apfl_io_write_string(w, *arg.string));
break;
case MATCHER_CAPTURE_TO_VAR_WITH_PATH: // with string, index and len
case MATCHER_CAPTURE_TO_VAR_LOCAL_WITH_PATH: // with string, index and len
GET_ARGUMENT_FOR_DUMP(w, milist, i, arg);
FMT_TRY(apfl_format_put_string(w, " "));
FMT_TRY(apfl_format_put_string(w, *arg.string));
FMT_TRY(apfl_io_write_string(w, " "));
FMT_TRY(apfl_io_write_string(w, *arg.string));
GET_ARGUMENT_FOR_DUMP(w, milist, i, arg);
FMT_TRY(apfl_format_put_string(w, ", "));
FMT_TRY(apfl_io_write_string(w, ", "));
FMT_TRY(apfl_format_put_int(w, (int)arg.index));
GET_ARGUMENT_FOR_DUMP(w, milist, i, arg);
FMT_TRY(apfl_format_put_string(w, ", "));
FMT_TRY(apfl_io_write_string(w, ", "));
FMT_TRY(apfl_format_put_int(w, (int)arg.len));
break;
case MATCHER_CHECK_CONST:
case MATCHER_CHECK_PRED:
GET_ARGUMENT_FOR_DUMP(w, milist, i, arg);
FMT_TRY(apfl_format_put_string(w, " "));
FMT_TRY(apfl_io_write_string(w, " "));
FMT_TRY(apfl_format_put_int(w, (int)arg.index));
break;
}
FMT_TRY(apfl_format_put_char(w, '\n'));
FMT_TRY(apfl_io_write_byte(w, '\n'));
}
return true;
}
bool
apfl_bytecode_dump(unsigned indent, struct apfl_format_writer w, struct instruction_list *ilist)
apfl_bytecode_dump(unsigned indent, struct apfl_io_writer w, struct instruction_list *ilist)
{
union instruction_or_arg arg;
for (size_t i = 0; i < ilist->len; i++) {
FMT_TRY(apfl_format_put_indent(w, indent));
FMT_TRY(apfl_format_put_string(w, apfl_instruction_to_string(ilist->instructions[i].instruction)));
FMT_TRY(apfl_io_write_string(w, apfl_instruction_to_string(ilist->instructions[i].instruction)));
switch (ilist->instructions[i].instruction) {
case INSN_NIL:
@ -331,20 +331,20 @@ apfl_bytecode_dump(unsigned indent, struct apfl_format_writer w, struct instruct
break;
case INSN_NUMBER:
GET_ARGUMENT_FOR_DUMP(w, ilist, i, arg);
FMT_TRY(apfl_format_put_string(w, " "));
FMT_TRY(apfl_io_write_string(w, " "));
FMT_TRY(apfl_format_put_number(w, arg.number));
break;
case INSN_LIST:
case INSN_SET_LINE:
case INSN_FUNC:
GET_ARGUMENT_FOR_DUMP(w, ilist, i, arg);
FMT_TRY(apfl_format_put_string(w, " "));
FMT_TRY(apfl_io_write_string(w, " "));
FMT_TRY(apfl_format_put_int(w, (int)arg.count));
break;
case INSN_GET_BY_INDEX_KEEP:
case INSN_MATCHER_SET_VAL:
GET_ARGUMENT_FOR_DUMP(w, ilist, i, arg);
FMT_TRY(apfl_format_put_string(w, " "));
FMT_TRY(apfl_io_write_string(w, " "));
FMT_TRY(apfl_format_put_int(w, (int)arg.index));
break;
case INSN_STRING:
@ -356,26 +356,26 @@ apfl_bytecode_dump(unsigned indent, struct apfl_format_writer w, struct instruct
case INSN_MOVE_TO_LOCAL_VAR:
case INSN_FUNC_SET_NAME:
GET_ARGUMENT_FOR_DUMP(w, ilist, i, arg);
FMT_TRY(apfl_format_put_string(w, " "));
FMT_TRY(apfl_format_put_string(w, *arg.string));
FMT_TRY(apfl_io_write_string(w, " "));
FMT_TRY(apfl_io_write_string(w, *arg.string));
break;
case INSN_FUNC_ADD_SUBFUNC:
GET_ARGUMENT_FOR_DUMP(w, ilist, i, arg);
FMT_TRY(apfl_format_put_string(w, " ilist{\n"));
FMT_TRY(apfl_io_write_string(w, " ilist{\n"));
FMT_TRY(apfl_bytecode_dump(indent+1, w, arg.body));
FMT_TRY(apfl_format_put_indent(w, indent));
FMT_TRY(apfl_format_put_string(w, "}"));
FMT_TRY(apfl_io_write_string(w, "}"));
break;
case INSN_MATCHER_PUSH:
GET_ARGUMENT_FOR_DUMP(w, ilist, i, arg);
FMT_TRY(apfl_format_put_string(w, " milist{\n"));
FMT_TRY(apfl_io_write_string(w, " milist{\n"));
FMT_TRY(apfl_bytecode_dump_matcher(indent+1, w, arg.matcher));
FMT_TRY(apfl_format_put_indent(w, indent));
FMT_TRY(apfl_format_put_string(w, "}"));
FMT_TRY(apfl_io_write_string(w, "}"));
break;
}
FMT_TRY(apfl_format_put_char(w, '\n'));
FMT_TRY(apfl_io_write_byte(w, '\n'));
}
return true;

View file

@ -101,8 +101,8 @@ void apfl_gc_matcher_instructions_traverse(struct matcher_instruction_list *, gc
struct matcher_instruction_list *apfl_matcher_instructions_new(struct gc *);
void apfl_matcher_instructions_deinit(struct apfl_allocator, struct matcher_instruction_list *);
bool apfl_bytecode_dump_matcher(unsigned indent, struct apfl_format_writer w, struct matcher_instruction_list *milist);
bool apfl_bytecode_dump(unsigned indent, struct apfl_format_writer w, struct instruction_list *ilist);
bool apfl_bytecode_dump_matcher(unsigned indent, struct apfl_io_writer w, struct matcher_instruction_list *milist);
bool apfl_bytecode_dump(unsigned indent, struct apfl_io_writer w, struct instruction_list *ilist);
#ifdef __cplusplus
}

View file

@ -230,7 +230,7 @@ struct errorfmt_data {
};
static bool
errorfmt_str(void *opaque, struct apfl_format_writer w, struct apfl_string_view arg)
errorfmt_str(void *opaque, struct apfl_io_writer w, struct apfl_string_view arg)
{
struct errorfmt_data *data = opaque;
@ -242,11 +242,11 @@ errorfmt_str(void *opaque, struct apfl_format_writer w, struct apfl_string_view
sv = va_arg(data->ap, struct apfl_string_view);
}
return apfl_format_put_string_view(w, sv);
return apfl_io_write_string_view(w, sv);
}
static bool
errorfmt_type(void *opaque, struct apfl_format_writer w, struct apfl_string_view arg)
errorfmt_type(void *opaque, struct apfl_io_writer w, struct apfl_string_view arg)
{
struct errorfmt_data *data = opaque;
@ -255,7 +255,7 @@ errorfmt_type(void *opaque, struct apfl_format_writer w, struct apfl_string_view
apfl_stackidx i = va_arg(data->ap, apfl_stackidx);
struct apfl_value val;
if (!apfl_stack_get(data->ctx, &val, i)) {
FMT_TRY(apfl_format_put_string(w, "{stack:type => invalid stack index}"));
FMT_TRY(apfl_io_write_string(w, "{stack:type => invalid stack index}"));
return true;
}
type = apfl_value_type_to_abstract_type(val.type);
@ -266,7 +266,7 @@ errorfmt_type(void *opaque, struct apfl_format_writer w, struct apfl_string_view
type = va_arg(data->ap, enum apfl_value_type);
}
return apfl_format_put_string_view(w, apfl_string_view_from(apfl_type_name(type)));
return apfl_io_write_string_view(w, apfl_string_view_from(apfl_type_name(type)));
}
static const struct format_spec errorfmt_specs[] = {
@ -284,7 +284,7 @@ apfl_raise_errorfmt(apfl_ctx ctx, const char *fmt, ...)
va_start(data.ap, fmt);
struct apfl_string_builder sb = apfl_string_builder_init(ctx->gc.allocator);
struct apfl_format_writer w = apfl_format_string_writer(&sb);
struct apfl_io_writer w = apfl_io_string_writer(&sb);
bool result = apfl_abstract_format(&data, errorfmt_specs, w, apfl_string_view_from(fmt));
va_end(data.ap);
@ -1436,7 +1436,7 @@ apfl_tostring(apfl_ctx ctx, apfl_stackidx index)
}
struct apfl_string_builder sb = apfl_string_builder_init(ctx->gc.allocator);
if (!apfl_value_format(value, apfl_format_string_writer(&sb))) {
if (!apfl_value_format(value, apfl_io_string_writer(&sb))) {
apfl_string_builder_deinit(&sb);
apfl_stack_drop(ctx, -1);
apfl_raise_alloc_error(ctx);
@ -1832,7 +1832,7 @@ apfl_get_native_object(apfl_ctx ctx, const struct apfl_native_object_type *type,
return value.native_object->memory;
}
struct apfl_format_writer apfl_get_output_writer(apfl_ctx ctx)
struct apfl_io_writer apfl_get_output_writer(apfl_ctx ctx)
{
return ctx->output_writer;
}
@ -1965,75 +1965,75 @@ apfl_call_stack_inspect(apfl_ctx ctx, size_t n)
static bool
format_defined_at(
struct apfl_format_writer w,
struct apfl_io_writer w,
struct apfl_call_stack_entry_info info,
struct apfl_string_view *filename
) {
FMT_TRY(apfl_format_put_string(w, "defined in "));
FMT_TRY(apfl_io_write_string(w, "defined in "));
if (filename != NULL && filename->len > 0) {
FMT_TRY(apfl_format_put_string(w, "file "));
FMT_TRY(apfl_format_put_string(w, *filename));
FMT_TRY(apfl_format_put_string(w, ", "));
FMT_TRY(apfl_io_write_string(w, "file "));
FMT_TRY(apfl_io_write_string(w, *filename));
FMT_TRY(apfl_io_write_string(w, ", "));
}
FMT_TRY(apfl_format_put_string(w, "line "));
FMT_TRY(apfl_io_write_string(w, "line "));
FMT_TRY(apfl_format_put_int(w, info.line_defined));
return true;
}
bool
apfl_call_stack_entry_info_format(struct apfl_format_writer w, struct apfl_call_stack_entry_info info)
apfl_call_stack_entry_info_format(struct apfl_io_writer w, struct apfl_call_stack_entry_info info)
{
switch (info.type) {
case APFL_CSE_FUNCTION:
if (info.filename.len > 0) {
FMT_TRY(apfl_format_put_string(w, "File "));
FMT_TRY(apfl_format_put_string(w, info.filename));
FMT_TRY(apfl_format_put_string(w, ", "));
FMT_TRY(apfl_io_write_string(w, "File "));
FMT_TRY(apfl_io_write_string(w, info.filename));
FMT_TRY(apfl_io_write_string(w, ", "));
}
FMT_TRY(apfl_format_put_string(w, "Line "));
FMT_TRY(apfl_io_write_string(w, "Line "));
FMT_TRY(apfl_format_put_int(w, info.line_current));
FMT_TRY(apfl_format_put_string(w, ", "));
FMT_TRY(apfl_io_write_string(w, ", "));
if (info.toplevel) {
FMT_TRY(apfl_format_put_string(w, "toplevel "));
FMT_TRY(apfl_io_write_string(w, "toplevel "));
} else if (info.name.len == 0) {
FMT_TRY(apfl_format_put_string(w, "anonymous function (subfunction "));
FMT_TRY(apfl_io_write_string(w, "anonymous function (subfunction "));
FMT_TRY(apfl_format_put_int(w, (int)info.subfunction_index));
FMT_TRY(apfl_format_put_string(w, "; "));
FMT_TRY(apfl_io_write_string(w, "; "));
FMT_TRY(format_defined_at(w, info, NULL));
FMT_TRY(apfl_format_put_string(w, ")"));
FMT_TRY(apfl_io_write_string(w, ")"));
} else {
FMT_TRY(apfl_format_put_string(w, "function "));
FMT_TRY(apfl_format_put_string(w, info.name));
FMT_TRY(apfl_format_put_string(w, " (subfunction "));
FMT_TRY(apfl_io_write_string(w, "function "));
FMT_TRY(apfl_io_write_string(w, info.name));
FMT_TRY(apfl_io_write_string(w, " (subfunction "));
FMT_TRY(apfl_format_put_int(w, (int)info.subfunction_index));
FMT_TRY(apfl_format_put_string(w, "; "));
FMT_TRY(apfl_io_write_string(w, "; "));
FMT_TRY(format_defined_at(w, info, NULL));
FMT_TRY(apfl_format_put_string(w, ")"));
FMT_TRY(apfl_io_write_string(w, ")"));
}
break;
case APFL_CSE_CFUNCTION:
if (info.name.len == 0) {
FMT_TRY(apfl_format_put_string(w, "Anonymous native function"));
FMT_TRY(apfl_io_write_string(w, "Anonymous native function"));
} else {
FMT_TRY(apfl_format_put_string(w, "Native function "));
FMT_TRY(apfl_format_put_string(w, info.name));
FMT_TRY(apfl_io_write_string(w, "Native function "));
FMT_TRY(apfl_io_write_string(w, info.name));
}
break;
case APFL_CSE_FUNCTION_DISPATCH:
FMT_TRY(apfl_format_put_string(w, "Dispatch for "));
FMT_TRY(apfl_io_write_string(w, "Dispatch for "));
if (info.name.len == 0) {
FMT_TRY(apfl_format_put_string(w, "anonymous function ("));
FMT_TRY(apfl_io_write_string(w, "anonymous function ("));
} else {
FMT_TRY(apfl_format_put_string(w, "function "));
FMT_TRY(apfl_format_put_string(w, info.name));
FMT_TRY(apfl_format_put_string(w, "("));
FMT_TRY(apfl_io_write_string(w, "function "));
FMT_TRY(apfl_io_write_string(w, info.name));
FMT_TRY(apfl_io_write_string(w, "("));
}
FMT_TRY(format_defined_at(w, info, &info.filename));
FMT_TRY(apfl_format_put_string(w, ")"));
FMT_TRY(apfl_io_write_string(w, ")"));
break;
case APFL_CSE_MATCHER:
FMT_TRY(apfl_format_put_string(w, "Matcher"));
FMT_TRY(apfl_io_write_string(w, "Matcher"));
break;
}

View file

@ -152,7 +152,7 @@ struct apfl_ctx_data {
struct iterative_runners_list iterative_runners;
struct apfl_format_writer output_writer;
struct apfl_io_writer output_writer;
};
void apfl_matcher_call_stack_entry_deinit(struct apfl_allocator, struct matcher_call_stack_entry *);
@ -198,7 +198,7 @@ struct call_stack_entry *apfl_call_stack_cur_entry(apfl_ctx);
struct scope *apfl_closure_scope_for_func(apfl_ctx, struct scopes);
struct apfl_format_writer apfl_get_output_writer(apfl_ctx);
struct apfl_io_writer apfl_get_output_writer(apfl_ctx);
enum apfl_result apfl_do_protected(
apfl_ctx ctx,

View file

@ -79,112 +79,112 @@ apfl_error_as_const_string(struct apfl_error error)
}
static bool
format_error(struct apfl_format_writer w, struct apfl_error error)
format_error(struct apfl_io_writer w, struct apfl_error error)
{
switch (error.type) {
case APFL_ERR_MALLOC_FAILED:
return apfl_format_put_string(w, apfl_messages.could_not_alloc_mem);
return apfl_io_write_string(w, apfl_messages.could_not_alloc_mem);
case APFL_ERR_INPUT_ERROR:
return apfl_format_put_string(w, apfl_messages.input_error_while_parsing);
return apfl_io_write_string(w, apfl_messages.input_error_while_parsing);
case APFL_ERR_UNEXPECTED_EOF:
return apfl_format_put_string(w, apfl_messages.unexpected_end_of_file);
return apfl_io_write_string(w, apfl_messages.unexpected_end_of_file);
case APFL_ERR_EXPECTED_EQ_AFTER_COLON:
TRY(apfl_format_put_string(w, "Expected '=' after ':' at "));
TRY(apfl_io_write_string(w, "Expected '=' after ':' at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_UNEXPECTED_BYTE:
TRY(apfl_format_put_string(w, "Unexpected byte '"));
TRY(apfl_format_put_char(w, error.byte));
TRY(apfl_format_put_string(w, "' (0x"));
TRY(apfl_io_write_string(w, "Unexpected byte '"));
TRY(apfl_io_write_byte(w, error.byte));
TRY(apfl_io_write_string(w, "' (0x"));
TRY(apfl_format_put_hexbyte(w, (unsigned char)error.byte));
TRY(apfl_format_put_string(w, ") at "));
TRY(apfl_io_write_string(w, ") at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_UNEXPECTED_BYTE_IN_NUMBER:
TRY(apfl_format_put_string(w, "Unexpected byte '"));
TRY(apfl_format_put_char(w, error.byte));
TRY(apfl_format_put_string(w, "' while parsing number at "));
TRY(apfl_io_write_string(w, "Unexpected byte '"));
TRY(apfl_io_write_byte(w, error.byte));
TRY(apfl_io_write_string(w, "' while parsing number at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_EXPECTED_DIGIT:
TRY(apfl_format_put_string(w, "Expected a digit at "));
TRY(apfl_io_write_string(w, "Expected a digit at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_EXPECTED_HEX_IN_HEX_ESCAPE:
TRY(apfl_format_put_string(w, "Expected a hex-digit in hex escape at "));
TRY(apfl_io_write_string(w, "Expected a hex-digit in hex escape at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_INVALID_ESCAPE_SEQUENCE:
TRY(apfl_format_put_string(w, "Invalid escape sequence \\"));
TRY(apfl_format_put_char(w, error.byte));
TRY(apfl_format_put_string(w, " at "));
TRY(apfl_io_write_string(w, "Invalid escape sequence \\"));
TRY(apfl_io_write_byte(w, error.byte));
TRY(apfl_io_write_string(w, " at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_NO_LINEBREAK_AFTER_CONTINUE_LINE:
TRY(apfl_format_put_string(w, "No line break (after optional comments) after \\ at "));
TRY(apfl_io_write_string(w, "No line break (after optional comments) after \\ at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_UNEXPECTED_TOKEN:
TRY(apfl_format_put_string(w, "Unexpected `"));
TRY(apfl_format_put_string(w, apfl_token_type_name(error.token_type)));
TRY(apfl_format_put_string(w, "` token at "));
TRY(apfl_io_write_string(w, "Unexpected `"));
TRY(apfl_io_write_string(w, apfl_token_type_name(error.token_type)));
TRY(apfl_io_write_string(w, "` token at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_MISMATCHING_CLOSING_BRACKET:
TRY(apfl_format_put_string(w, "Closing `"));
TRY(apfl_format_put_string(w, apfl_token_type_name(error.token_type)));
TRY(apfl_format_put_string(w, "` token at "));
TRY(apfl_io_write_string(w, "Closing `"));
TRY(apfl_io_write_string(w, apfl_token_type_name(error.token_type)));
TRY(apfl_io_write_string(w, "` token at "));
TRY(apfl_format_put_pos(w, error.position));
TRY(apfl_format_put_string(w, "Does not match opening `"));
TRY(apfl_format_put_string(w, apfl_token_type_name(error.token_type2)));
TRY(apfl_format_put_string(w, "` at "));
TRY(apfl_io_write_string(w, "Does not match opening `"));
TRY(apfl_io_write_string(w, apfl_token_type_name(error.token_type2)));
TRY(apfl_io_write_string(w, "` at "));
TRY(apfl_format_put_pos(w, error.position2));
return true;
case APFL_ERR_UNEXPECTED_EOF_AFTER_TOKEN:
TRY(apfl_format_put_string(w, "Unexpected end of file after `"));
TRY(apfl_format_put_string(w, apfl_token_type_name(error.token_type)));
TRY(apfl_format_put_string(w, "` token at "));
TRY(apfl_io_write_string(w, "Unexpected end of file after `"));
TRY(apfl_io_write_string(w, apfl_token_type_name(error.token_type)));
TRY(apfl_io_write_string(w, "` token at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_STATEMENTS_BEFORE_PARAMETERS:
TRY(apfl_format_put_string(w, "Unexpected statements before parameters near "));
TRY(apfl_io_write_string(w, "Unexpected statements before parameters near "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_EMPTY_ASSIGNMENT_BEFORE_PARAMETERS:
TRY(apfl_format_put_string(w, "Unexpected empty assignment before parameters near "));
TRY(apfl_io_write_string(w, "Unexpected empty assignment before parameters near "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_UNEXPECTED_EXPRESSION:
TRY(apfl_format_put_string(w, "Unexpected expression near "));
TRY(apfl_io_write_string(w, "Unexpected expression near "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_INVALID_ASSIGNMENT_LHS:
TRY(apfl_format_put_string(w, "Invalid left hand side of assignment near "));
TRY(apfl_io_write_string(w, "Invalid left hand side of assignment near "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_EMPTY_ASSIGNMENT:
TRY(apfl_format_put_string(w, "Empty assignment at "));
TRY(apfl_io_write_string(w, "Empty assignment at "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_ONLY_ONE_EXPAND_ALLOWED:
TRY(apfl_format_put_string(w, "Only one expansion (~) is allowed per level, near "));
TRY(apfl_io_write_string(w, "Only one expansion (~) is allowed per level, near "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_UNEXPECTED_CONSTANT_IN_MEMBER_ACCESS:
TRY(apfl_format_put_string(w, "Unexpected constant in member access near "));
TRY(apfl_io_write_string(w, "Unexpected constant in member access near "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_UNEXPECTED_EXPR_IN_MEMBER_ACCESS:
TRY(apfl_format_put_string(w, "Unexpected expression in member access near "));
TRY(apfl_io_write_string(w, "Unexpected expression in member access near "));
TRY(apfl_format_put_pos(w, error.position));
return true;
case APFL_ERR_UNEXPECTED_BLANK_IN_MEMBER_ACCESS:
TRY(apfl_format_put_string(w, "Unexpected blank (\"_\") in member access near "));
TRY(apfl_io_write_string(w, "Unexpected blank (\"_\") in member access near "));
TRY(apfl_format_put_pos(w, error.position));
return true;
}
TRY(apfl_format_put_string(w, "Unknown error "));
TRY(apfl_io_write_string(w, "Unknown error "));
TRY(apfl_format_put_int(w, (int)error.type));
return true;
}
@ -192,9 +192,9 @@ format_error(struct apfl_format_writer w, struct apfl_error error)
bool
apfl_error_print(struct apfl_error error, FILE *file)
{
struct apfl_format_writer w = apfl_format_file_writer(file);
struct apfl_io_writer w = apfl_io_file_writer(file);
TRY(format_error(w, error));
TRY(apfl_format_put_char(w, '\n'));
TRY(apfl_io_write_byte(w, '\n'));
return true;
}
@ -202,7 +202,7 @@ bool
apfl_error_as_string(struct apfl_error error, struct apfl_allocator allocator, struct apfl_string *out)
{
struct apfl_string_builder builder = apfl_string_builder_init(allocator);
TRY(format_error(apfl_format_string_writer(&builder), error));
TRY(format_error(apfl_io_string_writer(&builder), error));
*out = apfl_string_builder_move_string(&builder);
return true;
}

View file

@ -1410,13 +1410,13 @@ iterative_runner_eval_expr(apfl_iterative_runner runner, struct apfl_expr expr)
}
bool
apfl_debug_print_val(apfl_ctx ctx, apfl_stackidx index, struct apfl_format_writer w)
apfl_debug_print_val(apfl_ctx ctx, apfl_stackidx index, struct apfl_io_writer w)
{
struct apfl_value value;
if (!apfl_stack_pop(ctx, &value, index)) {
FMT_TRY(apfl_format_put_string(w, "apfl_debug_print_val: Invalid stack index "));
FMT_TRY(apfl_io_write_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"));
FMT_TRY(apfl_io_write_string(w, "\n"));
return true;
}
@ -1507,13 +1507,13 @@ iterative_runner_decorate_error(apfl_ctx ctx, void *opaque)
(void)opaque;
struct apfl_string_builder sb = apfl_string_builder_init(ctx->gc.allocator);
struct apfl_format_writer w = apfl_format_string_writer(&sb);
struct apfl_io_writer w = apfl_io_string_writer(&sb);
apfl_tostring(ctx, -1);
if (!(
apfl_format_put_string(w, apfl_get_string(ctx, -1))
&& apfl_format_put_string(w, "\n\nBacktrace:")
apfl_io_write_string(w, apfl_get_string(ctx, -1))
&& apfl_io_write_string(w, "\n\nBacktrace:")
)) {
goto fail;
}
@ -1521,10 +1521,10 @@ iterative_runner_decorate_error(apfl_ctx ctx, void *opaque)
size_t depth = apfl_call_stack_depth(ctx);
for (size_t i = 0; i < depth; i++) {
if (!(
apfl_format_put_string(w, "\n")
&& apfl_format_put_string(w, "#")
apfl_io_write_string(w, "\n")
&& apfl_io_write_string(w, "#")
&& apfl_format_put_int(w, (int)i+1)
&& apfl_format_put_string(w, ": ")
&& apfl_io_write_string(w, ": ")
&& apfl_call_stack_entry_info_format(
w,
apfl_call_stack_inspect(ctx, i)
@ -1581,8 +1581,8 @@ apfl_iterative_runner_stopped_because_of_error(apfl_iterative_runner runner)
bool
apfl_iterative_runner_run_repl(
apfl_iterative_runner runner,
struct apfl_format_writer w_out,
struct apfl_format_writer w_err
struct apfl_io_writer w_out,
struct apfl_io_writer w_err
) {
apfl_ctx ctx = runner->ctx;
@ -1596,19 +1596,19 @@ apfl_iterative_runner_run_repl(
}
break;
case APFL_RESULT_ERR:
FMT_TRY(apfl_format_put_string(w_err, "Error occurred during evaluation:\n"));
FMT_TRY(apfl_io_write_string(w_err, "Error occurred during evaluation:\n"));
if (apfl_get_type(ctx, -1) == APFL_VALUE_STRING) {
FMT_TRY(apfl_format_put_string(w_err, apfl_get_string(ctx, -1)));
FMT_TRY(apfl_io_write_string(w_err, apfl_get_string(ctx, -1)));
} else {
FMT_TRY(apfl_debug_print_val(ctx, -1, w_err));
}
FMT_TRY(apfl_format_put_char(w_err, '\n'));
FMT_TRY(apfl_io_write_byte(w_err, '\n'));
break;
case APFL_RESULT_ERRERR:
FMT_TRY(apfl_format_put_string(w_err, "Error occurred during error handling.\n"));
FMT_TRY(apfl_io_write_string(w_err, "Error occurred during error handling.\n"));
break;
case APFL_RESULT_ERR_ALLOC:
FMT_TRY(apfl_format_put_string(w_err, "Fatal: Could not allocate memory.\n"));
FMT_TRY(apfl_io_write_string(w_err, "Fatal: Could not allocate memory.\n"));
return false;
}
}

View file

@ -532,29 +532,29 @@ apfl_expr_at_move(struct apfl_expr_at *in)
}
static bool
format_expr_headline(struct apfl_format_writer w, const char *name, struct apfl_position pos, unsigned indent)
format_expr_headline(struct apfl_io_writer w, const char *name, struct apfl_position pos, unsigned indent)
{
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, name));
TRY(apfl_format_put_string(w, " @ "));
TRY(apfl_io_write_string(w, name));
TRY(apfl_io_write_string(w, " @ "));
TRY(apfl_format_put_pos(w, pos));
TRY(apfl_format_put_string(w, "\n"));
TRY(apfl_io_write_string(w, "\n"));
return true;
}
static bool
format_simple_headline(struct apfl_format_writer w, const char *headline, unsigned indent)
format_simple_headline(struct apfl_io_writer w, const char *headline, unsigned indent)
{
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, headline));
TRY(apfl_format_put_string(w, "\n"));
TRY(apfl_io_write_string(w, headline));
TRY(apfl_io_write_string(w, "\n"));
return true;
}
static bool format_expr(struct apfl_format_writer, struct apfl_expr *, unsigned indent);
static bool format_expr(struct apfl_io_writer, struct apfl_expr *, unsigned indent);
static bool
format_expr_list(struct apfl_format_writer w, struct apfl_expr_list *list, unsigned indent)
format_expr_list(struct apfl_io_writer w, struct apfl_expr_list *list, unsigned indent)
{
for (size_t i = 0; i < list->len; i++) {
unsigned item_indent = indent;
@ -569,7 +569,7 @@ format_expr_list(struct apfl_format_writer w, struct apfl_expr_list *list, unsig
}
static bool
format_body(struct apfl_format_writer w, struct apfl_expr_body *body, unsigned indent)
format_body(struct apfl_io_writer w, struct apfl_expr_body *body, unsigned indent)
{
for (size_t i = 0; i < body->len; i++) {
TRY(format_expr(w, &body->items[i], indent));
@ -579,7 +579,7 @@ format_body(struct apfl_format_writer w, struct apfl_expr_body *body, unsigned i
}
static bool
format_constant_with_pos(struct apfl_format_writer w, struct apfl_expr_const constant, struct apfl_position pos, unsigned indent)
format_constant_with_pos(struct apfl_io_writer w, struct apfl_expr_const constant, struct apfl_position pos, unsigned indent)
{
switch (constant.type) {
case APFL_EXPR_CONST_NIL:
@ -588,19 +588,19 @@ format_constant_with_pos(struct apfl_format_writer w, struct apfl_expr_const con
return format_expr_headline(w, constant.boolean ? "Const (true)" : "Const (false", pos, indent);
case APFL_EXPR_CONST_STRING:
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "Const ("));
TRY(apfl_format_put_string(w, constant.string));
TRY(apfl_format_put_string(w, ") @ "));
TRY(apfl_io_write_string(w, "Const ("));
TRY(apfl_io_write_string(w, constant.string));
TRY(apfl_io_write_string(w, ") @ "));
TRY(apfl_format_put_pos(w, pos));
TRY(apfl_format_put_string(w, "\n"));
TRY(apfl_io_write_string(w, "\n"));
return true;
case APFL_EXPR_CONST_NUMBER:
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "Number ("));
TRY(apfl_io_write_string(w, "Number ("));
TRY(apfl_format_put_number(w, constant.number));
TRY(apfl_format_put_string(w, ") @ "));
TRY(apfl_io_write_string(w, ") @ "));
TRY(apfl_format_put_pos(w, pos));
TRY(apfl_format_put_string(w, "\n"));
TRY(apfl_io_write_string(w, "\n"));
return true;
}
@ -609,7 +609,7 @@ format_constant_with_pos(struct apfl_format_writer w, struct apfl_expr_const con
}
static bool
format_constant(struct apfl_format_writer w, struct apfl_expr_const constant, unsigned indent)
format_constant(struct apfl_io_writer w, struct apfl_expr_const constant, unsigned indent)
{
switch (constant.type) {
case APFL_EXPR_CONST_NIL:
@ -618,15 +618,15 @@ format_constant(struct apfl_format_writer w, struct apfl_expr_const constant, un
return format_simple_headline(w, constant.boolean ? "Const (true)" : "Const (false", indent);
case APFL_EXPR_CONST_STRING:
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "Const ("));
TRY(apfl_format_put_string(w, constant.string));
TRY(apfl_format_put_string(w, ")\n"));
TRY(apfl_io_write_string(w, "Const ("));
TRY(apfl_io_write_string(w, constant.string));
TRY(apfl_io_write_string(w, ")\n"));
return true;
case APFL_EXPR_CONST_NUMBER:
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "Number ("));
TRY(apfl_io_write_string(w, "Number ("));
TRY(apfl_format_put_number(w, constant.number));
TRY(apfl_format_put_string(w, ")\n"));
TRY(apfl_io_write_string(w, ")\n"));
return true;
}
@ -634,10 +634,10 @@ format_constant(struct apfl_format_writer w, struct apfl_expr_const constant, un
return false;
}
static bool format_param(struct apfl_format_writer w, struct apfl_expr_param *, unsigned);
static bool format_param(struct apfl_io_writer w, struct apfl_expr_param *, unsigned);
static bool
format_params_item(struct apfl_format_writer w, struct apfl_expr_params_item *item, unsigned indent)
format_params_item(struct apfl_io_writer w, struct apfl_expr_params_item *item, unsigned indent)
{
if (item->expand) {
TRY(format_simple_headline(w, "Expand", indent));
@ -647,14 +647,14 @@ format_params_item(struct apfl_format_writer w, struct apfl_expr_params_item *it
}
static bool
format_param(struct apfl_format_writer w, struct apfl_expr_param *param, unsigned indent)
format_param(struct apfl_io_writer w, struct apfl_expr_param *param, unsigned indent)
{
switch (param->type) {
case APFL_EXPR_PARAM_VAR:
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "Var ("));
TRY(apfl_format_put_string(w, param->var));
TRY(apfl_format_put_string(w, ")\n"));
TRY(apfl_io_write_string(w, "Var ("));
TRY(apfl_io_write_string(w, param->var));
TRY(apfl_io_write_string(w, ")\n"));
return true;
case APFL_EXPR_PARAM_CONSTANT:
return format_constant(w, param->constant, indent);
@ -680,20 +680,20 @@ format_param(struct apfl_format_writer w, struct apfl_expr_param *param, unsigne
}
static bool
format_assignable_var_or_member(struct apfl_format_writer w, struct apfl_expr_assignable_var_or_member var_or_member, unsigned indent)
format_assignable_var_or_member(struct apfl_io_writer w, struct apfl_expr_assignable_var_or_member var_or_member, unsigned indent)
{
switch (var_or_member.type) {
case APFL_EXPR_ASSIGNABLE_VAR_OR_MEMBER_VAR:
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "Var ("));
TRY(apfl_format_put_string(w, var_or_member.var));
TRY(apfl_format_put_string(w, ")\n"));
TRY(apfl_io_write_string(w, "Var ("));
TRY(apfl_io_write_string(w, var_or_member.var));
TRY(apfl_io_write_string(w, ")\n"));
return true;
case APFL_EXPR_ASSIGNABLE_VAR_OR_MEMBER_DOT:
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "Dot ("));
TRY(apfl_format_put_string(w, var_or_member.dot.rhs));
TRY(apfl_format_put_string(w, ")\n"));
TRY(apfl_io_write_string(w, "Dot ("));
TRY(apfl_io_write_string(w, var_or_member.dot.rhs));
TRY(apfl_io_write_string(w, ")\n"));
TRY(format_assignable_var_or_member(w, *var_or_member.dot.lhs, indent+1));
return true;
case APFL_EXPR_ASSIGNABLE_VAR_OR_MEMBER_AT:
@ -710,7 +710,7 @@ format_assignable_var_or_member(struct apfl_format_writer w, struct apfl_expr_as
}
static bool
format_assignable(struct apfl_format_writer w, struct apfl_expr_assignable assignable, unsigned indent)
format_assignable(struct apfl_io_writer w, struct apfl_expr_assignable assignable, unsigned indent)
{
switch(assignable.type) {
case APFL_EXPR_ASSIGNABLE_VAR_OR_MEMBER:
@ -745,7 +745,7 @@ format_assignable(struct apfl_format_writer w, struct apfl_expr_assignable assig
}
static bool
format_expr(struct apfl_format_writer w, struct apfl_expr *expr, unsigned indent)
format_expr(struct apfl_io_writer w, struct apfl_expr *expr, unsigned indent)
{
switch (expr->type) {
case APFL_EXPR_LIST:
@ -795,11 +795,11 @@ format_expr(struct apfl_format_writer w, struct apfl_expr *expr, unsigned indent
return true;
case APFL_EXPR_DOT:
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "Dot ("));
TRY(apfl_format_put_string(w, expr->dot.rhs));
TRY(apfl_format_put_string(w, ") @ "));
TRY(apfl_io_write_string(w, "Dot ("));
TRY(apfl_io_write_string(w, expr->dot.rhs));
TRY(apfl_io_write_string(w, ") @ "));
TRY(apfl_format_put_pos(w, expr->position));
TRY(apfl_format_put_string(w, "\n"));
TRY(apfl_io_write_string(w, "\n"));
TRY(format_expr(w, expr->dot.lhs, indent+1));
return true;
case APFL_EXPR_AT:
@ -813,11 +813,11 @@ format_expr(struct apfl_format_writer w, struct apfl_expr *expr, unsigned indent
return format_constant_with_pos(w, expr->constant, expr->position, indent);
case APFL_EXPR_VAR:
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "Var ("));
TRY(apfl_format_put_string(w, expr->var));
TRY(apfl_format_put_string(w, ") @ "));
TRY(apfl_io_write_string(w, "Var ("));
TRY(apfl_io_write_string(w, expr->var));
TRY(apfl_io_write_string(w, ") @ "));
TRY(apfl_format_put_pos(w, expr->position));
TRY(apfl_format_put_string(w, "\n"));
TRY(apfl_io_write_string(w, "\n"));
return true;
case APFL_EXPR_BLANK:
return format_expr_headline(w, "Blank (_)", expr->position, indent);
@ -830,7 +830,7 @@ format_expr(struct apfl_format_writer w, struct apfl_expr *expr, unsigned indent
bool
apfl_expr_print(struct apfl_expr expr, FILE *f)
{
return format_expr(apfl_format_file_writer(f), &expr, 0);
return format_expr(apfl_io_file_writer(f), &expr, 0);
}
static bool

View file

@ -10,7 +10,7 @@
#define TRY FMT_TRY
static bool
write(struct apfl_format_writer w, const char *buf, size_t len)
write(struct apfl_io_writer w, const char *buf, size_t len)
{
return len == 0
? true
@ -31,26 +31,26 @@ write_string(void *opaque, const char *buf, size_t len)
return apfl_string_builder_append(sb, (struct apfl_string_view) {.bytes = buf, .len = len});
}
struct apfl_format_writer
apfl_format_file_writer(FILE *f)
struct apfl_io_writer
apfl_io_file_writer(FILE *f)
{
return (struct apfl_format_writer) {
return (struct apfl_io_writer) {
.write = write_file,
.opaque = f,
};
}
struct apfl_format_writer
apfl_format_string_writer(struct apfl_string_builder *sb)
struct apfl_io_writer
apfl_io_string_writer(struct apfl_string_builder *sb)
{
return (struct apfl_format_writer) {
return (struct apfl_io_writer) {
.write = write_string,
.opaque = sb,
};
}
bool
apfl_format_put_string_view(struct apfl_format_writer w, struct apfl_string_view sv)
apfl_io_write_string_view(struct apfl_io_writer w, struct apfl_string_view sv)
{
return write(w, sv.bytes, sv.len);
}
@ -58,7 +58,7 @@ apfl_format_put_string_view(struct apfl_format_writer w, struct apfl_string_view
#define PUT_INT_BUFSIZE 21 // ceil(log10(2**64)) + 1
bool
apfl_format_put_int(struct apfl_format_writer w, int i)
apfl_format_put_int(struct apfl_io_writer w, int i)
{
char buf[PUT_INT_BUFSIZE];
size_t len = snprintf(buf, PUT_INT_BUFSIZE, "%d", i);
@ -67,7 +67,7 @@ apfl_format_put_int(struct apfl_format_writer w, int i)
}
bool
apfl_format_put_char(struct apfl_format_writer w, char c)
apfl_io_write_byte(struct apfl_io_writer w, char c)
{
return write(w, &c, 1);
}
@ -75,7 +75,7 @@ apfl_format_put_char(struct apfl_format_writer w, char c)
#define PUT_HEXBYTE_BUFSIZE 3
bool
apfl_format_put_hexbyte(struct apfl_format_writer w, unsigned char c)
apfl_format_put_hexbyte(struct apfl_io_writer w, unsigned char c)
{
char buf[PUT_HEXBYTE_BUFSIZE];
size_t len = snprintf(buf, PUT_HEXBYTE_BUFSIZE, "%x", (unsigned) c);
@ -84,19 +84,19 @@ apfl_format_put_hexbyte(struct apfl_format_writer w, unsigned char c)
}
bool
apfl_format_put_pos(struct apfl_format_writer w, struct apfl_position pos)
apfl_format_put_pos(struct apfl_io_writer w, struct apfl_position pos)
{
TRY(apfl_format_put_int(w, pos.line));
TRY(apfl_format_put_string(w, ":"));
TRY(apfl_io_write_string(w, ":"));
TRY(apfl_format_put_int(w, pos.col));
return true;
}
bool
apfl_format_put_indent(struct apfl_format_writer w, unsigned indent)
apfl_format_put_indent(struct apfl_io_writer w, unsigned indent)
{
while (indent--) {
TRY(apfl_format_put_string(w, " "));
TRY(apfl_io_write_string(w, " "));
}
return true;
}
@ -104,13 +104,13 @@ apfl_format_put_indent(struct apfl_format_writer w, unsigned indent)
#define PUT_NUMBER_BUFSIZE 100 // Arbitrarily chosen ¯\_(ツ)_/¯
bool
apfl_format_put_number(struct apfl_format_writer w, apfl_number number)
apfl_format_put_number(struct apfl_io_writer w, apfl_number number)
{
char buf[PUT_NUMBER_BUFSIZE];
size_t len = snprintf(buf, PUT_NUMBER_BUFSIZE, "%.12G", number);
TRY(write(w, buf, len));
if (len >= PUT_NUMBER_BUFSIZE) {
TRY(apfl_format_put_string(w, "[...]"));
TRY(apfl_io_write_string(w, "[...]"));
}
return true;
}
@ -118,7 +118,7 @@ apfl_format_put_number(struct apfl_format_writer w, apfl_number number)
#define PUT_POINTER_BUFSIZE 34 // Enough for "0x" + 32 hex digits, so enough for a 128-bit pointer.
bool
apfl_format_put_poiner(struct apfl_format_writer w, void *ptr)
apfl_format_put_poiner(struct apfl_io_writer w, void *ptr)
{
char buf[PUT_POINTER_BUFSIZE];
size_t len = snprintf(buf, PUT_POINTER_BUFSIZE, "%p", ptr);
@ -139,11 +139,11 @@ search_next_char(struct apfl_string_view sv, size_t *off, char needle)
}
static bool
write_leading(struct apfl_format_writer w, struct apfl_string_view fmt, size_t start, size_t off)
write_leading(struct apfl_io_writer w, struct apfl_string_view fmt, size_t start, size_t off)
{
assert(start <= off);
TRY(apfl_format_put_string(w, apfl_string_view_substr(fmt, start, off - start)));
TRY(apfl_io_write_string(w, apfl_string_view_substr(fmt, start, off - start)));
return true;
}
@ -151,7 +151,7 @@ bool
apfl_abstract_format(
void *opaque,
const struct format_spec *specs,
struct apfl_format_writer w,
struct apfl_io_writer w,
struct apfl_string_view fmt
) {
size_t off = 0;
@ -168,7 +168,7 @@ apfl_abstract_format(
if (off < fmt.len && fmt.bytes[off] == '{') {
// We got "{{", used to escape "{". Write that and start over.
TRY(apfl_format_put_char(w, '{'));
TRY(apfl_io_write_byte(w, '{'));
off++;
start = off;
@ -178,7 +178,7 @@ apfl_abstract_format(
if (!search_next_char(fmt, &off, '}')) {
// No closing '}', write rest of fmt and abort.
TRY(apfl_format_put_string_view(w, apfl_string_view_offset(fmt, start)));
TRY(apfl_io_write_string_view(w, apfl_string_view_offset(fmt, start)));
return true;
}
@ -203,11 +203,11 @@ apfl_abstract_format(
}
}
TRY(apfl_format_put_string(w, "{UNKNOWN FORMAT \""));
TRY(apfl_format_put_string(w, arg));
TRY(apfl_format_put_string(w, ":"));
TRY(apfl_format_put_string(w, placeholder));
TRY(apfl_format_put_string(w, "\"}"));
TRY(apfl_io_write_string(w, "{UNKNOWN FORMAT \""));
TRY(apfl_io_write_string(w, arg));
TRY(apfl_io_write_string(w, ":"));
TRY(apfl_io_write_string(w, placeholder));
TRY(apfl_io_write_string(w, "\"}"));
continue_inner:;
}

View file

@ -13,13 +13,13 @@ extern "C" {
struct format_spec {
const char *name;
bool (*callback)(void *, struct apfl_format_writer, struct apfl_string_view arg);
bool (*callback)(void *, struct apfl_io_writer, struct apfl_string_view arg);
};
bool apfl_abstract_format(
void *opaque,
const struct format_spec *specs,
struct apfl_format_writer w,
struct apfl_io_writer w,
struct apfl_string_view fmt
);

View file

@ -121,7 +121,7 @@ static void
dump_stack_error(apfl_ctx ctx, apfl_iterative_runner runner)
{
if (apfl_iterative_runner_get_result(runner) == APFL_RESULT_ERR) {
assert(apfl_debug_print_val(ctx, -1, apfl_format_file_writer(stderr)));
assert(apfl_debug_print_val(ctx, -1, apfl_io_file_writer(stderr)));
}
}
@ -160,7 +160,7 @@ runtest(const char *filename)
apfl_ctx ctx = apfl_ctx_new((struct apfl_config) {
.allocator = allocator,
.output_writer = apfl_format_string_writer(&output),
.output_writer = apfl_io_string_writer(&output),
});
struct apfl_string_source_reader_data src_data = apfl_string_source_reader_create(parts.script);

View file

@ -440,7 +440,7 @@ sweep(struct gc *gc)
}
#ifdef GC_DEBUG_DUMP_GRAPH_ON_COLLECT
# define DUMP_ON_COLLECT() apfl_gc_debug_dump_graph(gc, apfl_format_file_writer(stderr))
# define DUMP_ON_COLLECT() apfl_gc_debug_dump_graph(gc, apfl_io_file_writer(stderr))
#else
# define DUMP_ON_COLLECT()
#endif
@ -526,7 +526,7 @@ type_to_string(enum gc_type type)
}
struct dump_graph_roots_visitor_data {
struct apfl_format_writer w;
struct apfl_io_writer w;
bool success;
};
@ -535,13 +535,13 @@ dump_graph_roots_visitor(void *opaque, struct gc_object *obj)
{
struct dump_graph_roots_visitor_data *data = opaque;
data->success = data->success
&& apfl_format_put_string(data->w, " ROOTS -> obj_")
&& apfl_io_write_string(data->w, " ROOTS -> obj_")
&& apfl_format_put_poiner(data->w, (void *)obj)
&& apfl_format_put_string(data->w, "\n");
&& apfl_io_write_string(data->w, "\n");
}
struct dump_graph_visitor_data {
struct apfl_format_writer w;
struct apfl_io_writer w;
struct gc_object *parent;
bool success;
};
@ -551,17 +551,17 @@ dump_graph_visitor(void *opaque, struct gc_object *obj)
{
struct dump_graph_visitor_data *data = opaque;
data->success = data->success
&& apfl_format_put_string(data->w, " obj_")
&& apfl_io_write_string(data->w, " obj_")
&& apfl_format_put_poiner(data->w, (void *)data->parent)
&& apfl_format_put_string(data->w, " -> obj_")
&& apfl_io_write_string(data->w, " -> obj_")
&& apfl_format_put_poiner(data->w, (void *)obj)
&& apfl_format_put_string(data->w, "\n");
&& apfl_io_write_string(data->w, "\n");
}
bool
apfl_gc_debug_dump_graph(struct gc *gc, struct apfl_format_writer w)
apfl_gc_debug_dump_graph(struct gc *gc, struct apfl_io_writer w)
{
FMT_TRY(apfl_format_put_string(w, "digraph G {\n"));
FMT_TRY(apfl_io_write_string(w, "digraph G {\n"));
struct dump_graph_roots_visitor_data roots_visitor_data = {
.w = w,
@ -580,24 +580,24 @@ apfl_gc_debug_dump_graph(struct gc *gc, struct apfl_format_writer w)
continue;
}
FMT_TRY(apfl_format_put_string(w, " blk_"));
FMT_TRY(apfl_io_write_string(w, " blk_"));
FMT_TRY(apfl_format_put_poiner(w, (void *)block));
FMT_TRY(apfl_format_put_string(w, " -> obj_"));
FMT_TRY(apfl_io_write_string(w, " -> obj_"));
FMT_TRY(apfl_format_put_poiner(w, (void *)obj));
FMT_TRY(apfl_format_put_string(w, "\n"));
FMT_TRY(apfl_io_write_string(w, "\n"));
FMT_TRY(apfl_format_put_string(w, " obj_"));
FMT_TRY(apfl_io_write_string(w, " obj_"));
FMT_TRY(apfl_format_put_poiner(w, (void *)obj));
FMT_TRY(apfl_format_put_string(w, "[style=filled,fillcolor="));
FMT_TRY(apfl_format_put_string(w, dump_graph_bgcolor(obj->status)));
FMT_TRY(apfl_format_put_string(w, ",fontcolor="));
FMT_TRY(apfl_format_put_string(w, dump_graph_fgcolor(obj->status)));
FMT_TRY(apfl_format_put_string(w, ",label=\"Object "));
FMT_TRY(apfl_io_write_string(w, "[style=filled,fillcolor="));
FMT_TRY(apfl_io_write_string(w, dump_graph_bgcolor(obj->status)));
FMT_TRY(apfl_io_write_string(w, ",fontcolor="));
FMT_TRY(apfl_io_write_string(w, dump_graph_fgcolor(obj->status)));
FMT_TRY(apfl_io_write_string(w, ",label=\"Object "));
FMT_TRY(apfl_format_put_poiner(w, (void *)obj));
FMT_TRY(apfl_format_put_string(w, "\\ntype: "));
FMT_TRY(apfl_format_put_string(w, type_to_string(obj->type)));
FMT_TRY(apfl_format_put_string(w, "\"];\n"));
FMT_TRY(apfl_io_write_string(w, "\\ntype: "));
FMT_TRY(apfl_io_write_string(w, type_to_string(obj->type)));
FMT_TRY(apfl_io_write_string(w, "\"];\n"));
struct dump_graph_visitor_data visitor_data = {
.w = w,
@ -609,26 +609,26 @@ apfl_gc_debug_dump_graph(struct gc *gc, struct apfl_format_writer w)
FMT_TRY(visitor_data.success);
}
FMT_TRY(apfl_format_put_string(w, " BLOCKS -> blk_"));
FMT_TRY(apfl_io_write_string(w, " BLOCKS -> blk_"));
FMT_TRY(apfl_format_put_poiner(w, (void *)block));
FMT_TRY(apfl_format_put_string(w, ";\n"));
FMT_TRY(apfl_io_write_string(w, ";\n"));
FMT_TRY(apfl_format_put_string(w, " blk_"));
FMT_TRY(apfl_io_write_string(w, " blk_"));
FMT_TRY(apfl_format_put_poiner(w, (void *)block));
FMT_TRY(apfl_format_put_string(w, " [label=\"Block "));
FMT_TRY(apfl_io_write_string(w, " [label=\"Block "));
FMT_TRY(apfl_format_put_poiner(w, (void *)block));
FMT_TRY(apfl_format_put_string(w, "\\nfree "));
FMT_TRY(apfl_io_write_string(w, "\\nfree "));
FMT_TRY(apfl_format_put_int(w, counts[GC_STATUS_FREE]));
FMT_TRY(apfl_format_put_string(w, ", black "));
FMT_TRY(apfl_io_write_string(w, ", black "));
FMT_TRY(apfl_format_put_int(w, counts[GC_STATUS_BLACK]));
FMT_TRY(apfl_format_put_string(w, ", grey "));
FMT_TRY(apfl_io_write_string(w, ", grey "));
FMT_TRY(apfl_format_put_int(w, counts[GC_STATUS_GREY]));
FMT_TRY(apfl_format_put_string(w, ", white "));
FMT_TRY(apfl_io_write_string(w, ", white "));
FMT_TRY(apfl_format_put_int(w, counts[GC_STATUS_WHITE]));
FMT_TRY(apfl_format_put_string(w, "\"];\n"));
FMT_TRY(apfl_io_write_string(w, "\"];\n"));
}
FMT_TRY(apfl_format_put_string(w, "}\n"));
FMT_TRY(apfl_io_write_string(w, "}\n"));
return true;
}

View file

@ -70,7 +70,7 @@ struct gc_object *apfl_gc_object_from_ptr(void *, enum gc_type);
void apfl_gc_init(struct gc *, struct apfl_allocator, gc_roots_getter, void *roots_getter_opaque);
void apfl_gc_deinit(struct gc *);
bool apfl_gc_debug_dump_graph(struct gc *, struct apfl_format_writer);
bool apfl_gc_debug_dump_graph(struct gc *, struct apfl_io_writer);
size_t apfl_gc_tmproots_begin(struct gc *gc);
void apfl_gc_tmproots_restore(struct gc *gc, size_t);

View file

@ -196,23 +196,23 @@ impl_join(apfl_ctx ctx)
static void
print(apfl_ctx ctx)
{
struct apfl_format_writer w = apfl_get_output_writer(ctx);
struct apfl_io_writer w = apfl_get_output_writer(ctx);
size_t len = apfl_len(ctx, 0);
for (size_t i = 0; i < len; i++) {
if (i > 0) {
TRY_FORMAT(ctx, apfl_format_put_string(w, " "));
TRY_FORMAT(ctx, apfl_io_write_string(w, " "));
}
apfl_get_list_member_by_index(ctx, 0, i);
apfl_tostring(ctx, -1);
struct apfl_string_view sv = apfl_get_string(ctx, -1);
TRY_FORMAT(ctx, apfl_format_put_string(w, sv));
TRY_FORMAT(ctx, apfl_io_write_string(w, sv));
apfl_drop(ctx, -1);
}
if (len > 0) {
TRY_FORMAT(ctx, apfl_format_put_string(w, "\n"));
TRY_FORMAT(ctx, apfl_io_write_string(w, "\n"));
}
apfl_push_nil(ctx);
@ -221,7 +221,7 @@ print(apfl_ctx ctx)
static void
dump(apfl_ctx ctx)
{
struct apfl_format_writer w = apfl_get_output_writer(ctx);
struct apfl_io_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));
@ -230,7 +230,7 @@ dump(apfl_ctx ctx)
static void
disasm(apfl_ctx ctx)
{
struct apfl_format_writer w = apfl_get_output_writer(ctx);
struct apfl_io_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);
@ -245,14 +245,14 @@ disasm(apfl_ctx ctx)
for (size_t i = 0; i < func->subfunctions_len; i++) {
struct subfunction *subfunction = &func->subfunctions[i];
TRY_FORMAT(ctx, apfl_format_put_string(w, "Subfunction #"));
TRY_FORMAT(ctx, apfl_io_write_string(w, "Subfunction #"));
TRY_FORMAT(ctx, apfl_format_put_number(w, (int)i));
TRY_FORMAT(ctx, apfl_format_put_string(w, "\n"));
TRY_FORMAT(ctx, apfl_io_write_string(w, "\n"));
TRY_FORMAT(ctx, apfl_format_put_indent(w, 1));
TRY_FORMAT(ctx, apfl_format_put_string(w, "Matcher\n"));
TRY_FORMAT(ctx, apfl_io_write_string(w, "Matcher\n"));
TRY_FORMAT(ctx, apfl_bytecode_dump_matcher(2, w, subfunction->matcher->instructions));
TRY_FORMAT(ctx, apfl_format_put_indent(w, 1));
TRY_FORMAT(ctx, apfl_format_put_string(w, "Instructions\n"));
TRY_FORMAT(ctx, apfl_io_write_string(w, "Instructions\n"));
TRY_FORMAT(ctx, apfl_bytecode_dump(2, w, subfunction->body));
}
}
@ -345,7 +345,7 @@ impl_gc(apfl_ctx ctx)
struct apfl_string_view sv = apfl_get_string(ctx, -1);
if (apfl_string_eq(sv, "dump")) {
struct apfl_format_writer w = apfl_get_output_writer(ctx);
struct apfl_io_writer w = apfl_get_output_writer(ctx);
TRY_FORMAT(ctx, apfl_gc_debug_dump_graph(&ctx->gc, w));
} else if (apfl_string_eq(sv, "collect")) {
apfl_gc_full(&ctx->gc);
@ -361,18 +361,18 @@ impl_backtrace(apfl_ctx ctx)
{
apfl_drop(ctx, -1);
struct apfl_format_writer w = apfl_get_output_writer(ctx);
struct apfl_io_writer w = apfl_get_output_writer(ctx);
size_t depth = apfl_call_stack_depth(ctx);
for (size_t i = 1; i < depth; i++) {
TRY_FORMAT(ctx, apfl_format_put_string(w, "#"));
TRY_FORMAT(ctx, apfl_io_write_string(w, "#"));
TRY_FORMAT(ctx, apfl_format_put_int(w, (int)i));
TRY_FORMAT(ctx, apfl_format_put_string(w, ": "));
TRY_FORMAT(ctx, apfl_io_write_string(w, ": "));
TRY_FORMAT(ctx, apfl_call_stack_entry_info_format(
w,
apfl_call_stack_inspect(ctx, i)
));
TRY_FORMAT(ctx, apfl_format_put_string(w, "\n"));
TRY_FORMAT(ctx, apfl_io_write_string(w, "\n"));
}
}

View file

@ -141,8 +141,8 @@ exit:
static int
repl_eval(void)
{
struct apfl_format_writer w_out = apfl_format_file_writer(stdout);
struct apfl_format_writer w_err = apfl_format_file_writer(stderr);
struct apfl_io_writer w_out = apfl_io_file_writer(stdout);
struct apfl_io_writer w_err = apfl_io_file_writer(stderr);
apfl_ctx ctx = apfl_ctx_new((struct apfl_config) {
.allocator = apfl_allocator_default(),

View file

@ -90,44 +90,44 @@ as_string_view(struct apfl_value value)
}
static bool
format(unsigned indent, struct apfl_format_writer w, struct apfl_value value, bool skip_first_indent)
format(unsigned indent, struct apfl_io_writer w, struct apfl_value value, bool skip_first_indent)
{
TRY(apfl_format_put_indent(w, skip_first_indent ? 0 : indent));
switch (value.type) {
case VALUE_NIL:
TRY(apfl_format_put_string(w, "nil"));
TRY(apfl_io_write_string(w, "nil"));
return true;
case VALUE_BOOLEAN:
TRY(apfl_format_put_string(w, value.boolean ? "true" : "false"));
TRY(apfl_io_write_string(w, value.boolean ? "true" : "false"));
return true;
case VALUE_NUMBER:
TRY(apfl_format_put_number(w, value.number));
return true;
case VALUE_STRING:
case VALUE_CONST_STRING:
TRY(apfl_format_put_string(w, "\""));
TRY(apfl_format_put_string(w, as_string_view(value)));
TRY(apfl_format_put_string(w, "\""));
TRY(apfl_io_write_string(w, "\""));
TRY(apfl_io_write_string(w, as_string_view(value)));
TRY(apfl_io_write_string(w, "\""));
return true;
case VALUE_LIST:
if (value.list->len == 0) {
return apfl_format_put_string(w, "[]");
return apfl_io_write_string(w, "[]");
}
TRY(apfl_format_put_string(w, "[\n"));
TRY(apfl_io_write_string(w, "[\n"));
for (size_t i = 0; i < value.list->len; i++) {
TRY(format(indent+1, w, value.list->items[i], false));
TRY(apfl_format_put_string(w, "\n"));
TRY(apfl_io_write_string(w, "\n"));
}
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "]"));
TRY(apfl_io_write_string(w, "]"));
return true;
case VALUE_DICT:
if (apfl_hashmap_count(value.dict->map) == 0) {
return apfl_format_put_string(w, "[->]");
return apfl_io_write_string(w, "[->]");
}
TRY(apfl_format_put_string(w, "[\n"));
TRY(apfl_io_write_string(w, "[\n"));
HASHMAP_EACH(&value.dict->map, cur) {
struct apfl_value *k = apfl_hashmap_cursor_peek_key(cur);
@ -136,36 +136,36 @@ format(unsigned indent, struct apfl_format_writer w, struct apfl_value value, bo
assert(v != NULL);
TRY(format(indent+1, w, *k, false));
TRY(apfl_format_put_string(w, " -> "));
TRY(apfl_io_write_string(w, " -> "));
TRY(format(indent+1, w, *v, true));
TRY(apfl_format_put_string(w, "\n"));
TRY(apfl_io_write_string(w, "\n"));
}
TRY(apfl_format_put_indent(w, indent));
TRY(apfl_format_put_string(w, "]"));
TRY(apfl_io_write_string(w, "]"));
return true;
case VALUE_FUNC:
TRY(apfl_format_put_string(w, "{ ... }"));
TRY(apfl_io_write_string(w, "{ ... }"));
if (value.func->name != NULL) {
TRY(apfl_format_put_string(w, " # "));
TRY(apfl_format_put_string(w, *value.func->name));
TRY(apfl_io_write_string(w, " # "));
TRY(apfl_io_write_string(w, *value.func->name));
}
return true;
case VALUE_CFUNC:
TRY(apfl_format_put_string(w, "{ native code }"));
TRY(apfl_io_write_string(w, "{ native code }"));
if (value.cfunc->name != NULL) {
TRY(apfl_format_put_string(w, " # "));
TRY(apfl_format_put_string(w, *value.cfunc->name));
TRY(apfl_io_write_string(w, " # "));
TRY(apfl_io_write_string(w, *value.cfunc->name));
}
return true;
case VALUE_USERDATA:
case VALUE_NATIVE_OBJECT:
TRY(apfl_format_put_string(w, "userdata"));
TRY(apfl_io_write_string(w, "userdata"));
return true;
}
TRY(apfl_format_put_string(w, "Unknown value ? ("));
TRY(apfl_io_write_string(w, "Unknown value ? ("));
TRY(apfl_format_put_number(w, (int)value.type));
TRY(apfl_format_put_string(w, ")"));
TRY(apfl_io_write_string(w, ")"));
return true;
}
@ -369,16 +369,16 @@ apfl_value_move(struct apfl_value *src)
}
bool
apfl_value_format(struct apfl_value value, struct apfl_format_writer w)
apfl_value_format(struct apfl_value value, struct apfl_io_writer w)
{
return format(0, w, value, false);
}
bool
apfl_value_print(struct apfl_value value, struct apfl_format_writer w)
apfl_value_print(struct apfl_value value, struct apfl_io_writer w)
{
TRY(apfl_value_format(value, w));
TRY(apfl_format_put_string(w, "\n"));
TRY(apfl_io_write_string(w, "\n"));
return true;
}

View file

@ -93,8 +93,8 @@ 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_format(struct apfl_value, struct apfl_format_writer);
bool apfl_value_print(struct apfl_value, struct apfl_format_writer);
bool apfl_value_format(struct apfl_value, struct apfl_io_writer);
bool apfl_value_print(struct apfl_value, struct apfl_io_writer);
apfl_hash apfl_value_hash(const struct apfl_value);
enum comparison_result {

View file

@ -65,15 +65,15 @@ playground_source_reader_cb(void *context, char *buf, size_t *len, bool need)
int
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};
struct apfl_io_writer w_ok = {.write = web_fmt_write, .opaque = (void *)0};
struct apfl_io_writer w_err = {.write = web_fmt_write, .opaque = (void *)1};
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"));
assert(apfl_io_write_string(w_err, "Failed to init the context\n"));
return 1;
}
@ -90,7 +90,7 @@ main(void)
});
if (runner == NULL) {
apfl_ctx_destroy(ctx);
assert(apfl_format_put_string(w_err, "Failed to init runner\n"));
assert(apfl_io_write_string(w_err, "Failed to init runner\n"));
return 1;
}