Common Lisp 中 reader 宏的限制是什么
What are limitations of reader macros in Common Lisp
我在 JavaScript 中有自己的 Lisp 解释器,我现在工作了一段时间,现在我想像 Common Lisp 一样实现 reader 宏。
我已经创建了 Streams(除了像 ,@ , ` '
这样的特殊符号几乎可以正常工作)但是当它加载包含脚本的页面时它会冻结浏览器几秒钟(有 400 行代码的 lisp 文件) ).这是因为我的流是基于子串函数的。如果我先拆分令牌,然后使用迭代令牌的 TokenStream,它工作正常。
所以我的问题是,字符串流真的是 Common Lisp 中的东西吗?你能在 CL 中添加 reader 宏来创建像 Python 这样的全新语法吗,这简化了我可以实现 """
宏的问题(不确定你是否可以有 3 个字符作为 reader 宏)或其他将在 lisp 中实现模板文字的字符,例如:
(let ((foo 10) (bar 20))
{lorem ipsum ${baz} and ${foo}})
或
(let ((foo 10) (bar 20))
""lorem ipsum ${baz} and ${foo}"")
或
(let ((foo 10) (bar 20))
:"lorem ipsum ${baz} and ${foo}")
会产生字符串
"lorem ipsum 10 and 20"
在 Common Lisp 中可以实现类似的功能吗?将 #\{
或 #\:
实现为 reader 宏有多难?
我能想到的在 Lisp 中使用模板文字的唯一方法是这样的:
(let ((foo 10) (bar 20))
(tag "lorem ipsum ${baz} and ${foo}")))
其中标记是 return 以 ${} 作为自由变量的字符串的宏。 reader 宏也可以 return 被评估的 lisp 代码吗?
还有一个问题,你能不能像这样实现 reader 宏:
(list :foo:bar)
(list foo:bar)
其中:是 reader 宏,如果它在符号之前,它将符号转换为
foo.bar
如果它在里面就会抛出错误。我问这个是因为基于标记的宏 :foo:bar
和 foo:bar
将是符号,不会被我的 reader 宏处理。
还有一个问题reader宏可以放在第一行第二行使用吗?这绝对只有在字符串流中才有可能,而我测试过的用 JavaScript.
编写的解释器是不可能的在某种意义上存在一些限制,例如,以 'implement your own token interpreter from scratch' 之外的任何方式干预令牌的解释是非常困难的。但是,好吧,如果你想这样做,你可以这样做:问题是你的代码需要像现有代码一样处理数字和事物,而 floating-point 解析之类的东西非常繁琐才能正确.
但是与宏字符关联的宏函数获取正在读取的流,并且它们可以自由读取尽可能多或尽可能少的流,以及 return 任何类型的对象 (或者没有对象,这就是注释的实现方式。
我强烈建议阅读 hyperspec 的 2 & 23 章,然后尝试实现。当您使用该实现时请注意,通过乱搞 reader 来完全楔入东西是非常容易的。至少我会建议这样的代码:
(defparameter *my-readtable* (copy-readtable nil))
;;; Now muck around with *my-readtable*, *not* the default readtable
;;;
(defun experimentally-read ((&key (stream *standard-input*)
(readtable *my-raedtable*)))
(let ((*readtable* readtable))
(read stream)))
这至少给了你一些从灾难中恢复过来的机会:如果你可以一次中止 experimentally-read
那么你就回到了 *readtable*
是明智的位置。
这是一个相当无用的例子,它显示了你可以用宏字符破坏多少语法:一个宏字符定义将导致 ( ...)
被读取为一个字符串。这可能没有完全调试,正如我所说,我看不出它有什么用。
(defun mindless-parenthesized-string-reader (stream open-paren)
;; Cause parenthesized groups to be read as strings:
;; - (a b) -> "a b"
;; - (a (b c) d) -> "a (b c) d"
;; - (a \) b) -> "a ) b"
;; This serves no useful purpose that I can see. Escapes (with #\))
;; and nested parens are dealt with.
;;
;; Real Programmers would write this with LOOP, but that was too
;; hard for me. This may well not be completely right.
(declare (ignore open-paren))
(labels ((collect-it (escaping depth accum)
(let ((char (read-char stream t nil t)))
(if escaping
(collect-it nil depth (cons char accum))
(case char
((#\)
(collect-it t depth accum))
((#\()
(collect-it nil (1+ depth) (cons char accum)))
((#\))
(if (zerop depth)
(coerce (nreverse accum) 'string)
(collect-it nil (1- depth) (cons char accum))))
(otherwise
(collect-it nil depth (cons char accum))))))))
(collect-it nil 0 '())))
(defvar *my-readtable* (copy-readtable nil))
(set-macro-character #\( #'mindless-parenthesized-string-reader
nil *my-readtable*)
(defun test-my-rt (&optional (stream *standard-input*))
(let ((*readtable* *my-readtable*))
(read stream)))
现在
> (test-my-rt)
12
12
> (test-my-rt)
x
x
> (test-my-rt)
(a string (with some parens) and \) and the end)
"a string (with some parens) and ) and the end"