Scope& Scope::AppendScope(const std::string& key) { Datum* datum = Find(key); //TODO: Push Back when removing from Vector //Found key, check if type is table if (datum) { if (datum->IsExternal() || (datum->GetType() != Datum::DatumType::Table)) { throw std::exception("This Datum is not of the right type"); } Scope* newScope = new Scope(); newScope->mParent = this; datum->Set(newScope, datum->Size()); return *datum->GetScope(datum->Size() - 1); } else { //key not found Datum& d = Append(key, Datum::DatumType::Table); Scope* newScope = new Scope(); newScope->mParent = this; d.Set(newScope, 0); return *d.GetScope(0); } }
void Scope::Adopt(Scope& childToAdopt, const std::string& key, const std::uint32_t index) { if (childToAdopt.GetParent()) childToAdopt.Orphan(); Datum * foundDatum = Find(key); if (foundDatum) { if ((foundDatum->GetType() != Datum::DatumType::Table) || foundDatum->IsExternal()) throw std::exception("You are trying to adopt into a invalid scope"); if ((foundDatum->GetType() == Datum::DatumType::Table)) { if (index >= foundDatum->Size()) { foundDatum->Set(&childToAdopt, index); foundDatum->GetScope(index)->mParent = this; } else { Scope* temp = foundDatum->GetScope(index); foundDatum->Set(&childToAdopt, index); foundDatum->GetScope(index)->mParent = this; for (std::uint32_t i = index + 1; i < foundDatum->Size(); ++i) { Scope* temp2 = foundDatum->GetScope(i); foundDatum->Set(temp, i); temp = temp2; } foundDatum->Set(temp, foundDatum->Size()); } } } else { Datum& d = Append(key, Datum::DatumType::Table); d.Set(&childToAdopt); d.GetScope()->mParent = this; } }