]> gitweb.factorcode.org Git - factor.git/commitdiff
python: new syntax PY-QUALIFIED-FROM, so you can import eg __builtin__:map and not...
authorBjörn Lindqvist <bjourne@gmail.com>
Thu, 23 Oct 2014 16:43:36 +0000 (18:43 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 24 Oct 2014 00:01:45 +0000 (17:01 -0700)
extra/python/syntax/syntax-docs.factor
extra/python/syntax/syntax.factor

index 15f836968b4448788e4950d89687c7f660a057fe..2aa5f4d8f3bc288c217771026f5cb350f181c428 100644 (file)
@@ -16,6 +16,16 @@ HELP: PY-FROM:
   }
 } ;
 
+HELP: PY-QUALIFIED-FROM:
+{ $syntax "PY-QUALIFIED-FROM: module => name-effects ;" }
+{ $values
+  { "module" "fully qualified name of a python module" }
+  { "name-effects" "pairs of names and effect declarations of bindings to import" }
+}
+{ $description
+  "Like " { $link \ PY-FROM: } " except all words are created with module as the given prefix."
+} ;
+
 HELP: PY-METHODS:
 { $syntax "PY-METHODS: class => name-effects ;" }
 { $values
@@ -36,6 +46,7 @@ HELP: PY-METHODS:
 
 ARTICLE: "python.syntax" "Syntax for python calls from factor"
 "The " { $vocab-link "python.syntax" } " vocab adds syntax to factor to make calls from factor to python natural and intuitive."
+{ $subsections \ PY-FROM: \ PY-QUALIFIED-FROM: \ PY-METHODS: }
 $nl
 { $examples "Here is how you bind and call a method namelist on a ZipFile instance created by importing the zipfile module:"
   { $code
index 9b4eb3d63f6141c5c479d36945de547ea347f2e3..9dba3b5994d338bcaf9400c8eb5adf913b52d78d 100644 (file)
@@ -33,16 +33,20 @@ SYMBOL: current-context
     [ in>> gather-args-quot ] [ out>> unpack-value-quot ] bi
     swapd '[ @ _ -rot call-object-full @ ] ;
 
-: function-callable ( name alien effect -- )
-    [ create-in ] 2dip [ make-function-quot ] keep define-inline ; inline
+: make-factor-words ( module name prefix? -- call-word obj-word )
+    [ [ ":" glue ] [ ":$" glue ] 2bi ] [ nip dup "$" prepend ] if
+    [ create-in ] bi@ ;
 
-: function-object ( name alien -- )
-    [ "$" prepend create-in ] [ '[ _ ] ] bi*
-    { } { "obj" } <effect> define-inline ; inline
+: import-getattr ( module name -- alien )
+    [ py-import ] dip getattr ;
 
-: add-function ( name effect -- )
-    [ dup current-context get py-import swap getattr 2dup ] dip
-    function-callable function-object ; inline
+: make-creator-quots ( alien effect -- call-quot obj-quot )
+    [ '[ _ _ [ make-function-quot ] keep define-inline ] ]
+    [ drop '[ [ _ ] { } { "obj" } <effect> define-inline ] ] 2bi ; inline
+
+: add-function ( effect module name prefix? -- )
+    [ make-factor-words ] [ drop import-getattr ] 3bi [ rot ] dip swap
+    make-creator-quots bi* ;
 
 : make-method-quot ( name effect -- quot )
     [ in>> 1 tail gather-args-quot ] [ out>> unpack-value-quot ] bi swapd
@@ -60,6 +64,12 @@ SYMBOL: current-context
 
 PRIVATE>
 
-SYNTAX: PY-FROM: [ add-function ] scan-definitions ; inline
+SYNTAX: PY-FROM: [
+    current-context get rot f add-function
+] scan-definitions ; inline
+
+SYNTAX: PY-QUALIFIED-FROM: [
+    current-context get rot t add-function
+] scan-definitions ; inline
 
 SYNTAX: PY-METHODS: [ add-method ] scan-definitions ; inline