Add string operations
This commit is contained in:
@@ -696,6 +696,54 @@ void interpretGroundInstruction(GroundInstruction inst, GroundScope* scope) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* STRING OPERATIONS
|
||||
* Allows easier manipulation of strings.
|
||||
* Instructions:
|
||||
* getstrcharat, getstrsize
|
||||
*/
|
||||
case GETSTRCHARAT: {
|
||||
if (in->args.length < 3) {
|
||||
runtimeError(TOO_FEW_ARGS, "Expecting 3 args", in, currentInstruction);
|
||||
}
|
||||
if (in->args.length > 3) {
|
||||
runtimeError(TOO_MANY_ARGS, "Expecting 3 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 != VALUE && in->args.args[1].value.value.type != INT) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting an Int for arg 2", in, currentInstruction);
|
||||
}
|
||||
if (in->args.args[2].type != DIRREF) {
|
||||
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 3", in, currentInstruction);
|
||||
}
|
||||
char* str = in->args.args[0].value.value.data.stringVal;
|
||||
int64_t idx = in->args.args[1].value.value.data.intVal;
|
||||
if (idx < strlen(str)) {
|
||||
addVariable(scope->variables, in->args.args[2].value.refName, createCharGroundValue(str[idx]));
|
||||
} else {
|
||||
runtimeError(STRING_ERROR, "Out of bounds index", in, currentInstruction);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GETSTRSIZE: {
|
||||
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(strlen(in->args.args[0].value.value.data.stringVal)));
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* COMPARISONS
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "include/uthash.h"
|
||||
|
||||
typedef enum GroundRuntimeError {
|
||||
ARG_TYPE_MISMATCH, TOO_FEW_ARGS, TOO_MANY_ARGS, UNKNOWN_LABEL, UNKNOWN_VARIABLE, LIST_ERROR, FIXME
|
||||
ARG_TYPE_MISMATCH, TOO_FEW_ARGS, TOO_MANY_ARGS, UNKNOWN_LABEL, UNKNOWN_VARIABLE, LIST_ERROR, STRING_ERROR, FIXME
|
||||
} GroundRuntimeError;
|
||||
|
||||
typedef struct GroundLabel {
|
||||
|
||||
Reference in New Issue
Block a user