diff --git a/src/driver/main.c b/src/driver/main.c index b1f867c..d645514 100644 --- a/src/driver/main.c +++ b/src/driver/main.c @@ -1,11 +1,13 @@ /** * Main C source code file for the JabaCat C Compiler. - * Initial commit: June 17, 2024 */ #include +#include + int main(int argc, char **argv) { - printf("%s\n", "Hello, World!"); + PRINT_DEFAULT("JabaCat C Compiler 2024"); + PRINT_DEBUG("Compilation not supported yet -- coming soon!"); return 0; } diff --git a/src/lexer/lex.c b/src/lexer/lex.c index 367c105..a5cfb56 100644 --- a/src/lexer/lex.c +++ b/src/lexer/lex.c @@ -2,6 +2,8 @@ #include // memcpy +#include // error reporting + int lex(Lexer *l, Token *t) { // If there are any tokens waiting in the putback buffer, read from there // instead. @@ -17,7 +19,10 @@ int lex(Lexer *l, Token *t) { int unlex(Lexer *l, Token *t) { // First, make sure we can actually fit it in the buffer. if (l->unlexed_count >= TOKEN_PUTBACKS) { - // TODO: error reporting + PRINT_ERROR( + "internal: tried to unlex more than %d tokens at a time", + TOKEN_PUTBACKS + ); return -1; // Error return code } memcpy(&l->unlexed[l->unlexed_count], t, sizeof(Token)); diff --git a/src/util/out.h b/src/util/out.h new file mode 100644 index 0000000..2085864 --- /dev/null +++ b/src/util/out.h @@ -0,0 +1,38 @@ +/** + * This provides facilities for properly-formatted output. This is just taken + * from yfc's output facilities. + */ + +#pragma once + +// All of the color codes. +#define CODE_RED 31 +#define CODE_YELLOW 33 +#define CODE_GREEN 32 +#define CODE_BLUE 34 +#define CODE_MAGENTA 35 +#define CODE_CYAN 36 +#define CODE_WHITE 37 + +// Print things with colors. +#define OUTPUT_STREAM stderr +#define SET_COLOR(color) fprintf(OUTPUT_STREAM, "\033[%dm", color) +#define RESET_COLOR(color) fprintf(OUTPUT_STREAM, "\033[0m") + +#define PRINT_WITH_COLOR(color, ...) do { \ + SET_COLOR(color); \ + fprintf(OUTPUT_STREAM, __VA_ARGS__); \ + RESET_COLOR(color); \ +} while (0) + +#define PRINT_ERROR(msg, ...) \ + PRINT_WITH_COLOR(CODE_RED, "Error: jccc: " msg "\n" ,##__VA_ARGS__) + +#define PRINT_WARNING(msg, ...) \ + PRINT_WITH_COLOR(CODE_YELLOW, "Warning: jccc: " msg "\n" ,##__VA_ARGS__) + +#define PRINT_DEBUG(msg, ...) \ + PRINT_WITH_COLOR(CODE_BLUE, "Debug: jccc: " msg "\n" ,##__VA_ARGS__) + +#define PRINT_DEFAULT(msg, ...) \ + PRINT_WITH_COLOR(CODE_WHITE, "jccc: " msg "\n" ,##__VA_ARGS__)