Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output capabilities #14

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2,6 +2,8 @@

#include <string.h> // memcpy

#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 @@ -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));
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__)
Loading