Skip to content

Commit

Permalink
Merge pull request #14 from adamhutchings/token
Browse files Browse the repository at this point in the history
Add output capabilities
  • Loading branch information
adamhutchings authored Jun 21, 2024
2 parents 6a4a851 + 889ad89 commit ebb1a62
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/driver/main.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/**
* Main C source code file for the JabaCat C Compiler.
* Initial commit: June 17, 2024
*/

#include <stdio.h>

#include <util/out.h>

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;
}
7 changes: 6 additions & 1 deletion src/lexer/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <string.h> // memcpy
#include <assert.h> // assert

#include <util/out.h> // error reporting

int lex(Lexer *l, Token *t) {
// If there are any tokens waiting in the putback buffer, read from there
// instead.
Expand All @@ -18,7 +20,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));
Expand Down
38 changes: 38 additions & 0 deletions src/util/out.h
Original file line number Diff line number Diff line change
@@ -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__)

0 comments on commit ebb1a62

Please sign in to comment.