Skip to content

Commit

Permalink
Update 2024-03-21-setjmp-plus-longjmp-equals-goto-but-awesome.md
Browse files Browse the repository at this point in the history
  • Loading branch information
MyNameIsTrez authored Dec 26, 2024
1 parent 2c0caa6 commit 6fd0c99
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion _posts/2024-03-21-setjmp-plus-longjmp-equals-goto-but-awesome.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,21 @@ int main() {

Compiling and running this program with `gcc foo.c && ./a.out` [on godbolt.org](https://godbolt.org/z/3P9j59d15) prints `foo` to stdout, then prints `The value of 42 was bigger than expected!` to stderr, and then exits with `EXIT_FAILURE`.

And this is how grug finally uses `snprintf()` and `longjmp()` to throw a formatted error message in a few dozen spots:
Although I dislike C's macros, `grug.c` is over 9k lines long and has over 400 spots where it needs to do throw if an error fails, so this is roughly what it uses:

```bettercpp
#define grug_error(...) {\
snprintf(error_msg, sizeof(error_msg), __VA_ARGS__);\
error_line_number = __LINE__;\
longjmp(error_jmp_buffer, 1);\
}
#define grug_assert(condition, ...) {\
if (!(condition)) {\
grug_error(__VA_ARGS__);\
}\
}
struct token peek_token(size_t token_index) {
grug_assert(token_index < tokens_size, "token_index %zu was out of bounds in peek_token()", token_index);
return tokens[token_index];
Expand Down

0 comments on commit 6fd0c99

Please sign in to comment.