Next: , Previous: Invoking the Pre-Scheme compiler, Up: Pre-Scheme


9.6 Example Pre-Scheme compiler usage

The ps-compiler/compile-vm.scm, ps-compiler/compile-gc.scm, and ps-compiler/compile-vm-no-gc.scm files give examples of running the Pre-Scheme compiler. They are Scheme48 command programs, to be loaded into the exec package after having already loaded the Pre-Scheme compiler. compile-vm.scm & compile-vm-no-gc.scm generate a new scheme48vm.c in the scheme/vm/ directory — compile-vm.scm includes the garbage collector, while compile-vm-no-gc.scm does not 1 —, and compile-gc.scm generates a new scheme48heap.c, scheme48read-image.c, & scheme48write-image.c in the scheme/vm/ directory.

Here is a somewhat simpler example. It assumes a pre-built image with the Pre-Scheme compiler loaded is in the ps-compiler.image file in the current directory (see Invoking the Pre-Scheme compiler, where there is a description of how to dump an image with the Pre-Scheme compiler loaded).

     % ls
     hello.scm               packages.scm            ps-compiler.image
     % cat hello.scm
     (define (main argc argv)
       (if (= argc 2)
           (let ((out (current-output-port)))
             (write-string "Hello, world, " out)
             (write-string (vector-ref argv 1) out)
             (write-char #\! out)
             (newline out)
             0)
           (let ((out (current-error-port)))
             (write-string "Usage: " out)
             (write-string (vector-ref argv 0) out)
             (write-string " <user>" out)
             (newline out)
             (write-string "  Greets the world & <user>." out)
             (newline out)
             -1)))
     % cat packages.scm
     (define-structure hello (export main)
       (open prescheme)
       (files hello))
     % scheme48 -i ps-compiler.image
     heap size 3000000 is too small, using 4770088
     Welcome to Scheme 48 1.3 (Pre-Scheme)
     Copyright (c) 1993-2005 by Richard Kelsey and Jonathan Rees.
     Please report bugs to scheme-48-bugs@s48.org.
     Get more information at http://www.s48.org/.
     Type ,? (comma question-mark) for help.
     > (prescheme-compiler 'hello '("packages.scm") 'hello-init "hello.c")
     packages.scm
      hello.scmChecking types
      main : ((integer **char) -> integer)
     In-lining single-use procedures
     Call Graph:
     <procedure name>
       <called non-tail-recursively>
       <called tail-recursively>
     main  (exported)
     Merging forms
     Translating
      main
     #{Unspecific}
     > ,exit
     % cat hello.c
     #include <stdio.h>
     #include "prescheme.h"
     
     long main(long, char**);
     
     
     long main(long argc_0X, char **argv_1X)
     {
       FILE * out_3X;
       FILE * out_2X;
      {  if ((1 == argc_0X)) {
         out_2X = stdout;
         ps_write_string("Hello, world, ", out_2X);
         ps_write_string((*(argv_1X + 1)), out_2X);
         { long ignoreXX;
         PS_WRITE_CHAR(33, out_2X, ignoreXX) }
         { long ignoreXX;
         PS_WRITE_CHAR(10, out_2X, ignoreXX) }
         return 0;}
       else {
         out_3X = stderr;
         ps_write_string("Usage: ", out_3X);
         ps_write_string((*(argv_1X + 0)), out_3X);
         ps_write_string(" <user>", out_3X);
         { long ignoreXX;
         PS_WRITE_CHAR(10, out_3X, ignoreXX) }
         ps_write_string("  Greets the world & <user>.", out_3X);
         { long ignoreXX;
         PS_WRITE_CHAR(10, out_3X, ignoreXX) }
         return -1;}}
     }
     %

Footnotes

[1] The actual distribution of Scheme48 separates the garbage collector and the main virtual machine.