interpolate: allow anonymous by-order stack arguments.
parent
5251d7c9bc
commit
e5b12a1c59
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue