diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-11-18T10·47+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-11-18T10·47+0000 |
commit | 8798fae30450a88c339c8f23d7e0c75f5df2ef1c (patch) | |
tree | 53d271e89ee3d04490a94a7ea82cf2dc3bbc3c30 /src/libutil/aterm.cc | |
parent | 45610ae675f6f8d6ecbd48c495cb7012b143d531 (diff) |
* Source tree refactoring.
Diffstat (limited to 'src/libutil/aterm.cc')
-rw-r--r-- | src/libutil/aterm.cc | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/libutil/aterm.cc b/src/libutil/aterm.cc new file mode 100644 index 000000000000..de7c359521a7 --- /dev/null +++ b/src/libutil/aterm.cc @@ -0,0 +1,93 @@ +#include "aterm.hh" + + +string atPrint(ATerm t) +{ + if (!t) throw Error("attempt to print null aterm"); + char * s = ATwriteToString(t); + if (!s) throw Error("cannot print term"); + return s; +} + + +ostream & operator << (ostream & stream, ATerm e) +{ + return stream << atPrint(e); +} + + +ATMatcher & atMatch(ATMatcher & pos, ATerm t) +{ + pos.t = t; + pos.pos = ATMatcher::funPos; + return pos; +} + + +static inline bool failed(const ATMatcher & pos) +{ + return pos.pos == ATMatcher::failPos; +} + + +static inline ATMatcher & fail(ATMatcher & pos) +{ + pos.pos = ATMatcher::failPos; + return pos; +} + + +ATMatcher & operator >> (ATMatcher & pos, ATerm & out) +{ + out = 0; + if (failed(pos)) return pos; + if (pos.pos == ATMatcher::funPos || + ATgetType(pos.t) != AT_APPL || + pos.pos >= (int) ATgetArity(ATgetAFun(pos.t))) + return fail(pos); + out = ATgetArgument(pos.t, pos.pos); + pos.pos++; + return pos; +} + + +ATMatcher & operator >> (ATMatcher & pos, string & out) +{ + out = ""; + if (pos.pos == ATMatcher::funPos) { + if (ATgetType(pos.t) != AT_APPL) return fail(pos); + out = ATgetName(ATgetAFun(pos.t)); + pos.pos = 0; + } else { + ATerm t; + pos = pos >> t; + if (failed(pos)) return pos; + if (ATgetType(t) != AT_APPL || + ATgetArity(ATgetAFun(t)) != 0) + return fail(pos); + out = ATgetName(ATgetAFun(t)); + } + return pos; +} + + +ATMatcher & operator >> (ATMatcher & pos, const string & s) +{ + string s2; + pos = pos >> s2; + if (failed(pos)) return pos; + if (s != s2) return fail(pos); + return pos; +} + + +ATMatcher & operator >> (ATMatcher & pos, ATermList & out) +{ + out = 0; + ATerm t; + pos = pos >> t; + if (failed(pos)) return pos; + if (ATgetType(t) != AT_LIST) return fail(pos); + out = (ATermList) t; + return pos; +} |