给定LISP eval函数的以下定义 - 添加defmacro函数需要什么?(甚至只是评估一个宏)
(defun null. (x)
(eq x '()))
(defun and. (x y)
(cond (x (cond (y 't) ('t '())))
('t '())))
(defun not. (x)
(cond (x '())
('t 't)))
(defun append. (x y)
(cond ((null. x) y)
('t (cons (car x) (append. (cdr x) y)))))
(defun list. (x y)
(cons x (cons y '())))
(defun pair. (x y)
(cond ((and. (null. x) (null. y)) '())
((and. (not. (atom x)) (not. (atom y)))
(cons (list. (car …Run Code Online (Sandbox Code Playgroud) 我在node.js上实现了自己的Lisp,我可以像这样运行s表达式:
(assert (= 3 (+ 1 2))) (def even? (fn [n] (= 0 (bit-and n 1)))) (assert (even? 4)) (assert (= false (even? 5)))
现在我想添加宏 - defmacro功能 - 但这是我被卡住的地方.我想知道的宏系统是如何在其他的Lisp实现,但我无法找到许多指针(除了这个和这个).
我看过Clojure宏系统 - 我最熟悉的Lisp - 但这看起来太复杂了,我找不到我可以轻易应用的其他线索(Clojure宏最终编译成不适用的字节码对于javascript,我也无法理解这个macroexpand1功能.)
所以我的问题是:给定一个没有宏但带有AST的Lisp实现,如何添加像Clojure宏系统这样的宏系统?这个宏系统可以在Lisp中实现,还是在宿主语言的实现中需要额外的功能?
还有一句话:我还没有实现quote(')因为我无法弄清楚返回列表中应该包含哪种值.如果它包含AST的元件或类似的对象Symbol和Keyword(后者是用于Clojure的情况下)?