If there is one feature I love in ruby whose absence I despise in java, it's string interpolation. The happy outcome of a complete rewrite of parser.arc is string interpolation for arc, as well as a mostly-complete implementation of 'read in arc. And it doesn't use 'ccc any more! It's true that for many purposes, (pr "foo is " foo) is more compact than (pr "foo is #(foo)"). But there are some cases where it's tidier to interpolate - for example when constructing filesystem paths, javascript identifiers, urls, or other strings not intended for human consumption - (wumble-file "#(root)/users/#(user)/#(id).w"), or (map [load "libs/#(_)"] (dir "libs"))
Here's a brief demo: arc> (load "lib/parser.arc")
nil
arc> (si-repl) ; opens a repl that uses 'parse from parser.arc to read expressions from the prompt
enjoy interpolating. type x! to return to the usual repl
arc$ (set world "you")
"you"
arc$ "hello, #(world)"
"hello, you"
arc$ (set fallen 3)
3
arc$ "there are #((- 10 fallen)) green bottles on the wall"
"there are 7 green bottles on the wall"
arc$ x!
nil
arc>
I know arc defines the prf macro which does much the same thing, but there are some imho slight deficiencies:# I want string interpolation to be the universal default; I don't want to have to tell arc which strings to interpolate. # I can write (prf "foo is #x") to get "foo is 3.14", but not (prf "foo is #x/10") to get "foo is 3.14/10". (Undefined symbol: x/10) # Nested strings are impossible - the reader can't tell that the quote delimiters of the nested string are not delimiting the outer string. parser.arc lives on anarki on github at lib/parser.arc. Its reader simply expands "hello, #(world), you have #((+ 1 2 3)) points"
to (string "hello, " world ", you have " (+ 1 2 3) " points")
so interpolations have full access to their lexical context. You can escape interpolations: arc$ "hello, \#(world)"
"hello, #(world)"
but careful, arc's native parser gets upset when it encounters "\#".parser.arc defines arc-tokeniser, parse, and si-repl. You can arc> (set read parse)
#<procedure: parse>
arc> (load "foo.arc")
nil
foo.arc and other subsequently loaded code can enjoy string interpolations. There are parser tests under lib/tests/parser-tests.arc (also on anarki). There are some constructs it doesn't support - notably scheme stuff (#f, #x3cf9), ad multi-line comments (for another day), and probably some others. Welder (the arc arc editor), although it is still horribly slow, supports proper syntax highlighting and paren matching for interpolations.I would love to enhance parser.arc so it is easy to extend the grammar - for example to support CatDancer's inline table syntax. It's not difficult to change parser.arc to do that, but it would be great to supply tokeniser/parser hooks instead. Any ideas? Interpolations are also supported by rainbow's reader. This was perhaps a mistake, as it involved a big fight with javacc and regular expressions, and rainbow's builtin parser is good enough to bootstrap and load parser.arc. Unless my regexps are all wrong, it was pretty amazing how much easier it was to describe the tokeniser as a recursive state-machine in arc. |