BIT-466: add redef += support to vectors

This commit is contained in:
Jon Siwek 2018-08-17 15:10:03 -05:00
parent 95c72f3717
commit bd24421734
8 changed files with 59 additions and 3 deletions

View file

@ -140,10 +140,11 @@ void ID::SetVal(Val* v, init_class c)
}
if ( type->Tag() != TYPE_TABLE &&
(type->Tag() != TYPE_PATTERN || c == INIT_REMOVE) )
(type->Tag() != TYPE_PATTERN || c == INIT_REMOVE) &&
(type->Tag() != TYPE_VECTOR || c == INIT_REMOVE) )
{
if ( c == INIT_EXTRA )
Error("+= initializer only applies to tables, sets and patterns", v);
Error("+= initializer only applies to tables, sets, vectors and patterns", v);
else
Error("-= initializer only applies to tables and sets", v);
}

View file

@ -3321,6 +3321,29 @@ bool VectorVal::AssignRepeat(unsigned int index, unsigned int how_many,
return true;
}
int VectorVal::AddTo(Val* val, int /* is_first_init */) const
{
if ( val->Type()->Tag() != TYPE_VECTOR )
{
val->Error("not a vector");
return 0;
}
VectorVal* v = val->AsVectorVal();
if ( ! same_type(type, v->Type()) )
{
type->Error("vector type clash", v->Type());
return 0;
}
auto last_idx = v->Size();
for ( auto i = 0u; i < Size(); ++i )
v->Assign(last_idx++, Lookup(i)->Ref());
return 1;
}
Val* VectorVal::Lookup(unsigned int index) const
{

View file

@ -1046,6 +1046,10 @@ public:
bool AssignRepeat(unsigned int index, unsigned int how_many,
Val* element);
// Add this value to the given value (if appropriate).
// Returns true if succcessful.
int AddTo(Val* v, int is_first_init) const override;
// Returns nil if no element was at that value.
// Lookup does NOT grow the vector to this size.
// The Val* variant assumes that the index Val* has been type-checked.