]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/interpolate/interpolate.factor
basis: use lint.vocabs tool to trim using lists
[factor.git] / basis / interpolate / interpolate.factor
index c1595e2a3749589066f6560d20599bd87355696d..cd300a585c73791bc08121fa464a880f61418cfe 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2008, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors fry generalizations io io.streams.string kernel
-locals macros make math math.order math.parser multiline
-namespaces present sequences splitting strings vocabs.parser ;
+make math math.order math.parser multiline namespaces present
+sequences splitting strings vocabs.parser ;
 IN: interpolate
 
 <PRIVATE
@@ -11,6 +11,8 @@ TUPLE: named-var name ;
 
 TUPLE: stack-var n ;
 
+TUPLE: anon-var ;
+
 : (parse-interpolate) ( str -- )
     [
         "${" split1-slice [
@@ -20,22 +22,30 @@ 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 [
         dup stack-var? [ n>> [ or ] keep max ] [ drop ] if
     ] reduce ;
 
-:: interpolate-quot ( str quot -- quot' )
+:: (interpolate-quot) ( str quot -- quot' )
     str parse-interpolate :> args
     args max-stack-var    :> vars
 
@@ -44,7 +54,7 @@ TUPLE: stack-var n ;
             name>> quot call '[ _ @ present write ]
         ] [
             dup stack-var? [
-                n>> vars swap - 1 + '[ _ npick present write ]
+                n>> '[ _ npick present write ]
             ] [
                 '[ _ write ]
             ] if
@@ -52,20 +62,29 @@ TUPLE: stack-var n ;
     ] map concat
 
     vars [
-        1 + '[ _ ndrop ] append
+        '[ _ ndrop ] append
     ] when* ; inline
 
 PRIVATE>
 
-MACRO: interpolate. ( str -- )
-    [ [ get ] ] interpolate-quot ;
+: interpolate-quot ( str -- quot )
+    [ [ get ] ] (interpolate-quot) ;
+
+MACRO: interpolate ( str -- quot )
+    interpolate-quot ;
+
+: interpolate>string ( str -- newstr )
+    [ interpolate ] with-string-writer ; inline
+
+: interpolate-locals-quot ( str -- quot )
+    [ dup search [ [ ] ] [ [ get ] ] ?if ] (interpolate-quot) ;
 
-: interpolate ( str -- newstr )
-    [ interpolate. ] with-string-writer ; inline
+MACRO: interpolate-locals ( str -- quot )
+    interpolate-locals-quot ;
 
-: interpolate-locals ( str -- quot )
-    [ dup search [ [ ] ] [ [ get ] ] ?if ] interpolate-quot ;
+: interpolate-locals>string ( str -- newstr )
+    [ interpolate-locals ] with-string-writer ; inline
 
-SYNTAX: I[
-    "]I" parse-multiline-string
-    interpolate-locals append! ;
+SYNTAX: [I
+    "I]" parse-multiline-string
+    interpolate-locals-quot append! ;