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
}
//callbacks
static void printCallback(const char* msg) {
fprintf(stdout, "%s\n", msg);
}
static void errorAndExitCallback(const char* msg) {
//error callbacks
static int errorAndExitCallback(const char* msg) {
fprintf(stderr, TOY_CC_ERROR "Error: %s" TOY_CC_RESET "\n", msg);
exit(-1);
}
static void errorAndContinueCallback(const char* msg) {
fprintf(stderr, TOY_CC_ERROR "Error: %s" TOY_CC_RESET "\n", msg);
static int errorAndContinueCallback(const char* 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);
exit(-1);
}
static void assertFailureAndContinueCallback(const char* msg) {
fprintf(stderr, TOY_CC_ASSERT "Assert Failure: %s" TOY_CC_RESET "\n", msg);
static int assertFailureAndContinueCallback(const char* 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
(void)msg;
return 0;
}
static void silentExitCallback(const char* msg) {
static int silentExitCallback(const char* msg) {
//NO-OP
(void)msg;
exit(-1);
@@ -312,7 +309,7 @@ static void debugScopePrint(Toy_Scope* scope, int depth) {
//repl function
int repl(const char* filepath, bool verbose) {
//output options
Toy_setPrintCallback(printCallback);
Toy_setPrintCallback(puts);
Toy_setErrorCallback(errorAndContinueCallback);
Toy_setAssertFailureCallback(assertFailureAndContinueCallback);
@@ -405,7 +402,7 @@ int repl(const char* filepath, bool verbose) {
//main file
int main(int argc, const char* argv[]) {
Toy_setPrintCallback(printCallback);
Toy_setPrintCallback(puts);
Toy_setErrorCallback(errorAndExitCallback);
Toy_setAssertFailureCallback(assertFailureAndExitCallback);
+6 -18
View File
@@ -2,21 +2,9 @@
#include <stdio.h>
static void outDefault(const char* msg) {
fprintf(stdout, "%s\n", msg);
}
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;
static Toy_callbackType printCallback = puts;
static Toy_callbackType errorCallback = puts;
static Toy_callbackType assertCallback = puts;
void Toy_print(const char* msg) {
printCallback(msg);
@@ -43,13 +31,13 @@ void Toy_setAssertFailureCallback(Toy_callbackType cb) {
}
void Toy_resetPrintCallback(void) {
printCallback = outDefault;
printCallback = puts;
}
void Toy_resetErrorCallback(void) {
errorCallback = errDefault;
errorCallback = puts;
}
void Toy_resetAssertFailureCallback(void) {
assertCallback = assertDefault;
assertCallback = puts;
}
+2 -2
View File
@@ -2,8 +2,8 @@
#include "toy_common.h"
//handle callbacks for printing to the terminal, or elsewhere
typedef void (*Toy_callbackType)(const char*);
//handle callbacks for printing to the terminal, or elsewhere (signature matches 'puts')
typedef int (*Toy_callbackType)(const char*);
TOY_API void Toy_print(const char* msg); //print keyword
TOY_API void Toy_error(const char* msg); //runtime errors
+2 -1
View File
@@ -5,9 +5,10 @@
int counter = 0;
void counterCallback(const char* msg) {
int counterCallback(const char* msg) {
(void)msg;
counter++;
return 0;
}
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 void callbackUtil(const char* msg) {
static int callbackUtil(const char* msg) {
if (msg != NULL) {
free(callbackUtilReceived);
callbackUtilReceived = (char*)malloc(strlen(msg) + 1);
strcpy(callbackUtilReceived, msg);
}
return 0;
}
int test_keyword_assert(Toy_Bucket** bucketHandle) {