2003-11-18 12:06:07 +00:00
|
|
|
#include "nixexpr.hh"
|
2003-11-18 11:22:29 +00:00
|
|
|
#include "storeexpr.hh"
|
2003-10-30 16:11:24 +00:00
|
|
|
|
|
|
|
|
2003-11-03 20:30:40 +00:00
|
|
|
ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct)
|
|
|
|
{
|
2004-01-21 14:49:32 +00:00
|
|
|
this->maxLoadPct = maxLoadPct;
|
2003-11-03 20:30:40 +00:00
|
|
|
table = ATtableCreate(initialSize, maxLoadPct);
|
|
|
|
if (!table) throw Error("cannot create ATerm table");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermMap::ATermMap(const ATermMap & map)
|
|
|
|
: table(0)
|
|
|
|
{
|
|
|
|
ATermList keys = map.keys();
|
|
|
|
|
|
|
|
/* !!! adjust allocation for load pct */
|
2004-01-21 14:49:32 +00:00
|
|
|
maxLoadPct = map.maxLoadPct;
|
|
|
|
table = ATtableCreate(ATgetLength(keys), maxLoadPct);
|
2003-11-03 20:30:40 +00:00
|
|
|
if (!table) throw Error("cannot create ATerm table");
|
|
|
|
|
2003-11-16 18:31:29 +00:00
|
|
|
for (ATermIterator i(keys); i; ++i)
|
|
|
|
set(*i, map.get(*i));
|
2003-11-03 20:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermMap::~ATermMap()
|
|
|
|
{
|
|
|
|
if (table) ATtableDestroy(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::set(ATerm key, ATerm value)
|
|
|
|
{
|
|
|
|
return ATtablePut(table, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::set(const string & key, ATerm value)
|
|
|
|
{
|
|
|
|
set(string2ATerm(key), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATerm ATermMap::get(ATerm key) const
|
|
|
|
{
|
|
|
|
return ATtableGet(table, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATerm ATermMap::get(const string & key) const
|
|
|
|
{
|
|
|
|
return get(string2ATerm(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::remove(ATerm key)
|
|
|
|
{
|
|
|
|
ATtableRemove(table, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ATermMap::remove(const string & key)
|
|
|
|
{
|
|
|
|
remove(string2ATerm(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATermList ATermMap::keys() const
|
|
|
|
{
|
|
|
|
ATermList keys = ATtableKeys(table);
|
|
|
|
if (!keys) throw Error("cannot query aterm map keys");
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ATerm string2ATerm(const string & s)
|
|
|
|
{
|
|
|
|
return (ATerm) ATmakeAppl0(ATmakeAFun((char *) s.c_str(), 0, ATtrue));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string aterm2String(ATerm t)
|
|
|
|
{
|
|
|
|
return ATgetName(ATgetAFun(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-30 16:11:24 +00:00
|
|
|
ATerm bottomupRewrite(TermFun & f, ATerm e)
|
|
|
|
{
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-10-30 16:11:24 +00:00
|
|
|
if (ATgetType(e) == AT_APPL) {
|
|
|
|
AFun fun = ATgetAFun(e);
|
|
|
|
int arity = ATgetArity(fun);
|
|
|
|
ATermList args = ATempty;
|
|
|
|
|
|
|
|
for (int i = arity - 1; i >= 0; i--)
|
|
|
|
args = ATinsert(args, bottomupRewrite(f, ATgetArgument(e, i)));
|
|
|
|
|
2003-11-03 11:59:35 +00:00
|
|
|
e = (ATerm) ATmakeApplList(fun, args);
|
2003-10-30 16:11:24 +00:00
|
|
|
}
|
|
|
|
|
2003-11-03 11:59:35 +00:00
|
|
|
else if (ATgetType(e) == AT_LIST) {
|
2003-10-30 16:11:24 +00:00
|
|
|
ATermList in = (ATermList) e;
|
|
|
|
ATermList out = ATempty;
|
|
|
|
|
2003-11-16 18:31:29 +00:00
|
|
|
for (ATermIterator i(in); i; ++i)
|
|
|
|
out = ATinsert(out, bottomupRewrite(f, *i));
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2003-11-03 11:59:35 +00:00
|
|
|
e = (ATerm) ATreverse(out);
|
2003-10-30 16:11:24 +00:00
|
|
|
}
|
|
|
|
|
2003-11-03 11:59:35 +00:00
|
|
|
return f(e);
|
2003-10-30 16:11:24 +00:00
|
|
|
}
|
2003-10-31 17:09:31 +00:00
|
|
|
|
|
|
|
|
2003-11-03 20:30:40 +00:00
|
|
|
void queryAllAttrs(Expr e, ATermMap & attrs)
|
2003-10-31 17:09:31 +00:00
|
|
|
{
|
2003-11-16 17:46:31 +00:00
|
|
|
ATMatcher m;
|
2003-10-31 17:09:31 +00:00
|
|
|
ATermList bnds;
|
2003-11-16 17:46:31 +00:00
|
|
|
if (!(atMatch(m, e) >> "Attrs" >> bnds))
|
2003-10-31 17:09:31 +00:00
|
|
|
throw badTerm("expected attribute set", e);
|
|
|
|
|
2003-11-16 18:31:29 +00:00
|
|
|
for (ATermIterator i(bnds); i; ++i) {
|
2003-11-16 17:46:31 +00:00
|
|
|
string s;
|
2003-10-31 17:09:31 +00:00
|
|
|
Expr e;
|
2003-11-16 18:31:29 +00:00
|
|
|
if (!(atMatch(m, *i) >> "Bind" >> s >> e))
|
2003-10-31 17:09:31 +00:00
|
|
|
abort(); /* can't happen */
|
2003-11-03 20:30:40 +00:00
|
|
|
attrs.set(s, e);
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr queryAttr(Expr e, const string & name)
|
|
|
|
{
|
2003-11-03 20:30:40 +00:00
|
|
|
ATermMap attrs;
|
2003-10-31 17:09:31 +00:00
|
|
|
queryAllAttrs(e, attrs);
|
2003-11-03 20:30:40 +00:00
|
|
|
return attrs.get(name);
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-03 20:30:40 +00:00
|
|
|
Expr makeAttrs(const ATermMap & attrs)
|
2003-10-31 17:09:31 +00:00
|
|
|
{
|
2003-11-16 18:31:29 +00:00
|
|
|
ATermList bnds = ATempty;
|
|
|
|
for (ATermIterator i(attrs.keys()); i; ++i)
|
2003-10-31 17:09:31 +00:00
|
|
|
bnds = ATinsert(bnds,
|
2003-11-16 18:31:29 +00:00
|
|
|
ATmake("Bind(<term>, <term>)", *i, attrs.get(*i)));
|
2003-10-31 17:09:31 +00:00
|
|
|
return ATmake("Attrs(<term>)", ATreverse(bnds));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-03 20:30:40 +00:00
|
|
|
Expr substitute(const ATermMap & subs, Expr e)
|
2003-10-31 17:09:31 +00:00
|
|
|
{
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-11-16 17:46:31 +00:00
|
|
|
ATMatcher m;
|
|
|
|
string s;
|
2003-10-31 17:09:31 +00:00
|
|
|
|
2003-11-16 17:46:31 +00:00
|
|
|
if (atMatch(m, e) >> "Var" >> s) {
|
2003-11-03 20:30:40 +00:00
|
|
|
Expr sub = subs.get(s);
|
|
|
|
return sub ? sub : e;
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In case of a function, filter out all variables bound by this
|
|
|
|
function. */
|
|
|
|
ATermList formals;
|
|
|
|
ATerm body;
|
2003-11-16 17:46:31 +00:00
|
|
|
if (atMatch(m, e) >> "Function" >> formals >> body) {
|
2003-11-03 20:30:40 +00:00
|
|
|
ATermMap subs2(subs);
|
2003-11-16 18:31:29 +00:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
2003-11-05 15:34:12 +00:00
|
|
|
Expr def;
|
2003-11-16 18:31:29 +00:00
|
|
|
if (!(atMatch(m, *i) >> "NoDefFormal" >> s) &&
|
|
|
|
!(atMatch(m, *i) >> "DefFormal" >> s >> def))
|
2003-11-05 15:34:12 +00:00
|
|
|
abort();
|
2003-11-03 20:30:40 +00:00
|
|
|
subs2.remove(s);
|
2003-10-31 17:09:31 +00:00
|
|
|
}
|
|
|
|
return ATmake("Function(<term>, <term>)", formals,
|
|
|
|
substitute(subs2, body));
|
|
|
|
}
|
|
|
|
|
2003-11-01 19:10:19 +00:00
|
|
|
/* Idem for a mutually recursive attribute set. */
|
2004-02-02 21:39:33 +00:00
|
|
|
ATermList rbnds, nrbnds;
|
|
|
|
if (atMatch(m, e) >> "Rec" >> rbnds >> nrbnds) {
|
2003-11-03 20:30:40 +00:00
|
|
|
ATermMap subs2(subs);
|
2004-02-02 21:39:33 +00:00
|
|
|
for (ATermIterator i(rbnds); i; ++i) {
|
2003-11-01 19:10:19 +00:00
|
|
|
Expr e;
|
2003-11-16 18:31:29 +00:00
|
|
|
if (!(atMatch(m, *i) >> "Bind" >> s >> e))
|
2003-11-01 19:10:19 +00:00
|
|
|
abort(); /* can't happen */
|
2003-11-03 20:30:40 +00:00
|
|
|
subs2.remove(s);
|
2003-11-01 19:10:19 +00:00
|
|
|
}
|
2004-02-02 21:39:33 +00:00
|
|
|
return ATmake("Rec(<term>, <term>)",
|
|
|
|
substitute(subs2, (ATerm) rbnds),
|
|
|
|
substitute(subs, (ATerm) nrbnds));
|
2003-11-01 19:10:19 +00:00
|
|
|
}
|
2003-10-31 17:09:31 +00:00
|
|
|
|
|
|
|
if (ATgetType(e) == AT_APPL) {
|
|
|
|
AFun fun = ATgetAFun(e);
|
|
|
|
int arity = ATgetArity(fun);
|
|
|
|
ATermList args = ATempty;
|
|
|
|
|
|
|
|
for (int i = arity - 1; i >= 0; i--)
|
|
|
|
args = ATinsert(args, substitute(subs, ATgetArgument(e, i)));
|
|
|
|
|
|
|
|
return (ATerm) ATmakeApplList(fun, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ATgetType(e) == AT_LIST) {
|
|
|
|
ATermList out = ATempty;
|
2003-11-16 18:31:29 +00:00
|
|
|
for (ATermIterator i((ATermList) e); i; ++i)
|
|
|
|
out = ATinsert(out, substitute(subs, *i));
|
2003-10-31 17:09:31 +00:00
|
|
|
return (ATerm) ATreverse(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
2003-11-05 16:27:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
Expr makeBool(bool b)
|
|
|
|
{
|
|
|
|
return b ? ATmake("Bool(True)") : ATmake("Bool(False)");
|
|
|
|
}
|