1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors fry generalizations io io.streams.string kernel
4 make math math.order math.parser multiline namespaces present
5 sequences splitting strings vocabs.parser ;
10 TUPLE: named-var name ;
16 : (parse-interpolate) ( str -- )
19 [ >string , ] unless-empty
24 >string dup string>number
26 [ [ anon-var new ] [ named-var boa ] if-empty ] ?if ,
28 [ (parse-interpolate) ] bi*
33 : deanonymize ( seq -- seq' )
36 drop 1 + dup stack-var boa
40 : parse-interpolate ( str -- seq )
41 [ (parse-interpolate) ] { } make deanonymize ;
43 : max-stack-var ( seq -- n/f )
45 dup stack-var? [ n>> [ or ] keep max ] [ drop ] if
48 :: (interpolate-quot) ( str quot -- quot' )
49 str parse-interpolate :> args
50 args max-stack-var :> vars
54 name>> quot call '[ _ @ present write ]
57 n>> '[ _ npick present write ]
70 : interpolate-quot ( str -- quot )
71 [ [ get ] ] (interpolate-quot) ;
73 MACRO: interpolate ( str -- quot )
76 : interpolate>string ( str -- newstr )
77 [ interpolate ] with-string-writer ; inline
79 : interpolate-locals-quot ( str -- quot )
80 [ dup search [ [ ] ] [ [ get ] ] ?if ] (interpolate-quot) ;
82 MACRO: interpolate-locals ( str -- quot )
83 interpolate-locals-quot ;
85 : interpolate-locals>string ( str -- newstr )
86 [ interpolate-locals ] with-string-writer ; inline
89 "I]" parse-multiline-string
90 interpolate-locals-quot append! ;