]> gitweb.factorcode.org Git - factor.git/commitdiff
python: syntax for methods and ditching of the auto-marshalling words, the syntax...
authorBjörn Lindqvist <bjourne@gmail.com>
Tue, 28 Jan 2014 23:31:43 +0000 (00:31 +0100)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 4 Mar 2014 17:23:04 +0000 (09:23 -0800)
extra/python/python.factor
extra/python/syntax/syntax-tests.factor
extra/python/syntax/syntax.factor

index be4052e87facef9c3d4e41f03aa0713fd7cebcdc..1f83f833778545f1bc3fb55c023af18352aca7e3 100644 (file)
@@ -99,6 +99,9 @@ ERROR: python-error type message ;
     [ length <py-tuple> dup ] keep
     [ rot py-tuple-set-item ] with each-index ;
 
+: py-tuple>array ( py-tuple -- arr )
+    dup py-tuple-size iota [ py-tuple-get-item ] with map ;
+
 GENERIC: (>py) ( obj -- obj' )
 M: string (>py) utf8>py-unicode ;
 M: math:fixnum (>py) PyLong_FromLong ;
@@ -130,9 +133,7 @@ DEFER: >factor
         ] }
         { "long" [ PyLong_AsLong ] }
         { "str" [ PyString_AsString (check-return) ] }
-        { "tuple" [
-            dup py-tuple-size iota [ py-tuple-get-item >factor ] with map
-        ] }
+        { "tuple" [ py-tuple>array [ >factor ] map ] }
         { "unicode" [ py-unicode>utf8 ] }
     } clone ;
 
index 0bd184732f37db52a11e6b2abc0a244cceb0dcc7..43a92bf1c76c1198296c88ae761e5c1f42008ef5 100644 (file)
@@ -1,73 +1,94 @@
-USING: assocs destructors fry kernel math namespaces python python.ffi
+USING: arrays assocs destructors fry kernel math namespaces python python.ffi
 python.syntax python.tests sequences tools.test ;
 IN: python.syntax.tests
 
-! Define your own type conversions.
-[ py-date>factor ] "date" py-type-dispatch get set-at
-
 ! Importing functions
 PY-FROM: os =>
     getpid ( -- y )
     system ( x -- y ) ;
 
-[ t ] [ getpid integer? ] unit-test
+[ t ] [ getpid >factor integer? ] unit-test
 
-! Automatic tuple unpacking
+! Automatic tuple unpacking
 PY-FROM: os.path =>
     basename ( x -- x' )
     splitext ( x -- base ext ) ;
 
-[ "hello.doc" ] [ "/some/path/hello.doc" basename ] unit-test
+[ "hello.doc" ] [ "/some/path/hello.doc" >py basename >factor ] unit-test
 
-[ "hello" ".doc" ] [ "hello.doc" splitext ] unit-test
+[ { "hello" ".doc" } ] [
+    "hello.doc" >py splitext 2array [ >factor ] map
+] unit-test
 
 PY-FROM: time => sleep ( n -- ) ;
 
-[ ] [ 0 sleep ] unit-test
+[ ] [ 0 >py sleep ] unit-test
 
-! Module variables are bound as zero-arg functions
+! Module variables are bound as zero-arg functions
 PY-FROM: sys => path ( -- seq ) ;
 
-[ t ] [ path sequence? ] unit-test
+[ t ] [ path >factor sequence? ] unit-test
 
-! Use the pipe functions to work on PyObjects.
+! Use the pipe functions to work on PyObjects.
 PY-FROM: __builtin__ =>
     callable ( obj -- ? )
+    open ( name mode -- file )
     int ( val -- s )
     len ( seq -- n )
     range ( n -- seq ) ;
 
-[ t ] [ path| |len| |int 5 > ] unit-test
+[ t ] [ path len int >factor 5 > ] unit-test
 
-[ 10 ] [ 10 range| |len ] py-test
+[ 10 ] [ 10 >py range len >factor ] unit-test
 
 ! Callables
 [ t ] [
     "os" import "getpid" getattr
-    [ |callable ] [ PyCallable_Check 1 = ] bi and
-] py-test
+    [ callable ] [ PyCallable_Check 1 = ] bi and
+] unit-test
 
 ! Reference counting
 PY-FROM: sys => getrefcount ( obj -- n ) ;
 
-[ 2 ] [ 3 <py-tuple> |getrefcount ] py-test
+[ 2 ] [ 3 <py-tuple> getrefcount >factor ] unit-test
 
 [ -2 ] [
     H{ { "foo" 33 } { "bar" 44 } } >py
-    [ "foo" py-dict-get-item-string |getrefcount ]
+    [ "foo" py-dict-get-item-string getrefcount >factor ]
     [
         '[
             500 [ _ "foo" py-dict-get-item-string drop ] times
         ] with-destructors
     ]
-    [ "foo" py-dict-get-item-string |getrefcount ] tri -
+    [ "foo" py-dict-get-item-string getrefcount >factor ] tri -
 ] py-test
 
 [ -2 ] [
     "abcd" >py <1py-tuple>
-    [ 0 py-tuple-get-item |getrefcount ]
+    [ 0 py-tuple-get-item getrefcount >factor ]
     [
         [ 100 [ swap 0 py-tuple-get-item drop ] with times ] with-destructors
     ]
-    [ 0 py-tuple-get-item |getrefcount ] tri -
+    [ 0 py-tuple-get-item getrefcount >factor ] tri -
+] py-test
+
+PY-METHODS: file =>
+    close ( self -- )
+    fileno ( self -- n )
+    tell ( self -- n ) ;
+
+[ t ] [
+    "testfile" >py "wb" >py open
+    [ ->tell ] [ ->fileno ] [ ->close ] tri
+    [ >factor integer? ] bi@ and
+] py-test
+
+PY-METHODS: str =>
+    title ( self -- self' )
+    startswith ( self str -- ? )
+    zfill ( self n -- str' ) ;
+
+! Method chaining
+[ t ] [
+    "hello there" >py ->title 20 >py ->zfill "00" >py ->startswith >factor
 ] py-test
index fa6644ffc27ac1a5d442f768daa5c14a7fa7e8e2..5b69f31654258b42c66c30e8799076e0d65dca02 100644 (file)
@@ -1,5 +1,5 @@
-USING: accessors arrays effects effects.parser formatting fry generalizations
-kernel lexer locals namespaces parser python python.ffi sequences
+USING: accessors arrays effects effects.parser fry generalizations
+kernel lexer locals math namespaces parser python python.ffi sequences
 sequences.generalizations vocabs.parser words ;
 IN: python.syntax
 
@@ -10,35 +10,16 @@ SYMBOL: current-module
 : call-or-eval ( args obj -- ret )
     dup PyCallable_Check 1 = [ swap call-object ] [ nip ] if ;
 
-: factor>factor-quot ( py-function effect -- quot )
-    [ in>> length ] [ out>> length ] bi swapd '[
-        _ narray >py _ call-or-eval >factor
-        _ [ 1 = [ 1array ] when ] [ firstn ] bi
-    ] ;
-
-: factor>py-quot ( py-function effect -- quot )
-    in>> length swap '[ _ narray >py _ call-or-eval ] ;
-
-: py>factor-quot ( py-function effect -- quot )
-    [ in>> length ] [ out>> length ] bi swapd '[
-        _ narray array>py-tuple _ call-or-eval >factor
-        _ [ 1 = [ 1array ] when ] [ firstn ] bi
-    ] ;
-
-: py>py-quot ( py-function effect -- quot )
-    in>> length swap '[ _ narray array>py-tuple _ call-or-eval ] ;
-
-:: make-function ( basename format effect quot -- )
-    basename format sprintf create-in
-    current-module get basename getattr
-    effect quot [ define-inline ] bi ; inline
-
 :: add-function ( function effect -- )
-    effect in>> { "ret" } <effect> :> py-effect
-    function "%s" effect [ factor>factor-quot ] make-function
-    function "|%s" effect [ py>factor-quot ] make-function
-    function "|%s|" py-effect [ py>py-quot ] make-function
-    function "%s|" py-effect [ factor>py-quot ] make-function ; inline
+    function create-in
+    effect [ in>> length ] [ out>> length ] bi
+    current-module get function getattr swap
+    '[
+        _ narray array>py-tuple _ call-or-eval
+        _ [ 0 = [ drop 0 <py-tuple> ] when ] keep
+        [ 1 = [ <1py-tuple> ] when ] keep
+        [ py-tuple>array ] dip firstn
+    ] effect define-inline ; inline
 
 : parse-python-word ( -- )
     scan-token dup ";" = [ drop ] [
@@ -47,3 +28,23 @@ SYMBOL: current-module
 
 SYNTAX: PY-FROM:
     scan-token import current-module set "=>" expect parse-python-word ; inline
+
+:: add-method ( attr effect -- )
+    attr "->" prepend create-in
+    effect [ in>> length 1 - ] [ out>> length ] bi
+
+    '[ _ narray array>py-tuple swap attr getattr swap call-object
+       _ [ 1 = [ 1array ] when ] [ firstn ] bi ]
+
+
+    ! '[ attr getattr _ narray array>py-tuple call-object
+    !    _ [ 1 = [ 1array ] when ] [ firstn ] bi ]
+    effect define-inline ;
+
+: parse-python-method ( -- )
+    scan-token dup ";" = [ drop ] [
+        scan-effect add-method parse-python-method
+    ] if ; inline recursive
+
+SYNTAX: PY-METHODS:
+    scan-token drop "=>" expect parse-python-method ; inline