我想尝试一些扩展Lisp的(计划,球拍,Clojure的,有的话)来运行外部命令如下:
; having
(define foo ...)
(define bar ...)
; on command
(ls (foo bar) baz)
; this lisp should evaluate (foo bar) as usual, with result "foobar", then
(ls foobar baz)
; here "ls" is not defined
; instead of rising "undefined identifier" exception
; it must look for "ls" command in the directories
; in the "PATH" environment variable
; and launch the first found "ls" command
; with strings "foobar" and "baz" on input
我只是想无论如何运行,而无需携带正确的转换,从Lisp的数据结构来串或处理退出码和命令的stdout/stderr
输出。
我认为这是没有办法正常的环境(像抓住了“不确定”之外,所有的时间)内将其扩展。解释本身的eval
程序必须改变。
这Lisp是最好的扩展它这样,它是如何做的?也许已经存在执行类似的项目?
在拍你可以重写#%top
:
#lang racket
(provide
(combine-out
(except-out (all-from-out racket) #%top)
(rename-out [shell-curry #%top])))
(require racket/system)
(define (stringify a)
(~a (if (cmd? a) (cmd-name a) a)))
(struct cmd (name proc)
#:property prop:procedure
(struct-field-index proc)
#:transparent
#:methods gen:custom-write
[(define (write-proc x port mode)
(display (string-append "#<cmd:" (stringify x) ">") port))])
(define (shell name)
(define (cmd-proxy . args)
(define cmd
(string-join (map stringify (cons name args))
" "))
(system cmd))
cmd-proxy)
(define-syntax shell-curry
(syntax-rules ()
((_ . id)
(cmd 'id (shell 'id)))))
保存为shell.rkt使这个runner.rkt在同一目录下:
#lang s-exp "shell.rkt"
(define test (list /bin/ls /usr/bin/file))
(second test) ; ==> #<cmd:/usr/bin/file>
(first test) ; ==> #<cmd:/bin/ls>
((second test) (first test))
; ==> t (prints that /bin/ls is an executable on my system)
现在,从这里,使之成为#lang myshell
或类似的东西是很容易的。
Common Lisp的有一个标准误差系统可用于实现。
在普通的Lisp实现,其提供一个或use-value
重启store-value
类型undefined-function
的误差。
例
CL-USER 69 > (flet ((call-use-value-restart (c)
(use-value (lambda (arg)
(format t "~%dummy function with arg ~a~%" arg))
c)))
(handler-bind ((undefined-function #'call-use-value-restart))
(this-function-does-not-exist "foo")))
dummy function with arg foo
NIL
在上面的例子中,函数this-function-does-not-exist
不存在。正如你所看到的,错误处理,另一个函数被调用,而不是,然后做一些输出。
如果我们呼吁自己的未定义的功能,我们得到一个错误:
CL-USER 70 > (this-function-does-not-exist "foo")
Error: Undefined operator THIS-FUNCTION-DOES-NOT-EXIST in form (THIS-FUNCTION-DOES-NOT-EXIST "foo").
1 (continue) Try invoking THIS-FUNCTION-DOES-NOT-EXIST again.
2 Return some values from the form (THIS-FUNCTION-DOES-NOT-EXIST "foo").
3 Try invoking something other than THIS-FUNCTION-DOES-NOT-EXIST with the same arguments.
4 Set the symbol-function of THIS-FUNCTION-DOES-NOT-EXIST to another function.
5 Set the macro-function of THIS-FUNCTION-DOES-NOT-EXIST to another function.
6 (abort) Return to top loop level 0.
Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.
CL-USER 71 : 1 >
我们的例子基本呼叫起动次数3编程:
它结合当类型call-use-value-restart
的错误发生,其调用函数undefined-function
的处理程序。
该函数然后call-use-value-restart
调用use-value
重启它提供的功能。在这里,您可以提供调用由(cell-error-name c)
给出的名称的外部程序的功能。该use-value
重启然后就调用所提供的功能,将继续执行该计划照常进行。
提示一个解决方案
典型地,一个会写在设置这样的处理程序小的顶层循环。
另一种方式来调用重启
在这个例子中,我们用一个钩子增加的情况下,错误发生的处理程序。这里我们使用全局变量*debugger-hook*
。这应该是一个函数,在我们的情况下,当条件c
的类型是undefined-function
的调用一个新的功能。
* (defun provide-a-function-hook (c hook)
(declare (ignore hook))
(typecase c
(undefined-function (use-value (lambda (arg)
(format t "~%dummy function with arg ~a~%" arg))
c))))
PROVIDE-A-FUNCTION-HOOK
* (setf *debugger-hook* #'provide-a-function-hook)
#<FUNCTION PROVIDE-A-FUNCTION-HOOK>
* (this-function-does-not-exist "foo")
; in: THIS-FUNCTION-DOES-NOT-EXIST "foo"
; (THIS-FUNCTION-DOES-NOT-EXIST "foo")
;
; caught STYLE-WARNING:
; undefined function: THIS-FUNCTION-DOES-NOT-EXIST
;
; compilation unit finished
; Undefined function:
; THIS-FUNCTION-DOES-NOT-EXIST
; caught 1 STYLE-WARNING condition
dummy function with arg foo
NIL