diff --git a/source/script/StringClass.c b/source/script/StringClass.c index c534d46..40e1903 100644 --- a/source/script/StringClass.c +++ b/source/script/StringClass.c @@ -1,6 +1,7 @@ #include "StringClass.h" #include "compat.h" #include "intClass.h" +#include "scriptError.h" #include char* getStringValue(Variable_t* var) { @@ -57,6 +58,19 @@ ClassFunction(stringBytes) { return copyVariableToPtr(v); } +ClassFunction(stringIndexGet) { + u32 len = strlen(caller->string.value); + u32 idx = args[0]->integer.value; + if (len < idx || idx < 0) { + SCRIPT_FATAL_ERR("Index of string out of range"); + } + + char* a = calloc(1,2); + a[0] = caller->string.value[idx]; + return newStringVariablePtr(a, 0, 0); +} + +u8 strOneIntArg[] = { IntClass }; u8 oneStringArg[] = { StringClass }; ClassFunctionTableEntry_t stringFunctions[] = { @@ -64,6 +78,7 @@ ClassFunctionTableEntry_t stringFunctions[] = { {"+", addStringVariables, 1, oneStringArg }, {"len", getStringLength, 0, 0}, {"bytes", stringBytes, 0, 0}, + {"get", stringIndexGet, 1, strOneIntArg}, }; Variable_t getStringMember(Variable_t* var, char* memberName) { diff --git a/source/script/genericClass.c b/source/script/genericClass.c index 7b9b412..695961c 100644 --- a/source/script/genericClass.c +++ b/source/script/genericClass.c @@ -208,7 +208,6 @@ Variable_t getGenericFunctionMember(Variable_t* var, char* memberName, ClassFunc } } - printScriptError(SCRIPT_FATAL, "Could not find member of class"); return (Variable_t){ 0 }; } @@ -216,8 +215,9 @@ Variable_t* callMemberFunction(Variable_t* var, char* memberName, CallArgs_t* ar for (u32 i = 0; i < ARRAY_SIZE(memberGetters); i++) { if (var->variableType == memberGetters[i].classType) { Variable_t funcRef = memberGetters[i].func(var, memberName); - if (funcRef.variableType == None) - return NULL; + if (funcRef.variableType == None) { + SCRIPT_FATAL_ERR("Did not find member '%s'", memberName); + } Variable_t* ptr = &funcRef; if (funcRef.variableType == ReferenceType) {