Skip to content

Commit

Permalink
Support #warning and #error directives
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenthuberdeau committed Aug 13, 2024
1 parent b307487 commit 8832d4b
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pnut.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ struct IncludeStack *include_stack, *include_stack2;
FILE *fp = 0; // Current file pointer that's being read
#endif

// Tokens and AST nodes
enum {
// Tokens and AST nodes
AUTO_KW = 300,
BREAK_KW,
CASE_KW,
Expand Down Expand Up @@ -640,6 +640,8 @@ int ENDIF_ID;
int DEFINE_ID;
int UNDEF_ID;
int INCLUDE_ID;
int WARNING_ID;
int ERROR_ID;
int INCLUDE_SHELL_ID;

int NOT_SUPPORTED_ID;
Expand Down Expand Up @@ -878,6 +880,22 @@ void handle_preprocessor_directive() {
}
} else if (tok == IDENTIFIER AND val == DEFINE_ID) {
handle_define();
} else if (tok == IDENTIFIER && val == WARNING_ID) {
get_tok_macro();
putstr("warning: ");
if (tok == STRING) {
putstr(string_pool + val);
} else {
syntax_error("#warning/#error directives can only be followed by a string");
}
} else if (tok == IDENTIFIER AND val == ERROR_ID) {
get_tok_macro();
putstr("error: ");
if (tok == STRING) {
syntax_error(string_pool + val);
} else {
syntax_error("#warning/#error directives can only be followed by a string");
}
} else {
putstr("tok="); putint(tok); putstr(": "); putstr(string_pool + heap[val + 1]); putchar('\n');
syntax_error("unsupported preprocessor directive");
Expand Down Expand Up @@ -992,6 +1010,8 @@ void init_ident_table() {
IFNDEF_ID = init_ident(IDENTIFIER, "ifndef");
ENDIF_ID = init_ident(IDENTIFIER, "endif");
DEFINE_ID = init_ident(IDENTIFIER, "define");
WARNING_ID = init_ident(IDENTIFIER, "warning");
ERROR_ID = init_ident(IDENTIFIER, "error");
UNDEF_ID = init_ident(IDENTIFIER, "undef");
INCLUDE_ID = init_ident(IDENTIFIER, "include");
INCLUDE_SHELL_ID = init_ident(IDENTIFIER, "include_shell");
Expand Down

0 comments on commit 8832d4b

Please sign in to comment.