2013-06-12 43 views
1

我想寫一些特殊的讀者宏:專門的閱讀器宏象[] {}

[hello "world"] ; <=> (funcall #'|hello| "world") 
{hello "my" ("world")} ; <=> (apply #'|hello| "my" ("world")) 

這能實現?你會怎麼做?

回答

6

是的,你想要的術語是readtableCommon Lisp HyperSpec chapter 23Common Lisp HyperSpec chapter 2談論相關概念)。

您需要先定義一個函數來讀取您感興趣的數據,然後以您想要的形式返回它。

(defun read-case-preserve-funcall-or-apply (stream char) 
    (let ((preserved-readtable-case (readtable-case *readtable*))) 
    (setf (readtable-case *readtable* :preserve)) 
    (let ((tmp (read-delimited-list (if (char= char #\[) #\] #\}) stream t))) 
     (let ((fun (car tmp)) 
     (args (cdr tmp))) 
    (cond ((char= char #\[) `(funcall (function ,fun) ,@args)) 
      ((char= char #\{) `(apply (function ,fun) ,@args))))))) 

之後,你需要把它掛到readtable和()一些語法標記複製到新的分隔符。

+0

非常感謝! –

+2

我還建議使用[named-readtales](http://common-lisp.net/project/named-readtables/)庫來完成它,它將允許您編寫不同的可讀取的修改,並幫助避免混亂其他人的 –