summary refs log tree commit diff
path: root/src/fix-ng/fix-expr.cc
blob: 1ce4a55e475a8c40b54ae9e072cdeb61fda1aa29 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "fix-expr.hh"
#include "expr.hh"


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));
}
    

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)));
        
        e = (ATerm) ATmakeApplList(fun, args);
    }

    else 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);
        }

        e = (ATerm) ATreverse(out);
    }

    return f(e);
}


void queryAllAttrs(Expr e, ATermMap & 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.set(s, e);
        bnds = ATgetNext(bnds);
    }
}


Expr queryAttr(Expr e, const string & name)
{
    ATermMap attrs;
    queryAllAttrs(e, attrs);
    return attrs.get(name);
}


Expr makeAttrs(const ATermMap & attrs)
{
    ATermList bnds = ATempty, keys = attrs.keys();
    while (!ATisEmpty(keys)) {
        Expr key = ATgetFirst(keys);
        bnds = ATinsert(bnds, 
            ATmake("Bind(<term>, <term>)", key, attrs.get(key)));
        keys = ATgetNext(keys);
    }
    return ATmake("Attrs(<term>)", ATreverse(bnds));
}


Expr substitute(const ATermMap & subs, Expr e)
{
    char * s;

    if (ATmatch(e, "Var(<str>)", &s)) {
        Expr sub = subs.get(s);
        return sub ? sub : e;
    }

    /* 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)) {
        ATermMap subs2(subs);
        ATermList fs = formals;
        while (!ATisEmpty(fs)) {
            Expr def;
            if (!ATmatch(ATgetFirst(fs), "NoDefFormal(<str>)", &s) &&
                !ATmatch(ATgetFirst(fs), "DefFormal(<str>, <term>)", &s))
                abort();
            subs2.remove(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)) {
        ATermMap subs2(subs);
        ATermList bnds = bindings;
        while (!ATisEmpty(bnds)) {
            Expr e;
            if (!ATmatch(ATgetFirst(bnds), "Bind(<str>, <term>)", &s, &e))
                abort(); /* can't happen */
            subs2.remove(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;
}


Expr makeBool(bool b)
{
    return b ? ATmake("Bool(True)") : ATmake("Bool(False)");
}