21
Before LISPs Just Part of the Past #9 cloaking printing

Before LISPs just Part of the Past ~ #9 cloaking printing ~

Embed Size (px)

DESCRIPTION

How to insert additional strings during the pretty printing.

Citation preview

Page 1: Before LISPs just Part of the Past ~ #9 cloaking printing ~

BeforeLISPs

Just Part of the Past

〜 #9 cloaking printing〜

Page 2: Before LISPs 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)))))

Page 3: Before LISPs just Part of the Past ~ #9 cloaking printing ~

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)))))

Page 4: Before LISPs just Part of the Past ~ #9 cloaking printing ~

Pretty Print

(DEFUN IOTA (N &OPTIONAL (START 0)) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH I (+ I START) LST))))

Page 5: Before LISPs just Part of the Past ~ #9 cloaking printing ~

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)))))

Page 6: Before LISPs just Part of the Past ~ #9 cloaking printing ~

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)))))

Page 7: Before LISPs just Part of the Past ~ #9 cloaking printing ~

Pretty Print

(DEFUN IOTA (N &OPTIONAL (START 0)) (LET (LST) (DOTIMES (I N (NREVERSE LST)) (PUSH (+ I START) LST))))

Page 8: Before LISPs just Part of the Past ~ #9 cloaking printing ~

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>

Page 9: Before LISPs just Part of the Past ~ #9 cloaking 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))))

Page 10: Before LISPs just Part of the Past ~ #9 cloaking printing ~

解決策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

Page 11: Before LISPs just Part of the Past ~ #9 cloaking printing ~

問題点

● 目印がPretty Printingに影響する

Page 12: Before LISPs just Part of the Past ~ #9 cloaking 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))))

Page 13: Before LISPs just Part of the Past ~ #9 cloaking 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))))

Page 14: Before LISPs just Part of the Past ~ #9 cloaking printing ~

問題点

● 目印がPretty Printingに影響する

● 一度構造を破壊して文字列にする– 目印が目印でないものと衝突している可能性

● つまりかっこわるい

Page 15: Before LISPs just Part of the Past ~ #9 cloaking printing ~

Cloaking Pretty Printing

● 出力の一部を覆い隠し、Pretty Printingに

影響を与えないようにする

● 72文字書ける場所に、120文字書いて70文字と

申告する

● 既存のPretty Printerを改造する

Page 16: Before LISPs just Part of the Past ~ #9 cloaking printing ~

clojure.pprint

● Clojureのpretty print● Clojure1.2 : clojure.contrib.print● Clojure1.3〜 : clojure.pprint● デフォルト : off

Page 17: Before LISPs just Part of the Past ~ #9 cloaking printing ~

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))))

Page 18: Before LISPs just Part of the Past ~ #9 cloaking printing ~

clojure.pprintの実装● 実装がちょっと妙

– SymbolはTokenで管理するのに、区切りのspaceは直接書き込む

– Tokenには:trailing-white-spaceフィールド

があり、格納もされる

– しかし使われない

Page 19: Before LISPs just Part of the Past ~ #9 cloaking printing ~

Cloaking Printing● clojure.pprintを改造してCloaking Printingを実

● Cloaking対象とする単語(の正規表現)のリストを

*cloaked-words*に動的に束縛

● Cloaking対象の単語の直後に続く空白もCloakingする

● Clojureで書き方がわからないところは適当

Page 20: Before LISPs just Part of the Past ~ #9 cloaking printing ~

使用例

(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))))

Page 21: Before LISPs just Part of the Past ~ #9 cloaking printing ~

CLerがClojureを使った感想● 様々なオブジェクトが関数として使える

– 予想 - 便利そう

– 感想 - 読みにくい。特に、関数に渡す場合でもCLでいう#'のよう

なものが不要なので「これは述語?それともオブジェクト?」と悩む

● カッコの種類が多い

– 予想 - 煩わしい

– 感想 - 予想通りだった。特に、閉じるときにどの閉じ括弧を入力す

るのか迷う