Previous: Code Generation Compiler Back-End, Up: Compiler [Contents][Index]
The run-time compiler back-end is also provided by the (rpc
compiler)
module. It reads XDR/RPC definitions and returns data
structures readily usable to deal with the XDR data types or RPC
programs described, at run-time. Actually, as of version
0.4, it does not have an API to deal with RPC programs, only
with XDR data types.
Read XDR type definitions from input and return an alist; element
of the returned alist is a pair whose car
is a string naming an
XDR data type and whose cdr
is an XDR data type object
(see XDR Type Representations). input can be either an input
port, a string, or an AST as returned by rpc-language->sexp
(see Parser).
This procedure can raise error conditions having a sub-type of
&compiler-error
.
Here is an example of two procedures that, given XDR type definitions, decode (respectively encode) an object of that type:
(use-modules (rpc compiler) (rpc xdr) (rnrs bytevectors) (rnrs io ports)) (define (decode-data type-defs type-name port) ;; Read binary data from PORT as an object of type ;; TYPE-NAME whose definition is given in TYPE-DEFS. (let* ((types (rpc-language->xdr-types type-defs)) (type (cdr (assoc type-name types)))) (xdr-decode type port))) (define (encode-data type-defs type-name object) ;; Encode OBJECT as XDR data type named TYPE-NAME from ;; the XDR type definitions in TYPE-DEFS. (let* ((types (rpc-language->xdr-types type-defs)) (type (cdr (assoc type-name types))) (size (xdr-type-size type object)) (bv (make-bytevector size))) (xdr-encode! bv 0 type object) (open-bytevector-input-port bv)))
These procedures can then be used as follows:
(let ((type-defs (string-append "typedef hyper chbouib<>;" "struct foo { " " int x; float y; chbouib z;" "};")) (type-name "foo") (object '(1 2.0 #(3 4 5)))) (equal? (decode-data type-defs type-name (encode-data type-defs type-name object)) object)) => #t
Note that in this example type-defs contains two type definitions, which is why the type-name argument is absolutely needed.
Previous: Code Generation Compiler Back-End, Up: Compiler [Contents][Index]