apfl/src/error.c

227 lines
8.7 KiB
C
Raw Normal View History

#include <stdio.h>
2021-12-10 20:22:16 +00:00
#include "apfl.h"
#include "format.h"
2021-12-10 20:22:16 +00:00
#define POSFMT "%d:%d"
#define POSARGS error.position.line, error.position.col
#define POS2ARGS error.position2.line, error.position2.col
#define TRY(x) do { if (!(x)) return false; } while (0)
2021-12-18 23:27:34 +00:00
const char *
apfl_error_type_name(enum apfl_error_type type)
{
switch (type) {
case APFL_ERR_MALLOC_FAILED:
return "APFL_ERR_MALLOC_FAILED";
case APFL_ERR_INPUT_ERROR:
return "APFL_ERR_INPUT_ERROR";
case APFL_ERR_UNEXPECTED_EOF:
return "APFL_ERR_UNEXPECTED_EOF";
case APFL_ERR_EXPECTED_EQ_AFTER_COLON:
return "APFL_ERR_EXPECTED_EQ_AFTER_COLON";
case APFL_ERR_UNEXPECTED_BYTE:
return "APFL_ERR_UNEXPECTED_BYTE";
2021-12-18 23:27:34 +00:00
case APFL_ERR_UNEXPECTED_BYTE_IN_NUMBER:
return "APFL_ERR_UNEXPECTED_BYTE_IN_NUMBER";
case APFL_ERR_EXPECTED_DIGIT:
return "APFL_ERR_EXPECTED_DIGIT";
case APFL_ERR_EXPECTED_HEX_IN_HEX_ESCAPE:
return "APFL_ERR_EXPECTED_HEX_IN_HEX_ESCAPE";
case APFL_ERR_INVALID_ESCAPE_SEQUENCE:
return "APFL_ERR_INVALID_ESCAPE_SEQUENCE";
case APFL_ERR_NO_LINEBREAK_AFTER_CONTINUE_LINE:
return "APFL_ERR_NO_LINEBREAK_AFTER_CONTINUE_LINE";
case APFL_ERR_UNEXPECTED_TOKEN:
return "APFL_ERR_UNEXPECTED_TOKEN";
case APFL_ERR_MISMATCHING_CLOSING_BRACKET:
return "APFL_ERR_MISMATCHING_CLOSING_BRACKET";
case APFL_ERR_UNEXPECTED_EOF_AFTER_TOKEN:
return "APFL_ERR_UNEXPECTED_EOF_AFTER_TOKEN";
case APFL_ERR_STATEMENTS_BEFORE_PARAMETERS:
return "APFL_ERR_STATEMENTS_BEFORE_PARAMETERS";
case APFL_ERR_EMPTY_ASSIGNMENT_BEFORE_PARAMETERS:
return "APFL_ERR_EMPTY_ASSIGNMENT_BEFORE_PARAMETERS";
case APFL_ERR_UNEXPECTED_EXPRESSION:
return "APFL_ERR_UNEXPECTED_EXPRESSION";
case APFL_ERR_INVALID_ASSIGNMENT_LHS:
return "APFL_ERR_INVALID_ASSIGNMENT_LHS";
case APFL_ERR_EMPTY_ASSIGNMENT:
return "APFL_ERR_EMPTY_ASSIGNMENT";
case APFL_ERR_ONLY_ONE_EXPAND_ALLOWED:
return "APFL_ERR_ONLY_ONE_EXPAND_ALLOWED";
case APFL_ERR_UNEXPECTED_CONSTANT_IN_MEMBER_ACCESS:
return "APFL_ERR_UNEXPECTED_CONSTANT_IN_MEMBER_ACCESS";
case APFL_ERR_UNEXPECTED_EXPR_IN_MEMBER_ACCESS:
return "APFL_ERR_UNEXPECTED_EXPR_IN_MEMBER_ACCESS";
case APFL_ERR_UNEXPECTED_BLANK_IN_MEMBER_ACCESS:
return "APFL_ERR_UNEXPECTED_BLANK_IN_MEMBER_ACCESS";
Implement mark&sweep garbage collection and bytecode compilation Instead of the previous refcount base garbage collection, we're now using a basic tri-color mark&sweep collector. This is done to support cyclical value relationships in the future (functions can form cycles, all values implemented up to this point can not). The collector maintains a set of roots and a set of objects (grouped into blocks). The GC enabled objects are no longer allocated manually, but will be allocated by the GC. The GC also wraps an allocator, this way the GC knows, if we ran out of memory and will try to get out of this situation by performing a full collection cycle. The tri-color abstraction was chosen for two reasons: - We don't have to maintain a list of objects that need to be marked, we can simply grab the next grey one. - It should allow us to later implement incremental collection (right now we only do a stop-the-world collection). This also switches to a bytecode based evaluation of the code: We no longer directly evaluate the AST, but first compile it into a series of instructions, that are evaluated in a separate step. This was done in preparation for inplementing functions: We only need to turn a function body into instructions instead of evaluating the node again with each call of the function. Also, since an instruction list is implemented as a GC object, this then removes manual memory management of the function body and it's child nodes. Since the GC and the bytecode go hand in hand, this was done in one (giant) commit. As a downside, we've now lost the ability do do list matching on assignments. I've already started to work on implementing this in the new architecture, but left it out of this commit, as it's already quite a large commit :)
2022-04-11 20:24:22 +00:00
case APFL_ERR_NOT_IMPLEMENTED:
return "APFL_ERR_NOT_IMPLEMENTED";
2021-12-18 23:27:34 +00:00
}
return "<unknown error>";
}
static bool
format_pos(struct apfl_format_writer w, struct apfl_position pos)
{
TRY(apfl_format_put_int(w, pos.line));
TRY(apfl_format_put_string(w, ":"));
TRY(apfl_format_put_int(w, pos.col));
return true;
}
static bool
format_error(struct apfl_format_writer w, struct apfl_error error)
2021-12-10 20:22:16 +00:00
{
switch (error.type) {
case APFL_ERR_MALLOC_FAILED:
return apfl_format_put_string(w, "Could not allocate memory");
2021-12-10 20:22:16 +00:00
case APFL_ERR_INPUT_ERROR:
return apfl_format_put_string(w, "Input error while parsing");
2021-12-10 20:22:16 +00:00
case APFL_ERR_UNEXPECTED_EOF:
return apfl_format_put_string(w, "Unexpected end of file");
2021-12-10 20:22:16 +00:00
case APFL_ERR_EXPECTED_EQ_AFTER_COLON:
TRY(apfl_format_put_string(w, "Expected '=' after ':' at "));
TRY(format_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_format_put_hexbyte(w, (unsigned char)error.byte));
TRY(apfl_format_put_string(w, ") at "));
TRY(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
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(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
case APFL_ERR_EXPECTED_DIGIT:
TRY(apfl_format_put_string(w, "Expected a digit at "));
TRY(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
case APFL_ERR_EXPECTED_HEX_IN_HEX_ESCAPE:
TRY(apfl_format_put_string(w, "Expected a hex-digit in hex escape at "));
TRY(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
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(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
case APFL_ERR_NO_LINEBREAK_AFTER_CONTINUE_LINE:
TRY(apfl_format_put_string(w, "No line break (after optional comments) after \\ at "));
TRY(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
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(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
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(format_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(format_pos(w, error.position2));
return true;
2021-12-10 20:22:16 +00:00
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(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
case APFL_ERR_STATEMENTS_BEFORE_PARAMETERS:
TRY(apfl_format_put_string(w, "Unexpected statements before parameters near "));
TRY(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
case APFL_ERR_EMPTY_ASSIGNMENT_BEFORE_PARAMETERS:
TRY(apfl_format_put_string(w, "Unexpected empty assignment before parameters near "));
TRY(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
case APFL_ERR_UNEXPECTED_EXPRESSION:
TRY(apfl_format_put_string(w, "Unexpected expression near "));
TRY(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
case APFL_ERR_INVALID_ASSIGNMENT_LHS:
TRY(apfl_format_put_string(w, "Invalid left hand side of assignment near "));
TRY(format_pos(w, error.position));
return true;
2021-12-10 20:22:16 +00:00
case APFL_ERR_EMPTY_ASSIGNMENT:
TRY(apfl_format_put_string(w, "Empty assignment at "));
TRY(format_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(format_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(format_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(format_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(format_pos(w, error.position));
return true;
Implement mark&sweep garbage collection and bytecode compilation Instead of the previous refcount base garbage collection, we're now using a basic tri-color mark&sweep collector. This is done to support cyclical value relationships in the future (functions can form cycles, all values implemented up to this point can not). The collector maintains a set of roots and a set of objects (grouped into blocks). The GC enabled objects are no longer allocated manually, but will be allocated by the GC. The GC also wraps an allocator, this way the GC knows, if we ran out of memory and will try to get out of this situation by performing a full collection cycle. The tri-color abstraction was chosen for two reasons: - We don't have to maintain a list of objects that need to be marked, we can simply grab the next grey one. - It should allow us to later implement incremental collection (right now we only do a stop-the-world collection). This also switches to a bytecode based evaluation of the code: We no longer directly evaluate the AST, but first compile it into a series of instructions, that are evaluated in a separate step. This was done in preparation for inplementing functions: We only need to turn a function body into instructions instead of evaluating the node again with each call of the function. Also, since an instruction list is implemented as a GC object, this then removes manual memory management of the function body and it's child nodes. Since the GC and the bytecode go hand in hand, this was done in one (giant) commit. As a downside, we've now lost the ability do do list matching on assignments. I've already started to work on implementing this in the new architecture, but left it out of this commit, as it's already quite a large commit :)
2022-04-11 20:24:22 +00:00
case APFL_ERR_NOT_IMPLEMENTED:
TRY(apfl_format_put_string(w, "Feature not implemented"));
return true;
2021-12-10 20:22:16 +00:00
}
TRY(apfl_format_put_string(w, "Unknown error "));
TRY(apfl_format_put_int(w, (int)error.type));
return true;
}
bool
apfl_error_print(struct apfl_error error, FILE *file)
{
struct apfl_format_writer w = apfl_format_file_writer(file);
TRY(format_error(w, error));
TRY(apfl_format_put_char(w, '\n'));
return true;
2021-12-10 20:22:16 +00:00
}
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, &builder);
TRY(format_error(apfl_format_string_writer(&builder), error));
*out = apfl_string_builder_move_string(&builder);
return true;
}
2021-12-10 20:22:16 +00:00
struct apfl_error
apfl_error_simple(enum apfl_error_type type)
{
return (struct apfl_error) { .type = type };
}
bool
apfl_error_is_fatal_type(enum apfl_error_type type)
{
switch (type) {
case APFL_ERR_MALLOC_FAILED:
case APFL_ERR_INPUT_ERROR:
return true;
default:
return false;
}
}