diff --git a/src/sfall_arrays.cc b/src/sfall_arrays.cc index 7626273..ec61d03 100644 --- a/src/sfall_arrays.cc +++ b/src/sfall_arrays.cc @@ -3,12 +3,12 @@ #include "sfall_script_value.h" #include +#include #include #include #include #include #include - namespace fallout { static ArrayId nextArrayID = 1; @@ -30,6 +30,9 @@ public: class SFallArrayList : public SFallArray { private: + // TODO: SFall copies strings + std::vector values; + public: SFallArrayList() = delete; @@ -39,9 +42,6 @@ public: mFlags = flags; } - // TODO: SFall copies strings - std::vector values; - int size() { return values.size(); @@ -84,6 +84,62 @@ public: } }; +class SFallArrayAssoc : public SFallArray { +private: + // TODO: SFall copies strings + std::vector keys; + std::map map; + +public: + SFallArrayAssoc() = delete; + + SFallArrayAssoc(uint32_t flags) + { + mFlags = flags; + } + + int size() + { + return keys.size(); + } + + ProgramValue GetArrayKey(int index) + { + // if (index < -1 || index > size()) { + // return SFallScriptValue(0); + // }; + // if (index == -1) { // special index to indicate if array is associative + // throw(std::invalid_argument("Not implemented yet")); + // }; + // // TODO: assoc + // return SFallScriptValue(index); + } + + ProgramValue GetArray(const SFallScriptValue& key) + { + // TODO assoc + + // auto element_index = key.asInt(); + // if (element_index < 0 || element_index >= size()) { + // return SFallScriptValue(0); + // }; + // return values[element_index]; + } + + void SetArray(const SFallScriptValue& key, const SFallScriptValue& val, bool allowUnset) + { + + // TODO: assoc + + // if (key.isInt()) { + // auto index = key.asInt(); + // if (index >= 0 && index < size()) { + // values[index] = key; + // } + // } + } +}; + using ArraysMap = std::unordered_map>; ArraysMap arrays; @@ -97,17 +153,19 @@ ArrayId CreateArray(int len, uint32_t flags) flags |= SFALL_ARRAYFLAG_ASSOC; // TODO: Implement throw(std::invalid_argument("Not implemented yet")); - }; - - if (len > ARRAY_MAX_SIZE) { + } else if (len > ARRAY_MAX_SIZE) { len = ARRAY_MAX_SIZE; // safecheck - } + }; ArrayId array_id = nextArrayID++; stackArrayId = array_id; - arrays.emplace(std::make_pair(array_id, std::make_unique(len, flags))); + if (flags & SFALL_ARRAYFLAG_ASSOC) { + arrays.emplace(std::make_pair(array_id, std::make_unique(flags))); + } else { + arrays.emplace(std::make_pair(array_id, std::make_unique(len, flags))); + } return array_id; }