]> gitweb.factorcode.org Git - factor.git/commitdiff
interpolate: allow anonymous by-order stack arguments.
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 20 Apr 2015 21:44:49 +0000 (14:44 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 20 Apr 2015 21:44:49 +0000 (14:44 -0700)
basis/interpolate/interpolate-docs.factor
basis/interpolate/interpolate-tests.factor
basis/interpolate/interpolate.factor

index 1f731ec05805a01b9d9a7234840521b5faa7589e..bf8a637f9bbf4809d636f030f1ede88bd9dbd678 100644 (file)
@@ -4,7 +4,7 @@ IN: interpolate
 HELP: interpolate
 { $values { "str" string } }
 { $description "String interpolation using named variables and/or stack arguments, writing to the " { $link output-stream } "." }
-{ $notes "Stack arguments are numbered from the top of the stack." }
+{ $notes "Stack arguments are numbered from the top of the stack, or provided anonymously by order of arguments." }
 { $examples
     { $example
         "USING: interpolate ;"
@@ -16,11 +16,16 @@ HELP: interpolate
         "\"Fred\" \"name\" [ \"Hi ${name}\" interpolate ] with-variable"
         "Hi Fred"
     }
+    { $example
+        "USING: interpolate ;"
+        "\"Mr.\" \"Anderson\"" "\"Hello, ${} ${}\" interpolate"
+        "Hello, Mr. Anderson"
+    }
 } ;
 
 HELP: interpolate>string
 { $values { "str" string } { "newstr" string } }
 { $description "String interpolation using named variables and/or stack arguments, captured as a " { $link string } "." }
-{ $notes "Stack arguments are numbered from the top of the stack." } ;
+{ $notes "Stack arguments are numbered from the top of the stack, or provided anonymously by order of arguments." } ;
 
 { interpolate interpolate>string } related-words
index b694878427c8b979f22027690a1583122012d67e..01d64699720bd91f89539784609f2d2bdcccb296 100644 (file)
@@ -6,6 +6,7 @@ IN: interpolate.tests
 { "A B" } [ "A" "B" "${1} ${0}" interpolate>string ] unit-test
 { "B A" } [ "A" "B" "${0} ${1}" interpolate>string ] unit-test
 { "C A" } [ "A" "B" "C" "${0} ${2}" interpolate>string ] unit-test
+{ "C B A" } [ "A" "B" "C" "${} ${1} ${2}" interpolate>string ] unit-test
 
 { "Hello, Jane." } [
     "Jane" "name" set
@@ -17,6 +18,18 @@ IN: interpolate.tests
     "Mr." "${0} ${name}" interpolate>string
 ] unit-test
 
+{ "Hello, Mr. Anderson" } [
+    "Mr." "Anderson"
+    "Hello, ${} ${}" interpolate>string
+] unit-test
+
+{ "Mixing named and stack variables... stacks are cool!" } [
+    "cool!" "what" set
+    "named" "stack"
+    "Mixing ${} and ${} variables... ${0}s are ${what}"
+    interpolate>string
+] unit-test
+
 { "Sup Dawg, we heard you liked rims, so we put rims on your rims so you can roll while you roll." } [
     "Dawg" "name" set
     "rims" "noun" set
index d8c7c965891f3b7a8987ccfeb71192f3e16c8d55..b9a83afa5ed3ef39aa06aac3d42b986af7bae32c 100644 (file)
@@ -11,6 +11,8 @@ TUPLE: named-var name ;
 
 TUPLE: stack-var n ;
 
+TUPLE: anon-var ;
+
 : (parse-interpolate) ( str -- )
     [
         "${" split1-slice [
@@ -20,15 +22,23 @@ TUPLE: stack-var n ;
                 "}" split1-slice
                 [
                     >string dup string>number
-                    [ stack-var boa ] [ named-var boa ] ?if ,
+                    [ 1 + stack-var boa ]
+                    [ [ anon-var new ] [ named-var boa ] if-empty ] ?if ,
                 ]
                 [ (parse-interpolate) ] bi*
             ] when*
         ] bi*
     ] unless-empty ;
 
+: deanonymize ( seq -- seq' )
+    0 over <reversed> [
+        dup anon-var? [
+            drop 1 + dup stack-var boa
+        ] when
+    ] map! 2drop ;
+
 : parse-interpolate ( str -- seq )
-    [ (parse-interpolate) ] { } make ;
+    [ (parse-interpolate) ] { } make deanonymize ;
 
 : max-stack-var ( seq -- n/f )
     f [
@@ -44,7 +54,7 @@ TUPLE: stack-var n ;
             name>> quot call '[ _ @ present write ]
         ] [
             dup stack-var? [
-                n>> 1 + '[ _ npick present write ]
+                n>> '[ _ npick present write ]
             ] [
                 '[ _ write ]
             ] if
@@ -52,7 +62,7 @@ TUPLE: stack-var n ;
     ] map concat
 
     vars [
-        1 + '[ _ ndrop ] append
+        '[ _ ndrop ] append
     ] when* ; inline
 
 PRIVATE>