Hi Anton,

On Mon, Dec 01, 2025 at 10:10:44PM +0100, Anton Idukov wrote:
> Hi, Diogo. As I understand, Crunch is a transpiler from a narrow subset of
> Scheme lang to C 1-to-1,

Yes, and this is why it is so appealing! I already know how to create a
C+crunch program/library with multiple crunch modules.

> so import such code in  Scheme back is
> foreign-function-work. emit-wrappers is kinda automation of such.

Right. That is what I would like to have. The final program should only be in
crunch, having no chicken runtime. I want to have chicken just to test the
crunch modules.

So in the previous example I sent, this is the generated wrapper:

    ;; foo-wrap.scm - created with
    ;;       chicken-crunch -emit-wrappers foo-wrap.scm foo.scm -o foo.c
    (module
      foo
      (foo)
      (import (scheme base) (chicken foreign))
      (define foo#foo (begin (foreign-lambda integer foo))))

I just don't know how to get a chicken library out of this file (plus the
crunched foo.c). Trying to translate it to .c gives me

    % csc -t foo-wrap.scm
    Warning: Exported identifier `foo' has not been defined.
    Error: module unresolved: foo

May be the generated wrapper isn't correct? How should it look like?

> But the easy way is  (crunch ...) form, for example

Yes. But would that make the module be rather a chicken module, right? I want
to have no chicken runtime except in the tests.

> 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)

I know what you mean :) A while back I got this ps2decoder written in crunch to
to run on a microcontroller: https://github.com/db7/crunch-ps2decoder

It ends up being quite verbose for the task, but it has its charm.

-d

--
> пн, 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