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

View File

@ -1,5 +1,6 @@
#include <vector> #include <vector>
#include <iostream>
#define TRM_GRAMMAR_NODES(Interface, Final) \ #define TRM_GRAMMAR_NODES(Interface, Final) \
Interface(Expr, Term, (0, ()), (0, ())) \ Interface(Expr, Term, (0, ()), (0, ())) \
@ -9,27 +10,46 @@
#include "term.hh" #include "term.hh"
#undef TRM_GRAMMAR_NODES #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() int main()
{ {
unsigned good, tests;
using namespace term; using namespace term;
AInt a = Int::make(1); AInt a = Int::make(1);
AInt b = Int::make(2); AInt b = Int::make(2);
AInt c = Int::make(1); AInt c = Int::make(1);
// assert(a == c);
Eval e; 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;
} }