| 
									
										
										
										
											2009-08-12 10:37:09 -04:00
										 |  |  | ! (c)Joe Groff bsd license | 
					
						
							| 
									
										
										
										
											2009-09-17 23:07:21 -04:00
										 |  |  | USING: accessors alien alien.c-types alien.data alien.prettyprint arrays | 
					
						
							| 
									
										
										
										
											2009-09-16 10:56:07 -04:00
										 |  |  | assocs classes classes.struct combinators combinators.short-circuit | 
					
						
							|  |  |  | continuations fry kernel libc make math math.parser mirrors | 
					
						
							|  |  |  | prettyprint.backend prettyprint.custom prettyprint.sections | 
					
						
							|  |  |  | see.private sequences slots strings summary words ;
 | 
					
						
							| 
									
										
										
										
											2009-08-12 10:37:09 -04:00
										 |  |  | IN: classes.struct.prettyprint | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-15 19:56:17 -04:00
										 |  |  | : struct-definer-word ( class -- word )
 | 
					
						
							|  |  |  |     struct-slots dup length 2 >=
 | 
					
						
							|  |  |  |     [ second offset>> 0 = \ UNION-STRUCT: \ STRUCT: ? ] | 
					
						
							|  |  |  |     [ drop \ STRUCT: ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | : struct>assoc ( struct -- assoc )
 | 
					
						
							| 
									
										
										
										
											2009-08-31 19:51:47 -04:00
										 |  |  |     [ class struct-slots ] [ struct-slot-values ] bi zip ;
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  | : pprint-struct-slot ( slot -- )
 | 
					
						
							|  |  |  |     <flow \ { pprint-word | 
					
						
							| 
									
										
										
										
											2009-09-10 16:59:27 -04:00
										 |  |  |     f <inset { | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  |         [ name>> text ] | 
					
						
							| 
									
										
										
										
											2009-09-16 10:56:07 -04:00
										 |  |  |         [ type>> pprint-c-type ] | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  |         [ read-only>> [ \ read-only pprint-word ] when ] | 
					
						
							|  |  |  |         [ initial>> [ \ initial: pprint-word pprint* ] when* ] | 
					
						
							| 
									
										
										
										
											2009-10-07 22:42:15 -04:00
										 |  |  |         [ | 
					
						
							|  |  |  |             dup struct-bit-slot-spec? | 
					
						
							|  |  |  |             [ \ bits: pprint-word bits>> pprint* ] | 
					
						
							|  |  |  |             [ drop ] if
 | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2009-09-10 16:59:27 -04:00
										 |  |  |     } cleave block> | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  |     \ } pprint-word block> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:46:31 -04:00
										 |  |  | : pprint-struct ( struct -- )
 | 
					
						
							| 
									
										
										
										
											2009-08-31 21:46:33 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         [ \ S{ ] dip
 | 
					
						
							|  |  |  |         [ class ] | 
					
						
							|  |  |  |         [ struct>assoc [ [ name>> ] dip ] assoc-map ] bi
 | 
					
						
							|  |  |  |         \ } (pprint-tuple) | 
					
						
							|  |  |  |     ] ?pprint-tuple ;
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:46:31 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : pprint-struct-pointer ( struct -- )
 | 
					
						
							| 
									
										
										
										
											2009-08-31 20:56:36 -04:00
										 |  |  |     \ S@ [ [ class pprint-word ] [ >c-ptr pprint* ] bi ] pprint-prefix ;
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:46:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-12 10:37:09 -04:00
										 |  |  | M: struct-class see-class* | 
					
						
							| 
									
										
										
										
											2009-09-15 19:56:17 -04:00
										 |  |  |     <colon dup struct-definer-word pprint-word dup pprint-word | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  |     <block struct-slots [ pprint-struct-slot ] each
 | 
					
						
							| 
									
										
										
										
											2009-08-12 10:37:09 -04:00
										 |  |  |     block> pprint-; block> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-12 15:59:33 -04:00
										 |  |  | M: struct pprint-delims | 
					
						
							|  |  |  |     drop \ S{ \ } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | M: struct >pprint-sequence | 
					
						
							|  |  |  |     [ class ] [ struct-slot-values ] bi class-slot-sequence ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct pprint* | 
					
						
							| 
									
										
										
										
											2009-08-30 21:46:31 -04:00
										 |  |  |     [ pprint-struct ] | 
					
						
							|  |  |  |     [ pprint-struct-pointer ] pprint-c-object ;
 | 
					
						
							| 
									
										
										
										
											2009-08-31 21:32:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: struct summary | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         dup class name>> % | 
					
						
							|  |  |  |         " struct of " % | 
					
						
							|  |  |  |         byte-length # | 
					
						
							|  |  |  |         " bytes " % | 
					
						
							|  |  |  |     ] "" make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 23:26:03 -04:00
										 |  |  | TUPLE: struct-mirror { object read-only } ;
 | 
					
						
							|  |  |  | C: <struct-mirror> struct-mirror | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : get-struct-slot ( struct slot -- value present? )
 | 
					
						
							|  |  |  |     over class struct-slots slot-named | 
					
						
							|  |  |  |     [ name>> reader-word execute( struct -- value ) t ] | 
					
						
							|  |  |  |     [ drop f f ] if* ;
 | 
					
						
							|  |  |  | : set-struct-slot ( value struct slot -- )
 | 
					
						
							|  |  |  |     over class struct-slots slot-named | 
					
						
							|  |  |  |     [ name>> writer-word execute( value struct -- ) ] | 
					
						
							|  |  |  |     [ 2drop ] if* ;
 | 
					
						
							|  |  |  | : reset-struct-slot ( struct slot -- )
 | 
					
						
							|  |  |  |     over class struct-slots slot-named | 
					
						
							|  |  |  |     [ [ initial>> swap ] [ name>> writer-word ] bi execute( value struct -- ) ] | 
					
						
							|  |  |  |     [ drop ] if* ;
 | 
					
						
							|  |  |  | : reset-struct-slots ( struct -- )
 | 
					
						
							|  |  |  |     dup class struct-prototype | 
					
						
							|  |  |  |     dup byte-length memcpy ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-mirror at* | 
					
						
							|  |  |  |     object>> { | 
					
						
							|  |  |  |         { [ over "underlying" = ] [ nip >c-ptr t ] } | 
					
						
							|  |  |  |         { [ over { [ array? ] [ length 1 >= ] } 1&& ] [ swap first get-struct-slot ] } | 
					
						
							|  |  |  |         [ 2drop f f ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-mirror set-at | 
					
						
							|  |  |  |     object>> { | 
					
						
							|  |  |  |         { [ over "underlying" = ] [ 3drop ] } | 
					
						
							|  |  |  |         { [ over array? ] [ swap first set-struct-slot ] } | 
					
						
							|  |  |  |         [ 3drop ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-mirror delete-at | 
					
						
							|  |  |  |     object>> { | 
					
						
							|  |  |  |         { [ over "underlying" = ] [ 2drop ] } | 
					
						
							|  |  |  |         { [ over array? ] [ swap first reset-struct-slot ] } | 
					
						
							|  |  |  |         [ 2drop ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-mirror clear-assoc | 
					
						
							|  |  |  |     object>> reset-struct-slots ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-mirror >alist ( mirror -- alist )
 | 
					
						
							|  |  |  |     object>> [ | 
					
						
							|  |  |  |         [ drop "underlying" ] [ >c-ptr ] bi 2array 1array
 | 
					
						
							| 
									
										
										
										
											2009-08-31 21:32:00 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         '[ | 
					
						
							|  |  |  |             _ struct>assoc | 
					
						
							| 
									
										
										
										
											2009-09-15 20:10:05 -04:00
										 |  |  |             [ [ [ name>> ] [ type>> ] bi 2array ] dip ] assoc-map
 | 
					
						
							| 
									
										
										
										
											2009-08-31 21:32:00 -04:00
										 |  |  |         ] [ drop { } ] recover
 | 
					
						
							|  |  |  |     ] bi append ;
 | 
					
						
							| 
									
										
										
										
											2009-08-31 23:26:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: struct make-mirror <struct-mirror> ;
 | 
					
						
							| 
									
										
										
										
											2009-09-03 23:31:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: struct-mirror assoc |