You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2024-03-21-setjmp-plus-longjmp-equals-goto-but-awesome.md
+12-15Lines changed: 12 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,15 @@ If you've ever written a decently sized C program before, you will have struggle
8
8
9
9
I am implementing [my modding language called grug]({{ site.baseurl }} {% link _posts/2024-02-29-creating-the-perfect-modding-language.md %}) in C right now, and so far I have just printed the error and exited the program whenever an error was encountered.
10
10
11
-
Here is a snippet from grug's tokenizer. The if-statement is necessary to prevent `tokens.tokens[token_index]` from segfaulting the entire game whenever a mod has a typo in it:
11
+
Here is a snippet from grug's tokenizer. The if-statement is necessary to prevent `tokens[token_index]` from segfaulting the entire game whenever a mod has a typo in it:
12
12
13
13
```bettercpp
14
-
static token get_token(size_t token_index) {
14
+
struct token peek_token(size_t token_index) {
15
15
if (token_index >= tokens.size) {
16
-
fprintf(stderr, "token_index %zu was out of bounds in get_token()\n", token_index);
16
+
fprintf(stderr, "token_index %zu was out of bounds in peek_token()\n", token_index);
17
17
exit(EXIT_FAILURE);
18
18
}
19
-
return tokens.tokens[token_index];
19
+
return tokens[token_index];
20
20
}
21
21
```
22
22
@@ -40,20 +40,20 @@ bool error_happened = false;
40
40
Which are used to print the message to a buffer, and to remember that something went wrong, like so:
41
41
42
42
```bettercpp
43
-
static token get_token(size_t token_index) {
43
+
struct token peek_token(size_t token_index) {
44
44
if (token_index >= tokens.size) {
45
-
snprintf(error_msg, sizeof(error_msg), "token_index %zu was out of bounds in get_token()", token_index);
45
+
snprintf(error_msg, sizeof(error_msg), "token_index %zu was out of bounds in peek_token()", token_index);
46
46
error_happened = true;
47
-
return (token){0}; // Just `return;` won't compile, so return an empty token :(
47
+
return (struct token){0}; // Just `return;` won't compile, so return an empty token :(
48
48
}
49
-
return tokens.tokens[token_index];
49
+
return tokens[token_index];
50
50
}
51
51
```
52
52
53
53
But now that the program doesn't immediately exit on error, `if (error_happened) return;` has to pasted after every single call that can directly or indirectly(!) throw an error:
0 commit comments