I'm being greedy here ... how can I modify this code so that if I have:<br><br>(c-define-type Foo:Bar "FooBar")<br><br>(define-array-allocator-and-accessors Foo:Bar)<br><br>will result in using "FooBar" in the generated *.c file rather than Foo:Bar<br>
<br><br><br><div class="gmail_quote">On Sat, Feb 14, 2009 at 9:32 PM, Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca">feeley@iro.umontreal.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d"><br>
On 15-Feb-09, at 12:29 AM, lowly coder wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
... wow ... thanks!<br>
<br>
On Sat, Feb 14, 2009 at 9:14 PM, Marc Feeley <<a href="mailto:feeley@iro.umontreal.ca" target="_blank">feeley@iro.umontreal.ca</a>> wrote:<br>
<br>
On 14-Feb-09, at 11:58 PM, lowly coder wrote:<br>
<br>
question 1:<br>
<br>
Is it possible to write gambit code to produce the equiv of:<br>
int foo[200]; ?<br>
<br>
the current best I have in mind is:<br>
(let ((foo (malloc (* 200 8)))) ... )<br>
</blockquote>
<br></div>
Here is another, more generic, approach which uses a macro to generate the various functions:<div class="Ih2E3d"><br>
<br>
(c-declare #<<end-of-c-declare<br>
<br>
#include <stdlib.h><br>
<br></div>
end-of-c-declare<br>
)<br>
<br>
(define-macro (define-array-allocator-and-accessors type)<br>
<br>
  (define (sym . syms)<br>
    (string->symbol (apply string-append<br>
                           (map symbol->string syms))))<br>
<br>
  (let ((type-str (symbol->string type)))<br>
    `(begin<br>
<br>
       (define ,(sym 'alloc- type '-array)<br>
         (c-lambda (int) (pointer ,type)<br>
                   ,(string-append<br>
                     "___result_voidstar = malloc( ___arg1 * sizeof("<br>
                     type-str<br>
                     ") );")))<br>
<br>
       (define ,(sym 'free- type '-array)<br>
         (c-lambda ((pointer ,type)) void<br>
                   "free( ___arg1 );"))<br>
<br>
       (define ,(sym type '-array-ref)<br>
         (c-lambda ((pointer ,type) int) ,type<br>
                   "___result = ___arg1[___arg2];"))<br>
<br>
       (define ,(sym type '-array-set!)<br>
         (c-lambda ((pointer ,type) int ,type) void<div class="Ih2E3d"><br>
                   "___arg1[___arg2] = ___arg3;")))))<br>
<br>
;; test it:<br>
<br></div>
(define-array-allocator-and-accessors int)<br>
(define-array-allocator-and-accessors double)<br>
<br>
(define a (alloc-int-array 5))<br>
(define b (alloc-double-array 10))<div class="Ih2E3d"><br>
<br>
(int-array-set! a 1 111)<br>
(int-array-set! a 2 222)<br>
(pp (+ (int-array-ref a 1) (int-array-ref a 2))) ;; 333<br>
<br></div>
(double-array-set! b 1 1.5)<br>
(double-array-set! b 2 2.0)<br>
(pp (+ (double-array-ref b 1) (double-array-ref b 2))) ;; 3.5<br>
<br>
(free-int-array a)<br>
(free-double-array b)<br>
<br>
<br>
<br>
</blockquote></div><br>