Check-in [3ae09b8cbf]
Not logged in
Overview
SHA1 Hash:3ae09b8cbf8188c7dd163a953e95b80b2ffbd5d7
Date: 2010-11-24 21:14:00
User: kinaba
Comment:changed if-then-else syntax
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified doc/_common.html from [f3e3c7b7f7a68def] to [4967f08e96ea62fb].

22 22 23 23 The list of modules globaly used inside Polemy.<br><br> 24 24 25 25 </td></tr> 26 26 <tr><td id="docfooter"> 27 27 Page was generated with 28 28 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 29 - on Wed Nov 24 20:33:10 2010 29 + on Wed Nov 24 21:12:49 2010 30 30 31 31 </td></tr> 32 32 </table> 33 33 </div> 34 34 <script> 35 35 explorer.packageExplorer.addModule("index"); 36 36 explorer.packageExplorer.addModule("main");

Modified doc/ast.html from [e60729d606cec00d] to [abe7fd96d97ab5fa].

563 563 <script>explorer.outline.decSymbolLevel();</script> 564 564 565 565 566 566 </td></tr> 567 567 <tr><td id="docfooter"> 568 568 Page was generated with 569 569 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 570 - on Wed Nov 24 20:33:10 2010 570 + on Wed Nov 24 21:12:49 2010 571 571 572 572 </td></tr> 573 573 </table> 574 574 </div> 575 575 <script> 576 576 explorer.packageExplorer.addModule("index"); 577 577 explorer.packageExplorer.addModule("main");

Modified doc/eval.html from [b923ea618ae1d22f] to [8cb6c47f9236dfb9].

128 128 <script>explorer.outline.decSymbolLevel();</script> 129 129 130 130 131 131 </td></tr> 132 132 <tr><td id="docfooter"> 133 133 Page was generated with 134 134 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 135 - on Wed Nov 24 20:33:10 2010 135 + on Wed Nov 24 21:12:49 2010 136 136 137 137 </td></tr> 138 138 </table> 139 139 </div> 140 140 <script> 141 141 explorer.packageExplorer.addModule("index"); 142 142 explorer.packageExplorer.addModule("main");

Modified doc/failure.html from [80c8731cdbc62770] to [ad8b9e8445bd882d].

178 178 <script>explorer.outline.decSymbolLevel();</script> 179 179 180 180 181 181 </td></tr> 182 182 <tr><td id="docfooter"> 183 183 Page was generated with 184 184 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 185 - on Wed Nov 24 20:33:10 2010 185 + on Wed Nov 24 21:12:49 2010 186 186 187 187 </td></tr> 188 188 </table> 189 189 </div> 190 190 <script> 191 191 explorer.packageExplorer.addModule("index"); 192 192 explorer.packageExplorer.addModule("main");

Modified doc/fresh.html from [23033a1fbfc08c9c] to [9086e73b015a5783].

46 46 <script>explorer.outline.decSymbolLevel();</script> 47 47 48 48 49 49 </td></tr> 50 50 <tr><td id="docfooter"> 51 51 Page was generated with 52 52 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 53 - on Wed Nov 24 20:33:10 2010 53 + on Wed Nov 24 21:12:49 2010 54 54 55 55 </td></tr> 56 56 </table> 57 57 </div> 58 58 <script> 59 59 explorer.packageExplorer.addModule("index"); 60 60 explorer.packageExplorer.addModule("main");

Modified doc/index.html from [49fcca2a72fbb7a0] to [f03a1357a018cf8a].

114 114 115 115 <font color=green># 演算子など</font> 116 116 | "(" E ")" <font color=green># ただの括弧</font> 117 117 | E BINOP E <font color=green># 二項演算子いろいろ</font> 118 118 | E "." ID <font color=green># テーブルのフィールドアクセス</font> 119 119 | E ".?" ID <font color=green># テーブルにフィールドがあるか否か</font> 120 120 | E "{" ENTRYS "}" <font color=green># テーブル拡張</font> 121 - | "if" "(" E ")" "{" E "}" 122 - | "if" "(" E ")" "{" E "}" "else "{" E "}" 121 + | "if" E ("then"|":"|"then" ":") E 122 + | "if" E ("then"|":"|"then" ":") E "else" ":"? E 123 123 124 124 <font color=green># パターンマッチ</font> 125 - | "case" "(" E ")" ("when" "(" PATTERN ")" "{" E "}")* 125 + | "case" E ("when" PATTERN ":" E )* 126 126 127 127 where PATTERN ::= 式がだいたいなんでも書ける気がする 128 128 129 129 <font color=green># レイヤ指定実行</font> 130 130 | LAYER "(" E ")" 131 131 </pre> 132 132 </dd> ................................................................................ 145 145 <br/> 146 146 パターンマッチも全部 <tt>if</tt> と <tt>==</tt> と <tt>&amp;&amp;</tt> と 147 147 <tt>.</tt> と <tt>.?</tt> を使った関数呼び出し式に書き換えられていますが、 148 148 規則の詳細を説明するのが面倒なので適当に想像して下さい。 149 149 他の書き換えはこんな感じです。 150 150 </p> 151 151 <pre> 152 - if (E) {E} ⇒ if( E, fun(){E}, fun(){} ) 153 - if (E) {E} else {E} ⇒ if( E, fun(){E}, fun(){E} ) 152 + if E then E ⇒ if( E, fun(){E}, fun(){} ) 153 + if E then E else E ⇒ if( E, fun(){E}, fun(){E} ) 154 154 E BINOP E ⇒ BINOP(E, E) 155 155 { ENTRIES } ⇒ {}{ ENTRIES } 156 156 {} ⇒ {}() 157 157 E {ID:E, ...} ⇒ .=(E, ID, E) { ... } 158 158 </pre> 159 159 <p> 160 160 変数宣言に色々ありますが、<tt>let</tt> と <tt>var</tt> と <tt>def</tt> は同じ扱いで、 ................................................................................ 166 166 def x = E in E 167 167 let x = E ; E 168 168 var x = E ; E 169 169 def x = E ; E 170 170 </pre> 171 171 <p> 172 172 以上のどれも同じ意味なので、なんとなく関数型っぽく書きたい気分の日は <tt>let in</tt> を、 173 -手続き型っぽく書きたい気分の日は <tt>var ;</tt> を使うとよいでしょう。 173 +手続き型っぽく書きたい気分の日は <tt>var ;</tt> を使うとよいと思います。 174 +<tt>if then else</tt> も微妙にコロンがあったりなかったりバリエーションがありますが好みで使います。 174 175 </p> 175 176 <p> 176 177 関数を宣言するときは、<tt>fun</tt> や <tt>λ</tt> を省略できます。 177 178 以下の書き換えが行われます。 178 179 </p> 179 180 <pre> 180 181 def f( ARGS ) { E }; E ⇒ def f = fun(ARGS){E}; E ................................................................................ 208 209 <pre> 209 210 let x=21 in let x=x+x in x <font color=green># 42</font> 210 211 </pre> 211 212 <p> 212 213 一方で、"let rec" のような特別な構文はありませんが、 213 214 </p> 214 215 <pre> 215 - let f = fun(x) { if(x==0){1}else{x*f(x-1)} } in f(10) <font color=green># 3628800</font> 216 + let f = fun(x) { if x==0 then 1 else x*f(x-1) } in f(10) <font color=green># 3628800</font> 216 217 </pre> 217 218 <p> 218 219 再帰的な関数定義なども、おそらく意図されたとおりに動きます。 219 220 内部の詳細は、諸般の事情により、 220 221 マジカルで破壊的なスコープ規則になっているのですが、 221 222 同名の変数を激しく重ねて使ったりしなければ、 222 223 だいたい自然な動きをすると思います、たぶん、はい。 ................................................................................ 315 316 <dd><p> 316 317 適当に実装されたパターンマッチがあります。 317 318 リストの 2n 番目と 2n+1 番目を足して長さを半分にする関数: 318 319 </p> 319 320 <pre> 320 321 def adjSum(lst) 321 322 { 322 - case( lst ) 323 - when( {car:x, cdr:{car: y, cdr:z}} ) { {car: x+y, cdr: adjSum(z)} } 324 - when( {car:x, cdr:{}} ) { lst } 325 - when( {} ) { {} } 323 + case lst 324 + when {car:x, cdr:{car: y, cdr:z}}: {car: x+y, cdr: adjSum(z)} 325 + when {car:x, cdr:{}}: lst 326 + when {}: {} 326 327 } 327 328 </pre> 328 329 <p> 329 330 動かすときには、処理系がそれっぽい if-then-else に展開しています。 330 331 <tt>when</tt> を上から試していって、最初にマッチしたところを実行します。 331 332 </p> 332 333 <pre> ................................................................................ 339 340 変数パターンは常にマッチして、値をその変数に束縛します。 340 341 ワイルドカードも常にマッチしますが、変数束縛しません。 341 342 値パターンは、任意の式が書けます。その式を評価した結果と <tt>==</tt> ならマッチします。 342 343 外で束縛された変数を値パターンとして配置、は直接はできないので 343 344 </p> 344 345 <pre> 345 346 var x = 123; 346 - case( foo ) 347 - when( {val: x+0} ) { ... } <font color=green># これは任意の {val:123} と同じ</font> 348 - when( {val: x} ) { ... } <font color=green># これは任意の foo.?val なら常にマッチ</font> 347 + case foo 348 + when {val: x+0}: ... <font color=green># これは {val:123} と同じ</font> 349 + when {val: x}: ... <font color=green># これは任意の foo.?val なら常にマッチ</font> 349 350 </pre> 350 351 <p> 351 352 適当にちょっと複雑な式にしてやるとよいかも(裏技)。 352 353 </p> 353 354 <p> 354 355 テーブルパターンは、書かれたキーが全てあればマッチします。 355 356 <tt>{a: _}</tt> は、<tt>.a</tt> を持ってさえいればマッチするので、 356 357 <tt>{a: 123, b: 456}</tt> なんかにもマッチします。 357 358 なので、リストに対するパターンを書くときには、car/cdr の場合を先に書かないと 358 -<tt>when({})</tt> を上に書くと全部マッチしちゃいます。注意。 359 +<tt>when {}</tt> を上に書くと全部マッチしちゃいます。注意。 359 360 </p> 360 361 </dd> 361 362 </dl> 362 363 <script>explorer.outline.decSymbolLevel();</script> 363 364 364 365 365 366 </dd> ................................................................................ 800 801 <script>explorer.outline.decSymbolLevel();</script> 801 802 802 803 803 804 </td></tr> 804 805 <tr><td id="docfooter"> 805 806 Page was generated with 806 807 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 807 - on Wed Nov 24 20:33:08 2010 808 + on Wed Nov 24 21:12:48 2010 808 809 809 810 </td></tr> 810 811 </table> 811 812 </div> 812 813 <script> 813 814 explorer.packageExplorer.addModule("index"); 814 815 explorer.packageExplorer.addModule("main");

Modified doc/layer.html from [63d3b80a5cea1d9b] to [53e06d787604d0ed].

150 150 <script>explorer.outline.decSymbolLevel();</script> 151 151 152 152 153 153 </td></tr> 154 154 <tr><td id="docfooter"> 155 155 Page was generated with 156 156 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 157 - on Wed Nov 24 20:33:10 2010 157 + on Wed Nov 24 21:12:49 2010 158 158 159 159 </td></tr> 160 160 </table> 161 161 </div> 162 162 <script> 163 163 explorer.packageExplorer.addModule("index"); 164 164 explorer.packageExplorer.addModule("main");

Modified doc/lex.html from [e1ae052f9e9d69b0] to [583ce05aaa701505].

307 307 <script>explorer.outline.decSymbolLevel();</script> 308 308 309 309 310 310 </td></tr> 311 311 <tr><td id="docfooter"> 312 312 Page was generated with 313 313 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 314 - on Wed Nov 24 20:33:10 2010 314 + on Wed Nov 24 21:12:49 2010 315 315 316 316 </td></tr> 317 317 </table> 318 318 </div> 319 319 <script> 320 320 explorer.packageExplorer.addModule("index"); 321 321 explorer.packageExplorer.addModule("main");

Modified doc/main.html from [736095fc16c333ba] to [9fdfd4c7d1034144].

62 62 <script>explorer.outline.decSymbolLevel();</script> 63 63 64 64 65 65 </td></tr> 66 66 <tr><td id="docfooter"> 67 67 Page was generated with 68 68 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 69 - on Wed Nov 24 20:33:10 2010 69 + on Wed Nov 24 21:12:49 2010 70 70 71 71 </td></tr> 72 72 </table> 73 73 </div> 74 74 <script> 75 75 explorer.packageExplorer.addModule("index"); 76 76 explorer.packageExplorer.addModule("main");

Modified doc/parse.html from [84e792c2da714358] to [0556ccaeaa174e09].

56 56 <script>explorer.outline.decSymbolLevel();</script> 57 57 58 58 59 59 </td></tr> 60 60 <tr><td id="docfooter"> 61 61 Page was generated with 62 62 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 63 - on Wed Nov 24 20:33:10 2010 63 + on Wed Nov 24 21:12:49 2010 64 64 65 65 </td></tr> 66 66 </table> 67 67 </div> 68 68 <script> 69 69 explorer.packageExplorer.addModule("index"); 70 70 explorer.packageExplorer.addModule("main");

Modified doc/repl.html from [4efdaf2835d713ce] to [712f2862bf0c7df9].

156 156 <script>explorer.outline.decSymbolLevel();</script> 157 157 158 158 159 159 </td></tr> 160 160 <tr><td id="docfooter"> 161 161 Page was generated with 162 162 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 163 - on Wed Nov 24 20:33:10 2010 163 + on Wed Nov 24 21:12:49 2010 164 164 165 165 </td></tr> 166 166 </table> 167 167 </div> 168 168 <script> 169 169 explorer.packageExplorer.addModule("index"); 170 170 explorer.packageExplorer.addModule("main");

Modified doc/runtime.html from [efdc8bef87ec66a6] to [35487d5e4b345dff].

42 42 <script>explorer.outline.decSymbolLevel();</script> 43 43 44 44 45 45 </td></tr> 46 46 <tr><td id="docfooter"> 47 47 Page was generated with 48 48 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 49 - on Wed Nov 24 20:33:10 2010 49 + on Wed Nov 24 21:12:49 2010 50 50 51 51 </td></tr> 52 52 </table> 53 53 </div> 54 54 <script> 55 55 explorer.packageExplorer.addModule("index"); 56 56 explorer.packageExplorer.addModule("main");

Modified doc/test.html from [27184f7aacc74186] to [aa27a0b83e9de985].

143 143 <script>explorer.outline.decSymbolLevel();</script> 144 144 145 145 146 146 </td></tr> 147 147 <tr><td id="docfooter"> 148 148 Page was generated with 149 149 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 150 - on Wed Nov 24 20:33:10 2010 150 + on Wed Nov 24 21:12:49 2010 151 151 152 152 </td></tr> 153 153 </table> 154 154 </div> 155 155 <script> 156 156 explorer.packageExplorer.addModule("index"); 157 157 explorer.packageExplorer.addModule("main");

Modified doc/tricks.html from [5816ccc4efa21149] to [7c435a81bebd30e5].

232 232 <script>explorer.outline.decSymbolLevel();</script> 233 233 234 234 235 235 </td></tr> 236 236 <tr><td id="docfooter"> 237 237 Page was generated with 238 238 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 239 - on Wed Nov 24 20:33:10 2010 239 + on Wed Nov 24 21:12:49 2010 240 240 241 241 </td></tr> 242 242 </table> 243 243 </div> 244 244 <script> 245 245 explorer.packageExplorer.addModule("index"); 246 246 explorer.packageExplorer.addModule("main");

Modified doc/value.html from [9601d3816a1aa4af] to [34785dddb366db48].

162 162 <script>explorer.outline.decSymbolLevel();</script> 163 163 164 164 165 165 </td></tr> 166 166 <tr><td id="docfooter"> 167 167 Page was generated with 168 168 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 169 - on Wed Nov 24 20:33:10 2010 169 + on Wed Nov 24 21:12:49 2010 170 170 171 171 </td></tr> 172 172 </table> 173 173 </div> 174 174 <script> 175 175 explorer.packageExplorer.addModule("index"); 176 176 explorer.packageExplorer.addModule("main");

Modified doc/valueconv.html from [a3104ec3e37a093d] to [21597c550ad00a0c].

84 84 <script>explorer.outline.decSymbolLevel();</script> 85 85 86 86 87 87 </td></tr> 88 88 <tr><td id="docfooter"> 89 89 Page was generated with 90 90 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 91 - on Wed Nov 24 20:33:10 2010 91 + on Wed Nov 24 21:12:49 2010 92 92 93 93 </td></tr> 94 94 </table> 95 95 </div> 96 96 <script> 97 97 explorer.packageExplorer.addModule("index"); 98 98 explorer.packageExplorer.addModule("main");

Modified index.dd from [b4c241e12eddc95c] to [9e5b0b6dd62ac7dd].

65 65 66 66 $(D_COMMENT # 演算子など) 67 67 | "(" E ")" $(D_COMMENT # ただの括弧) 68 68 | E BINOP E $(D_COMMENT # 二項演算子いろいろ) 69 69 | E "." ID $(D_COMMENT # テーブルのフィールドアクセス) 70 70 | E ".?" ID $(D_COMMENT # テーブルにフィールドがあるか否か) 71 71 | E "{" ENTRYS "}" $(D_COMMENT # テーブル拡張) 72 - | "if" "(" E ")" "{" E "}" 73 - | "if" "(" E ")" "{" E "}" "else "{" E "}" 72 + | "if" E ("then"|":"|"then" ":") E 73 + | "if" E ("then"|":"|"then" ":") E "else" ":"? E 74 74 75 75 $(D_COMMENT # パターンマッチ) 76 - | "case" "(" E ")" ("when" "(" PATTERN ")" "{" E "}")* 76 + | "case" E ("when" PATTERN ":" E )* 77 77 78 78 where PATTERN ::= 式がだいたいなんでも書ける気がする 79 79 80 80 $(D_COMMENT # レイヤ指定実行) 81 81 | LAYER "(" E ")" 82 82 </pre> 83 83 )) ................................................................................ 88 88 <br/> 89 89 パターンマッチも全部 <tt>if</tt> と <tt>==</tt> と <tt>&amp;&amp;</tt> と 90 90 <tt>.</tt> と <tt>.?</tt> を使った関数呼び出し式に書き換えられていますが、 91 91 規則の詳細を説明するのが面倒なので適当に想像して下さい。 92 92 他の書き換えはこんな感じです。 93 93 </p> 94 94 <pre> 95 - if (E) {E} ⇒ if( E, fun(){E}, fun(){} ) 96 - if (E) {E} else {E} ⇒ if( E, fun(){E}, fun(){E} ) 95 + if E then E ⇒ if( E, fun(){E}, fun(){} ) 96 + if E then E else E ⇒ if( E, fun(){E}, fun(){E} ) 97 97 E BINOP E ⇒ BINOP(E, E) 98 98 { ENTRIES } ⇒ {}{ ENTRIES } 99 99 {} ⇒ {}() 100 100 E {ID:E, ...} ⇒ .=(E, ID, E) { ... } 101 101 </pre> 102 102 <p> 103 103 変数宣言に色々ありますが、<tt>let</tt> と <tt>var</tt> と <tt>def</tt> は同じ扱いで、 ................................................................................ 109 109 def x = E in E 110 110 let x = E ; E 111 111 var x = E ; E 112 112 def x = E ; E 113 113 </pre> 114 114 <p> 115 115 以上のどれも同じ意味なので、なんとなく関数型っぽく書きたい気分の日は <tt>let in</tt> を、 116 -手続き型っぽく書きたい気分の日は <tt>var ;</tt> を使うとよいでしょう。 116 +手続き型っぽく書きたい気分の日は <tt>var ;</tt> を使うとよいと思います。 117 +<tt>if then else</tt> も微妙にコロンがあったりなかったりバリエーションがありますが好みで使います。 117 118 </p> 118 119 <p> 119 120 関数を宣言するときは、<tt>fun</tt> や <tt>λ</tt> を省略できます。 120 121 以下の書き換えが行われます。 121 122 </p> 122 123 <pre> 123 124 def f( ARGS ) { E }; E ⇒ def f = fun(ARGS){E}; E ................................................................................ 143 144 <pre> 144 145 let x=21 in let x=x+x in x $(D_COMMENT # 42) 145 146 </pre> 146 147 <p> 147 148 一方で、"let rec" のような特別な構文はありませんが、 148 149 </p> 149 150 <pre> 150 - let f = fun(x) { if(x==0){1}else{x*f(x-1)} } in f(10) $(D_COMMENT # 3628800) 151 + let f = fun(x) { if x==0 then 1 else x*f(x-1) } in f(10) $(D_COMMENT # 3628800) 151 152 </pre> 152 153 <p> 153 154 再帰的な関数定義なども、おそらく意図されたとおりに動きます。 154 155 内部の詳細は、諸般の事情により、 155 156 マジカルで破壊的なスコープ規則になっているのですが、 156 157 同名の変数を激しく重ねて使ったりしなければ、 157 158 だいたい自然な動きをすると思います、たぶん、はい。 ................................................................................ 222 223 <p> 223 224 適当に実装されたパターンマッチがあります。 224 225 リストの 2n 番目と 2n+1 番目を足して長さを半分にする関数: 225 226 </p> 226 227 <pre> 227 228 def adjSum(lst) 228 229 { 229 - case( lst ) 230 - when( {car:x, cdr:{car: y, cdr:z}} ) { {car: x+y, cdr: adjSum(z)} } 231 - when( {car:x, cdr:{}} ) { lst } 232 - when( {} ) { {} } 230 + case lst 231 + when {car:x, cdr:{car: y, cdr:z}}: {car: x+y, cdr: adjSum(z)} 232 + when {car:x, cdr:{}}: lst 233 + when {}: {} 233 234 } 234 235 </pre> 235 236 <p> 236 237 動かすときには、処理系がそれっぽい if-then-else に展開しています。 237 238 <tt>when</tt> を上から試していって、最初にマッチしたところを実行します。 238 239 </p> 239 240 <pre> ................................................................................ 246 247 変数パターンは常にマッチして、値をその変数に束縛します。 247 248 ワイルドカードも常にマッチしますが、変数束縛しません。 248 249 値パターンは、任意の式が書けます。その式を評価した結果と <tt>==</tt> ならマッチします。 249 250 外で束縛された変数を値パターンとして配置、は直接はできないので 250 251 </p> 251 252 <pre> 252 253 var x = 123; 253 - case( foo ) 254 - when( {val: x+0} ) { ... } $(D_COMMENT # これは任意の {val:123} と同じ) 255 - when( {val: x} ) { ... } $(D_COMMENT # これは任意の foo.?val なら常にマッチ) 254 + case foo 255 + when {val: x+0}: ... $(D_COMMENT # これは {val:123} と同じ) 256 + when {val: x}: ... $(D_COMMENT # これは任意の foo.?val なら常にマッチ) 256 257 </pre> 257 258 <p> 258 259 適当にちょっと複雑な式にしてやるとよいかも(裏技)。 259 260 </p> 260 261 <p> 261 262 テーブルパターンは、書かれたキーが全てあればマッチします。 262 263 <tt>{a: _}</tt> は、<tt>.a</tt> を持ってさえいればマッチするので、 263 264 <tt>{a: 123, b: 456}</tt> なんかにもマッチします。 264 265 なので、リストに対するパターンを書くときには、car/cdr の場合を先に書かないと 265 -<tt>when({})</tt> を上に書くと全部マッチしちゃいます。注意。 266 +<tt>when {}</tt> を上に書くと全部マッチしちゃいます。注意。 266 267 </p> 267 268 )) 268 269 ) 269 270 )) 270 271 271 272 272 273

Modified polemy/eval.d from [4eba423d1304ad73] to [bd6a863bd24c7016].

395 395 unittest 396 396 { 397 397 auto e = new Evaluator; 398 398 enrollRuntimeLibrary(e); 399 399 assert_eq( e.evalString(` 400 400 @@3(x){x}; 401 401 def incr(x) { x+1 }; 402 -@ 3 incr(x) {@value( if(@ 3(x)+1< 3){@ 3(x)+1}else{0} )}; 402 +@ 3 incr(x) {@value( if @ 3(x)+1< 3 then @3(x)+1 else 0 )}; 403 403 def fb(n @value @3) { @3(n) }; 404 404 fb(incr(incr(incr(0)))) 405 405 `), new IntValue(BigInt(0)) ); 406 406 } 407 407 408 408 unittest 409 409 {

Modified polemy/parse.d from [9751c1054b6569e6] to [f6a85fc83dd90cb0].

297 297 } 298 298 scope(exit) lex.popFront; 299 299 return new Var(pos, lex.front.str); 300 300 } 301 301 302 302 AST parseIfAfterIf(LexPosition pos) 303 303 { 304 - eat("(", "after if"); 305 304 auto cond = E(0); 306 - eat(")", "after if condition"); 307 - auto thenPos = lex.front.pos; 308 - AST th; 309 - if( tryEat("{") ) { 310 - th = Body(); 311 - eat("}", "after if-then body"); 312 - } else { 313 - th = E(0); 305 + auto thenPos = currentPosition(); 306 + if(!tryEat(":")) { 307 + eat("then", "after if condition"); 308 + tryEat(":"); 314 309 } 310 + AST th = E(0); 315 311 auto el = doNothingExpression(); 316 - auto elsePos = (lex.empty ? LexPosition.dummy : lex.front.pos); 317 - if( tryEat("else") ) 318 - if( tryEat("{") ) { 319 - el = Body(); 320 - eat("}", "after else body"); 321 - } else { 322 - el = E(0); 323 - } 312 + auto elsePos = currentPosition(); 313 + if( tryEat("else") ) { 314 + tryEat(":"); 315 + el = E(0); 316 + } 324 317 return new App(pos, new Var(pos,"if"), cond, new Fun(thenPos,[],th), new Fun(elsePos,[],el)); 325 318 } 326 319 327 320 AST parsePatternMatch(LexPosition pos) 328 321 { 329 322 // case pmExpr CASES 330 323 //==> ................................................................................ 584 577 assert_eq(parseString(`1;2;`), let("_","",intl(1),intl(2))); 585 578 assert_eq(parseString(`let x=1 in 2`), let("x","",intl(1),intl(2))); 586 579 assert_eq(parseString(`var x=1;2;`), let("x","",intl(1),intl(2))); 587 580 assert_eq(parseString(`def x=1`), let("x","",intl(1),var("x"))); 588 581 assert_eq(parseString(`@val x=1;`), let("x","@val",intl(1),var("x"))); 589 582 assert_eq(parseString(`@typ x="#int";`), let("x","@typ",strl("#int"),var("x"))); 590 583 assert_eq(parseString(`f(1,2)`), call(var("f"),intl(1),intl(2))); 591 - assert_eq(parseString(`if(1){2}`), call(var("if"),intl(1),fun([],intl(2)),fun([],strl("(empty function body)")))); 592 - assert_eq(parseString(`if(1){2}else{3}`), call(var("if"),intl(1),fun([],intl(2)),fun([],intl(3)))); 593 - assert_eq(parseString(`if(1){}else{3}()()`), 584 + assert_eq(parseString(`if 1 then 2`), call(var("if"),intl(1),fun([],intl(2)),fun([],strl("(empty function body)")))); 585 + assert_eq(parseString(`if 1 then: 2 else(3)`), call(var("if"),intl(1),fun([],intl(2)),fun([],intl(3)))); 586 + assert_eq(parseString(`(if 1 then () else 3)()()`), 594 587 call(call(call(var("if"),intl(1),fun([],strl("(empty function body)")),fun([],intl(3)))))); 595 588 assert_eq(parseString(`1+2*3`), call(var("+"),intl(1),call(var("*"),intl(2),intl(3)))); 596 589 assert_eq(parseString(`(1+2)*3`), call(var("*"),call(var("+"),intl(1),intl(2)),intl(3))); 597 590 assert_eq(parseString(`1*(2+3)`), call(var("*"),intl(1),call(var("+"),intl(2),intl(3)))); 598 591 assert_eq(parseString(`1*2+3`), call(var("+"),call(var("*"),intl(1),intl(2)),intl(3))); 599 592 assert_eq(parseString(`@x(1)`), lay("@x", intl(1))); 600 593 assert_eq(parseString(`fun(x @v @t, y, z @t){}`), ................................................................................ 605 598 let y = 200; #comment!!!!! 606 599 x+y 607 600 `), 608 601 let("x", "", intl(100), let("y", "", intl(200), call(var("+"), var("x"), var("y")))) 609 602 ); 610 603 611 604 assert_eq(parseString(` 612 - var fac = fun(x){ if(x <= 1) {1} else {x*fac(x-1)} }; 605 + var fac = fun(x){ if(x <= 1) then 1 else x*fac(x-1) }; 613 606 fac(10) 614 607 `), 615 608 let("fac", "", fun(["x"], 616 609 call(var("if"), 617 610 call(var("<="), var("x"), intl(1)), 618 611 fun([], intl(1)), 619 612 fun([], call(var("*"), var("x"), call(var("fac"),call(var("-"),var("x"),intl(1))))) ................................................................................ 666 659 `)); 667 660 assert_nothrow(parseString(` 668 661 case 1 669 662 when {aaaa:@value(x)}: 1 670 663 when {aaaa:{bbb:_}, ccc:123}: 1 671 664 `)); 672 665 } 673 - 674 -unittest 675 -{ 676 - // test for omitting { .. } 677 - assert_nothrow(parseString(` 678 - if(1) 2 else 3 679 - `)); 680 - assert_nothrow(parseString(` 681 - if(1) x{y:z} else 3 682 - `)); 683 -}

Modified sample/fib.pmy from [8a19e45bd42355a9] to [5b2c3c452aca5b18].

1 1 def fib(x) 2 2 { 3 - if( x < 2 ) { 1 } 4 - else { fib(x-1) + fib(x-2) } 3 + if x<2 then 1 else fib(x-1) + fib(x-2) 5 4 }; 6 5 7 -let upto = λ(n, f){ 8 - if( n > 0 ){ upto(n-1,f) }; 6 +let upto = fun(n, f){ 7 + if n > 0: upto(n-1,f); 9 8 f(n) 10 9 }; 11 10 12 11 var compose = fun(f,g){ fun(x){f(g(x))} }; 13 12 var "<<" = compose; 14 13 15 14 upto(16, print<<fib);

Modified sample/fizzbuzz.pmy from [6b6d7ff7af65edf2] to [8920e9fd1d398081].

2 2 # Not at all a good example of the usage of layers, but anyway... 3 3 # 4 4 5 5 @@3(x){x}; 6 6 @@5(x){x}; 7 7 @@15(x){x}; 8 8 def incr(x) { x+1 }; 9 -@ 3 incr(x) {@value( if(@ 3(x)+1< 3){@ 3(x)+1}else{0} )}; 10 -@ 5 incr(x) {@value( if(@ 5(x)+1< 5){@ 5(x)+1}else{0} )}; 11 -@15 incr(x) {@value( if(@15(x)+1<15){@15(x)+1}else{0} )}; 9 +@ 3 incr(x) {@value( if(@ 3(x)+1< 3)then@ 3(x)+1 else 0 )}; 10 +@ 5 incr(x) {@value( if(@ 5(x)+1< 5)then@ 5(x)+1 else 0 )}; 11 +@15 incr(x) {@value( if(@15(x)+1<15)then@15(x)+1 else 0 )}; 12 12 13 13 def fb(n @value @3 @5 @15) { 14 - print( 15 - if( @15(n) ) { if( @5(n) ) { if( @3(n) ) { n } else { "Fizz" } } else { "Buzz" } } else { "FizzBuzz" } 16 - ); 14 + print( if @15(n): if @5(n): if @3(n): n else: "Fizz" else "Buzz" else "FizzBuzz" ); 17 15 fb(incr(n)) 18 16 }; 19 17 20 18 fb(0)

Modified sample/macro.pmy from [977cd2f997805f91] to [b31646ec7576a8a6].

1 1 @macro twice = fun(x) { x; x }; 2 2 @macro max(x,y) { 3 3 var _x = x; # no hygenic macro btw.... 4 4 var _y = y; # no hygenic macro btw.... 5 - if(_x<_y){_y}else{_x} 5 + if(_x<_y)then _y else _x 6 6 }; 7 7 def maxNormal(x,y) { 8 - if(x<y){y}else{x} 8 + if(x<y) 9 + then: y 10 + else: x 9 11 }; 10 12 @macro maxBad(x,y) { 11 - if(x<y){y}else{x} 13 + if x<y: y else: x 12 14 }; 13 15 14 16 @macro LetItBe(x, y) { let it = x in y }; 15 17 16 18 @macro pow10(x) { 17 19 @value( 18 20 def pow(y, n) { 19 - if( n == 1 ) { y } 20 - else { 21 + if( n == 1 ) then y 22 + else 21 23 @macro( @value(y) * @value(pow(y,n-1)) ) 22 - } 23 24 } 24 25 in 25 26 pow(@macro(x+1),10) 26 27 ) 27 28 }; 28 29 29 30 @macro pow10Hard(x) { 30 31 @value( 31 32 def pow(x, n) { 32 - if( n == 1 ) { x } 33 - else { 33 + if( n == 1 ) then x 34 + else 34 35 @macro( @value(x) * @value(pow(x,n-1)) ) 35 - } 36 36 } 37 37 in 38 38 pow(@macro(x+1),10) 39 39 ) 40 40 }; 41 41 42 42 def printAndReturn(x)

Modified sample/pattern.pmy from [3044d19024152b10] to [2c8664960dfdd96b].

1 1 def fromTo(x, y) 2 2 { 3 - if( x > y ) { {} } 4 - else { {car: x, cdr: fromTo(x+1,y)} } 3 + if x > y : {} 4 + else : {car: x, cdr: fromTo(x+1,y)} 5 5 }; 6 6 7 7 def length(lst) 8 8 { 9 - case( lst ) 10 - when( {car:_, cdr:x} ) { length(x)+1 } 11 - when( _ ) { 0 } 9 + case lst 10 + when {car:_, cdr:x}: length(x)+1 11 + when _: 0 12 12 }; 13 13 14 14 def adjSum(lst) 15 15 { 16 - case( lst ) 17 - when( {car:x, cdr:{car: y, cdr:z}} ) { {car: x+y, cdr: adjSum(z)} } 18 - when( {car:x, cdr:{}} ) { {car: x, cdr: {}} } 19 - when( {} ) { {} } 16 + case lst 17 + when {car:x, cdr:{car: y, cdr:z}} : {car: x+y, cdr: adjSum(z)} 18 + when {car:x, cdr:{}}: {car: x, cdr: {}} 19 + when {}: {} 20 20 }; 21 21 22 22 var xs = fromTo(1,101); 23 23 24 24 print( xs ); 25 25 print( length(xs) ); 26 26 print( adjSum(xs) ); 27 27 print( length(adjSum(xs)) );

Modified sample/type.pmy from [b35cfd66e862f7bf] to [f80a12af3228ea33].

1 1 @@type = fun(x){ 2 - if( _isint(x) ) { "int" } 3 - else { if( _isstr(x) ) { "str" } 4 - else { if( _isundefined(x) ) { "undefined" } 5 - else { "any" }}} 2 + if _isint(x): "int" 3 + else if _isstr(x): "str" 4 + else if _isundefined(x): "undefined" 5 + else: "any" 6 6 }; 7 7 8 8 def binop(a,b,c) { 9 9 fun(x,y){@value( 10 - if( @type(x)=="undefined" || @type(y)=="undefined" ) { "undefined" } else { 11 - if( @type(x)==a && @type(y)==b ) { c } else { "error" } 12 - } 10 + if( @type(x)=="undefined" || @type(y)=="undefined" ) then "undefined" else 11 + if( @type(x)==a && @type(y)==b ) then c else "error" 13 12 )} 14 13 }; 15 14 16 15 @type "+" = binop("int", "int", "int"); 17 16 @type "-" = binop("int", "int", "int"); 18 17 @type "<" = binop("int", "int", "int"); 19 18 @type ">" = binop("int", "int", "int"); 20 19 21 20 def mergeType(a,b) { 22 - if( a == "undefined" ) { if(b=="undefined"){"error"}else{b} } else { a } 21 + if( a == "undefined" ): ( if(b=="undefined"):"error" else b ) else ( a ) 23 22 }; 24 23 25 24 @type "if" = fun(c,t,e) {@value( 26 - if(@type(c)=="int" ) { mergeType(@type(t()), @type(e())) } else { "error" } 25 + if(@type(c)=="int" ): mergeType(@type(t()), @type(e())) else : "error" 27 26 )};