diff --git a/src/sfall_arrays.cc b/src/sfall_arrays.cc index ec61d03..002250d 100644 --- a/src/sfall_arrays.cc +++ b/src/sfall_arrays.cc @@ -53,9 +53,8 @@ public: return SFallScriptValue(0); }; if (index == -1) { // special index to indicate if array is associative - throw(std::invalid_argument("Not implemented yet")); + return SFallScriptValue(0); }; - // TODO: assoc return SFallScriptValue(index); } @@ -105,25 +104,24 @@ public: 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); + if (index < -1 || index > size()) { + return SFallScriptValue(0); + }; + if (index == -1) { // special index to indicate if array is associative + return SFallScriptValue(1); + }; + + return keys[index]; } ProgramValue GetArray(const SFallScriptValue& key) { - // TODO assoc + auto iter = map.find(key); + if (iter == map.end()) { + return SFallScriptValue(0); + }; - // auto element_index = key.asInt(); - // if (element_index < 0 || element_index >= size()) { - // return SFallScriptValue(0); - // }; - // return values[element_index]; + return iter->second; } void SetArray(const SFallScriptValue& key, const SFallScriptValue& val, bool allowUnset) diff --git a/src/sfall_script_value.cc b/src/sfall_script_value.cc index 5ef45bf..d6a059a 100644 --- a/src/sfall_script_value.cc +++ b/src/sfall_script_value.cc @@ -25,7 +25,6 @@ SFallScriptValue::SFallScriptValue(ProgramValue& value) switch (opcode) { case VALUE_TYPE_DYNAMIC_STRING: case VALUE_TYPE_STRING: - // TODO: Copy string pointerValue = value.pointerValue; break; case VALUE_TYPE_PTR: @@ -67,4 +66,24 @@ int SFallScriptValue::asInt() const } } +bool SFallScriptValue::operator<(SFallScriptValue const& other) const +{ + if (opcode != other.opcode) { + return opcode < other.opcode; + } + + switch (opcode) { + case VALUE_TYPE_DYNAMIC_STRING: + case VALUE_TYPE_STRING: + case VALUE_TYPE_PTR: + return pointerValue < other.pointerValue; + case VALUE_TYPE_INT: + return integerValue < other.integerValue; + case VALUE_TYPE_FLOAT: + return floatValue < other.floatValue; + default: + throw(std::exception()); + } +} + } \ No newline at end of file diff --git a/src/sfall_script_value.h b/src/sfall_script_value.h index 32ce1a5..0d6405d 100644 --- a/src/sfall_script_value.h +++ b/src/sfall_script_value.h @@ -17,6 +17,8 @@ public: bool isFloat() const; bool isPointer() const; int asInt() const; + + bool operator<(SFallScriptValue const& other) const; }; }