Tweaked output callbacks to match 'puts' signature

This commit is contained in:
2026-05-08 11:12:27 +10:00
parent 185f3896c5
commit be84a8dfe2
5 changed files with 24 additions and 37 deletions
+12 -15
View File
@@ -79,35 +79,32 @@ int getFileName(char* dest, const char* src, size_t destLength) {
#undef MIN #undef MIN
} }
//callbacks //error callbacks
static void printCallback(const char* msg) { static int errorAndExitCallback(const char* msg) {
fprintf(stdout, "%s\n", msg);
}
static void errorAndExitCallback(const char* msg) {
fprintf(stderr, TOY_CC_ERROR "Error: %s" TOY_CC_RESET "\n", msg); fprintf(stderr, TOY_CC_ERROR "Error: %s" TOY_CC_RESET "\n", msg);
exit(-1); exit(-1);
} }
static void errorAndContinueCallback(const char* msg) { static int errorAndContinueCallback(const char* msg) {
fprintf(stderr, TOY_CC_ERROR "Error: %s" TOY_CC_RESET "\n", msg); return fprintf(stderr, TOY_CC_ERROR "Error: %s" TOY_CC_RESET "\n", msg);
} }
static void assertFailureAndExitCallback(const char* msg) { static int assertFailureAndExitCallback(const char* msg) {
fprintf(stderr, TOY_CC_ASSERT "Assert Failure: %s" TOY_CC_RESET "\n", msg); fprintf(stderr, TOY_CC_ASSERT "Assert Failure: %s" TOY_CC_RESET "\n", msg);
exit(-1); exit(-1);
} }
static void assertFailureAndContinueCallback(const char* msg) { static int assertFailureAndContinueCallback(const char* msg) {
fprintf(stderr, TOY_CC_ASSERT "Assert Failure: %s" TOY_CC_RESET "\n", msg); return fprintf(stderr, TOY_CC_ASSERT "Assert Failure: %s" TOY_CC_RESET "\n", msg);
} }
static void noOpCallback(const char* msg) { static int noOpCallback(const char* msg) {
//NO-OP //NO-OP
(void)msg; (void)msg;
return 0;
} }
static void silentExitCallback(const char* msg) { static int silentExitCallback(const char* msg) {
//NO-OP //NO-OP
(void)msg; (void)msg;
exit(-1); exit(-1);
@@ -312,7 +309,7 @@ static void debugScopePrint(Toy_Scope* scope, int depth) {
//repl function //repl function
int repl(const char* filepath, bool verbose) { int repl(const char* filepath, bool verbose) {
//output options //output options
Toy_setPrintCallback(printCallback); Toy_setPrintCallback(puts);
Toy_setErrorCallback(errorAndContinueCallback); Toy_setErrorCallback(errorAndContinueCallback);
Toy_setAssertFailureCallback(assertFailureAndContinueCallback); Toy_setAssertFailureCallback(assertFailureAndContinueCallback);
@@ -405,7 +402,7 @@ int repl(const char* filepath, bool verbose) {
//main file //main file
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
Toy_setPrintCallback(printCallback); Toy_setPrintCallback(puts);
Toy_setErrorCallback(errorAndExitCallback); Toy_setErrorCallback(errorAndExitCallback);
Toy_setAssertFailureCallback(assertFailureAndExitCallback); Toy_setAssertFailureCallback(assertFailureAndExitCallback);
+6 -18
View File
@@ -2,21 +2,9 @@
#include <stdio.h> #include <stdio.h>
static void outDefault(const char* msg) { static Toy_callbackType printCallback = puts;
fprintf(stdout, "%s\n", msg); static Toy_callbackType errorCallback = puts;
} static Toy_callbackType assertCallback = puts;
static void errDefault(const char* msg) {
fprintf(stderr, "%s\n", msg);
}
static void assertDefault(const char* msg) {
fprintf(stderr, "%s\n", msg);
}
static Toy_callbackType printCallback = outDefault;
static Toy_callbackType errorCallback = errDefault;
static Toy_callbackType assertCallback = assertDefault;
void Toy_print(const char* msg) { void Toy_print(const char* msg) {
printCallback(msg); printCallback(msg);
@@ -43,13 +31,13 @@ void Toy_setAssertFailureCallback(Toy_callbackType cb) {
} }
void Toy_resetPrintCallback(void) { void Toy_resetPrintCallback(void) {
printCallback = outDefault; printCallback = puts;
} }
void Toy_resetErrorCallback(void) { void Toy_resetErrorCallback(void) {
errorCallback = errDefault; errorCallback = puts;
} }
void Toy_resetAssertFailureCallback(void) { void Toy_resetAssertFailureCallback(void) {
assertCallback = assertDefault; assertCallback = puts;
} }
+2 -2
View File
@@ -2,8 +2,8 @@
#include "toy_common.h" #include "toy_common.h"
//handle callbacks for printing to the terminal, or elsewhere //handle callbacks for printing to the terminal, or elsewhere (signature matches 'puts')
typedef void (*Toy_callbackType)(const char*); typedef int (*Toy_callbackType)(const char*);
TOY_API void Toy_print(const char* msg); //print keyword TOY_API void Toy_print(const char* msg); //print keyword
TOY_API void Toy_error(const char* msg); //runtime errors TOY_API void Toy_error(const char* msg); //runtime errors
+2 -1
View File
@@ -5,9 +5,10 @@
int counter = 0; int counter = 0;
void counterCallback(const char* msg) { int counterCallback(const char* msg) {
(void)msg; (void)msg;
counter++; counter++;
return 0;
} }
int test_callbacks(void) { int test_callbacks(void) {
+2 -1
View File
@@ -161,12 +161,13 @@ int test_opcode_not_equal(Toy_Bucket** bucketHandle) {
} }
static char* callbackUtilReceived = NULL; static char* callbackUtilReceived = NULL;
static void callbackUtil(const char* msg) { static int callbackUtil(const char* msg) {
if (msg != NULL) { if (msg != NULL) {
free(callbackUtilReceived); free(callbackUtilReceived);
callbackUtilReceived = (char*)malloc(strlen(msg) + 1); callbackUtilReceived = (char*)malloc(strlen(msg) + 1);
strcpy(callbackUtilReceived, msg); strcpy(callbackUtilReceived, msg);
} }
return 0;
} }
int test_keyword_assert(Toy_Bucket** bucketHandle) { int test_keyword_assert(Toy_Bucket** bucketHandle) {