| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | namespace factor | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | template<typename Type> cell tag(Type *value) | 
					
						
							| 
									
										
										
										
											2009-05-02 05:43:58 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-02 04:25:39 -05:00
										 |  |  | 	return RETAG(value,Type::type_number); | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 05:50:24 -04:00
										 |  |  | inline static cell tag_dynamic(object *value) | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-10 22:06:36 -05:00
										 |  |  | 	return RETAG(value,value->type()); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:43:58 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | template<typename Type> | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | struct tagged | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	cell value_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 19:10:34 -05:00
										 |  |  | 	cell type() const | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-11-02 04:25:39 -05:00
										 |  |  | 		return TAG(value_); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-20 10:37:24 -04:00
										 |  |  | 	bool type_p(cell type_) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return type() == type_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool type_p() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if(Type::type_number == TYPE_COUNT) | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			return type_p(Type::type_number); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 19:10:34 -05:00
										 |  |  | 	cell value() const | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | #ifdef FACTOR_DEBUG
 | 
					
						
							| 
									
										
										
										
											2011-11-17 23:42:30 -05:00
										 |  |  | 		FACTOR_ASSERT(type_p()); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-10-25 09:07:36 -04:00
										 |  |  | 		return value_; | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-11-02 19:10:34 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Type *untagged() const | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | #ifdef FACTOR_DEBUG
 | 
					
						
							| 
									
										
										
										
											2011-11-17 23:42:30 -05:00
										 |  |  | 		FACTOR_ASSERT(type_p()); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-10-25 09:07:36 -04:00
										 |  |  | 		return (Type *)(UNTAG(value_)); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 19:10:34 -05:00
										 |  |  | 	Type *untag_check(factor_vm *parent) const | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-10-25 09:07:36 -04:00
										 |  |  | 		if(!type_p()) | 
					
						
							|  |  |  | 			parent->type_error(Type::type_number,value_); | 
					
						
							|  |  |  | 		return untagged(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	explicit tagged(cell tagged) : value_(tagged) {} | 
					
						
							|  |  |  | 	explicit tagged(Type *untagged) : value_(factor::tag(untagged)) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	Type *operator->() const { return untagged(); } | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | 	cell *operator&() const { return &value_; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	const tagged<Type> &operator=(const Type *x) { value_ = tag(x); return *this; } | 
					
						
							|  |  |  | 	const tagged<Type> &operator=(const cell &x) { value_ = x; return *this; } | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	bool operator==(const tagged<Type> &x) { return value_ == x.value_; } | 
					
						
							|  |  |  | 	bool operator!=(const tagged<Type> &x) { return value_ != x.value_; } | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	template<typename NewType> tagged<NewType> as() { return tagged<NewType>(value_); } | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | template<typename Type> Type *factor_vm::untag_check(cell value) | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	return tagged<Type>(value).untag_check(this); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 21:12:57 -04:00
										 |  |  | template<typename Type> Type *untag(cell value) | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-03 09:47:05 -04:00
										 |  |  | 	return tagged<Type>(value).untagged(); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:15 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | } |