Subject: Re: Problems with my interpreter
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 27 Apr 2001 03:58:03 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9caqob$3hbju$1@fido.engr.sgi.com>
<mariom@newknow.com> wrote, re MIT Scheme:
+---------------
| The problem is that i can just use it at command line as with a python
| interactive mode, but i cant interprete it as a solely file, that is i 
| cant do something like:    scheme my_program.scm
| Do you know something about another tool which solves my problem?
+---------------

MzScheme (part of DrScheme <URL:http://www.drscheme.org/>)
lets you do that, using the "-r" option:

    % cat foo.scm
    (display "Hello, world!")
    (newline)
    % mzscheme -r foo.scm
    Hello, world!
    % 

If you just want to pre-load the file, but still go into interactive mode,
use the "-f" option:

    % mzscheme -f foo.scm
    Welcome to MzScheme version 101, Copyright (c) 1995-99 PLT (Matthew Flatt)
    Hello, world!
    > (* 2 3)
    6
    > ^D

Finally, under Unix you can make your scripts be executable "programs"
themselves, using the "#!" convention[*]:

    % cat foo
    #!/usr/local/bin/mzscheme -r
    (display "Hello, world!")
    (newline)
    % ./foo
    Hello, world!
    % 

In the latter case, remaining command-line arguments are available in
the pre-defined global variable "argv" (a vector of strings):

    % cat foo2
    #!/usr/local/bin/mzscheme -r
    (display "Hello, world!")
    (newline)
    (display "Here are my args: ")
    (write argv)
    (newline)
    % ./foo2
    Hello, world!
    Here are my args: #0()
    % ./foo2 bar baz -q gorp "some spaces here"
    Hello, world!
    Here are my args: #5("bar" "baz" "-q" "gorp" "some spaces here")
    % 


-Rob

[*] Note that this only works as shown because I've made
    /usr/local/bin/mzscheme on my system be a direct link
    to /usr/local/lib/plt/.bin/mips-irix/mzscheme, rather
    than using the architecture-independent "wrapper" script
    that comes with the PLT distribution. If you can't or
    prefer not to do that, you can use the following hack
    (modified from plt/collects/doc/help/scheme/misc/script.html).

    #!/bin/sh
    eval ; exec /usr/local/lib/plt/bin/mzscheme -r "$0" ${1+"$@"}
    (display "Hello, world!")
    (newline)
    (display "Here are my args: ")
    (write argv)
    (newline)

-----
Rob Warnock, 31-2-510		rpw3@sgi.com
SGI Network Engineering		<URL:http://reality.sgi.com/rpw3/>
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA