]> gitweb.factorcode.org Git - factor.git/commitdiff
infix: adding support for "seq[from:to]" slice notation.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 18 Mar 2011 05:42:23 +0000 (22:42 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 18 Mar 2011 05:42:23 +0000 (22:42 -0700)
extra/infix/ast/ast.factor
extra/infix/infix-docs.factor
extra/infix/infix-tests.factor
extra/infix/infix.factor
extra/infix/parser/parser.factor
extra/infix/tokenizer/tokenizer.factor

index 1908b3d39bc54af20633d9b22b08e8dc9ab22dcd..ef5bed35d6dcea147bfd5afd2eec7bc90343965b 100644 (file)
@@ -5,6 +5,7 @@ IN: infix.ast
 TUPLE: ast-number value ;
 TUPLE: ast-local name ;
 TUPLE: ast-array name index ;
+TUPLE: ast-slice name from to ;
 TUPLE: ast-function name arguments ;
 TUPLE: ast-op left right op ;
 TUPLE: ast-negation term ;
index 3885fcc6134aeb8c770cf33e05dc8ecc8d1b8afa..52df131b0ede467b76f781e62a60e14d69cea16f 100644 (file)
@@ -65,6 +65,13 @@ $nl
     "[let { 1 2 3 4 } :> myarr [infix myarr[4/2]*3 infix] ] ."
     "9"
 }
+$nl
+"You can create sub-" { $vocab-link "sequences" } " inside infix expressions using " { $snippet "arr[from:to]" } " notation."
+{ $example
+    "USING: arrays locals infix ;"
+    "[let \"foobar\" :> s [infix s[0:3] infix] ] ."
+    "\"foo\""
+}
 ;
 
 ABOUT: "infix"
index c2b0d9d7b4474b357a8d03820942bb4879d144fb..79e077f2acf1d2b80e264e2f7ed13c84221ef94b 100644 (file)
@@ -32,3 +32,13 @@ IN: infix.tests
 [ 10 ] [ [infix stupid_function (0, 1, 2, 3, 4) infix] ] unit-test
 
 [ -1 ] [ [let 1 :> a [infix -a infix] ] ] unit-test
+
+[ CHAR: f ] [ [let "foo" :> s [infix s[0] infix] ] ] unit-test
+[ CHAR: r ] [ [let "bar" :> s [infix s[-1] infix] ] ] unit-test
+[ "foo" ] [ [let "foobar" :> s [infix s[0:3] infix] ] ] unit-test
+[ "foo" ] [ [let "foobar" :> s [infix s[:3] infix] ] ] unit-test
+[ "bar" ] [ [let "foobar" :> s [infix s[-3:] infix] ] ] unit-test
+[ "foobar" ] [ [let "foobar" :> s [infix s[:] infix] ] ] unit-test
+[ "bar" ] [ [let "foobar" :> s [infix s[-3:100] infix] ] ] unit-test
+[ "foobar" ] [ [let "foobar" :> s [infix s[-100:100] infix] ] ] unit-test
+
index 48ac35264b2081eb302d5c9624a20e1304a57537..1945b2e9065080a654120271c42459ac1f3d8f28 100644 (file)
@@ -1,9 +1,10 @@
 ! Copyright (C) 2009 Philipp Brüschweiler
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors assocs combinators combinators.short-circuit
-effects fry infix.parser infix.ast kernel locals.parser
-locals.types math multiline namespaces parser quotations
-sequences summary words vocabs.parser ;
+effects fry infix.parser infix.ast kernel locals locals.parser
+locals.types math math.order multiline namespaces parser
+quotations sequences summary words vocabs.parser ;
+
 IN: infix
 
 <PRIVATE
@@ -33,9 +34,23 @@ M: ast-number infix-codegen value>> ;
 M: ast-local infix-codegen
     name>> >local-word ;
 
+:: infix-nth ( n seq -- elt )
+    n dup 0 < [ seq length + ] when seq nth ;
+
 M: ast-array infix-codegen
     [ index>> infix-codegen prepare-operand ]
-    [ name>> >local-word ] bi '[ @ _ nth ] ;
+    [ name>> >local-word ] bi '[ @ _ infix-nth ] ;
+
+:: infix-subseq ( from to seq -- subseq )
+    seq length :> len
+    from 0 or dup 0 < [ len + ] when
+    to [ dup 0 < [ len + ] when ] [ len ] if*
+    [ 0 len clamp ] bi@ dupd max seq subseq ;
+
+M: ast-slice infix-codegen
+    [ from>> [ infix-codegen prepare-operand ] [ [ f ] ] if* ]
+    [ to>> [ infix-codegen prepare-operand ] [ [ f ] ] if* ]
+    [ name>> >local-word ] tri '[ @ @ _ infix-subseq ] ;
 
 M: ast-op infix-codegen
     [ left>> infix-codegen ] [ right>> infix-codegen ]
index 2f9ab03d18b9925e8cf17ff323245da2a3aa6ee6..ca1570e1119588d9ada08b4db1e5b12b8d4495a6 100644 (file)
@@ -8,6 +8,7 @@ EBNF: parse-infix
 Number      = . ?[ ast-number? ]?
 Identifier  = . ?[ string? ]?
 Array       = Identifier:i "[" Sum:s "]" => [[ i s ast-array boa ]]
+Slice       = Identifier:i "[" Sum?:s ":" Sum?:t "]" => [[ i s t ast-slice boa ]]
 Function    = Identifier:i "(" FunArgs?:a ")" => [[ i a [ V{ } ] unless* ast-function boa ]]
 
 FunArgs     =   FunArgs:a "," Sum:s => [[ s a push a ]]
@@ -15,7 +16,7 @@ FunArgs     =   FunArgs:a "," Sum:s => [[ s a push a ]]
 
 Terminal    =   ("-"|"+"):op Terminal:term => [[ term op "-" = [ ast-negation boa ] when ]]
               | "(" Sum:s ")" => [[ s ]]
-              | Number | Array | Function
+              | Number | Array | Slice | Function
               | Identifier => [[ ast-local boa ]]
 
 Product     =   Product:p ("*"|"/"|"%"):op Terminal:term  => [[ p term op ast-op boa ]]
index f5bce4b6d7270e860c4001f83a90bc0a2a3d0bc6..7bf1bddcd4cf80b517c6041bb683d6719e79c51c 100644 (file)
@@ -17,6 +17,7 @@ NameRest          = NameFirst | Digit
 Name              = NameFirst NameRest* => [[ first2 swap prefix >string ]]
 Special           =   [+*/%(),] | "-" => [[ CHAR: - ]]
                     | "[" => [[ CHAR: [ ]] | "]" => [[ CHAR: ] ]]
+                    | ":" => [[ CHAR: : ]]
 Tok               = Spaces (Name | Number | Special )
 End               = !(.)
 Toks              = Tok* Spaces End