RosettaCodeData/Task/String-interpolation-included-/Forth/string-interpolation-included-.fth
2023-07-01 13:44:08 -04:00

26 lines
764 B
Forth

variable 'src
variable #src
variable 'out
variable #out
: Replace~ dup [char] ~ = \ test for escape char
if 'out @ 1+ #out @ type drop \ replace the escape char
else emit \ otherwise write char
then ;
: format 0
begin dup #src @ u<
while 1+ dup 'src @ + c@
replace~
repeat ;
\ Test of function
Here 'src ! ," Mary had a ~ lamb" here 'src @ - #src !
page
cr ." Original : "
'src @ 1+ #src @ type
cr ." 1st Replacement : "
here 'out ! ," little" here 'out @ - #out !
format
cr ." 2nd Replacement : "
here 'out ! ," BIG" here 'out @ - #out !
format