VM: Refactor tagged.hpp to Factor style

db4
Erik Charlebois 2013-05-11 22:29:22 -04:00
parent 2e058e99c1
commit 238e9d9810
1 changed files with 49 additions and 58 deletions

View File

@ -1,85 +1,76 @@
namespace factor namespace factor {
{
template<typename Type> cell tag(Type *value) template <typename Type> cell tag(Type* value) {
{ return RETAG(value, Type::type_number);
return RETAG(value,Type::type_number);
} }
inline static cell tag_dynamic(object *value) inline static cell tag_dynamic(object* value) {
{ return RETAG(value, value->type());
return RETAG(value,value->type());
} }
template<typename Type> template <typename Type> struct tagged {
struct tagged cell value_;
{
cell value_;
cell type() const cell type() const { return TAG(value_); }
{
return TAG(value_);
}
bool type_p(cell type_) const bool type_p(cell type_) const { return type() == type_; }
{
return type() == type_;
}
bool type_p() const bool type_p() const {
{ if (Type::type_number == TYPE_COUNT)
if(Type::type_number == TYPE_COUNT) return true;
return true; else
else return type_p(Type::type_number);
return type_p(Type::type_number); }
}
cell value() const cell value() const {
{
#ifdef FACTOR_DEBUG #ifdef FACTOR_DEBUG
FACTOR_ASSERT(type_p()); FACTOR_ASSERT(type_p());
#endif #endif
return value_; return value_;
} }
Type *untagged() const Type* untagged() const {
{
#ifdef FACTOR_DEBUG #ifdef FACTOR_DEBUG
FACTOR_ASSERT(type_p()); FACTOR_ASSERT(type_p());
#endif #endif
return (Type *)(UNTAG(value_)); return (Type*)(UNTAG(value_));
} }
Type *untag_check(factor_vm *parent) const Type* untag_check(factor_vm* parent) const {
{ if (!type_p())
if(!type_p()) parent->type_error(Type::type_number, value_);
parent->type_error(Type::type_number,value_); return untagged();
return untagged(); }
}
explicit tagged(cell tagged) : value_(tagged) {} explicit tagged(cell tagged) : value_(tagged) {}
explicit tagged(Type *untagged) : value_(factor::tag(untagged)) {} explicit tagged(Type* untagged) : value_(factor::tag(untagged)) {}
Type *operator->() const { return untagged(); } Type* operator->() const { return untagged(); }
cell *operator&() const { return &value_; } cell* operator&() const { return &value_; }
const tagged<Type> &operator=(const Type *x) { value_ = tag(x); return *this; } const tagged<Type>& operator=(const Type* x) {
const tagged<Type> &operator=(const cell &x) { value_ = x; return *this; } value_ = tag(x);
return *this;
}
const tagged<Type>& operator=(const cell& x) {
value_ = x;
return *this;
}
bool operator==(const tagged<Type> &x) { return value_ == x.value_; } bool operator==(const tagged<Type>& x) { return value_ == x.value_; }
bool operator!=(const tagged<Type> &x) { return value_ != x.value_; } bool operator!=(const tagged<Type>& x) { return value_ != x.value_; }
template<typename NewType> tagged<NewType> as() { return tagged<NewType>(value_); } template <typename NewType> tagged<NewType> as() {
return tagged<NewType>(value_);
}
}; };
template<typename Type> Type *factor_vm::untag_check(cell value) template <typename Type> Type* factor_vm::untag_check(cell value) {
{ return tagged<Type>(value).untag_check(this);
return tagged<Type>(value).untag_check(this);
} }
template<typename Type> Type *untag(cell value) template <typename Type> Type* untag(cell value) {
{ return tagged<Type>(value).untagged();
return tagged<Type>(value).untagged();
} }
} }