diff --git a/src/interpreter.c b/src/interpreter.c index b540286..709459e 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -2,6 +2,7 @@ #include "parser.h" #include "types.h" #include "include/uthash.h" +#include #include #include @@ -763,6 +764,107 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop break; } + case STOI: { + 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 != STRING) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a String 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[2].value.refName, createIntGroundValue(atoll(in->args.args[0].value.value.data.stringVal))); + break; + } + case STOD: { + 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 != STRING) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a String 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(atof(in->args.args[0].value.value.data.stringVal))); + break; + } + case TOSTRING: { + 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) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value for arg 1", in, currentInstruction); + } + if (in->args.args[1].type != DIRREF) { + runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction); + } + GroundValue* value = &in->args.args[0].value.value; + switch (value->type) { + case INT: { + char* buf = malloc(sizeof(char) * 256); + snprintf(buf, sizeof(char) * 256, "%" PRId64, value->data.intVal); + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf)); + break; + } + case DOUBLE: { + char* buf = malloc(sizeof(char) * 256); + snprintf(buf, sizeof(char) * 256, "%f", value->data.doubleVal); + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf)); + break; + } + case STRING: { + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(value->data.stringVal)); + break; + } + case CHAR: { + char* buf = malloc(sizeof(char) * 2); + buf[0] = value->data.charVal; + buf[1] = '\0'; + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf)); + break; + } + case BOOL: { + if (value->data.boolVal) { + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("true")); + } else { + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("false")); + } + break; + } + case LIST: { + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("")); + break; + } + case FUNCTION: { + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("")); + break; + } + case CUSTOM: { + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("")); + break; + } + case NONE: + default: { + addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("")); + break; + + } + } + + break; + } + /* * MATHS * These instructions allow running mathematical operations on values. @@ -1186,9 +1288,6 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop addVariable(scope->variables, in->args.args[1].value.refName, createBoolGroundValue(condition)); } - default: { - runtimeError(FIXME, "Currently unimplemented instruction", in, currentInstruction); - } case GREATER: { if (in->args.length < 3) { runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction); @@ -1368,6 +1467,9 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop currentInstruction = currentCurrentInstruction; break; } + default: { + runtimeError(FIXME, "Currently unimplemented instruction", in, currentInstruction); + } } freeGroundInstruction(in);