From 7faccc9923a5e2a128b44ab59511a1745eb98d83 Mon Sep 17 00:00:00 2001 From: Vasilii Rogin Date: Sun, 14 May 2023 21:26:32 +0300 Subject: [PATCH] Add string subscript --- src/sfall_opcodes.cc | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/sfall_opcodes.cc b/src/sfall_opcodes.cc index c9a76bd..97d8d6e 100644 --- a/src/sfall_opcodes.cc +++ b/src/sfall_opcodes.cc @@ -568,16 +568,27 @@ static void opScanArray(Program* program) // get_array static void opGetArray(Program* program) { - // TODO: If type is string then do substr instead of array operation - auto key = programStackPopValue(program); auto arrayId = programStackPopValue(program); if (arrayId.isInt()) { auto value = GetArray(arrayId.integerValue, key); programStackPushValue(program, value); - } else if (arrayId.isString()) { - throw std::invalid_argument("String subscript is not implemented yet!"); + } else if (arrayId.isString() && key.isInt()) { + + auto& strVal = arrayId; + auto pos = key.asInt(); + auto str = programGetString(program, strVal.opcode, strVal.integerValue); + + // TODO: Is this works? + + char buf[2] = { 0 }; + if (pos < strlen(str)) { + buf[0] = str[pos]; + programPushString(program, buf); + } else { + programPushString(program, buf); + } } }