chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-hackers] Tweaking the numbers egg to support syntax in compiled


From: Peter Bex
Subject: [Chicken-hackers] Tweaking the numbers egg to support syntax in compiled code
Date: Sun, 18 Mar 2012 22:06:20 +0100
User-agent: Mutt/1.4.2.3i

Hi hackers!

I've been messing around with the numbers egg some more and I think I've
found a decent way of getting number syntax supported in compiled code!
Currently one important limitation is that you must use string->number
for all numbers that are not fixnums or flonums.

It's really quite easy.  I simply have added the following code to
numbers.scm (it's a bit of a hack, I admit):

;; By overriding this we get rid of "illegal atomic form" when compiling.
;; We rely on basic struct and bytevector serialization for non-core numbers.
;;
;; TODO: Figure out if this is enough (on 64-bit, larger-than-32-bits numbers
;;       use string->number, but I don't know if it uses the egg's version)
(set! ##compiler#constant?       ; Note that this definition is
      (lambda (x)                ; identical to that in support.scm
        (or (number? x)          ; but now we use this egg's predicate
            (char? x)
            (string? x)
            (boolean? x)
            (eof-object? x)
            (and (pair? x) (eq? 'quote (car x))) )))

Then when the user uses numbers as a compiler extension via  "csc -X numbers"
this code overwrites the constant check which normally would give a "illegal
atomic form" error (compiler.scm:499).  The comment says it all, really :)

The TODO is hopefully the last remaining hurdle; on 64-bit machines, code
is generated to call C_decode_literal() with the string representation of
fixnums that are larger than the maximum fixnum for 32-bit numbers, to
ensure portable C code is generated in all cases.  In fact, this change
already works perfectly when you compile and run on the same machine.

This can be easily verified by writing a C file that contains this code:
(print 1073741824)
and then calling "csc -t foo.scm", moving the resulting C file to a
32-bit machine and using the right cc incantation on that machine.
Then, when running it, unfortunately I get this output, even if I put
(use numbers) in the code and compile with "-X numbers": 1073741824.0

It makes sense, because C_decode_literal calls the low-level C function
convert_string_to_number() rather than the Scheme string->number.
The numbers egg has no way to hook into this.  It has a hook at the
reader level, but the code in question is below that.

So my question is, how would we best go about fixing this so that the
reader hook is invoked even for compiled code?  Would it be possible at
all?

Cheers,
Peter
-- 
http://sjamaan.ath.cx
--
"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
                                                        -- Donald Knuth



reply via email to

[Prev in Thread] Current Thread [Next in Thread]