Hi, Diogo. As I understand, Crunch is a transpiler from a narrow subset of
Scheme lang to C 1-to-1, so import such code in  Scheme back is
foreign-function-work. emit-wrappers is kinda automation of such.
But the easy way is  (crunch ...) form, for example  in mine Mandelbrot
function
https://codeberg.org/Corbas/uik/src/branch/main/examples/broth-mod.scm,
than it is simple imported in main program
https://codeberg.org/Corbas/uik/src/branch/main/examples/broth.scm  . Ok I
agree, some quirky  way of doing C (especially for those who know C well)

пн, 1 дек. 2025 г. в 19:46, Diogo via Chicken-users <
[email protected]>:

> Dear list,
>
> I'm again trying to understand how the mix CHICKEN+CRUNCH works.
>
> My use case is (still)
> - a program which should be written only in CRUNCH (and C)
> - a bunch of test cases written in CHICKEN to test the CRUNCH modules.
>
> Let's say we have this CRUNCH module:
>
>     ;; foo.scm
>     (module foo
>         (foo)
>         (import (scheme base)
>                 (crunch c))
>         (define (foo)
>             (c-value byte "0xAA"))
>     )
>
> And this silly test case:
>
>     ;; foo-test.scm
>     (import (scheme base)
>             (scheme write)
>             foo)
>     (display (foo))
>     (newline)
>
> I know how to compile the CRUNCH module to use in a C program:
>
>     chicken-crunch foo.scm -o foo.c
>     gcc -I ... -c -o foo.o foo.c
>     and so on
>
> But now, I want to import this in a clean way in the test case. I see two
> flags
> in chicken-crunch that seem to be for that, but I don't understand them.
>
>     chicken-crunch -J -emit-wrappers wrap.scm foo.scm
>
> The generated files (besides foo.c) are foo.import.scm:
>
>     (begin
>       (##sys#with-environment
>         (lambda ()
>           (##sys#register-compiled-module
>             'foo
>             '#f
>             (scheme#list)
>             '((foo . foo#foo))
>             (scheme#list)
>             (scheme#list)
>             (scheme#list))))
>       (if (memq #:crunched ##sys#features)
>         (begin
>           (crunch.compiler#declare-entity 'foo#foo '(procedure () integer)
> 'foo))))
>
> And wrap.scm:
>
>     (module
>       foo
>       (foo)
>       (import (scheme base) (chicken foreign))
>       (define foo#foo (begin (foreign-lambda integer foo))))
>
> // By the way, I had to do a small change in crunch-driver.scm again, to
> get the
> // output in wrap.scm (see below)
>
> Now, with these 3 files (foo.c, foo.import.scm, wrap.scm), what is the
> right way
> to build foo-test.scm with csc?
>
> Thanks for your help.
>
> Best,
> -Diogo
>
> --
> The following patch allows me to get some output with -emit-wrappers.
>
> Index: crunch-driver.scm
> ===================================================================
> --- crunch-driver.scm   (revision 45088)
> +++ crunch-driver.scm   (working copy)
> @@ -58,7 +58,7 @@
>    (define (emit-wrappers out)
>      (lambda (mod exps)
>        (if mod
> -          (emit-scheme-wrappers/module (##sys#module-name mod) exps out)
> +          (emit-scheme-wrappers/module mod exps out)
>            (emit-scheme-wrappers/exported exps out))))
>
>    (define short-options (string->list "wxvdJ"))
>
>

Reply via email to