从终端命令提示符处运行Common Lisp函数 [英] Running a Common Lisp function from a Terminal command prompt

查看:85
本文介绍了从终端命令提示符处运行Common Lisp函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到答案,所以也许是不可能的.我希望能够从命令行加载/编译lisp文件(即不在emacs内),然后再从命令行运行该文件中的lisp函数之一的灵活性.毫无疑问,这是特定于实现的功能,因此任何提供此功能的实现的指针(或者也许是相当标准的,我不知道).我正在使用SBCL并喜欢它,所以如果能这样做,那就太好了.

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.

我也在使用Mac OSX和Terminal.

Also I'm using Mac OSX and Terminal.

推荐答案

SBCL手册介绍了三个有用的选项

The SBCL manual describes three useful options

3.3.1运行时选项

--noinform
禁止打印任何横幅或其他 启动时的参考消息.这使得编写Lisp更容易 在Unix管道中可以正常工作的程序.另请参阅--noprint--disable-debugger选项.

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顶级选项

--eval command
在执行任何初始化文件之后,但在对标准输入启动read-eval-print循环之前,请进行读取和评估 给出的命令.可以使用多个--eval选项,并且所有 将按照它们在命令中出现的顺序进行读取和执行 线.

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
这等效于--eval '(load "filename")'.特殊语法旨在减少调用时的引用头痛 Shell脚本中的SBCL.

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

提供一个包含内容的文件test.lisp

Given a file test.lisp with contents

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

我们可以使用SBCL做到这一点:

we can do this with SBCL:

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

HELLO-WORLD 

(progn ... (sb-ext:quit))确保执行(hello-world)后程序结束.否则,您将进入SBCL提示符.由于代码是在SBCL中自动编译的,因此在运行(hello-world)时,已经在编译您正在运行的函数.如果预先编译了文件,则可以将编译后的文件传递给--load.例如

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 

实际上,考虑到--load--eval (load "filename")的等效性,您可以仅使用文件名的基数,如果有编译版本,则SBCL应加载该文件,如果没有,则SBCL将加载该文件.加载源文件,您将通过这种方式获得编译后的代码.例如,在下面,我们仅使用--load test:

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 

这篇关于从终端命令提示符处运行Common Lisp函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆