diff options
Diffstat (limited to 'public/posts/2021-10-02')
7 files changed, 187 insertions, 187 deletions
diff --git a/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html b/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html index 10e4693..39ab3cd 100644 --- a/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html +++ b/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html @@ -10,7 +10,7 @@ <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <title>【C++】 属性構文の属性名にはキーワードが使える | REPL: Rest-Eat-Program Loop</title> <link rel="stylesheet" href="/style.css?h=17cf97a767ec5fb6e64967729f40f30a"> - <link rel="stylesheet" href="/hl.css?h=208c52e3b7c9db1cad782c5d30b4698f"> + <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b"> </head> <body class="single"> <header class="header"> @@ -66,24 +66,24 @@ タイトル落ち。まずはこのコードを見て欲しい。 </p> - <pre class="highlight" language="cpp" linenumbering="unnumbered"><code>#include <iostream> + <pre class="highlight" language="cpp" linenumbering="unnumbered"><code class="highlight"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><iostream></span></span> -[[alignas]] [[alignof]] [[and]] [[and_eq]] [[asm]] [[auto]] [[bitand]] -[[bitor]] [[bool]] [[break]] [[case]] [[catch]] [[char]] [[char16_t]] -[[char32_t]] [[class]] [[compl]] [[const]] [[const_cast]] [[constexpr]] -[[continue]] [[decltype]] [[default]] [[delete]] [[do]] [[double]] -[[dynamic_cast]] [[else]] [[enum]] [[explicit]] [[export]] [[extern]] [[false]] -[[final]] [[float]] [[for]] [[friend]] [[goto]] [[if]] [[inline]] [[int]] -[[long]] [[mutable]] [[namespace]] [[new]] [[noexcept]] [[not]] [[not_eq]] -[[nullptr]] [[operator]] [[or]] [[or_eq]] [[override]] [[private]] -[[protected]] [[public]] [[register]] [[reinterpret_cast]] [[return]] [[short]] -[[signed]] [[sizeof]] [[static]] [[static_assert]] [[static_cast]] [[struct]] -[[switch]] [[template]] [[this]] [[thread_local]] [[throw]] [[true]] [[try]] -[[typedef]] [[typeid]] [[typename]] [[union]] [[unsigned]] -[[virtual]] [[void]] [[volatile]] [[wchar_t]] [[while]] [[xor]] [[xor_eq]] -// [[using]] -int main() { -std::cout << "Hello, World!" << std::endl; +[[<span class="hljs-keyword">alignas</span>]] [[<span class="hljs-keyword">alignof</span>]] [[<span class="hljs-keyword">and</span>]] [[<span class="hljs-keyword">and_eq</span>]] [[<span class="hljs-keyword">asm</span>]] [[<span class="hljs-keyword">auto</span>]] [[<span class="hljs-keyword">bitand</span>]] +[[<span class="hljs-keyword">bitor</span>]] [[<span class="hljs-type">bool</span>]] [[<span class="hljs-keyword">break</span>]] [[<span class="hljs-keyword">case</span>]] [[<span class="hljs-keyword">catch</span>]] [[<span class="hljs-type">char</span>]] [[<span class="hljs-type">char16_t</span>]] +[[<span class="hljs-type">char32_t</span>]] [[<span class="hljs-keyword">class</span>]] [[<span class="hljs-keyword">compl</span>]] [[<span class="hljs-type">const</span>]] [[<span class="hljs-keyword">const_cast</span>]] [[<span class="hljs-keyword">constexpr</span>]] +[[<span class="hljs-keyword">continue</span>]] [[<span class="hljs-keyword">decltype</span>]] [[<span class="hljs-keyword">default</span>]] [[<span class="hljs-keyword">delete</span>]] [[<span class="hljs-keyword">do</span>]] [[<span class="hljs-type">double</span>]] +[[<span class="hljs-keyword">dynamic_cast</span>]] [[<span class="hljs-keyword">else</span>]] [[<span class="hljs-keyword">enum</span>]] [[<span class="hljs-keyword">explicit</span>]] [[<span class="hljs-keyword">export</span>]] [[<span class="hljs-keyword">extern</span>]] [[<span class="hljs-literal">false</span>]] +[[<span class="hljs-keyword">final</span>]] [[<span class="hljs-type">float</span>]] [[<span class="hljs-keyword">for</span>]] [[<span class="hljs-keyword">friend</span>]] [[<span class="hljs-keyword">goto</span>]] [[<span class="hljs-keyword">if</span>]] [[<span class="hljs-keyword">inline</span>]] [[<span class="hljs-type">int</span>]] +[[<span class="hljs-type">long</span>]] [[<span class="hljs-keyword">mutable</span>]] [[<span class="hljs-keyword">namespace</span>]] [[<span class="hljs-keyword">new</span>]] [[<span class="hljs-keyword">noexcept</span>]] [[<span class="hljs-keyword">not</span>]] [[<span class="hljs-keyword">not_eq</span>]] +[[<span class="hljs-literal">nullptr</span>]] [[<span class="hljs-keyword">operator</span>]] [[<span class="hljs-keyword">or</span>]] [[<span class="hljs-keyword">or_eq</span>]] [[<span class="hljs-keyword">override</span>]] [[<span class="hljs-keyword">private</span>]] +[[<span class="hljs-keyword">protected</span>]] [[<span class="hljs-keyword">public</span>]] [[<span class="hljs-keyword">register</span>]] [[<span class="hljs-keyword">reinterpret_cast</span>]] [[<span class="hljs-keyword">return</span>]] [[<span class="hljs-type">short</span>]] +[[<span class="hljs-type">signed</span>]] [[<span class="hljs-keyword">sizeof</span>]] [[<span class="hljs-type">static</span>]] [[<span class="hljs-keyword">static_assert</span>]] [[<span class="hljs-keyword">static_cast</span>]] [[<span class="hljs-keyword">struct</span>]] +[[<span class="hljs-keyword">switch</span>]] [[<span class="hljs-keyword">template</span>]] [[<span class="hljs-keyword">this</span>]] [[<span class="hljs-keyword">thread_local</span>]] [[<span class="hljs-keyword">throw</span>]] [[<span class="hljs-literal">true</span>]] [[<span class="hljs-keyword">try</span>]] +[[<span class="hljs-keyword">typedef</span>]] [[<span class="hljs-keyword">typeid</span>]] [[<span class="hljs-keyword">typename</span>]] [[<span class="hljs-keyword">union</span>]] [[<span class="hljs-type">unsigned</span>]] +[[<span class="hljs-keyword">virtual</span>]] [[<span class="hljs-type">void</span>]] [[<span class="hljs-keyword">volatile</span>]] [[<span class="hljs-type">wchar_t</span>]] [[<span class="hljs-keyword">while</span>]] [[<span class="hljs-keyword">xor</span>]] [[<span class="hljs-keyword">xor_eq</span>]] +<span class="hljs-comment">// [[using]]</span> +<span class="hljs-type">int</span> <span class="hljs-built_in">main</span>() { +std::cout << <span class="hljs-string">"Hello, World!"</span> << std::endl; }</code></pre> <blockquote> @@ -134,8 +134,8 @@ std::cout << "Hello, World!" << std::endl; 上のコードでは<code>[[using]]</code>をコメントアウトしているが、これは<code>using</code>キーワードのみ属性構文の中で意味を持つからであり、このコメントアウトを外すとコンパイルに失敗する。 </p> - <pre class="highlight" language="cpp" linenumbering="unnumbered"><code>// using の例 -[[using foo: attr1, attr2]] int x; // [[foo::attr1, foo::attr2]] の糖衣構文</code></pre> + <pre class="highlight" language="cpp" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment">// using の例</span> +[[<span class="hljs-keyword">using</span> foo: attr1, attr2]] <span class="hljs-type">int</span> x; <span class="hljs-comment">// [[foo::attr1, foo::attr2]] の糖衣構文</span></code></pre> <p> C++17 の仕様も見てみる (正確には標準化前のドラフト)。 diff --git a/public/posts/2021-10-02/python-unbound-local-error/index.html b/public/posts/2021-10-02/python-unbound-local-error/index.html index 4461af8..1631ccb 100644 --- a/public/posts/2021-10-02/python-unbound-local-error/index.html +++ b/public/posts/2021-10-02/python-unbound-local-error/index.html @@ -10,7 +10,7 @@ <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <title>【Python】 クロージャとUnboundLocalError: local variable 'x' referenced before assignment | REPL: Rest-Eat-Program Loop</title> <link rel="stylesheet" href="/style.css?h=17cf97a767ec5fb6e64967729f40f30a"> - <link rel="stylesheet" href="/hl.css?h=208c52e3b7c9db1cad782c5d30b4698f"> + <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b"> </head> <body class="single"> <header class="header"> @@ -70,10 +70,10 @@ Python でクロージャを作ろうと、次のようなコードを書いた。 </p> - <pre class="highlight" language="python" linenumbering="unnumbered"><code>def f(): -x = 0 -def g(): -x += 1 + <pre class="highlight" language="python" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>(): +x = <span class="hljs-number">0</span> +<span class="hljs-keyword">def</span> <span class="hljs-title function_">g</span>(): +x += <span class="hljs-number">1</span> g() f()</code></pre> @@ -92,26 +92,26 @@ f()</code></pre> local変数<code>x</code>が代入前に参照された、とある。これは、<code>f</code>の<code>x</code>を参照するのではなく、新しく別の変数を<code>g</code>内に作ってしまっているため。 前述のコードを宣言と代入を便宜上分けて書き直すと次のようになる。<code>var</code>を変数宣言のための構文として擬似的に利用している。 </p> - <pre class="highlight" language="python" linenumbering="unnumbered"><code># 注: var は正しい Python の文法ではない。上記参照のこと -def f(): -var x # f の local変数 'x' を宣言 -x = 0 # x に 0 を代入 -def g(): # f の内部関数 g を定義 -var x # g の local変数 'x' を宣言 -# たまたま f にも同じ名前の変数があるが、それとは別の変数 -x += 1 # x に 1 を加算 (x = x + 1 の糖衣構文) -# 加算する前の値を参照しようとするが、まだ代入されていないためエラー + <pre class="highlight" language="python" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment"># 注: var は正しい Python の文法ではない。上記参照のこと</span> +<span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>(): +var x <span class="hljs-comment"># f の local変数 'x' を宣言</span> +x = <span class="hljs-number">0</span> <span class="hljs-comment"># x に 0 を代入</span> +<span class="hljs-keyword">def</span> <span class="hljs-title function_">g</span>(): <span class="hljs-comment"># f の内部関数 g を定義</span> +var x <span class="hljs-comment"># g の local変数 'x' を宣言</span> +<span class="hljs-comment"># たまたま f にも同じ名前の変数があるが、それとは別の変数</span> +x += <span class="hljs-number">1</span> <span class="hljs-comment"># x に 1 を加算 (x = x + 1 の糖衣構文)</span> +<span class="hljs-comment"># 加算する前の値を参照しようとするが、まだ代入されていないためエラー</span> g()</code></pre> <p> 当初の意図を表現するには、次のように書けばよい。 </p> - <pre class="highlight" language="python" linenumbering="unnumbered"><code>def f(): -x = 0 -def g(): -nonlocal x ## (*) -x += 1 + <pre class="highlight" language="python" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">def</span> <span class="hljs-title function_">f</span>(): +x = <span class="hljs-number">0</span> +<span class="hljs-keyword">def</span> <span class="hljs-title function_">g</span>(): +<span class="hljs-keyword">nonlocal</span> x <span class="hljs-comment">## (*)</span> +x += <span class="hljs-number">1</span> g()</code></pre> <p> diff --git a/public/posts/2021-10-02/ruby-detect-running-implementation/index.html b/public/posts/2021-10-02/ruby-detect-running-implementation/index.html index 06b5eb5..03bae40 100644 --- a/public/posts/2021-10-02/ruby-detect-running-implementation/index.html +++ b/public/posts/2021-10-02/ruby-detect-running-implementation/index.html @@ -10,7 +10,7 @@ <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <title>【Ruby】 自身を実行している処理系の種類を判定する | REPL: Rest-Eat-Program Loop</title> <link rel="stylesheet" href="/style.css?h=17cf97a767ec5fb6e64967729f40f30a"> - <link rel="stylesheet" href="/hl.css?h=208c52e3b7c9db1cad782c5d30b4698f"> + <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b"> </head> <body class="single"> <header class="header"> @@ -75,10 +75,10 @@ 上記ページの例から引用する: </p> - <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ ruby-1.9.1 -ve 'p RUBY_ENGINE' + <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code class="highlight">$ ruby-1.9.1 -ve 'p RUBY_ENGINE' ruby 1.9.1p0 (2009-03-04 revision 22762) [x86_64-linux] "ruby" -$ jruby -ve 'p RUBY_ENGINE' +$ jruby -ve 'p RUBY_ENGINE' jruby 1.2.0 (ruby 1.8.6 patchlevel 287) (2009-03-16 rev 9419) [i386-java] "jruby"</code></pre> @@ -200,10 +200,10 @@ jruby 1.2.0 (ruby 1.8.6 patchlevel 287) (2009-03-16 rev 9419) [i386-java] <a href="https://github.com/mruby/mruby/blob/ed29d74bfd95362eaeb946fcf7e865d80346b62b/include/mruby/version.h#L32-L35">mruby 該当部分のソース</a>より引用: </p> - <pre class="highlight" language="c" linenumbering="unnumbered"><code>/* + <pre class="highlight" language="c" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment">/* * Ruby engine. -*/ -#define MRUBY_RUBY_ENGINE "mruby"</code></pre> +*/</span> +<span class="hljs-meta">#<span class="hljs-keyword">define</span> MRUBY_RUBY_ENGINE <span class="hljs-string">"mruby"</span></span></code></pre> </div> </article> </main> diff --git a/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html b/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html index 49127f6..f66ae0b 100644 --- a/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html +++ b/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html @@ -10,7 +10,7 @@ <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <title>【Ruby】 then キーワードと case in | REPL: Rest-Eat-Program Loop</title> <link rel="stylesheet" href="/style.css?h=17cf97a767ec5fb6e64967729f40f30a"> - <link rel="stylesheet" href="/hl.css?h=208c52e3b7c9db1cad782c5d30b4698f"> + <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b"> </head> <body class="single"> <header class="header"> @@ -75,36 +75,36 @@ 使われることは稀だが、Ruby では<code>then</code>がキーワードになっている。次のように使う: </p> - <pre class="highlight" language="ruby" linenumbering="unnumbered"><code>if cond then - puts "Y" - else - puts "N" - end</code></pre> + <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">if</span> cond <span class="hljs-keyword">then</span> + puts <span class="hljs-string">"Y"</span> + <span class="hljs-keyword">else</span> + puts <span class="hljs-string">"N"</span> + <span class="hljs-keyword">end</span></code></pre> <p> このキーワードが現れうる場所はいくつかあり、<code>if</code>、<code>unless</code>、<code>rescue</code>、<code>case</code>構文がそれに当たる。 上記のように、何か条件を書いた後<code>then</code>を置き、式がそこで終了していることを示すマーカーとして機能する。 </p> - <pre class="highlight" language="ruby" linenumbering="unnumbered"><code># Example: + <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment"># Example:</span> -if x then +<span class="hljs-keyword">if</span> x <span class="hljs-keyword">then</span> a -end +<span class="hljs-keyword">end</span> -unless x then +<span class="hljs-keyword">unless</span> x <span class="hljs-keyword">then</span> a -end +<span class="hljs-keyword">end</span> -begin +<span class="hljs-keyword">begin</span> a -rescue then +<span class="hljs-keyword">rescue</span> <span class="hljs-keyword">then</span> b -end +<span class="hljs-keyword">end</span> -case x -when p then +<span class="hljs-keyword">case</span> x +<span class="hljs-keyword">when</span> p <span class="hljs-keyword">then</span> a -end</code></pre> +<span class="hljs-keyword">end</span></code></pre> </section> <section id="section--_なぜ普段は書かなくてもよいのか"> @@ -113,17 +113,17 @@ end</code></pre> 普通 Ruby のコードで<code>then</code>を書くことはない。なぜか。次のコードを実行してみるとわかる。 </p> - <pre class="highlight" language="ruby" linenumbering="unnumbered"><code>if true puts 'Hello, World!' end</code></pre> + <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">if</span> <span class="hljs-literal">true</span> puts <span class="hljs-string">'Hello, World!'</span> <span class="hljs-keyword">end</span></code></pre> <p> 次のような構文エラーが出力される。 </p> - <pre class="monospaced highlight"><code>20:1: syntax error, unexpected local variable or method, expecting `then' or ';' or '\n' -if true puts 'Hello, World!' end + <pre class="monospaced highlight"><code>20:1: syntax error, unexpected local variable or method, expecting `then' or ';' or '\n' +if true puts 'Hello, World!' end ^~~~ -20:1: syntax error, unexpected `end', expecting end-of-input -...f true puts 'Hello, World!' end</code></pre> +20:1: syntax error, unexpected `end', expecting end-of-input +...f true puts 'Hello, World!' end</code></pre> <p> 二つ目のメッセージは無視して一つ目を読むと、<code>then</code>か<code>;</code>か改行が来るはずのところ変数だかメソッドだかが現れたことによりエラーとなっているようだ。 @@ -133,8 +133,8 @@ if true puts 'Hello, World!' end ポイントは改行が<code>then</code>(や<code>;</code>) の代わりとなることである。<code>true</code>の後に改行を入れてみる。 </p> - <pre class="highlight" language="ruby" linenumbering="unnumbered"><code>if true -puts 'Hello, World!' end</code></pre> + <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">if</span> <span class="hljs-literal">true</span> +puts <span class="hljs-string">'Hello, World!'</span> <span class="hljs-keyword">end</span></code></pre> <p> 無事 Hello, World! と出力されるようになった。 @@ -147,21 +147,21 @@ puts 'Hello, World!' end</code></pre> なぜ<code>then</code>や<code>;</code>や改行 (以下 「<code>then</code>等」) が必要なのだろうか。次の例を見てほしい: </p> - <pre class="highlight" language="ruby" linenumbering="unnumbered"><code>if a b end</code></pre> + <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">if</span> a b <span class="hljs-keyword">end</span></code></pre> <p> <code>then</code>も<code>;</code>も改行もないのでエラーになるが、これは条件式がどこまで続いているのかわからないためだ。 この例は二通りに解釈できる。 </p> - <pre class="highlight" language="ruby" linenumbering="unnumbered"><code># a という変数かメソッドの評価結果が truthy なら b という変数かメソッドを評価 -if a then + <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment"># a という変数かメソッドの評価結果が truthy なら b という変数かメソッドを評価</span> +<span class="hljs-keyword">if</span> a <span class="hljs-keyword">then</span> b -end</code></pre> +<span class="hljs-keyword">end</span></code></pre> - <pre class="highlight" language="ruby" linenumbering="unnumbered"><code># a というメソッドに b という変数かメソッドの評価結果を渡して呼び出し、 -# その結果が truthy なら何もしない -if a(b) then -end</code></pre> + <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment"># a というメソッドに b という変数かメソッドの評価結果を渡して呼び出し、</span> +<span class="hljs-comment"># その結果が truthy なら何もしない</span> +<span class="hljs-keyword">if</span> a(b) <span class="hljs-keyword">then</span> +<span class="hljs-keyword">end</span></code></pre> <p> <code>then</code>等はこの曖昧性を排除するためにあり、条件式は<code>if</code>から<code>then</code>等までの間にある、ということを明確にする。 C系の<code>if</code>後に来る<code>(</code>/<code>)</code>や、Python の<code>:</code>、Rust/Go/Swift などの<code>{</code>も同じ役割を持つ。 @@ -182,7 +182,7 @@ end</code></pre> <a href="https://github.com/ruby/ruby/blob/221ca0f8281d39f0dfdfe13b2448875384bbf735/parse.y#L3961-L3986">https://github.com/ruby/ruby/blob/221ca0f8281d39f0dfdfe13b2448875384bbf735/parse.y#L3961-L3986</a> </p> - <pre class="highlight" language="yacc" linenumbering="unnumbered"><code>p_case_body : keyword_in + <pre class="highlight" language="yacc" linenumbering="unnumbered"><code class="highlight">p_case_body : keyword_in { SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); p->command_start = FALSE; @@ -213,7 +213,7 @@ end</code></pre> 簡略版: </p> - <pre class="highlight" language="yacc" linenumbering="unnumbered"><code>p_case_body : keyword_in p_top_expr then compstmt p_cases + <pre class="highlight" language="yacc" linenumbering="unnumbered"><code class="highlight">p_case_body : keyword_in p_top_expr then compstmt p_cases ;</code></pre> <p> @@ -224,36 +224,36 @@ end</code></pre> これにより、<code>case</code>-<code>when</code>による従来の構文と同じように、<code>then</code>等をパターンの後ろに挿入すればよいことがわかった。つまり次の3通りのいずれかになる: </p> - <pre class="highlight" language="ruby" linenumbering="unnumbered"><code>case x -in 1 then a -in 2 then b -in 3 then c -end + <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">case</span> x +<span class="hljs-keyword">in</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> a +<span class="hljs-keyword">in</span> <span class="hljs-number">2</span> <span class="hljs-keyword">then</span> b +<span class="hljs-keyword">in</span> <span class="hljs-number">3</span> <span class="hljs-keyword">then</span> c +<span class="hljs-keyword">end</span> -case x -in 1 +<span class="hljs-keyword">case</span> x +<span class="hljs-keyword">in</span> <span class="hljs-number">1</span> a -in 2 +<span class="hljs-keyword">in</span> <span class="hljs-number">2</span> b -in 3 +<span class="hljs-keyword">in</span> <span class="hljs-number">3</span> c -end +<span class="hljs-keyword">end</span> -case x -in 1; a -in 2; b -in 3; c -end</code></pre> +<span class="hljs-keyword">case</span> x +<span class="hljs-keyword">in</span> <span class="hljs-number">1</span>; a +<span class="hljs-keyword">in</span> <span class="hljs-number">2</span>; b +<span class="hljs-keyword">in</span> <span class="hljs-number">3</span>; c +<span class="hljs-keyword">end</span></code></pre> <p> ところで、<code>p_top_expr</code>には<code>if</code>による guard clause が書けるので、その場合は<code>if</code>-<code>then</code>と似たような見た目になる。 </p> - <pre class="highlight" language="ruby" linenumbering="unnumbered"><code>case x -in 0 then a -in n if n < 0 then b -in n then c -end</code></pre> + <pre class="highlight" language="ruby" linenumbering="unnumbered"><code class="highlight"><span class="hljs-keyword">case</span> x +<span class="hljs-keyword">in</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> a +<span class="hljs-keyword">in</span> n <span class="hljs-keyword">if</span> n < <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> b +<span class="hljs-keyword">in</span> n <span class="hljs-keyword">then</span> c +<span class="hljs-keyword">end</span></code></pre> </section> <section id="section--_まとめ"> diff --git a/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html b/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html index e86d724..bc40aaa 100644 --- a/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html +++ b/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html @@ -10,7 +10,7 @@ <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <title>Rust のプリミティブ型はどこからやって来るか | REPL: Rest-Eat-Program Loop</title> <link rel="stylesheet" href="/style.css?h=17cf97a767ec5fb6e64967729f40f30a"> - <link rel="stylesheet" href="/hl.css?h=208c52e3b7c9db1cad782c5d30b4698f"> + <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b"> </head> <body class="single"> <header class="header"> @@ -65,26 +65,26 @@ Rust において、プリミティブ型の名前は予約語でない。したがって、次のコードは合法である。 </p> - <pre class="highlight" language="rust" linenumbering="unnumbered"><code>#![allow(non_camel_case_types)] -#![allow(dead_code)] + <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-meta">#![allow(non_camel_case_types)]</span> +<span class="hljs-meta">#![allow(dead_code)]</span> -struct bool; -struct char; -struct i8; -struct i16; -struct i32; -struct i64; -struct i128; -struct isize; -struct u8; -struct u16; -struct u32; -struct u64; -struct u128; -struct usize; -struct f32; -struct f64; -struct str;</code></pre> +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">bool</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">char</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i8</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i16</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i32</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i64</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i128</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">isize</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u8</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u16</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u32</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u64</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u128</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">usize</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">f32</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">f64</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">str</span>;</code></pre> <p> では、普段単に<code>bool</code>と書いたとき、この<code>bool</code>は一体どこから来ているのか。rustc のソースを追ってみた。 @@ -119,20 +119,20 @@ struct str;</code></pre> <code>rustc</code>はセルフホストされている (=<code>rustc</code>自身が Rust で書かれている) ので、<code>bool</code>や<code>char</code>などで適当に検索をかけてもノイズが多すぎて話にならない。 しかし、お誂え向きなことに<code>i128</code>/<code>u128</code>というコンパイラ自身が使うことがなさそうな型が存在するのでこれを使って<code>git grep</code>してみる。 </p> - <pre class="monospaced highlight"><code>$ git grep "\bi128\b" | wc # i128 + <pre class="monospaced highlight"><code>$ git grep "\bi128\b" | wc # i128 165 1069 15790 -$ git grep "\bu128\b" | wc # u128 +$ git grep "\bu128\b" | wc # u128 293 2127 26667 -$ git grep "\bbool\b" | wc # cf. bool の結果 +$ git grep "\bbool\b" | wc # cf. bool の結果 3563 23577 294659</code></pre> <p> 165 程度であれば探すことができそうだ。今回は、クレート名を見ておおよその当たりをつけた。 </p> - <pre class="monospaced highlight"><code>$ git grep "\bi128\b" + <pre class="monospaced highlight"><code>$ git grep "\bi128\b" ... rustc_resolve/src/lib.rs: table.insert(sym::i128, Int(IntTy::I128)); ...</code></pre> @@ -141,36 +141,36 @@ rustc_resolve/src/lib.rs: table.insert(sym::i128, Int(IntTy::I128)); <code>rustc_resolve</code>というのはいかにも名前解決を担いそうなクレート名である。該当箇所を見てみる。 </p> - <pre class="highlight" language="rust" linenumbering="unnumbered"><code>/// Interns the names of the primitive types. -/// -/// All other types are defined somewhere and possibly imported, but the primitive ones need -/// special handling, since they have no place of origin. -struct PrimitiveTypeTable { + <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment">/// Interns the names of the primitive types.</span> +<span class="hljs-comment">///</span> +<span class="hljs-comment">/// All other types are defined somewhere and possibly imported, but the primitive ones need</span> +<span class="hljs-comment">/// special handling, since they have no place of origin.</span> +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">PrimitiveTypeTable</span> { primitive_types: FxHashMap<Symbol, PrimTy>, } -impl PrimitiveTypeTable { -fn new() -> PrimitiveTypeTable { -let mut table = FxHashMap::default(); +<span class="hljs-keyword">impl</span> <span class="hljs-title class_">PrimitiveTypeTable</span> { +<span class="hljs-keyword">fn</span> <span class="hljs-title function_">new</span>() <span class="hljs-punctuation">-></span> PrimitiveTypeTable { +<span class="hljs-keyword">let</span> <span class="hljs-keyword">mut </span><span class="hljs-variable">table</span> = FxHashMap::<span class="hljs-title function_ invoke__">default</span>(); -table.insert(sym::bool, Bool); -table.insert(sym::char, Char); -table.insert(sym::f32, Float(FloatTy::F32)); -table.insert(sym::f64, Float(FloatTy::F64)); -table.insert(sym::isize, Int(IntTy::Isize)); -table.insert(sym::i8, Int(IntTy::I8)); -table.insert(sym::i16, Int(IntTy::I16)); -table.insert(sym::i32, Int(IntTy::I32)); -table.insert(sym::i64, Int(IntTy::I64)); -table.insert(sym::i128, Int(IntTy::I128)); -table.insert(sym::str, Str); -table.insert(sym::usize, Uint(UintTy::Usize)); -table.insert(sym::u8, Uint(UintTy::U8)); -table.insert(sym::u16, Uint(UintTy::U16)); -table.insert(sym::u32, Uint(UintTy::U32)); -table.insert(sym::u64, Uint(UintTy::U64)); -table.insert(sym::u128, Uint(UintTy::U128)); -Self { primitive_types: table } +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">bool</span>, Bool); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">char</span>, Char); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">f32</span>, <span class="hljs-title function_ invoke__">Float</span>(FloatTy::F32)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">f64</span>, <span class="hljs-title function_ invoke__">Float</span>(FloatTy::F64)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">isize</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::Isize)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i8</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I8)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i16</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I16)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i32</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I32)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i64</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I64)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i128</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I128)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">str</span>, Str); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">usize</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::Usize)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u8</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U8)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u16</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U16)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u32</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U32)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u64</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U64)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u128</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U128)); +<span class="hljs-keyword">Self</span> { primitive_types: table } } }</code></pre> @@ -188,26 +188,26 @@ Self { primitive_types: table } とある。次はこの struct の使用箇所を追う。追うと言っても使われている箇所は次の一箇所しかない。なお説明に不要な箇所は大きく削っている。 </p> - <pre class="highlight" language="rust" linenumbering="unnumbered"><code> /// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope. -/// (略) -fn resolve_ident_in_lexical_scope( -&mut self, -mut ident: Ident, + <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"> <span class="hljs-comment">/// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope.</span> +<span class="hljs-comment">/// (略)</span> +<span class="hljs-keyword">fn</span> <span class="hljs-title function_">resolve_ident_in_lexical_scope</span>( +&<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>, +<span class="hljs-keyword">mut</span> ident: Ident, ns: Namespace, -// (略) -) -> Option<LexicalScopeBinding<'a>> { -// (略) +<span class="hljs-comment">// (略)</span> +) <span class="hljs-punctuation">-></span> <span class="hljs-type">Option</span><LexicalScopeBinding<<span class="hljs-symbol">'a</span>>> { +<span class="hljs-comment">// (略)</span> -if ns == TypeNS { -if let Some(prim_ty) = self.primitive_type_table.primitive_types.get(&ident.name) { -let binding = -(Res::PrimTy(*prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::root()) -.to_name_binding(self.arenas); -return Some(LexicalScopeBinding::Item(binding)); +<span class="hljs-keyword">if</span> ns == TypeNS { +<span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> <span class="hljs-variable">Some</span>(prim_ty) = <span class="hljs-keyword">self</span>.primitive_type_table.primitive_types.<span class="hljs-title function_ invoke__">get</span>(&ident.name) { +<span class="hljs-keyword">let</span> <span class="hljs-variable">binding</span> = +(Res::<span class="hljs-title function_ invoke__">PrimTy</span>(*prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::<span class="hljs-title function_ invoke__">root</span>()) +.<span class="hljs-title function_ invoke__">to_name_binding</span>(<span class="hljs-keyword">self</span>.arenas); +<span class="hljs-keyword">return</span> <span class="hljs-title function_ invoke__">Some</span>(LexicalScopeBinding::<span class="hljs-title function_ invoke__">Item</span>(binding)); } } -None +<span class="hljs-literal">None</span> }</code></pre> <p> @@ -226,12 +226,12 @@ None 動作がわかったところで、例として次のコードを考える。 </p> - <pre class="highlight" language="rust" linenumbering="unnumbered"><code>#![allow(non_camel_case_types)] + <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-meta">#![allow(non_camel_case_types)]</span> -struct bool; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">bool</span>; -fn main() { -let _: bool = bool; +<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { +<span class="hljs-keyword">let</span> <span class="hljs-variable">_</span>: <span class="hljs-type">bool</span> = <span class="hljs-type">bool</span>; }</code></pre> <p> diff --git a/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre/index.html b/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre/index.html index 049afdc..f45df6b 100644 --- a/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre/index.html +++ b/public/posts/2021-10-02/vim-difference-between-autocmd-bufwrite-and-bufwritepre/index.html @@ -10,7 +10,7 @@ <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <title>【Vim】 autocmd events の BufWrite/BufWritePre の違い | REPL: Rest-Eat-Program Loop</title> <link rel="stylesheet" href="/style.css?h=17cf97a767ec5fb6e64967729f40f30a"> - <link rel="stylesheet" href="/hl.css?h=208c52e3b7c9db1cad782c5d30b4698f"> + <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b"> </head> <body class="single"> <header class="header"> @@ -122,24 +122,24 @@ <a href="https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L85-L86">https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L85-L86</a> </p> - <pre class="highlight" language="c" linenumbering="unnumbered"><code> {"BufAdd", EVENT_BUFADD}, -{"BufCreate", EVENT_BUFADD},</code></pre> + <pre class="highlight" language="c" linenumbering="unnumbered"><code class="highlight"> {<span class="hljs-string">"BufAdd"</span>, EVENT_BUFADD}, +{<span class="hljs-string">"BufCreate"</span>, EVENT_BUFADD},</code></pre> <p> <a href="https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L95-L97">https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L95-L97</a> </p> - <pre class="highlight" language="c" linenumbering="unnumbered"><code> {"BufRead", EVENT_BUFREADPOST}, -{"BufReadCmd", EVENT_BUFREADCMD}, -{"BufReadPost", EVENT_BUFREADPOST},</code></pre> + <pre class="highlight" language="c" linenumbering="unnumbered"><code class="highlight"> {<span class="hljs-string">"BufRead"</span>, EVENT_BUFREADPOST}, +{<span class="hljs-string">"BufReadCmd"</span>, EVENT_BUFREADCMD}, +{<span class="hljs-string">"BufReadPost"</span>, EVENT_BUFREADPOST},</code></pre> <p> <a href="https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L103-L105">https://github.com/vim/vim/blob/8e6be34338f13a6a625f19bcef82019c9adc65f2/src/autocmd.c#L103-L105</a> </p> - <pre class="highlight" language="c" linenumbering="unnumbered"><code> {"BufWrite", EVENT_BUFWRITEPRE}, -{"BufWritePost", EVENT_BUFWRITEPOST}, -{"BufWritePre", EVENT_BUFWRITEPRE},</code></pre> + <pre class="highlight" language="c" linenumbering="unnumbered"><code class="highlight"> {<span class="hljs-string">"BufWrite"</span>, EVENT_BUFWRITEPRE}, +{<span class="hljs-string">"BufWritePost"</span>, EVENT_BUFWRITEPOST}, +{<span class="hljs-string">"BufWritePre"</span>, EVENT_BUFWRITEPRE},</code></pre> </section> <section id="section--_neovim_のソースコード"> @@ -152,11 +152,11 @@ <a href="https://github.com/neovim/neovim/blob/71d4f5851f068eeb432af34850dddda8cc1c71e3/src/nvim/auevents.lua#L119-L124">https://github.com/neovim/neovim/blob/71d4f5851f068eeb432af34850dddda8cc1c71e3/src/nvim/auevents.lua#L119-L124</a> </p> - <pre class="highlight" language="lua" linenumbering="unnumbered"><code> aliases = { -BufCreate = 'BufAdd', -BufRead = 'BufReadPost', -BufWrite = 'BufWritePre', -FileEncoding = 'EncodingChanged', + <pre class="highlight" language="lua" linenumbering="unnumbered"><code class="highlight"> aliases = { +BufCreate = <span class="hljs-string">'BufAdd'</span>, +BufRead = <span class="hljs-string">'BufReadPost'</span>, +BufWrite = <span class="hljs-string">'BufWritePre'</span>, +FileEncoding = <span class="hljs-string">'EncodingChanged'</span>, },</code></pre> <p> diff --git a/public/posts/2021-10-02/vim-swap-order-of-selected-lines/index.html b/public/posts/2021-10-02/vim-swap-order-of-selected-lines/index.html index 366912c..8dbea05 100644 --- a/public/posts/2021-10-02/vim-swap-order-of-selected-lines/index.html +++ b/public/posts/2021-10-02/vim-swap-order-of-selected-lines/index.html @@ -10,7 +10,7 @@ <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <title>Vimで選択した行の順番を入れ替える | REPL: Rest-Eat-Program Loop</title> <link rel="stylesheet" href="/style.css?h=17cf97a767ec5fb6e64967729f40f30a"> - <link rel="stylesheet" href="/hl.css?h=208c52e3b7c9db1cad782c5d30b4698f"> + <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b"> </head> <body class="single"> <header class="header"> @@ -125,7 +125,7 @@ なお、<code>:g/^/m0</code>は全ての行を入れ替えるが、<code>:N,Mg/^/mN-1</code>とすることで N行目から M行目を処理範囲とするよう拡張できる。手でこれを入力するわけにはいかないので、次のようなコマンドを用意する。 </p> - <pre class="highlight" language="vim" linenumbering="unnumbered"><code>command! -bar -range=% + <pre class="highlight" language="vim" linenumbering="unnumbered"><code class="highlight">command! -bar -range=% \ Reverse \ <line1>,<line2>g/^/m<line1>-1</code></pre> @@ -160,7 +160,7 @@ 前述した<code>:Reverse</code>コマンドの定義を少し変えて、次のようにする: </p> - <pre class="highlight" language="vim" linenumbering="unnumbered"><code>function! s:reverse_lines(from, to) abort + <pre class="highlight" language="vim" linenumbering="unnumbered"><code class="highlight">function! s:reverse_lines(from, to) abort execute printf("%d,%dg/^/m%d", a:from, a:to, a:from - 1) endfunction @@ -217,7 +217,7 @@ </p> </blockquote> - <pre class="highlight" language="vim" linenumbering="unnumbered"><code>command! -bar -range=% + <pre class="highlight" language="vim" linenumbering="unnumbered"><code class="highlight">command! -bar -range=% \ Reverse \ keeppatterns <line1>,<line2>g/^/m<line1>-1</code></pre> @@ -234,7 +234,7 @@ <section id="section--_コピペ用再掲"> <h2><a href="#section--_コピペ用再掲">コピペ用再掲</a></h2> - <pre class="highlight" language="vim" linenumbering="unnumbered"><code>" License: Public Domain + <pre class="highlight" language="vim" linenumbering="unnumbered"><code class="highlight">" License: Public Domain command! -bar -range=% \ Reverse |
