|
I just have a suggestion about your code below. Namely, cat-str function is a little clumsy. I think it originated as a function that I needed in the C internals, and then I just made it public. While it has its uses, in the situations below, you end up with a lot nicer code if you use quasiliterals.
For instance
(defun italic(text) `\\*[IT]@text\\*[PREV]`)
done!
There is a list of strings there that is catenated: it's a compile-time list of argument expressions that are evaluated. You can see the generated code like this, right from your system command line:
$ txr -p '(quote `\\*[IT]@text\\*[PREV]`)'
(sys:quasi "\\*[IT]" @text "\\*[PREV]")
A quasiliteral is compiled to a sys:quasi form, in other words. This is not documented because there is little reason for anyone to use it directly. Note how the second argument prints as @text, which looks like the original syntax. This is because the object printer recognizes that object and renders it that way. The object is actually (sys:var text).
$ txr -p '(quote (sys:var text))'
@text
I forgot to teach the printer to turn sys:quasi into the backtick syntax, so we see it as it is! :)
A few releases ago, I also added word list literals and quasiliterals to TXR. These are similar to Perl's qw (quoted vords) and %w in Ruby and similar features in other languages. It's a shorthand for writing a list of strings, when you are able to delimit on whitespace. The syntax is very simple: just a hash mark before the ordinary literal. Spaces can be escaped with backslash so they are taken literally, as the "d e" shows:
$ txr -p '(quote #"a b c d\ e")'
("a" "b" "c" "d e")
$ txr -p '(quote #`a b c @(+ 2 2) d\ e`)'
(sys:quasilist (sys:quasi "a") (sys:quasi "b") (sys:quasi "c") (sys:quasi @(+ 2 2)) (sys:quasi "d e"))
$ txr -e '(prinl #`a b c @(+ 2 2) d\ e`))'
("a" "b" "c" "4" "d e")
$ txr -e '(put-lines #`a b c @(+ 2 2) d\ e`))'
a
b
c
4
d e
That could come in handy to you.
Cheers ...
On 24.09.2014 18:44, Yves Cloutier wrote:
Haha, starting to get somehwere !
@(do
; This function translates the <italic<...> command
(defun italic(text)
(cat-str (list "\\*[IT]" text "\\*[PREV]"))
)
; This function translates the <bolder n<...> command
(defun bolder (value text)
(cat-str (list ".SETBOLDER " (tostring value) "\n\\*[BOLDER]" text "\\*[BOLDERX]"))
)
; This function translates the up n<...> command
(defun bolder (value text)
(cat-str (list "\\*[UP " (tostring value) "]" text "\\*[DOWN " (tostring value) "]" ))
)
; This function translates the [heading] ... command
(defun chapter (title)
(cat-str (list ".HEADING 1 \"" title "\"\n" ))
)
; This function translates <blankline> command
(defun blankline ()
".\n"
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Main program
(pprint (cat-str (list
(chapter "The Infinite")
(blankline)
"Once upon a time there was a " (bolder 3 (italic "great")) " king...")))
)
This message was posted to the following mailing lists: | ||||
---|---|---|---|---|
TXR Users Mailing list info | Nearby messages |
Kaz'z Mailing List Archives adminstered by Kaz | Lurker (version 2.3, substantialy hacked by Kaz). Icon artwork from VisualPharm. |