[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Chicken-users] can you believe it? compile time vs. run time issues, ag
From: |
Michele Simionato |
Subject: |
[Chicken-users] can you believe it? compile time vs. run time issues, again! |
Date: |
Sun, 29 May 2005 02:49:28 -0400 |
How many times I have been bitten by compile-time vs. run-time issues?
10, 12? One would think I had understood the lesson, however ... :-(
My motivation here was a nifty trick to generate dynamics records
without using macros:
(define (dynamic-record . fields)
(define getters (map (lambda (f) `((',f) ,f)) fields))
(define setters (map (lambda (f) `((',f v) (set! ,f v))) fields))
(define signature (map (lambda (f) `(,f #f)) fields))
(eval
`(lambda (#!key ,@signature)
(match-lambda*
(('->vector) (vector ,@fields))
(('->string) (with-output-to-string (cut write (vector ,@fields))))
(('->fields) ',fields)
,@getters
,@setters
(else (error (format "missing field ~s" (car else))))))))
(the idea was to read the fields from a database, without knowing a
priori the structure of the records). This works fine in the interpreter:
> (define fields '(title author))
> (define book (apply dynamic-record fields))
> (define chicken-man (book title: "The Chicken Manual" author: "Felix"))
> (chicken-man 'title)
"The Chicken Manual"
> (chicken-man 'author)
"Felix"
> (chicken-man 'author "Felix Winkelmann")
> (chicken-man 'author)
"Felix Winkelmann"
> (chicken-man '->vector)
#("The Chicken Manual" "Felix Winkelmann")
> (chicken-man '->string)
"#(\"The Chicken Manual\" \"Felix Winkelmann\")"
> (chicken-man '->fields)
(title author)
Then I tried to compile the program and it did not work. Actually,
it compiles, but it does not run since the match-lambda* macro
is not available to the evaluator environment. Fine, I thought, this
is a compile time vs. run time issue again and I know my lesson,
let me compile with -run-time-macros and it should work :-)
It did NOT work :-(
So I did some experiments and discovered that in order to have
-run-time-macros work, I need to *include* the macros, not
just import it. For instance
(include "miscmacros")
(print (eval '(begin (define x 1) (inc! x) x))) ;=> 2
works when compiled with -run-time-macros, but it does not work
with (use miscmacros). So I tried to include "match-support.scm"
and "chicken-match-macros", but it does not even compile
$ csc -run-time-macros dynamic-record.scm
/usr/lib/gcc-lib/i486-linux/3.3.5/../../../crt1.o(.text+0x18): In
function `_start':
../sysdeps/i386/elf/start.S:98: undefined reference to `main'
collect2: ld returned 1 exit status
*** Shell command terminated with exit status 1: gcc -o dynamic-record
dynamic-record.o -lchicken -lsrfi-chicken -lstuffed-chicken
-L/usr/local/lib -Wl,-R/usr/local/lib -ldl -lpcre -lm -ldl -lpcre
I suppose I must declare my program as an unit or something like that.
Furthermore, I suppose that if one had compiled the match module with
the -run-time-macros option, one could "use" it instead of "include".
>From previous experience, I also suppose the option -syntax will give me
further headaches :-(
Are my suppositions correct?
Michele Simionato
- [Chicken-users] can you believe it? compile time vs. run time issues, again!,
Michele Simionato <=