Adding assoc arrays

This commit is contained in:
Vasilii Rogin 2023-04-23 19:56:09 +03:00
parent a7800a6d49
commit d092ed31dc
3 changed files with 36 additions and 17 deletions

View File

@ -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)

View File

@ -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());
}
}
}

View File

@ -17,6 +17,8 @@ public:
bool isFloat() const;
bool isPointer() const;
int asInt() const;
bool operator<(SFallScriptValue const& other) const;
};
}