From b348ca7626064060c040dad76ded75f5aa33248e Mon Sep 17 00:00:00 2001 From: DiamondNether90 Date: Sat, 24 Jan 2026 18:18:52 +1100 Subject: [PATCH] More type conversions --- src/interpreter.c | 32 ++++++++++++++++++++++++++++++++ src/parser.c | 2 ++ src/types.c | 6 ++++++ src/types.h | 2 +- tests/convs.grnd | 4 ++++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/interpreter.c b/src/interpreter.c index 02d90a3..a3246e1 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -1126,6 +1126,38 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop addVariable(scope->variables, in->args.args[1].value.refName, createDoubleGroundValue(atof(in->args.args[0].value.value.data.stringVal))); break; } + case ITOC: { + if (in->args.length < 2) { + runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction); + } + if (in->args.length > 2) { + runtimeError(TOO_MANY_ARGS, "Expecting 2 args", in, currentInstruction); + } + if (in->args.args[0].type != VALUE || in->args.args[0].value.value.type != INT) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 1", in, currentInstruction); + } + if (in->args.args[1].type != DIRREF) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction); + } + addVariable(scope->variables, in->args.args[1].value.refName, createCharGroundValue((char)in->args.args[0].value.value.data.intVal)); + break; + } + case CTOI: { + if (in->args.length < 2) { + runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction); + } + if (in->args.length > 2) { + runtimeError(TOO_MANY_ARGS, "Expecting 2 args", in, currentInstruction); + } + if (in->args.args[0].type != VALUE || in->args.args[0].value.value.type != CHAR) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a Char for arg 1", in, currentInstruction); + } + if (in->args.args[1].type != DIRREF) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction); + } + addVariable(scope->variables, in->args.args[1].value.refName, createIntGroundValue((int)in->args.args[0].value.value.data.charVal)); + break; + } case TOSTRING: { if (in->args.length < 2) { runtimeError(TOO_FEW_ARGS, "Expecting 2 args", in, currentInstruction); diff --git a/src/parser.c b/src/parser.c index 59a2b29..7cfad14 100644 --- a/src/parser.c +++ b/src/parser.c @@ -159,6 +159,8 @@ static GroundInstType getInstructionType(const char* inst) { if (strcmp(inst, "lesser") == 0) return LESSER; if (strcmp(inst, "stoi") == 0) return STOI; if (strcmp(inst, "stod") == 0) return STOD; + if (strcmp(inst, "ctoi") == 0) return CTOI; + if (strcmp(inst, "itoc") == 0) return ITOC; if (strcmp(inst, "tostring") == 0) return TOSTRING; if (strcmp(inst, "fun") == 0) return FUN; if (strcmp(inst, "return") == 0) return RETURN; diff --git a/src/types.c b/src/types.c index b8adf83..19b1903 100644 --- a/src/types.c +++ b/src/types.c @@ -413,6 +413,12 @@ void printGroundInstruction(GroundInstruction* gi) { case STOD: printf("stod"); break; + case CTOI: + printf("ctoi"); + break; + case ITOC: + printf("itoc"); + break; case TOSTRING: printf("tostring"); break; diff --git a/src/types.h b/src/types.h index a288cc8..b4e3d40 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, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD + 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, ITOC, CTOI, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD } GroundInstType; typedef enum GroundValueType { diff --git a/tests/convs.grnd b/tests/convs.grnd index 44d0988..e00121e 100644 --- a/tests/convs.grnd +++ b/tests/convs.grnd @@ -1,7 +1,11 @@ tostring 32 &str stoi "12" &int stod "3.14" &dou +itoc 353 &chr +ctoi 'a' &it2 println $str println $int println $dou +println $chr +println $it2 -- 2.47.3