Allow delete statement for tables, sets and vectors

Relates to #3472. This allow "delete tbl" as an alternative for
clear_table(tbl). Also works for vectors.
This commit is contained in:
Arne Welzel 2024-02-21 17:36:20 +01:00
parent 6d86a48a6a
commit 2f1893bc58
7 changed files with 68 additions and 0 deletions

View file

@ -408,6 +408,24 @@ NameExpr::NameExpr(IDPtr arg_id, bool const_init) : Expr(EXPR_NAME), id(std::mov
#pragma GCC diagnostic pop
}
bool NameExpr::CanDel() const {
if ( IsError() )
return true; // avoid cascading the error report
return GetType()->Tag() == TYPE_TABLE || GetType()->Tag() == TYPE_VECTOR;
}
void NameExpr::Delete(Frame* f) {
if ( auto v = Eval(f) ) {
if ( GetType()->Tag() == TYPE_TABLE )
v->AsTableVal()->RemoveAll();
else if ( GetType()->Tag() == TYPE_VECTOR )
v->AsVectorVal()->Resize(0);
else
RuntimeError("delete unsupported");
}
}
// This isn't in-lined to avoid needing to pull in ID.h.
const IDPtr& NameExpr::IdPtr() const { return id; }