From 55774c251861c6b980c36df6e9bcadf1c4d27a0d Mon Sep 17 00:00:00 2001 From: Vasilii Rogin Date: Thu, 20 Apr 2023 01:20:05 +0300 Subject: [PATCH] Add get_array_key --- src/sfall_arrays.cc | 30 ++++++++++++++++++++++++++++++ src/sfall_opcodes.cc | 4 ++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/sfall_arrays.cc b/src/sfall_arrays.cc index 7bf4aad..c561151 100644 --- a/src/sfall_arrays.cc +++ b/src/sfall_arrays.cc @@ -19,6 +19,11 @@ public: opcode = VALUE_TYPE_INT; integerValue = 0; } + SFallArrayElement(int value) + { + opcode = VALUE_TYPE_INT; + integerValue = value; + } }; #define ARRAYFLAG_ASSOC (1) // is map @@ -41,6 +46,10 @@ public: data.resize(len); } std::vector data; + int size() + { + return data.size(); + } }; using ArraysMap = std::unordered_map; @@ -77,4 +86,25 @@ ArrayId CreateTempArray(int len, uint32_t flags) return array_id; } +static SFallArray* get_array_by_id(ArrayId array_id) +{ + auto iter = arrays.find(array_id); + if (iter == arrays.end()) { + return nullptr; + }; + return &iter->second; +} + +ProgramValue GetArrayKey(ArrayId array_id, int index) +{ + auto arr = get_array_by_id(array_id); + if (arr == nullptr || index < -1 || index > arr->size()) { + return SFallArrayElement(0); + }; + if (index == -1) { // special index to indicate if array is associative + throw(std::invalid_argument("Not implemented yet")); + }; + return SFallArrayElement(index); +} + } \ No newline at end of file diff --git a/src/sfall_opcodes.cc b/src/sfall_opcodes.cc index 16a5afa..6c92042 100644 --- a/src/sfall_opcodes.cc +++ b/src/sfall_opcodes.cc @@ -294,8 +294,8 @@ static void opGetArrayKey(Program* program) { auto index = programStackPopInteger(program); auto arrayId = programStackPopInteger(program); - - programStackPushInteger(program, 33); + auto value = GetArrayKey(arrayId, index); + programStackPushValue(program, value); } // create_array