1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#include "fix-expr.hh"
#include "expr.hh"
ATerm bottomupRewrite(TermFun & f, ATerm e)
{
e = f(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)));
return (ATerm) ATmakeApplList(fun, args);
}
if (ATgetType(e) == AT_LIST) {
ATermList in = (ATermList) e;
ATermList out = ATempty;
while (!ATisEmpty(in)) {
out = ATinsert(out, bottomupRewrite(f, ATgetFirst(in)));
in = ATgetNext(in);
}
return (ATerm) ATreverse(out);
}
return e;
}
void queryAllAttrs(Expr e, Attrs & attrs)
{
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 */
attrs[s] = e;
bnds = ATgetNext(bnds);
}
}
Expr queryAttr(Expr e, const string & name)
{
Attrs attrs;
queryAllAttrs(e, attrs);
Attrs::iterator i = attrs.find(name);
return i == attrs.end() ? 0 : i->second;
}
Expr makeAttrs(const Attrs & attrs)
{
ATermList bnds = ATempty;
for (Attrs::const_iterator i = attrs.begin(); i != attrs.end(); i++)
bnds = ATinsert(bnds,
ATmake("Bind(<str>, <term>)", i->first.c_str(), i->second));
return ATmake("Attrs(<term>)", ATreverse(bnds));
}
ATerm substitute(Subs & subs, ATerm e)
{
char * s;
if (ATmatch(e, "Var(<str>)", &s)) {
Subs::iterator i = subs.find(s);
if (i == subs.end())
return e;
else
return i->second;
}
/* 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)) {
Subs subs2(subs);
ATermList fs = formals;
while (!ATisEmpty(fs)) {
if (!ATmatch(ATgetFirst(fs), "<str>", &s)) abort();
subs2.erase(s);
fs = ATgetNext(fs);
}
return ATmake("Function(<term>, <term>)", formals,
substitute(subs2, body));
}
/* Idem for a mutually recursive attribute set. */
ATermList bindings;
if (ATmatch(e, "Rec([<list>])", &bindings)) {
Subs subs2(subs);
ATermList bnds = bindings;
while (!ATisEmpty(bnds)) {
Expr e;
if (!ATmatch(ATgetFirst(bnds), "Bind(<str>, <term>)", &s, &e))
abort(); /* can't happen */
subs2.erase(s);
bnds = ATgetNext(bnds);
}
return ATmake("Rec(<term>)", substitute(subs2, (ATerm) bindings));
}
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;
}
|