From 45920f763c9c3b0d37d80cba31324086eef84a70 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 22 Aug 2022 05:17:17 +0100 Subject: [PATCH] Tweaked error messages --- source/compiler.c | 14 +++--- source/interpreter.c | 88 ++++++++++++++++++------------------- source/literal.c | 12 ++--- source/literal_dictionary.c | 8 ++-- source/memory.c | 4 +- source/parser.c | 14 +++--- source/repl_main.c | 10 ++--- 7 files changed, 78 insertions(+), 72 deletions(-) diff --git a/source/compiler.c b/source/compiler.c index b9a7b2e..9133480 100644 --- a/source/compiler.c +++ b/source/compiler.c @@ -53,7 +53,7 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) { break; default: - fprintf(stderr, "[Internal] Unrecognized key node type in writeNodeCompoundToCache()"); + fprintf(stderr, ERROR "[internal] Unrecognized key node type in writeNodeCompoundToCache()" RESET); return -1; } @@ -78,7 +78,7 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) { break; default: - fprintf(stderr, "[Internal] Unrecognized value node type in writeNodeCompoundToCache()"); + fprintf(stderr, ERROR "[internal] Unrecognized value node type in writeNodeCompoundToCache()" RESET); return -1; } } @@ -109,7 +109,7 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) { break; default: - fprintf(stderr, "[Internal] Unrecognized node type in writeNodeCompoundToCache()"); + fprintf(stderr, ERROR "[internal] Unrecognized node type in writeNodeCompoundToCache()" RESET); return -1; } } @@ -118,7 +118,7 @@ static int writeNodeCompoundToCache(Compiler* compiler, Node* node) { index = pushLiteralArray(&compiler->literalCache, TO_ARRAY_LITERAL(store)); } else { - fprintf(stderr, "[Internal] Unrecognized compound type in writeNodeCompoundToCache()"); + fprintf(stderr, ERROR "[Internal] Unrecognized compound type in writeNodeCompoundToCache()" RESET); } return index; @@ -444,7 +444,7 @@ static void writeCompilerWithJumps(Compiler* compiler, Node* node, void* breakAd case NODE_PATH_BREAK: { if (!breakAddressesPtr) { - fprintf(stderr, "Can't place a break statement here\n"); + fprintf(stderr, ERROR "ERROR: Can't place a break statement here\n" RESET); break; } @@ -460,7 +460,7 @@ static void writeCompilerWithJumps(Compiler* compiler, Node* node, void* breakAd case NODE_PATH_CONTINUE: { if (!continueAddressesPtr) { - fprintf(stderr, "Can't place a continue statement here\n"); + fprintf(stderr, ERROR "ERROR: Can't place a continue statement here\n" RESET); break; } @@ -730,7 +730,7 @@ unsigned char* collateCompiler(Compiler* compiler, int* size) { break; default: - fprintf(stderr, "[Internal] Unknown literal type encountered within literal cache: %d\n", compiler->literalCache.literals[i].type); + fprintf(stderr, ERROR "[internal] Unknown literal type encountered within literal cache: %d\n" RESET, compiler->literalCache.literals[i].type); return NULL; } } diff --git a/source/interpreter.c b/source/interpreter.c index 3657d40..999e38b 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -8,14 +8,14 @@ #include static void stdoutWrapper(const char* output) { - fprintf(stdout, "%s", output); - fprintf(stdout, "\n"); //default new line + printf("%s", output); + printf("\n"); //default new line } static void stderrWrapper(const char* output) { - fprintf(stderr, "Assertion failure: "); + fprintf(stderr, ERROR "Assertion failure: "); fprintf(stderr, "%s", output); - fprintf(stderr, "\n"); //default new line + fprintf(stderr, "\n" RESET); //default new line } void initInterpreter(Interpreter* interpreter) { @@ -114,9 +114,9 @@ static bool parseIdentifierToValue(Interpreter* interpreter, Literal* literalPtr //this converts identifiers to values if (IS_IDENTIFIER(*literalPtr)) { if (!getScopeVariable(interpreter->scope, *literalPtr, literalPtr)) { - printf("Undeclared variable \"");; + printf(ERROR "Error: Undeclared variable \"");; printLiteral(*literalPtr); - printf("\"\n"); + printf("\"\n" RESET); return false; } } @@ -131,9 +131,9 @@ static bool execAssert(Interpreter* interpreter) { parseIdentifierToValue(interpreter, &lhs); if (!IS_STRING(rhs)) { - printf("The assert keyword needs a string as the second argument, received: "); + printf(ERROR "ERROR: The assert keyword needs a string as the second argument, received: "); printLiteral(rhs); - printf("\n"); + printf("\n" RESET); return false; } @@ -203,9 +203,9 @@ static bool execNegate(Interpreter* interpreter) { lit = TO_FLOAT_LITERAL(-AS_FLOAT(lit)); } else { - printf("[internal] The interpreter can't negate that literal: "); + printf(ERROR "[internal] The interpreter can't negate that literal: "); printLiteral(lit); - printf("\n"); + printf("\n" RESET); return false; } @@ -225,9 +225,9 @@ static bool execInvert(Interpreter* interpreter) { lit = TO_BOOLEAN_LITERAL(!AS_BOOLEAN(lit)); } else { - printf("[internal] The interpreter can't invert that literal: "); + printf(ERROR "[internal] The interpreter can't invert that literal: "); printLiteral(lit); - printf("\n"); + printf("\n" RESET); return false; } @@ -246,7 +246,7 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) { if (IS_STRING(lhs) && IS_STRING(rhs)) { //check for overflow if (STRLEN(lhs) + STRLEN(rhs) > MAX_STRING_LENGTH) { - printf("Can't concatenate these strings (result is too long)\n"); + printf(ERROR "ERROR: Can't concatenate these strings (result is too long)\n" RESET); return false; } @@ -284,7 +284,7 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) { case OP_DIVISION: if (AS_INTEGER(rhs) == 0) { - printf("Can't divide by zero (error found in interpreter)"); + printf(ERROR "ERROR: Can't divide by zero (error found in interpreter)" RESET); return false; } pushLiteralArray(&interpreter->stack, TO_INTEGER_LITERAL( AS_INTEGER(lhs) / AS_INTEGER(rhs) )); @@ -292,7 +292,7 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) { case OP_MODULO: if (AS_INTEGER(rhs) == 0) { - printf("Can't modulo by zero (error found in interpreter)"); + printf(ERROR "ERROR: Can't modulo by zero (error found in interpreter)" RESET); return false; } pushLiteralArray(&interpreter->stack, TO_INTEGER_LITERAL( AS_INTEGER(lhs) % AS_INTEGER(rhs) )); @@ -306,7 +306,7 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) { //catch bad modulo if (opcode == OP_MODULO) { - printf("Bad arithmetic argument (modulo on floats not allowed)\n"); + printf(ERROR "ERROR: Bad arithmetic argument (modulo on floats not allowed)\n" RESET); return false; } @@ -326,24 +326,24 @@ static bool execArithmetic(Interpreter* interpreter, Opcode opcode) { case OP_DIVISION: if (AS_FLOAT(rhs) == 0) { - printf("Can't divide by zero (error found in interpreter)"); + printf(ERROR "ERROR: Can't divide by zero (error found in interpreter)" RESET); return false; } pushLiteralArray(&interpreter->stack, TO_FLOAT_LITERAL( AS_FLOAT(lhs) / AS_FLOAT(rhs) )); return true; default: - printf("[internal] bad opcode argument passed to execArithmetic()"); + printf(ERROR "[internal] bad opcode argument passed to execArithmetic()" RESET); return false; } } //wrong types - printf("Bad arithmetic argument "); + printf(ERROR "ERROR: Bad arithmetic argument "); printLiteral(lhs); printf(" and "); printLiteral(rhs); - printf("\n"); + printf("\n" RESET); return false; } @@ -367,9 +367,9 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) { parseIdentifierToValue(interpreter, &type); if (!declareScopeVariable(interpreter->scope, identifier, type)) { - printf("Can't redefine the variable \""); + printf(ERROR "ERROR: Can't redefine the variable \""); printLiteral(identifier); - printf("\"\n"); + printf("\"\n" RESET); return false; } @@ -377,9 +377,9 @@ static bool execVarDecl(Interpreter* interpreter, bool lng) { parseIdentifierToValue(interpreter, &val); if (!IS_NULL(val) && !setScopeVariable(interpreter->scope, identifier, val, false)) { - printf("Incorrect type assigned to variable \""); + printf(ERROR "ERROR: Incorrect type assigned to variable \""); printLiteral(identifier); - printf("\"\n"); + printf("\"\n" RESET); return false; } @@ -393,23 +393,23 @@ static bool execVarAssign(Interpreter* interpreter) { parseIdentifierToValue(interpreter, &rhs); if (!IS_IDENTIFIER(lhs)) { - printf("Can't assign to a non-variable \""); + printf(ERROR "ERROR: Can't assign to a non-variable \""); printLiteral(lhs); - printf("\"\n"); + printf("\"\n" RESET); return false; } if (!isDelcaredScopeVariable(interpreter->scope, lhs)) { - printf("Undeclared variable \""); + printf(ERROR "ERROR: Undeclared variable \""); printLiteral(lhs); - printf("\"\n"); + printf("\"\n" RESET); return false; } if (!setScopeVariable(interpreter->scope, lhs, rhs, true)) { - printf("Incorrect type assigned to variable \""); + printf(ERROR "ERROR Incorrect type assigned to variable \""); printLiteral(lhs); - printf("\"\n"); + printf("\"\n" RESET); return false; } @@ -427,7 +427,7 @@ static bool execValCast(Interpreter* interpreter) { Literal result = TO_NULL_LITERAL; if (IS_NULL(value)) { - printf("Can't cast a null value\n"); + printf(ERROR "ERROR: Can't cast a null value\n" RESET); return false; } @@ -488,7 +488,7 @@ static bool execValCast(Interpreter* interpreter) { break; default: - printf("Unknown cast type found %d, terminating\n", AS_TYPE(type).typeOf); + printf(ERROR"ERROR: Unknown cast type found %d, terminating\n" RESET, AS_TYPE(type).typeOf); return false; } @@ -524,16 +524,16 @@ static bool execCompareLess(Interpreter* interpreter, bool invert) { //not a number, return falure if (!(IS_INTEGER(lhs) || IS_FLOAT(lhs))) { - printf("Incorrect type in comparison, value \""); + printf(ERROR "ERROR: Incorrect type in comparison, value \""); printLiteral(lhs); - printf("\"\n"); + printf("\"\n" RESET); return false; } if (!(IS_INTEGER(rhs) || IS_FLOAT(rhs))) { - printf("Incorrect type in comparison, value \""); + printf(ERROR "ERROR: Incorrect type in comparison, value \""); printLiteral(rhs); - printf("\"\n"); + printf("\"\n" RESET); return false; } @@ -569,16 +569,16 @@ static bool execCompareLessEqual(Interpreter* interpreter, bool invert) { //not a number, return falure if (!(IS_INTEGER(lhs) || IS_FLOAT(lhs))) { - printf("Incorrect type in comparison, value \""); + printf(ERROR "ERROR: Incorrect type in comparison, value \""); printLiteral(lhs); - printf("\"\n"); + printf("\"\n" RESET); return false; } if (!(IS_INTEGER(rhs) || IS_FLOAT(rhs))) { - printf("Incorrect type in comparison, value \""); + printf(ERROR "ERROR: Incorrect type in comparison, value \""); printLiteral(rhs); - printf("\"\n"); + printf("\"\n" RESET); return false; } @@ -609,7 +609,7 @@ static bool execJump(Interpreter* interpreter) { int target = (int)readShort(interpreter->bytecode, &interpreter->count); if (target + interpreter->codeStart > interpreter->length) { - printf("Jump out of range\n"); + printf(ERROR "[internal] Jump out of range\n" RESET); return false; } @@ -623,7 +623,7 @@ static bool execFalseJump(Interpreter* interpreter) { int target = (int)readShort(interpreter->bytecode, &interpreter->count); if (target + interpreter->codeStart > interpreter->length) { - printf("Jump out of range (false jump)\n"); + printf(ERROR "[internal] Jump out of range (false jump)\n" RESET); return false; } @@ -635,7 +635,7 @@ static bool execFalseJump(Interpreter* interpreter) { } if (IS_NULL(lit)) { - printf("Null detected in comparison\n"); + printf(ERROR "Error: Null detected in comparison\n" RESET); return false; } @@ -785,7 +785,7 @@ static void execInterpreter(Interpreter* interpreter) { break; default: - printf("Unknown opcode found %d, terminating\n", opcode); + printf(ERROR "Error: Unknown opcode found %d, terminating\n" RESET, opcode); printLiteralArray(&interpreter->stack, "\n"); return; } diff --git a/source/literal.c b/source/literal.c index dace79b..8f8cba1 100644 --- a/source/literal.c +++ b/source/literal.c @@ -4,6 +4,8 @@ #include "literal_array.h" #include "literal_dictionary.h" +#include "console_colors.h" + #include #include @@ -265,7 +267,7 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) { default: //should never be seen - fprintf(stderr, "[Internal] Unrecognized literal type in print type: %d\n", AS_TYPE(literal).typeOf); + fprintf(stderr, ERROR "[internal] Unrecognized literal type in print type: %d\n" RESET, AS_TYPE(literal).typeOf); } //const (printed last) @@ -293,7 +295,7 @@ void printLiteralCustom(Literal literal, void (printFn)(const char*)) { default: //should never bee seen - fprintf(stderr, "[Internal] Unrecognized literal type in print: %d\n", literal.type); + fprintf(stderr, ERROR "[internal] Unrecognized literal type in print: %d\n" RESET, literal.type); } } @@ -327,7 +329,7 @@ void freeLiteral(Literal literal) { bool _isTruthy(Literal x) { if (IS_NULL(x)) { - fprintf(stderr, "Null is neither true nor false"); + fprintf(stderr, ERROR "ERROR: Null is neither true nor false" RESET); return false; } @@ -476,7 +478,7 @@ bool literalsAreEqual(Literal lhs, Literal rhs) { default: //should never bee seen - fprintf(stderr, "[Internal] Unrecognized literal type in equality: %d\n", lhs.type); + fprintf(stderr, ERROR "[internal] Unrecognized literal type in equality: %d\n" RESET, lhs.type); return false; } } @@ -531,7 +533,7 @@ int hashLiteral(Literal lit) { default: //should never bee seen - fprintf(stderr, "[Internal] Unrecognized literal type in hash: %d\n", lit.type); + fprintf(stderr, ERROR "[internal] Unrecognized literal type in hash: %d\n" RESET, lit.type); return 0; } } diff --git a/source/literal_dictionary.c b/source/literal_dictionary.c index f907709..e63f1c3 100644 --- a/source/literal_dictionary.c +++ b/source/literal_dictionary.c @@ -2,6 +2,8 @@ #include "memory.h" +#include "console_colors.h" + #include //util functions @@ -173,7 +175,7 @@ void freeLiteralDictionary(LiteralDictionary* dictionary) { void setLiteralDictionary(LiteralDictionary* dictionary, Literal key, Literal value) { if (IS_NULL(key)) { - fprintf(stderr, "[Internal] Dictionaries can't have null keys\n"); + fprintf(stderr, ERROR "[internal] Dictionaries can't have null keys\n" RESET); return; } @@ -186,7 +188,7 @@ void setLiteralDictionary(LiteralDictionary* dictionary, Literal key, Literal va Literal getLiteralDictionary(LiteralDictionary* dictionary, Literal key) { if (IS_NULL(key)) { - fprintf(stderr, "[Internal] Dictionaries can't have null keys\n"); + fprintf(stderr, ERROR "[internal] Dictionaries can't have null keys\n" RESET); return TO_NULL_LITERAL; } @@ -202,7 +204,7 @@ Literal getLiteralDictionary(LiteralDictionary* dictionary, Literal key) { void removeLiteralDictionary(LiteralDictionary* dictionary, Literal key) { if (IS_NULL(key)) { - fprintf(stderr, "[Internal] Dictionaries can't have null keys\n"); + fprintf(stderr, ERROR "[internal] Dictionaries can't have null keys\n" RESET); return; } diff --git a/source/memory.c b/source/memory.c index ac7c528..5b46930 100644 --- a/source/memory.c +++ b/source/memory.c @@ -1,5 +1,7 @@ #include "memory.h" +#include "console_colors.h" + #include #include @@ -13,7 +15,7 @@ void* reallocate(void* pointer, size_t oldSize, size_t newSize) { void* mem = realloc(pointer, newSize); if (mem == NULL) { - fprintf(stderr, "[Internal]Memory allocation error (requested %d for %d, replacing %d)\n", (int)newSize, (int)pointer, (int)oldSize); + fprintf(stderr, ERROR "[internal]Memory allocation error (requested %d for %d, replacing %d)\n" ERROR, (int)newSize, (int)pointer, (int)oldSize); exit(-1); } diff --git a/source/parser.c b/source/parser.c index 3387cc4..5642ad5 100644 --- a/source/parser.c +++ b/source/parser.c @@ -15,7 +15,7 @@ static void error(Parser* parser, Token token, const char* message) { //keep going while panicing if (parser->panic) return; - fprintf(stderr, "[Line %d] Error", token.line); + fprintf(stderr, ERROR "[Line %d] Error", token.line); //check type if (token.type == TOKEN_EOF) { @@ -27,7 +27,7 @@ static void error(Parser* parser, Token token, const char* message) { } //finally - fprintf(stderr, ": %s\n", message); + fprintf(stderr, ": %s\n" RESET, message); parser->error = true; parser->panic = true; } @@ -60,7 +60,7 @@ static void consume(Parser* parser, TokenType tokenType, const char* msg) { static void synchronize(Parser* parser) { if (command.verbose) { - printf(ERROR "synchronizing\n" RESET); + fprintf(stderr, ERROR "synchronizing\n" RESET); } while (parser->current.type != TOKEN_EOF) { @@ -244,7 +244,7 @@ static Opcode string(Parser* parser, Node** nodeHandle) { if (length > MAX_STRING_LENGTH) { length = MAX_STRING_LENGTH; char buffer[256]; - snprintf(buffer, 256, "Strings can only be a maximum of %d characters long", MAX_STRING_LENGTH); + snprintf(buffer, 256, ERROR "Strings can only be a maximum of %d characters long" RESET, MAX_STRING_LENGTH); error(parser, parser->previous, buffer); } @@ -781,14 +781,14 @@ static bool calcStaticBinaryArithmetic(Parser* parser, Node** nodeHandle) { break; default: - printf("[internal] bad opcode argument passed to calcStaticBinaryArithmetic()"); + error(parser, parser->previous, "[internal] bad opcode argument passed to calcStaticBinaryArithmetic()"); return false; } } //catch bad modulo if ((IS_FLOAT(lhs) || IS_FLOAT(rhs)) && (*nodeHandle)->binary.opcode == OP_MODULO) { - printf("Bad arithmetic argument (modulo on floats not allowed)"); + error(parser, parser->previous, "Bad arithmetic argument (modulo on floats not allowed)"); return false; } @@ -839,7 +839,7 @@ static bool calcStaticBinaryArithmetic(Parser* parser, Node** nodeHandle) { break; default: - printf("[internal] bad opcode argument passed to calcStaticBinaryArithmetic()"); + error(parser, parser->previous, "[internal] bad opcode argument passed to calcStaticBinaryArithmetic()"); return false; } } diff --git a/source/repl_main.c b/source/repl_main.c index d7365f6..166565c 100644 --- a/source/repl_main.c +++ b/source/repl_main.c @@ -16,7 +16,7 @@ char* readFile(char* path, size_t* fileSize) { FILE* file = fopen(path, "rb"); if (file == NULL) { - fprintf(stderr, "Could not open file \"%s\"\n", path); + fprintf(stderr, ERROR "Could not open file \"%s\"\n" RESET, path); exit(-1); } @@ -27,7 +27,7 @@ char* readFile(char* path, size_t* fileSize) { char* buffer = (char*)malloc(*fileSize + 1); if (buffer == NULL) { - fprintf(stderr, "Not enough memory to read \"%s\"\n", path); + fprintf(stderr, ERROR "Not enough memory to read \"%s\"\n" RESET, path); exit(-1); } @@ -36,7 +36,7 @@ char* readFile(char* path, size_t* fileSize) { buffer[*fileSize] = '\0'; //NOTE: fread doesn't append this if (bytesRead < *fileSize) { - fprintf(stderr, "Could not read file \"%s\"\n", path); + fprintf(stderr, ERROR "Could not read file \"%s\"\n" RESET, path); exit(-1); } @@ -49,14 +49,14 @@ void writeFile(char* path, unsigned char* bytes, size_t size) { FILE* file = fopen(path, "wb"); if (file == NULL) { - fprintf(stderr, "Could not open file \"%s\"\n", path); + fprintf(stderr, ERROR "Could not open file \"%s\"\n" RESET, path); exit(-1); } int written = fwrite(bytes, size, 1, file); if (written != 1) { - fprintf(stderr, "Could not write file \"%s\"\n", path); + fprintf(stderr, ERROR "Could not write file \"%s\"\n" RESET, path); exit(-1); }