gerbil icon indicating copy to clipboard operation
gerbil copied to clipboard

Compiling R7RS code to executable

Open Retropikzel opened this issue 3 weeks ago • 7 comments

I noticed that on the website the --r7rs flag is only for running scripts with interpreter, is there something similar for compiling R7RS code?

Assuming files. main.scm

(import (scheme base)
        (foo bar))
(hello)

foo/bar.sld

(define-library
  (foo bar)
  (import (scheme base)
          (scheme write))
  (export hello)
  (include "bar.scm"))

foo/bar.scm

(define (hello)
  (display "Hello world")
  (newline))

You can compile it with different R7RS supporting implementations like so:

  • Loko
    • loko -std=r7rs --compile main.scm
  • Cyclone
    • cyclone main.scm
  • Chicken
    • csc -R r7rs -X r7rs -c -J -static -unit foo.bar -o foo.bar.a foo/bar.sld
    • csc -R r7rs -X r7rs -static -uses foo.bar main.scm

They all produce main executable which can then be just run with ./main

Retropikzel avatar Nov 29 '25 15:11 Retropikzel

This is an oversight, the compiler should support both --r7rs and general custom preludes.

I will make sure this is implemented before release, hopefully sooner.

vyzo avatar Nov 29 '25 15:11 vyzo

We should also add support for recognizing .sld files as r7rs by default, maybe for the .scm files too so that you don't even have to specify the prelude flag and it just works out of the box.

vyzo avatar Nov 29 '25 16:11 vyzo

another UX improvement detail should be automatic main (or not requiring a main at all) for r7rs.

Since you are well versed with multiple r7rs implementations, what other flags are common?

vyzo avatar Nov 29 '25 16:11 vyzo

Most implementations also have flags for adding directories into the beginning of load path and at the end of load path. Imagine your foo directory was in ~/libraries. Then you could run something like: gxc --r7rs -I ~/libraries -o main main.scm

If the flags -I and -A are free then I recommend them. They are used often in new implementations and they come from https://srfi.schemers.org/srfi-138/srfi-138.html

Edit: I noticed that there is GERBIL_LOADPATH, but sometimes it can be handy to have the option to put the library path in front of the list, or at the end of the list. For example an SRFI library in the directory can break things if it is loaded first.

Retropikzel avatar Nov 29 '25 16:11 Retropikzel

yeah, both -I and -A are free. -I is easy to add and makes sense, we should also add it to gxi.

What does -A do? like -I but append instead of prepend?

vyzo avatar Nov 29 '25 18:11 vyzo

ok i see, prepend and append indeed. I will add them.

vyzo avatar Nov 29 '25 18:11 vyzo

I will also add -D, we already use it in gxtest.

vyzo avatar Nov 29 '25 18:11 vyzo