2003-10-30 18:11:24 +02:00
|
|
|
#include "fix-expr.hh"
|
|
|
|
#include "expr.hh"
|
|
|
|
|
|
|
|
|
2003-11-03 22:30:40 +02:00
|
|
|
ATermMap::ATermMap(unsigned int initialSize, unsigned int maxLoadPct)
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
table = ATtableCreate(ATgetLength(keys), map.maxLoadPct);
|
|
|
|
if (!table) throw Error("cannot create ATerm table");
|
|
|
|
|
|
|
|
for (; !ATisEmpty(keys); keys = ATgetNext(keys)) {
|
|
|
|
ATerm key = ATgetFirst(keys);
|
|
|
|
set(key, map.get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 18:11:24 +02:00
|
|
|
ATerm bottomupRewrite(TermFun & f, ATerm e)
|
|
|
|
{
|
|
|
|
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 13:59:35 +02:00
|
|
|
e = (ATerm) ATmakeApplList(fun, args);
|
2003-10-30 18:11:24 +02:00
|
|
|
}
|
|
|
|
|
2003-11-03 13:59:35 +02:00
|
|
|
else if (ATgetType(e) == AT_LIST) {
|
2003-10-30 18:11:24 +02:00
|
|
|
ATermList in = (ATermList) e;
|
|
|
|
ATermList out = ATempty;
|
|
|
|
|
|
|
|
while (!ATisEmpty(in)) {
|
|
|
|
out = ATinsert(out, bottomupRewrite(f, ATgetFirst(in)));
|
|
|
|
in = ATgetNext(in);
|
|
|
|
}
|
|
|
|
|
2003-11-03 13:59:35 +02:00
|
|
|
e = (ATerm) ATreverse(out);
|
2003-10-30 18:11:24 +02:00
|
|
|
}
|
|
|
|
|
2003-11-03 13:59:35 +02:00
|
|
|
return f(e);
|
2003-10-30 18:11:24 +02:00
|
|
|
}
|
2003-10-31 19:09:31 +02:00
|
|
|
|
|
|
|
|
2003-11-03 22:30:40 +02:00
|
|
|
void queryAllAttrs(Expr e, ATermMap & attrs)
|
2003-10-31 19:09:31 +02:00
|
|
|
{
|
|
|
|
ATermList bnds;
|
|
|
|
if (!ATmatch(e, "Attrs([<list>])", &bnds))
|
|
|
|
throw badTerm("expected attribute set", e);
|
|
|
|
|
|
|
|
while (!ATisEmpty(bnds)) {
|
|
|
|
char * s;
|
|
|
|
Expr e;
|
|
|
|
if (!ATmatch(ATgetFirst(bnds), "Bind(<str>, <term>)", &s, &e))
|
|
|
|
abort(); /* can't happen */
|
2003-11-03 22:30:40 +02:00
|
|
|
attrs.set(s, e);
|
2003-10-31 19:09:31 +02:00
|
|
|
bnds = ATgetNext(bnds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr queryAttr(Expr e, const string & name)
|
|
|
|
{
|
2003-11-03 22:30:40 +02:00
|
|
|
ATermMap attrs;
|
2003-10-31 19:09:31 +02:00
|
|
|
queryAllAttrs(e, attrs);
|
2003-11-03 22:30:40 +02:00
|
|
|
return attrs.get(name);
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-03 22:30:40 +02:00
|
|
|
Expr makeAttrs(const ATermMap & attrs)
|
2003-10-31 19:09:31 +02:00
|
|
|
{
|
2003-11-03 22:30:40 +02:00
|
|
|
ATermList bnds = ATempty, keys = attrs.keys();
|
|
|
|
while (!ATisEmpty(keys)) {
|
|
|
|
Expr key = ATgetFirst(keys);
|
2003-10-31 19:09:31 +02:00
|
|
|
bnds = ATinsert(bnds,
|
2003-11-03 22:30:40 +02:00
|
|
|
ATmake("Bind(<term>, <term>)", key, attrs.get(key)));
|
|
|
|
keys = ATgetNext(keys);
|
|
|
|
}
|
2003-10-31 19:09:31 +02:00
|
|
|
return ATmake("Attrs(<term>)", ATreverse(bnds));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-03 22:30:40 +02:00
|
|
|
Expr substitute(const ATermMap & subs, Expr e)
|
2003-10-31 19:09:31 +02:00
|
|
|
{
|
|
|
|
char * s;
|
|
|
|
|
|
|
|
if (ATmatch(e, "Var(<str>)", &s)) {
|
2003-11-03 22:30:40 +02:00
|
|
|
Expr sub = subs.get(s);
|
|
|
|
return sub ? sub : e;
|
2003-10-31 19:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In case of a function, filter out all variables bound by this
|
|
|
|
function. */
|
|
|
|
ATermList formals;
|
|
|
|
ATerm body;
|
|
|
|
if (ATmatch(e, "Function([<list>], <term>)", &formals, &body)) {
|
2003-11-03 22:30:40 +02:00
|
|
|
ATermMap subs2(subs);
|
2003-10-31 19:09:31 +02:00
|
|
|
ATermList fs = formals;
|
|
|
|
while (!ATisEmpty(fs)) {
|
|
|
|
if (!ATmatch(ATgetFirst(fs), "<str>", &s)) abort();
|
2003-11-03 22:30:40 +02:00
|
|
|
subs2.remove(s);
|
2003-10-31 19:09:31 +02:00
|
|
|
fs = ATgetNext(fs);
|
|
|
|
}
|
|
|
|
return ATmake("Function(<term>, <term>)", formals,
|
|
|
|
substitute(subs2, body));
|
|
|
|
}
|
|
|
|
|
2003-11-01 21:10:19 +02:00
|
|
|
/* Idem for a mutually recursive attribute set. */
|
|
|
|
ATermList bindings;
|
|
|
|
if (ATmatch(e, "Rec([<list>])", &bindings)) {
|
2003-11-03 22:30:40 +02:00
|
|
|
ATermMap subs2(subs);
|
2003-11-01 21:10:19 +02:00
|
|
|
ATermList bnds = bindings;
|
|
|
|
while (!ATisEmpty(bnds)) {
|
|
|
|
Expr e;
|
|
|
|
if (!ATmatch(ATgetFirst(bnds), "Bind(<str>, <term>)", &s, &e))
|
|
|
|
abort(); /* can't happen */
|
2003-11-03 22:30:40 +02:00
|
|
|
subs2.remove(s);
|
2003-11-01 21:10:19 +02:00
|
|
|
bnds = ATgetNext(bnds);
|
|
|
|
}
|
|
|
|
return ATmake("Rec(<term>)", substitute(subs2, (ATerm) bindings));
|
|
|
|
}
|
2003-10-31 19:09:31 +02: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 in = (ATermList) e;
|
|
|
|
ATermList out = ATempty;
|
|
|
|
|
|
|
|
while (!ATisEmpty(in)) {
|
|
|
|
out = ATinsert(out, substitute(subs, ATgetFirst(in)));
|
|
|
|
in = ATgetNext(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ATerm) ATreverse(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|