Check-in [005474ba5b]
Not logged in
Overview
SHA1 Hash:005474ba5bd65e7a36165fe7e31ff082fb2cb3a8
Date: 2010-11-27 23:23:54
User: kinaba
Comment:changed: not to lift _|_
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified doc/index.html from [bdbfe9ee7589f910] to [7883429c0af25534].

390 390 391 391 </span></dt> 392 392 <script>explorer.outline.writeEnabled = false;</script> 393 393 394 394 <dd><p> 395 395 この言語の唯一の特徴的な部分は、「レイヤ」機能です。 396 396 </p> 397 +<p> 398 +ひとつのコードに複数の「意味」を持たせるのが、レイヤ機能の目的です。 399 +</p> 397 400 398 401 <script>explorer.outline.incSymbolLevel();</script> 399 402 <dl> 400 403 <script>explorer.outline.writeEnabled = true;</script> 401 404 <dt><span class="decl"> 402 -<span class="currsymbol">Layers</span> 403 -<script>explorer.outline.addDecl('Layers');</script> 405 +<span class="currsymbol">概要</span> 406 +<script>explorer.outline.addDecl('概要');</script> 404 407 405 408 </span></dt> 406 409 <script>explorer.outline.writeEnabled = false;</script> 407 410 408 - <dd><pre> 411 + <dd><p> 412 +普通に Polemy のコードを動かすと、そのコードは「<tt>@value</tt> レイヤ」で動作します。 413 +インタプリタで実験。 414 +</p> 415 +<pre> 416 + $ bin/polemy 417 + Welcome to Polemy 0.1.0 418 + &gt;&gt; 1 + 2 419 + 3 420 +</pre> 421 +この、普通に、数字の 1 は数字の 1 として、2 は 2 として、足し算は足し算として実行するのが、 422 +「<tt>@value</tt> レイヤ」です。 423 +レイヤを明示的に指定するには、<tt>レイヤ名( ... )</tt> という構文を使います。 424 +なので、以下のように書いても同じ意味です。 425 +<pre> 426 + &gt;&gt; @value( 1 + 2 ) 427 + 3 428 +</pre> 429 +他のレイヤで動かしてみましょう。適当に。「<tt>@hoge</tt> レイヤ」で。 430 +<pre> 431 + &gt;&gt; @hoge( 3 ) 432 + polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\eval.d(138): 433 + [<REPL>:4:8] lift function for @hoge is not registered 434 +</pre> 435 +<p> 436 +エラーになりました。Polemy のインタプリタは、起動時には、<tt>@value</tt> 437 +レイヤでのコードの意味しか知りません。<tt>@hoge</tt> レイヤでは <tt>3</tt> 438 +というのがどんな意味なのか、わかりません!というエラーが出ています。 439 +</p> 440 +<p> 441 +これを教えてあげるためには、<tt>@hoge</tt> レイヤの <font color=red><b>リフト関数</b></font> を定義します。 442 +</p> 443 +<pre> 444 + &gt;&gt; @@hoge = fun(x){ x*2 } 445 + (function:1bdc5c0:1ba8580) 446 +</pre> 447 +<p> 448 +「<tt>@ レイヤ名 = ...</tt>」文で、 449 +</p> 450 +<pre> 409 451 [Layers :: Overview] 410 452 411 453 Polemy's runtime environment has many "layer"s. 412 454 Usual execution run in the @value layer. 413 455 414 456 &gt;&gt; 1 + 2 415 457 3 ................................................................................ 952 994 <script>explorer.outline.decSymbolLevel();</script> 953 995 954 996 955 997 </td></tr> 956 998 <tr><td id="docfooter"> 957 999 Page was generated with 958 1000 <img src="candydoc/img/candydoc.gif" style="vertical-align:middle; position:relative; top:-1px"> 959 - on Sat Nov 27 20:49:57 2010 1001 + on Sat Nov 27 22:01:28 2010 960 1002 961 1003 </td></tr> 962 1004 </table> 963 1005 </div> 964 1006 <script> 965 1007 explorer.packageExplorer.addModule("index"); 966 1008 explorer.packageExplorer.addModule("main");

Modified index.dd from [a3c506d4caf580c9] to [388d91fa67a7c403].

286 286 287 287 288 288 289 289 $(SECTION Layers, $(SECBODY 290 290 <p> 291 291 この言語の唯一の特徴的な部分は、「レイヤ」機能です。 292 292 </p> 293 +<p> 294 +ひとつのコードに複数の「意味」を持たせるのが、レイヤ機能の目的です。 295 +</p> 293 296 $(DDOC_MEMBERS 294 -$(SECTION Layers, $(SECBODY 297 +$(SECTION 概要, $(SECBODY 298 +<p> 299 +普通に Polemy のコードを動かすと、そのコードは「<tt>@value</tt> レイヤ」で動作します。 300 +インタプリタで実験。 301 +</p> 302 +<pre> 303 + $ bin/polemy 304 + Welcome to Polemy 0.1.0 305 + &gt;&gt; 1 + 2 306 + 3 307 +</pre> 308 +この、普通に、数字の 1 は数字の 1 として、2 は 2 として、足し算は足し算として実行するのが、 309 +「<tt>@value</tt> レイヤ」です。 310 +レイヤを明示的に指定するには、<tt>レイヤ名( ... )</tt> という構文を使います。 311 +$(RED $(B レイヤ指定式)) と読んでいます。 312 +つまり、さっきのコードは以下のようにも書けます。 313 +<pre> 314 + &gt;&gt; @value( 1 + 2 ) 315 + 3 316 +</pre> 317 +他のレイヤで動かしてみましょう。適当に。「<tt>@hoge</tt> レイヤ」で。 318 +<pre> 319 + &gt;&gt; @hoge( 3 ) 320 + polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\eval.d(138): 321 + [<REPL>:4:8] lift function for @hoge is not registered 322 +</pre> 323 +<p> 324 +エラーになりました。Polemy のインタプリタは、起動時には、<tt>@value</tt> 325 +レイヤでのコードの意味しか知りません。<tt>@hoge</tt> レイヤでは <tt>3</tt> 326 +というのがどんな意味なのか、わかりません!というエラーが出ています。 327 +</p> 328 +<p> 329 +これを教えてあげるためには、<tt>@hoge</tt> レイヤの $(RED $(B リフト関数)) というものを定義します。 330 +</p> 331 +<pre> 332 + &gt;&gt; @@hoge = fun(x){ x*2 } 333 + (function:1bdc5c0:1ba8580) 334 +</pre> 335 +<p> 336 +<tt>@hoge</tt> レイヤでは、<tt>1</tt> というコードの意味は <tt>2</tt>、 337 +<tt>2</tt> というコードの意味は <tt>4</tt>、…、という、全部「2倍した意味」を持っていることにします。 338 +「<tt>@ レイヤ名 = ...</tt>」 という構文を使います。 339 +ここには、「<tt>@value</tt> レイヤでの値 <tt>x</tt> は <tt>@hoge</tt> レイヤではどういう意味になるか?」 340 +を計算して返す関数を登録します。 341 +これで、Polemy にも、<tt>@hoge</tt> レイヤの意味がわかるようになりました。 342 +</p> 343 +<pre> 344 + &gt;&gt; @hoge( 3 ) 345 + 6 346 +</pre> 347 +<p> 348 +では、1+2 を <tt>@hoge</tt> レイヤで動かしてみましょう。 349 +</p> 350 +<pre> 351 + &gt;&gt; @hoge( 1 + 2 ) 352 + polemy.failure.RuntimeException@C:\Develop\Projects\Polemy\polemy\eval.d(466): 353 + [<REPL>:3:7] only @value layer can call native function: + 354 + [<REPL>:3:7] + 355 +</pre> 356 +<p> 357 +まだエラーですね。これは 実は、リフト関数は 358 +</p> 359 +<pre> 360 + &gt;&gt; @hoge "+" = fun(x, y) {x} 361 +</pre> 295 362 <pre> 296 363 [Layers :: Overview] 297 364 298 365 Polemy's runtime environment has many "layer"s. 299 366 Usual execution run in the @value layer. 300 367 301 368 >> 1 + 2 ................................................................................ 715 782 <br /> 716 783 717 784 $(SECTION データ型判定, $(SECBODY 718 785 $(TABLE 719 786 $(TR $(TH _isint) $(TD (a)) $(TD a が整数なら 1、でなければ 0)) 720 787 $(TR $(TH _isstr) $(TD (a)) $(TD a が文字列なら 1、でなければ 0)) 721 788 $(TR $(TH _isfun) $(TD (a)) $(TD a が関数なら 1、でなければ 0)) 722 - $(TR $(TH _istable) $(TD (a)) $(TD a がテーブルなら 1、でなければ 0)) 723 - $(TR $(TH _isundefined) $(TD (a)) $(TD a が未定義値なら 1、でなければ 0)) 789 + $(TR $(TH _istbl) $(TD (a)) $(TD a がテーブルなら 1、でなければ 0)) 790 + $(TR $(TH _isbot) $(TD (a)) $(TD a が未定義値なら 1、でなければ 0)) 724 791 ) 725 792 )) 726 793 ) 727 794 )) 728 795 729 796 ) 730 797 Macros: 731 798 TITLE=Polemy Reference Manual 732 799 DOCFILENAME=index.html 733 800 SECTION=$(DDOC_DECL $(DDOC_PSYMBOL $1)) $(DDOC_DECL_DD $2) 734 801 SECBODY=$0

Modified polemy/eval.d from [6063476e92f43069] to [09f6f71388735bcb].

415 415 MemokeyType memokey = new MemokeyType(cast(void*)ast, lay, ctx); 416 416 if(auto p = memokey in memo) 417 417 { 418 418 (*p)[1] ++; 419 419 return (*p)[0]; 420 420 } 421 421 else 422 - memo[memokey] = tuple(evlay.lift(new BottomValue, ctx, pos), 0); 422 + memo[memokey] = tuple(cast(Value)new BottomValue, 0); 423 423 424 424 Value r = nonMemoizedRun(); 425 425 426 426 int touched = memo[memokey][1]; 427 427 memo[memokey] = tuple(r, 12345678); 428 428 return r; 429 429 } ................................................................................ 463 463 override Value invoke(Layer lay, Table ctx, LexPosition pos) 464 464 { 465 465 if( lay != defLay ) 466 466 throw genex!RuntimeException(pos, 467 467 text("only ", defLay, " layer can call native function: ", name)); 468 468 T typed_args; 469 469 foreach(i, Ti; T) { 470 - typed_args[i] = cast(Ti) ctx.get(text(i), ValueLayer, pos); 470 + Value vi = ctx.get(text(i), ValueLayer, pos); 471 + typed_args[i] = cast(Ti) vi; 471 472 if( typed_args[i] is null ) 472 473 throw genex!RuntimeException(pos, 473 - sprintf!"type mismatch on the argument %d of native function: %s"(i+1,name)); 474 + sprintf!"type mismatch on the argument %d of native function %s. %s required but %s passed."(i+1,name,typeid(Ti),vi)); 474 475 } 475 476 try { 476 477 return dg(typed_args); 477 478 } catch( RuntimeException e ) { 478 479 throw e.pos is null ? new RuntimeException(pos, e.msg, e.file, e.line) : e; 479 480 } 480 481 } ................................................................................ 481 482 } 482 483 theContext.set(name, defLay, new NativeFunValue(dg)); 483 484 } 484 485 } 485 486 486 487 version(unittest) 487 488 import polemy.runtime; 489 + 490 +unittest 491 +{ 492 + auto e = new Evaluator; 493 + enrollRuntimeLibrary(e); 494 + assert_eq( e.evalString(` 495 +@@foo(x){x*2}; 496 +@foo "+" (x,y){x}; 497 +@foo(3+4) 498 +`), new IntValue(6) ); 499 +} 488 500 489 501 unittest 490 502 { 491 503 auto e = new Evaluator; 492 504 enrollRuntimeLibrary(e); 493 505 auto r = assert_nothrow( e.evalString(`var x = 21; x + x*x;`) ); 494 506 assert_eq( r, new IntValue(BigInt(21+21*21)) );

Modified polemy/parse.d from [454c7b7367a1305b] to [de23904b83613a4d].

380 380 foreach(p; path) 381 381 e = new App(pos, new Var(pos, "."), e, new Str(pos, p)); 382 382 return e; 383 383 } 384 384 private AST has(AST e, string k) { 385 385 auto pos = currentPosition(); 386 386 return opAndAnd( 387 - new App(pos, new Var(pos, "_istable"), e), 387 + new App(pos, new Var(pos, "_istbl"), e), 388 388 new App(pos, new Var(pos, ".?"), e, new Str(pos, k)) 389 389 ); 390 390 } 391 391 private AST opAndAnd(AST a, AST b) { 392 392 if( a is null ) return b; 393 393 if( b is null ) return a; 394 394 auto pos = currentPosition();

Modified polemy/runtime.d from [d6bfebbae7e5dfaf] to [00f787cfb5f7b260].

56 56 // type test 57 57 e.addPrimitive("_isint", ValueLayer, 58 58 (Value v){return new IntValue(cast(IntValue)v !is null);} ); 59 59 e.addPrimitive("_isstr", ValueLayer, 60 60 (Value v){return new IntValue(cast(StrValue)v !is null);} ); 61 61 e.addPrimitive("_isfun", ValueLayer, 62 62 (Value v){return new IntValue(cast(FunValue)v !is null);} ); 63 - e.addPrimitive("_isundefined", ValueLayer, 63 + e.addPrimitive("_isbot", ValueLayer, 64 64 (Value v){return new IntValue(cast(BottomValue)v !is null);} ); 65 - e.addPrimitive("_istable", ValueLayer, 65 + e.addPrimitive("_istbl", ValueLayer, 66 66 (Value v){return new IntValue(cast(Table)v !is null);} ); 67 67 // table 68 68 e.addPrimitive(".", ValueLayer, (Table t, StrValue s){ 69 69 if( t.has(s.data, ValueLayer) ) 70 70 return t.get(s.data, ValueLayer); 71 71 throw genex!RuntimeException(text("table do not have the field ",s)); 72 72 });

Modified sample/helloworld.pmy from [0b93991cb774eeae] to [57f3802ead548897].

1 1 print( "Hello, World" ); 2 2 3 3 # Omake. How to use argv 4 4 def print_list(xs) 5 5 { 6 - case xs when {car: x, cdr: xs}: ( 7 - print(x); 8 - print_list(xs) 9 - ) 6 + case xs 7 + when {car: x, cdr: xs}: (print(x); print_list(xs)) 8 + when {} : () 10 9 }; 11 10 print_list(argv);

Modified sample/type.pmy from [12b974146da19906] to [6a40040679ba714c].

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

Modified tricks/test.d from [3ba75b9c3bef69b2] to [3ff977492e8e9816].

38 38 try { test(); } catch( Throwable e ) { logError(e); } 39 39 } 40 40 41 41 bool report() 42 42 { 43 43 if(firstError is null) 44 44 return true; 45 - derr.writefln("[TEST] The first error was:\n%s", firstError); 46 45 derr.writeString("[TEST] press enter to exit."); 47 46 din.readLine(); 48 47 return false; 49 48 } 50 49 51 50 foreach(m; ModuleInfo) 52 51 if(m && m.unitTest)