83 lines
5.9 KiB
Markdown
83 lines
5.9 KiB
Markdown
---
|
|
title: マクロを使って HTTP 接続を行う構文を作ってみた
|
|
author: kazu634
|
|
date: 2011-05-30
|
|
wordtwit_post_info:
|
|
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5431;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
|
|
categories:
|
|
- gauche
|
|
- Lisp
|
|
|
|
---
|
|
<div class="section">
|
|
<p>
|
|
マクロを使って HTTP 接続を行う構文を作ってみようと決意したのですが、うまくできず。。。なぜだろう???
|
|
</p>
|
|
|
|
<h4>
|
|
やってみたいこと
|
|
</h4>
|
|
|
|
<p>
|
|
こういうパターンが良く出てきました:
|
|
</p>
|
|
|
|
<pre class="syntax-highlight">
|
|
<span class="synSpecial">(</span>receive <span class="synSpecial">(</span>status header body<span class="synSpecial">)</span>
|
|
<span class="synSpecial">(</span>http-get ...<span class="synSpecial">)</span>
|
|
<span class="synSpecial">(</span><span class="synStatement">cond</span>
|
|
[<span class="synSpecial">(</span><span class="synStatement">equal</span>? <span class="synConstant">"200"</span> status<span class="synSpecial">)</span> <span class="synSpecial">(</span>proc body<span class="synSpecial">)</span>] <span class="synComment">;; ここの手続きは指定したいなぁ</span>
|
|
[<span class="synSpecial">(</span><span class="synStatement">equal</span>? <span class="synConstant">"400"</span> status<span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">print</span> <span class="synConstant">"Authentication failure."</span><span class="synSpecial">)</span>]
|
|
[else <span class="synSpecial">(</span><span class="synStatement">print</span> status header body<span class="synSpecial">)</span>]<span class="synSpecial">))</span>
|
|
</pre>
|
|
|
|
<p>
|
|
こういうパターンをこんなように書けたらいいかなぁと思いました:
|
|
</p>
|
|
|
|
<pre class="syntax-highlight">
|
|
<span class="synSpecial">(</span>wrap-http <span class="synSpecial">(</span>http-get ...<span class="synSpecial">)</span> proc <span class="synSpecial">'((</span><span class="synConstant">"400"</span> . <span class="synConstant">"Authentication failure."</span><span class="synSpecial">)))</span>
|
|
</pre>
|
|
|
|
<h4>
|
|
とりあえず書いてみた
|
|
</h4>
|
|
|
|
<p>
|
|
こんなのを書いてみました:
|
|
</p>
|
|
|
|
<pre class="syntax-highlight">
|
|
<span class="synSpecial">(</span>define-macro <span class="synSpecial">(</span>wrap-http http-method proc status-msg<span class="synSpecial">)</span>
|
|
<span class="synSpecial">(</span><span class="synStatement">let*</span> <span class="synSpecial">((</span>success <span class="synPreProc">`(</span><span class="synSpecial">(</span><span class="synStatement">equal</span>? <span class="synConstant">"200"</span> status<span class="synSpecial">)</span> <span class="synSpecial">(</span>,proc body<span class="synSpecial">)</span><span class="synPreProc">)</span><span class="synSpecial">)</span>
|
|
<span class="synSpecial">(</span>else-msg <span class="synPreProc">`(</span>else <span class="synSpecial">(</span><span class="synStatement">print</span> status header body<span class="synSpecial">)</span><span class="synPreProc">)</span><span class="synSpecial">))</span>
|
|
<span class="synPreProc">`(</span>receive <span class="synSpecial">(</span>status header body<span class="synSpecial">)</span>
|
|
,http-method
|
|
<span class="synSpecial">(</span><span class="synStatement">cond</span>
|
|
,success
|
|
,@<span class="synSpecial">(</span><span class="synStatement">map</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>s<span class="synSpecial">)</span>
|
|
<span class="synPreProc">`(</span><span class="synSpecial">(</span><span class="synStatement">equal</span>? ,<span class="synSpecial">(</span><span class="synStatement">car</span> s<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">print</span> ,<span class="synSpecial">(</span><span class="synStatement">cdr</span> s<span class="synSpecial">))</span><span class="synPreProc">)</span><span class="synSpecial">)</span>
|
|
status-msg<span class="synSpecial">)</span>
|
|
,else-msg<span class="synSpecial">)</span><span class="synPreProc">)</span><span class="synSpecial">))</span>
|
|
</pre>
|
|
|
|
<p>
|
|
でもこれを使おうとすると、「*** ERROR: Compile Error: pair required, but got quote」となってしまいました。。。なぜなのでしょう。。。
|
|
</p>
|
|
|
|
<h4>
|
|
ちなみに
|
|
</h4>
|
|
|
|
<p>
|
|
これは無事に実行出来るようなんだよなぁ:
|
|
</p>
|
|
|
|
<pre class="syntax-highlight">
|
|
gosh> <span class="synSpecial">(</span><span class="synStatement">map</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>i<span class="synSpecial">)</span>
|
|
<span class="synPreProc">`(</span><span class="synSpecial">(</span><span class="synStatement">equal</span>? ,<span class="synSpecial">(</span><span class="synStatement">car</span> i<span class="synSpecial">))</span> <span class="synSpecial">(</span><span class="synStatement">print</span> ,<span class="synSpecial">(</span><span class="synStatement">cdr</span> i<span class="synSpecial">))</span><span class="synPreProc">)</span><span class="synSpecial">)</span>
|
|
<span class="synSpecial">'((</span><span class="synConstant">"404"</span> . <span class="synConstant">"Not found"</span><span class="synSpecial">)))</span>
|
|
<span class="synSpecial">(((</span><span class="synStatement">equal</span>? <span class="synConstant">"404"</span><span class="synSpecial">)</span> <span class="synSpecial">(</span><span class="synStatement">print</span> <span class="synConstant">"Not found"</span><span class="synSpecial">)))</span>
|
|
</pre>
|
|
</div>
|