| 
									
										
										
										
											2014-01-31 11:00:26 -05:00
										 |  |  | USING: accessors arrays combinators effects effects.parser fry generalizations | 
					
						
							| 
									
										
										
										
											2014-10-27 08:41:03 -04:00
										 |  |  | kernel lexer locals math namespaces parser python python.ffi python.objects | 
					
						
							|  |  |  | sequences sequences.generalizations vocabs.parser words ;
 | 
					
						
							| 
									
										
										
										
											2014-01-28 13:19:57 -05:00
										 |  |  | IN: python.syntax | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-31 06:18:12 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-29 12:19:07 -05:00
										 |  |  | SYMBOL: current-context | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-each-definition ( quot -- )
 | 
					
						
							|  |  |  |     scan-token dup ";" = [ 2drop ] [ | 
					
						
							|  |  |  |         scan-effect rot [ call( tok eff -- ) ] keep with-each-definition | 
					
						
							| 
									
										
										
										
											2014-01-28 13:19:57 -05:00
										 |  |  |     ] if ; inline recursive
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-29 12:19:07 -05:00
										 |  |  | : scan-definitions ( quot -- )
 | 
					
						
							|  |  |  |     scan-token current-context set "=>" expect with-each-definition ; inline
 | 
					
						
							| 
									
										
										
										
											2014-01-28 18:31:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-31 11:00:26 -05:00
										 |  |  | : gather-args-quot ( in-effect -- quot )
 | 
					
						
							|  |  |  |     dup ?last "**" = [ | 
					
						
							|  |  |  |         but-last length '[ [ _ narray array>py-tuple ] dip ] | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         length '[ _ narray array>py-tuple f ] | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : unpack-value-quot ( out-effect -- quot )
 | 
					
						
							|  |  |  |     length { | 
					
						
							|  |  |  |         { 0 [ [ drop ] ] } | 
					
						
							|  |  |  |         { 1 [ [ ] ] } | 
					
						
							|  |  |  |         [ '[ py-tuple>array _ firstn ] ] | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-27 08:41:03 -04:00
										 |  |  | : make-function-quot ( obj-quot effect -- quot )
 | 
					
						
							| 
									
										
										
										
											2014-01-31 11:00:26 -05:00
										 |  |  |     [ in>> gather-args-quot ] [ out>> unpack-value-quot ] bi
 | 
					
						
							| 
									
										
										
										
											2014-10-27 08:41:03 -04:00
										 |  |  |     swapd '[ @ @ -rot call-object-full @ ] ;
 | 
					
						
							| 
									
										
										
										
											2014-01-28 18:31:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-23 12:43:36 -04:00
										 |  |  | : make-factor-words ( module name prefix? -- call-word obj-word )
 | 
					
						
							|  |  |  |     [ [ ":" glue ] [ ":$" glue ] 2bi ] [ nip dup "$" prepend ] if
 | 
					
						
							| 
									
										
										
										
											2015-06-08 15:38:38 -04:00
										 |  |  |     [ create-word-in ] bi@ ;
 | 
					
						
							| 
									
										
										
										
											2014-01-28 18:31:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-23 12:43:36 -04:00
										 |  |  | : import-getattr ( module name -- alien )
 | 
					
						
							|  |  |  |     [ py-import ] dip getattr ;
 | 
					
						
							| 
									
										
										
										
											2014-01-28 18:31:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-27 08:41:03 -04:00
										 |  |  | :: add-function ( name effect module prefix? -- )
 | 
					
						
							|  |  |  |     module name prefix? make-factor-words :> ( call-word obj-word )
 | 
					
						
							|  |  |  |     obj-word module name '[ _ _ import-getattr ] ( -- o ) define-inline | 
					
						
							|  |  |  |     call-word obj-word def>> effect make-function-quot effect define-inline ;
 | 
					
						
							| 
									
										
										
										
											2014-01-29 12:19:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-03 05:28:43 -05:00
										 |  |  | : make-method-quot ( name effect -- quot )
 | 
					
						
							| 
									
										
										
										
											2014-11-29 20:44:43 -05:00
										 |  |  |     [ in>> rest gather-args-quot ] [ out>> unpack-value-quot ] bi swapd
 | 
					
						
							| 
									
										
										
										
											2014-02-03 05:28:43 -05:00
										 |  |  |     '[ @ rot _ getattr -rot call-object-full @ ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : method-callable ( name effect -- )
 | 
					
						
							| 
									
										
										
										
											2015-06-08 15:38:38 -04:00
										 |  |  |     [ dup create-word-in swap ] dip [ make-method-quot ] keep define-inline ;
 | 
					
						
							| 
									
										
										
										
											2014-01-29 12:19:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : method-object ( name -- )
 | 
					
						
							| 
									
										
										
										
											2015-06-08 15:38:38 -04:00
										 |  |  |     [ "$" prepend create-word-in ] [ '[ _ getattr ] ] bi
 | 
					
						
							| 
									
										
										
										
											2014-01-29 12:19:07 -05:00
										 |  |  |     { "obj" } { "obj'" } <effect> define-inline ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : add-method ( name effect -- )
 | 
					
						
							| 
									
										
										
										
											2014-02-03 05:28:43 -05:00
										 |  |  |     dupd method-callable method-object ;
 | 
					
						
							| 
									
										
										
										
											2014-01-28 18:31:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-31 06:18:12 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-23 12:43:36 -04:00
										 |  |  | SYNTAX: PY-FROM: [ | 
					
						
							| 
									
										
										
										
											2014-10-27 08:41:03 -04:00
										 |  |  |     current-context get f add-function | 
					
						
							| 
									
										
										
										
											2014-10-23 12:43:36 -04:00
										 |  |  | ] scan-definitions ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: PY-QUALIFIED-FROM: [ | 
					
						
							| 
									
										
										
										
											2014-10-27 08:41:03 -04:00
										 |  |  |     current-context get t add-function | 
					
						
							| 
									
										
										
										
											2014-10-23 12:43:36 -04:00
										 |  |  | ] scan-definitions ; inline
 | 
					
						
							| 
									
										
										
										
											2014-01-31 06:18:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-29 12:19:07 -05:00
										 |  |  | SYNTAX: PY-METHODS: [ add-method ] scan-definitions ; inline
 |