Running a Common Lisp function from a Terminal command prompt
Asked Answered
P

4

15

I'm having some difficulty finding an answer to this, so maybe it isn't possible. I'd like the flexibility of being able to load/compile a lisp file from a command line, i.e. not inside emacs, and then also run one of the lisp functions in that file also from the command line. This is no doubt implementation specific feature, so any pointers on an implementation that offers this (or perhaps it is fairly standard, I don't know). I'm using SBCL and like it, so it would be great if that could do this.

Also I'm using Mac OSX and Terminal.

Pentyl answered 30/11, 2013 at 14:56 Comment(0)
S
24

The SBCL manual describes three useful options

3.3.1 Runtime Options

--noinform
Suppress the printing of any banner or other informational message at startup. This makes it easier to write Lisp programs which work cleanly in Unix pipelines. See also the --noprint and --disable-debugger options.

3.3.2 Toplevel Options

--eval command
After executing any initialization file, but before starting the read-eval-print loop on standard input, read and evaluate the command given. More than one --eval option can be used, and all will be read and executed, in the order they appear on the command line.

--load filename
This is equivalent to --eval '(load "filename")'. The special syntax is intended to reduce quoting headaches when invoking SBCL from shell scripts.

Given a file test.lisp with contents

(defun hello-world ()
  (print 'hello-world)
  (terpri))

we can do this with SBCL:

$ sbcl --noinform --load test.lisp --eval '(progn (hello-world) (sb-ext:quit))'

HELLO-WORLD 

The (progn ... (sb-ext:quit)) makes sure that the program ends after executing (hello-world). Otherwise you get dropped into the SBCL prompt. Since code is compiled automatically in SBCL, the function that you're running is already compiled by the time (hello-world) is run. If you've compiled the file in advance, you can pass the compiled file to --load. E.g.,

$ sbcl --noinform --load test.fasl --eval '(hello-world)'

HELLO-WORLD 

In fact,given the equivalence of --load to --eval (load "filename"), you can just use the base of the file name, and if there's a compiled version, then SBCL should load that, and if there's not, then SBCL will load the source file and you'll get compiled code that way. E.g., in the following, we use just --load test:

$ sbcl --noinform --load test --eval '(hello-world)'

HELLO-WORLD 
Surname answered 30/11, 2013 at 16:44 Comment(0)
T
5

The following is a paste from the first google result:

CLISP

If you're using the CLISP Common Lisp environment, the following applies.

To compile a program named test.lisp program into a bytecode file named test.fas, do the following.

clisp -c test.lisp

If you want less output to be shown, do the following instead.

clisp -q -c test.lisp

To run the compiled (or even uncompiled, if you skip the above step) Lisp file, do the following, assuming that your entry function is named main. Normally the result of the main function is shown when it's done, but the (quit) command prevents that. The -on-error abort option prevents clisp from dropping into a debugging prompt, and exits instead when there is an error.

clisp -q -q -on-error abort -x '(progn (load "test") (main) (quit))'

I just tested it on my arch linux terminal and you can do the following:

$ clisp myprogram.lisp

This will run the program right in the terminal. If you want to compile it to run later, see the above bit.

Towpath answered 30/11, 2013 at 15:22 Comment(3)
interesting, thanks. I didn't find anything as clear for SBCL, which I'd prefer to use over Clisp for performance reasons.Pentyl
@OpenLearner, perhaps sbcl --eval '(compile-file "filename")'. At least, its manual says that --load filename does the equivalent of --eval '(load "filename")'.Striking
Well, I tried looking for an answer and the closest I got for you is this: ` $ sbcl --script filename.lisp` This still prints a big debugger message though, I couldn't get rid of it.Towpath
S
1

As other people have shown, pretty much all implementations have some of running Lisp code via the terminal. There are also some wrappers that provide a common interface to many implementations: CIM and cl-launch.

Smirk answered 9/6, 2015 at 18:23 Comment(0)
B
1

I am trying to integrate sbcl Common-Lisp into R through knitr. Here are few tests on how to send a request for evaluation to sbcl and then return it to R. This works for sending forms, not a file. From the terminal:

sbcl --noinform --eval '(progn (print (* 2 3 4 5)) (sb-ext:quit))'
;; 120

sbcl --noinform --eval '(progn (print (* 2 3 4 5)) (sb-ext:quit))'
;; HELLO-WORLD

sbcl --noinform --eval "(progn (dotimes (i 5) (print i)) (sb-ext:quit))"
;; 0
;; 1
;; 2
;; 3
;; 4

Booted answered 8/2, 2020 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.