|
A Scribe back-end is a Bigloo
library that implements a code generator for a certain
target format. The Scribe
version 1.1b provides back-ends for the
following formats:
- HTML
- TeX
- Ascii
- Man (nroff)
- Info
This chapter explains how to implement new back-ends. It pre-supposes
the capacity to implement, compile and install Bigloo libraries.
7.1 Implementing a back-end
|
A Scribe back-end is implemented by the means of a Bigloo function
that accepts Scribe values and that write on the current
output channel the target file.
A Scribe value is either
- a
- procedure
- of zero argument
- It belongs to the back-end to apply the function
and re-process the result of this application.
- a text
- string
- It belongs to the back-end to handle escape
sequences. For instance, the HTML back-end intercepts the character
"<" and replaces with the appropriate HTML characters sequence.
- a
- number
- a
- character
- a
- list
- of Scribe values
- The back-end must process in sequence the contained
values.
- an instance of the
- %node
- class
- The back-end must dispatch over
that class hierarchy in order to decide how to handle the node. For this, it
is recommended to implement the back-end by the means of a generic
function.
All other values should be ignored.
For the sake of the example, here is the definition of the function
implementing the Man back-end.
(define-generic (man obj::obj)
(cond
((and (procedure? obj) (correct-arity? obj 0))
(man (obj)))
((string? obj)
(display obj))
((number? obj)
(display (number->string obj)))
((char? obj)
(display obj))
((eq? obj #unspecified)
obj)
((list? obj)
(for-each man obj))
((or (symbol? obj) (boolean? obj))
"")
(else
(with-access::%node obj (loc)
(error/location "man"
"Can't find method for node"
(find-runtime-type obj)
(car loc)
(cdr loc)))))) |
And here is the implementation of the generic function man for
the instances of class %pre:
(define-method (man obj::%pre)
(newline)
(print ".Sp\n.nf")
(man (%pre-body obj))
(newline)
(print ".Sp\n.fi"))
|
7.2 Registering a back-end
|
Once implemented a Bigloo function implementing a back-end must
registered in order to enable Scribe to use it.
(register-backend! ::symbol ::procedure) | Scribe function |
Registers the procedure under the name symbol.
That is, when Scribe is to compile a Scribe source file for a
target format, it searches a back-end has
been recorded under the name of the format.
For instance, to register the man back-end, the implementation
of the Bigloo library, contains the following top-level call:
(register-backend! 'man man)
|
|