From 7717a40574d41fc85d249ac78ae8faad7a6097bf Mon Sep 17 00:00:00 2001 From: DiamondNether90 Date: Mon, 19 Jan 2026 21:14:48 +1100 Subject: [PATCH] Error function --- src/interpreter.c | 31 ++++++++++++++++++++++++++++++- src/parser.c | 1 + src/types.c | 3 +++ src/types.h | 2 +- tests/error.grnd | 2 ++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/error.grnd diff --git a/src/interpreter.c b/src/interpreter.c index 5e6ac0a..07f73f3 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -13,6 +13,23 @@ int currentInstruction = 0; bool isMainScopeGlobal = true; +[[noreturn]] void customError(GroundArg type, GroundArg what, GroundInstruction* where, int whereLine, int exitCode) { + printf("Ground runtime error:\n ErrorType: "); + printGroundArg(&type); + printf("\n ErrorContext: "); + printGroundArg(&what); + printf("\n"); + if (where != NULL) { + printf(" ErrorInstruction: "); + printGroundInstruction(where); + printf("\n"); + } + if (whereLine > -1) { + printf(" ErrorLine: %d\n", whereLine + 1); + } + exit(exitCode); +} + [[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine) { printf("Ground runtime error:\n ErrorType: "); switch (error) { @@ -813,7 +830,19 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop } break; } - + case ERRORCMD: { + if (in->args.length < 3) { + runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction); + } else if (in->args.length > 3) { + runtimeError(TOO_MANY_ARGS, "Expecting 3 args", in, currentInstruction); + } + if (in->args.args[2].value.value.type != INT ) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting an int for arg 2", in, currentInstruction); + } + + customError(in->args.args[0], in->args.args[1], in, currentInstruction, in->args.args[2].value.value.data.intVal); + break; + } /* * VARIABLES AND LISTS * These instructions are for initializing variables and lists. diff --git a/src/parser.c b/src/parser.c index d956934..57a3395 100644 --- a/src/parser.c +++ b/src/parser.c @@ -137,6 +137,7 @@ static GroundInstType getInstructionType(const char* inst) { if (strcmp(inst, "input") == 0 || strcmp(inst, "stdin") == 0) return INPUT; if (strcmp(inst, "print") == 0 || strcmp(inst, "stdout") == 0) return PRINT; if (strcmp(inst, "println") == 0 || strcmp(inst, "stdlnout") == 0) return PRINTLN; + if (strcmp(inst, "error") == 0) return ERRORCMD; if (strcmp(inst, "set") == 0) return SET; if (strcmp(inst, "gettype") == 0) return GETTYPE; if (strcmp(inst, "exists") == 0) return EXISTS; diff --git a/src/types.c b/src/types.c index 01becb4..334a466 100644 --- a/src/types.c +++ b/src/types.c @@ -412,6 +412,9 @@ void printGroundInstruction(GroundInstruction* gi) { break; case CREATELABEL: break; + case ERRORCMD: + printf("error"); + break; default: printf("FIXME"); break; diff --git a/src/types.h b/src/types.h index 0b3c903..65690dc 100644 --- a/src/types.h +++ b/src/types.h @@ -9,7 +9,7 @@ #include "include/uthash.h" typedef enum GroundInstType { - IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP + IF, JUMP, END, INPUT, PRINT, PRINTLN, SET, GETTYPE, EXISTS, SETLIST, SETLISTAT, GETLISTAT, GETLISTSIZE, LISTAPPEND, GETSTRSIZE, GETSTRCHARAT, ADD, SUBTRACT, MULTIPLY, DIVIDE, EQUAL, INEQUAL, NOT, GREATER, LESSER, STOI, STOD, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD } GroundInstType; typedef enum GroundValueType { diff --git a/tests/error.grnd b/tests/error.grnd new file mode 100644 index 0000000..460bb30 --- /dev/null +++ b/tests/error.grnd @@ -0,0 +1,2 @@ +setlist &dat 1 2 3 "Hi!" +error "Hello" $dat 1 -- 2.47.3