View
3.236
Download
0
Category
Preview:
DESCRIPTION
How to insert additional strings during the pretty printing.
Citation preview
BeforeLISPs
Just Part of the Past
〜 #9 cloaking printing〜
Syntax Tree Diff
(defun iota (n) (let (lst) (dotimes (i n (nreverse lst)) (push i lst))))
(defun iota (n &optional (start 0)) (let (lst) (dotimes (i n (nreverse lst)) (push (+ i start) lst)))))
Inner Representation
((#:REF857 0 0) (#:REF857 1 0) ((#:REF857 0 2 0) &OPTIONAL (START 0)) ((#:REF857 0 3 0) (#:REF857 1 3 0) ((#:REF857 0 2 3 0) (#:REF857 1 2 3 0) ((#:REF857 0 2 2 3 0) (#:LOST858 1 2 2 3 0) (+ I START) (#:REF857 2 2 2 3 0)))))
Pretty Print
(DEFUN IOTA (N &OPTIONAL (START 0)) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH I (+ I START) LST))))
Another canditate
(defun iota (n) (let (lst) (dotimes (i n (nreverse lst)) (push i lst))))
(defun iota (n &optional (start 0)) (let (lst) (dotimes (i n (nreverse lst)) (push (+ i start) lst)))))
inner representation
((REF 0 0) (REF 1 0) ((REF 0 2 0) &OPTIONAL (START 0)) ((REF 0 3 0) (REF 1 3 0) ((REF 0 2 3 0) (REF 1 2 3 0) ((REF 0 2 2 3 0) (+ (REF 1 2 2 3 0) START) (REF 2 2 2 3 0)))))
Pretty Print
(DEFUN IOTA (N &OPTIONAL (START 0)) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH (+ I START) LST))))
HTMLで出力
<html><head><title> diff </title></head><body bgcolor="000000" text="ffffff"><p><pre>(DEFUN IOTA (N <font color="00FF00">&OPTIONAL</font> <font color="00FF00">(START 0)</font>) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH <font color="FF0000">I</font> <font color="00FF00">(+ I START)</font> LST))))</pre></p></body></html>
ターミナルに出力
(DEFUN IOTA (N ^[[32m&OPTIONAL^[[0m ^[[32m(START 0)^[[0m) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH ^[[31mI^[[0m ^[[32m(+ I START)^[[0m LST))))
解決策1● 色を付けたいところに目印をつけながら文字列にpretty printして、あとから置換する
– なるべくpretty printを邪魔しないように、目印に
は小さなシンボルを使う
(DEFUN IOTA (N ({ &OPTIONAL }) ({ (START 0) })) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH ([ I ]) ({ (+ I START) }) LST))))
({x}) ^[[32xm^[[0m => → x([y]) ^[[31xm^[[0m => → y
問題点
● 目印がPretty Printingに影響する
ターミナルに出力
(DEFUN IOTA (N ^[[32m&OPTIONAL^[[0m ^[[32m(START 0)^[[0m) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH ^[[31mI^[[0m ^[[32m(+ I START)^[[0m LST))))
(DEFUN IOTA (N ^[[32m&OPTIONAL^[[0m ^[[32m (START 0) ^[[0m ^[[32 (STEP 1) ^[[0m) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH ^[[31mI^[[0m ^[[32m (+ I START) ^[[0m LST))))
ターミナルに出力
(DEFUN IOTA (N ^[[32m&OPTIONAL^[[0m ^[[32m(START 0)^[[0m) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH ^[[31mI^[[0m ^[[32m(+ I START)^[[0m LST))))
(DEFUN IOTA (N ^[[32m&OPTIONAL^[[0m ^[[32m (START 0) ^[[0m ^[[32 (STEP 1) ^[[0m) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH ^[[31mI^[[0m ^[[32m (+ I START) ^[[0m LST))))
問題点
● 目印がPretty Printingに影響する
● 一度構造を破壊して文字列にする– 目印が目印でないものと衝突している可能性
● つまりかっこわるい
Cloaking Pretty Printing
● 出力の一部を覆い隠し、Pretty Printingに
影響を与えないようにする
● 72文字書ける場所に、120文字書いて70文字と
申告する
● 既存のPretty Printerを改造する
clojure.pprint
● Clojureのpretty print● Clojure1.2 : clojure.contrib.print● Clojure1.3〜 : clojure.pprint● デフォルト : off
clojure.pprintの使用例(with-pprint-dispatch code-dispatch (pprint '(defn- pprint-code-list [alis] (if-not (pprint-reader-macro alis) (if-let [special-form-specialized-format (*code-table* (first alis))] (special-form-specialized-format alis) (pprint-simple-code-list alis))))))
(defn- pprint-code-list [alis] (if-not (pprint-reader-macro alis) (if-let [special-form-specialized-format (*code-table* (first alis))] (special-form-specialized-format alis) (pprint-simple-code-list alis))))
clojure.pprintの実装● 実装がちょっと妙
– SymbolはTokenで管理するのに、区切りのspaceは直接書き込む
– Tokenには:trailing-white-spaceフィールド
があり、格納もされる
– しかし使われない
Cloaking Printing● clojure.pprintを改造してCloaking Printingを実
装
● Cloaking対象とする単語(の正規表現)のリストを
*cloaked-words*に動的に束縛
● Cloaking対象の単語の直後に続く空白もCloakingする
● Clojureで書き方がわからないところは適当
使用例
(with-pprint-dispatch code-dispatch (binding [*cloaked-words* (list #"special-form-specialized-format" #"first")] (pprint '(defn- pprint-code-list [alis] (if-not (pprint-reader-macro alis) (if-let [special-form-specialized-format (*code-table* (first alis))] (special-form-specialized-format alis) (pprint-simple-code-list alis)))))))
(defn- pprint-code-list [alis] (if-not (pprint-reader-macro alis) (if-let [special-form-specialized-format (*code-table* (first alis))] (special-form-specialized-format alis) (pprint-simple-code-list alis))))
CLerがClojureを使った感想● 様々なオブジェクトが関数として使える
– 予想 - 便利そう
– 感想 - 読みにくい。特に、関数に渡す場合でもCLでいう#'のよう
なものが不要なので「これは述語?それともオブジェクト?」と悩む
● カッコの種類が多い
– 予想 - 煩わしい
– 感想 - 予想通りだった。特に、閉じるときにどの閉じ括弧を入力す
るのか迷う
Recommended