~dieggsy/r7rs-tools

db7396da60949497a816c9e96cfe919b937c69ef — dieggsy 2 years ago 4416e7b
Cleanup and documentation
6 files changed, 94 insertions(+), 32 deletions(-)

M r7csc.scm
D r7csi-bin.scm
M r7csi.scm
A r7rs-env.scm
M r7rs-tools.egg
A r7rs-tools.svnwiki
M r7csc.scm => r7csc.scm +2 -1
@@ 9,7 9,8 @@
         (scheme base)
         (chicken foreign))

 (define r7rs-options '("-x" "-X" "r7rs" "-R" "r7rs" "-uses" "library" "-uses" "eval"))
 (define r7rs-options '("-x" "-X" "r7rs" "-R" "r7rs" "-uses" "library" "-uses" "eval"
                        "-X" "r7rs-env" "-R" "r7rs-env"))

 (define (compile)
   (let*-values ([(csc) (foreign-value "C_CSC_PROGRAM" c-string)]

D r7csi-bin.scm => r7csi-bin.scm +0 -20
@@ 1,20 0,0 @@
(module r7csi ()
 (import (only (chicken process-context) get-environment-variable command-line-arguments)
         (only (chicken process) process-execute)
         (only (chicken base) exit)
         (only (chicken pathname) make-pathname)
         (only (chicken file) file-exists?)
         (scheme base)
         (chicken foreign))

 (define r7rs-options
   (append '("-R" "r7csi")))

 (define (interpret)
   (let*-values ([(csi) (foreign-value "C_CSI_PROGRAM" c-string)]
                 [(path) (foreign-value "C_INSTALL_BIN_HOME" c-string)]
                 [(csipath) (or (file-exists? (make-pathname path csi)) "csi")])
     (process-execute csipath
                      (append r7rs-options (command-line-arguments)))))

 (interpret))

M r7csi.scm => r7csi.scm +17 -5
@@ 1,8 1,20 @@
(module r7csi ()
  (import (scheme base)
          (scheme eval))
 (import (only (chicken process-context) get-environment-variable command-line-arguments)
         (only (chicken process) process-execute)
         (only (chicken base) exit)
         (only (chicken pathname) make-pathname)
         (only (chicken file) file-exists?)
         (scheme base)
         (chicken foreign))

  (define env (environment '(only chicken.base exit)))
  (##sys#current-environment (##sys#slot env 2))
  (##sys#macro-environment (##sys#slot env 2)))
 (define r7rs-options
   (append '("-R" "r7rs-env")))

 (define (interpret)
   (let*-values ([(csi) (foreign-value "C_CSI_PROGRAM" c-string)]
                 [(path) (foreign-value "C_INSTALL_BIN_HOME" c-string)]
                 [(csipath) (or (file-exists? (make-pathname path csi)) "csi")])
     (process-execute csipath
                      (append r7rs-options (command-line-arguments)))))

 (interpret))

A r7rs-env.scm => r7rs-env.scm +11 -0
@@ 0,0 1,11 @@
(module r7rs-env ()
  (import (scheme base)
          (scheme eval)
          (srfi 1))

  (define env (environment '(only chicken.base exit)))
  (##sys#current-environment (##sys#slot env 2))
  (##sys#macro-environment (filter-map
                            (lambda (e)
                              (and (list? e) e))
                            (##sys#slot env 2))))

M r7rs-tools.egg => r7rs-tools.egg +4 -6
@@ 1,10 1,8 @@
((synopsis "'Pure' r7rs compiler and interpreter")
((synopsis "\"Pure\" r7rs compiler and interpreter")
 (category tools)
 (author "Diego A. Mundo")
 (license "public domain")
 (dependencies r7rs)
 (components (program r7csi-bin
                      (csc-options "-O3" "-d0")
                      (install-name r7csi))
             (extension r7csi)
 (dependencies r7rs srfi-1)
 (components (program r7csi (csc-options "-O3" "-d0"))
             (extension r7rs-env)
             (program r7csc (csc-options "-O3" "-d0"))))

A r7rs-tools.svnwiki => r7rs-tools.svnwiki +60 -0
@@ 0,0 1,60 @@
[[tags: egg]]
[[toc:]]

== r7rs-tools

"Pure" r7rs compiler and interpreter.

"Pure" = only {{(scheme base)}} by default, no {{scheme}}, {{chicken.base}}, or
{{chicken.syntax}})

=== r7rs-env module

The core of {{r7rs-tools}}, uses a hack around {{##sys#current-environment}}
and {{##sys#macro-environment}} to set the environment to only include
procedures and syntax from {{(scheme base)}}. You can manually reset the
environment to {{(scheme base)}} by using {{(import r7rs-env)}}.

The following is the full implementation of this module:

<enscript highlight="scheme">
(module r7rs-env ()
  (import (scheme base)
          (scheme eval)
          (srfi 1))

  (define env (environment '(only chicken.base exit)))
  (##sys#current-environment (##sys#slot env 2))
  (##sys#macro-environment (filter-map
                            (lambda (e)
                              (and (list? e) e))
                            (##sys#slot env 2))))
</enscript>

=== r7csc program

The {{r7csc}} program is equivalent to the following invocation of {{csc}}:

  csc -x -X r7rs -R r7rs -uses library -X r7rs-env -R r7rs-env [args ...]

This makes {{csc}} compile with an initial environment containing only exports
of {{(scheme base)}}.

=== r7csi program

The {{r7csi}} program is equivalent to the following invocation of {{csi}}:

  csi -R r7rs-env [args ...]

This starts {{csi}} with an initial environment containing only exports of
{{(scheme base)}}

=== Caveats

The implementation currently uses CHICKEN internals because I couldn't really
find a satisfactory way to achieve it otherwise, which means it may be subject
to more instability with new updates to the core system.

I'm not an expert on CHICKEN's internals, so at the end of the day this is
simply a best effort through kludges and trial and error that seems to work.
More testing may still be required. Suggestions and improvements welcome!