From e537844f4ebc53df13f52722fb16bbeb1f4cbd18 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 30 Oct 2003 16:11:24 +0000 Subject: * Bottomup rewrite function. --- src/fix-ng/fix-expr.cc | 33 +++++++++++++++++++++++++++++++++ src/fix-ng/fix-expr.hh | 27 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/fix-ng/fix-expr.cc create mode 100644 src/fix-ng/fix-expr.hh diff --git a/src/fix-ng/fix-expr.cc b/src/fix-ng/fix-expr.cc new file mode 100644 index 0000000000..00795da4c4 --- /dev/null +++ b/src/fix-ng/fix-expr.cc @@ -0,0 +1,33 @@ +#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); + } + + throw badTerm("cannot rewrite", e); +} diff --git a/src/fix-ng/fix-expr.hh b/src/fix-ng/fix-expr.hh new file mode 100644 index 0000000000..5c50e9170f --- /dev/null +++ b/src/fix-ng/fix-expr.hh @@ -0,0 +1,27 @@ +#ifndef __FIXEXPR_H +#define __FIXEXPR_H + +#include + +#include "util.hh" + + +/* Fix expressions are represented as ATerms. The maximal sharing + property of the ATerm library allows us to implement caching of + normals forms efficiently. */ +typedef ATerm Expr; + + +/* Generic bottomup traversal over ATerms. The traversal first + recursively descends into subterms, and then applies the given term + function to the resulting term. */ + +struct TermFun +{ + virtual ATerm operator () (ATerm e) = 0; +}; + +ATerm bottomupRewrite(TermFun & f, ATerm e); + + +#endif /* !__FIXEXPR_H */ -- cgit 1.4.1