Fix compilations errors of the dummy test.

This commit is contained in:
Nicolas Pierron 2010-02-19 19:36:03 +00:00
parent 405c763aa8
commit e0406330fa
2 changed files with 77 additions and 50 deletions

View File

@ -3,7 +3,7 @@
# define _LIBTERM_TERM_HH
# include <set>
# include <pair>
# include <utility>
# include <boost/preprocessor/tuple.hpp>
# include <boost/preprocessor/seq.hpp>
@ -61,6 +61,12 @@ namespace term
{
}
inline
ATerm()
: ptr_(0)
{
}
protected:
inline
ATerm(const ATermImpl* ptr)
@ -94,6 +100,12 @@ namespace term
return ptr_ < rhs.ptr_;
}
inline
operator bool()
{
return ptr_;
}
public:
inline
const ATermImpl*
@ -103,16 +115,7 @@ namespace term
}
protected:
const ATermImpl* /*const*/ ptr_;
};
class ATermNil : public ATerm
{
public:
ATermNil()
: ATerm(0)
{
}
const ATermImpl* ptr_;
};
@ -157,7 +160,7 @@ namespace term
public:
inline
bool operator <(const this_type&)
bool operator <(const this_type&) const
{
return false;
}
@ -388,7 +391,7 @@ namespace term
# define TRM_LESS_GRAMMAR_NODE_OP(Name, Base, Attributes, BaseArgs) \
public: \
inline \
bool operator <(const Name& arg_rhs) \
bool operator <(const Name& arg_rhs) const \
{ \
return TRM_APPLY(TRM_LESS_RHS_OR, Attributes) \
parent::operator < (arg_rhs); \
@ -415,6 +418,11 @@ namespace term
{ \
} \
\
A ## Name () \
: parent() \
{ \
} \
\
protected: \
explicit A ## Name (const ATermImpl* ptr) \
: parent(ptr) \
@ -432,13 +440,15 @@ namespace term
A ## Name (const A ## Name& t) \
: parent(t) \
{ \
} \
\
A ## Name () \
: parent() \
{ \
} \
\
const Name & \
operator() () const; \
{ \
return *static_cast<const Name *>(ptr_); \
} \
\
protected: \
explicit A ## Name (const ATermImpl* ptr) \
@ -512,48 +522,45 @@ namespace term
// definition of the ATermVisitor visit functions.
# define TRM_VISITOR(Name, Base, Attributes, BaseArgs) \
ATerm \
ATermVisitor::visit(const A ## Name) { \
return ATermNil(); \
ATermVisitor::visit(const A ## Name t) { \
return t; \
}
TRM_VISITOR(Term, TRM_NIL, TRM_NIL, TRM_NIL)
TRM_GRAMMAR_NODES(TRM_VISITOR, TRM_VISITOR)
# undef TRM_VISITOR
template <typename T>
class getVisitor : ATermVisitor
namespace impl
{
public:
getVisitor(ATerm t)
template <typename T>
class asVisitor : ATermVisitor
{
t.accept(*this);
}
public:
asVisitor(ATerm t)
: res()
{
t.accept(*this);
}
ATerm visit(const T t)
{
res = std::pair<bool, T>(true, t);
return ATermNil();
}
ATerm visit(const T t)
{
return res = t;
}
std::pair<bool, T> res;
};
public:
T res;
};
}
// This function will return a zero ATerm if the element does not have the
// expected type.
template <typename T>
std::pair<bool, T>
is_a(ATerm t)
T as(ATerm t)
{
getVisitor<T> v(t);
impl::asVisitor<T> v(t);
return v.res;
}
template <typename T>
T
as(ATerm t)
{
getVisitor<T> v(t);
return v.res.second;
}
}
#endif

View File

@ -1,5 +1,6 @@
#include <vector>
#include <iostream>
#define TRM_GRAMMAR_NODES(Interface, Final) \
Interface(Expr, Term, (0, ()), (0, ())) \
@ -9,27 +10,46 @@
#include "term.hh"
#undef TRM_GRAMMAR_NODES
struct Eval : public term::ATermVisitor
using namespace term;
struct Eval : public ATermVisitor
{
int run(const term::ATerm t)
int run(const ATerm t)
{
return term::as<term::AInt>(t.accept(*this))().value;
return as<AInt>(t.accept(*this))().value;
}
term::ATerm visit(const term::APlus p)
ATerm visit(const APlus p)
{
return term::Int::make(run(p().lhs) + run(p().rhs));
return Int::make(run(p().lhs) + run(p().rhs));
}
};
#define CHECK(Cond, Msg) \
if (Cond) \
{ \
good++; \
std::cout << "Ok: " << Msg << std::endl; \
} \
else \
{ \
std::cout << "Ko: " << Msg << std::endl; \
} \
tests++
int main()
{
unsigned good, tests;
using namespace term;
AInt a = Int::make(1);
AInt b = Int::make(2);
AInt c = Int::make(1);
// assert(a == c);
Eval e;
return e.run(Plus::make(a, Plus::make(b, c)));
CHECK(a == c, "Terms are shared.");
CHECK(!as<APlus>(a), "Bad convertion returns a zero ATerm.");
CHECK(as<AInt>(a), "Good convertion returns a non-zero ATerm.");
CHECK(e.run(Plus::make(a, Plus::make(b, c))) == 4, "Visitors are working.");
return tests - good;
}