interpolate: allow anonymous by-order stack arguments.

db4
John Benediktsson 2015-04-20 14:44:49 -07:00
parent 5251d7c9bc
commit e5b12a1c59
3 changed files with 34 additions and 6 deletions

View File

@ -4,7 +4,7 @@ IN: interpolate
HELP: interpolate HELP: interpolate
{ $values { "str" string } } { $values { "str" string } }
{ $description "String interpolation using named variables and/or stack arguments, writing to the " { $link output-stream } "." } { $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 { $examples
{ $example { $example
"USING: interpolate ;" "USING: interpolate ;"
@ -16,11 +16,16 @@ HELP: interpolate
"\"Fred\" \"name\" [ \"Hi ${name}\" interpolate ] with-variable" "\"Fred\" \"name\" [ \"Hi ${name}\" interpolate ] with-variable"
"Hi Fred" "Hi Fred"
} }
{ $example
"USING: interpolate ;"
"\"Mr.\" \"Anderson\"" "\"Hello, ${} ${}\" interpolate"
"Hello, Mr. Anderson"
}
} ; } ;
HELP: interpolate>string HELP: interpolate>string
{ $values { "str" string } { "newstr" string } } { $values { "str" string } { "newstr" string } }
{ $description "String interpolation using named variables and/or stack arguments, captured as a " { $link 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 { interpolate interpolate>string } related-words

View File

@ -6,6 +6,7 @@ IN: interpolate.tests
{ "A B" } [ "A" "B" "${1} ${0}" interpolate>string ] unit-test { "A B" } [ "A" "B" "${1} ${0}" interpolate>string ] unit-test
{ "B A" } [ "A" "B" "${0} ${1}" 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 A" } [ "A" "B" "C" "${0} ${2}" interpolate>string ] unit-test
{ "C B A" } [ "A" "B" "C" "${} ${1} ${2}" interpolate>string ] unit-test
{ "Hello, Jane." } [ { "Hello, Jane." } [
"Jane" "name" set "Jane" "name" set
@ -17,6 +18,18 @@ IN: interpolate.tests
"Mr." "${0} ${name}" interpolate>string "Mr." "${0} ${name}" interpolate>string
] unit-test ] 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." } [ { "Sup Dawg, we heard you liked rims, so we put rims on your rims so you can roll while you roll." } [
"Dawg" "name" set "Dawg" "name" set
"rims" "noun" set "rims" "noun" set

View File

@ -11,6 +11,8 @@ TUPLE: named-var name ;
TUPLE: stack-var n ; TUPLE: stack-var n ;
TUPLE: anon-var ;
: (parse-interpolate) ( str -- ) : (parse-interpolate) ( str -- )
[ [
"${" split1-slice [ "${" split1-slice [
@ -20,15 +22,23 @@ TUPLE: stack-var n ;
"}" split1-slice "}" split1-slice
[ [
>string dup string>number >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* [ (parse-interpolate) ] bi*
] when* ] when*
] bi* ] bi*
] unless-empty ; ] 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 ( str -- seq )
[ (parse-interpolate) ] { } make ; [ (parse-interpolate) ] { } make deanonymize ;
: max-stack-var ( seq -- n/f ) : max-stack-var ( seq -- n/f )
f [ f [
@ -44,7 +54,7 @@ TUPLE: stack-var n ;
name>> quot call '[ _ @ present write ] name>> quot call '[ _ @ present write ]
] [ ] [
dup stack-var? [ dup stack-var? [
n>> 1 + '[ _ npick present write ] n>> '[ _ npick present write ]
] [ ] [
'[ _ write ] '[ _ write ]
] if ] if
@ -52,7 +62,7 @@ TUPLE: stack-var n ;
] map concat ] map concat
vars [ vars [
1 + '[ _ ndrop ] append '[ _ ndrop ] append
] when* ; inline ] when* ; inline
PRIVATE> PRIVATE>