1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 08:31:16 +02:00

* Drop ATmake / ATMatcher also in handling store expressions.

This commit is contained in:
Eelco Dolstra 2004-10-29 11:22:49 +00:00
parent ed09821859
commit a69534fc21
19 changed files with 118 additions and 258 deletions

View file

@ -4,10 +4,3 @@ libutil_a_SOURCES = util.cc util.hh hash.cc hash.hh \
archive.cc archive.hh md5.c md5.h aterm.cc aterm.hh
AM_CXXFLAGS = -Wall -I.. ${aterm_include}
check_PROGRAMS = test-aterm
test_aterm_SOURCES = test-aterm.cc
test_aterm_LDADD = ./libutil.a ../boost/format/libformat.a \
${aterm_lib}

View file

@ -16,95 +16,6 @@ ostream & operator << (ostream & stream, ATerm 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, int & n)
{
n = 0;
ATerm t;
pos = pos >> t;
if (failed(pos)) return pos;
if (ATgetType(t) != AT_INT) return fail(pos);
n = ATgetInt((ATermInt) t);
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;
}
Error badTerm(const format & f, ATerm t)
{
char * s = ATwriteToString(t);

View file

@ -36,48 +36,6 @@ public:
};
/* Type-safe matching. */
struct ATMatcher
{
ATerm t;
int pos;
const static int failPos = -2;
const static int funPos = -1;
ATMatcher() : t(0), pos(failPos)
{
}
operator bool() const
{
return pos != failPos;
}
};
/* Initiate matching of a term. */
ATMatcher & atMatch(ATMatcher & pos, ATerm t);
/* Get the next argument of an application. */
ATMatcher & operator >> (ATMatcher & pos, ATerm & out);
/* Get the name of the function symbol of an application, or the next
argument of an application as a string. */
ATMatcher & operator >> (ATMatcher & pos, string & out);
/* Like the previous, but check that the string is equal to the given
string. */
ATMatcher & operator >> (ATMatcher & pos, const string & s);
/* Get the next argument of an application, and verify that it is a
integer. */
ATMatcher & operator >> (ATMatcher & pos, int & n);
/* Get the next argument of an application, and verify that it is a
list. */
ATMatcher & operator >> (ATMatcher & pos, ATermList & out);
/* Throw an exception with an error message containing the given
aterm. */
Error badTerm(const format & f, ATerm t);

View file

@ -1,66 +0,0 @@
#include "aterm.hh"
#include <iostream>
void runTests()
{
verbosity = lvlDebug;
ATMatcher pos;
ATerm t = ATmake("Call(Foo, Bar, \"xyz\")");
debug(format("term: %1%") % t);
string fun, arg3;
ATerm lhs, rhs;
if (!(atMatch(pos, t) >> "Call" >> lhs >> rhs >> arg3))
throw Error("should succeed");
if (arg3 != "xyz") throw Error("bad 1");
if (!(atMatch(pos, t) >> fun >> lhs >> rhs >> arg3))
throw Error("should succeed");
if (fun != "Call") throw Error("bad 2");
if (arg3 != "xyz") throw Error("bad 3");
if (!(atMatch(pos, t) >> fun >> lhs >> rhs >> "xyz"))
throw Error("should succeed");
if (atMatch(pos, t) >> fun >> lhs >> rhs >> "abc")
throw Error("should fail");
if (atMatch(pos, t) >> "Call" >> lhs >> rhs >> "abc")
throw Error("should fail");
t = ATmake("X([A, B, C], \"abc\")");
ATerm t1, t2, t3;
if (atMatch(pos, t) >> "X" >> t1 >> t2 >> t3)
throw Error("should fail");
if (!(atMatch(pos, t) >> "X" >> t1 >> t2))
throw Error("should succeed");
ATermList ts;
if (!(atMatch(pos, t) >> "X" >> ts >> t2))
throw Error("should succeed");
if (ATgetLength(ts) != 3)
throw Error("bad");
if (atMatch(pos, t) >> "X" >> t1 >> ts)
throw Error("should fail");
}
int main(int argc, char * * argv)
{
ATerm bottomOfStack;
ATinit(argc, argv, &bottomOfStack);
try {
runTests();
} catch (Error & e) {
printMsg(lvlError, format("error: %1%") % e.msg());
return 1;
}
return 0;
}