diff --git a/repl/main.c b/repl/main.c index 499ddcf..efb71df 100644 --- a/repl/main.c +++ b/repl/main.c @@ -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); diff --git a/source/toy_print.c b/source/toy_print.c index 4d02d10..5db3cd9 100644 --- a/source/toy_print.c +++ b/source/toy_print.c @@ -2,21 +2,9 @@ #include -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; } diff --git a/source/toy_print.h b/source/toy_print.h index b358011..313a549 100644 --- a/source/toy_print.h +++ b/source/toy_print.h @@ -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 diff --git a/tests/units/test_print.c b/tests/units/test_print.c index ccca559..aaae961 100644 --- a/tests/units/test_print.c +++ b/tests/units/test_print.c @@ -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) { diff --git a/tests/units/test_vm.c b/tests/units/test_vm.c index bef630c..aa0a545 100644 --- a/tests/units/test_vm.c +++ b/tests/units/test_vm.c @@ -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) {