Check-in [c75f0d5f1e]
Not logged in
Overview
SHA1 Hash:c75f0d5f1ecba9a0bff8d1e76650fa35f082fae3
Date: 2010-11-24 21:32:01
User: kinaba
Comment:Enriched the runtime. a.b is now runtime error, not undefined value if the field b does not exist. Added rand(n) and gensym().
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified index.dd from [9e5b0b6dd62ac7dd] to [5308ff05201ec907].

599 599 </p> 600 600 )) 601 601 602 602 $(SECTION 外部とのやりとり, $(SECBODY 603 603 $(TABLE 604 604 $(TR $(TH print) $(TD (a)) $(TD a を文字列化標準出力に改行付きで表示)) 605 605 $(TR $(TH argv) $(TD ) $(TD スクリプトに渡された引数文字列のconsリスト)) 606 + $(TR $(TH gensym) $(TD ()) $(TD エセgensym。変数名として他とかぶらなそうな文字列を返します)) 607 + $(TR $(TH rand) $(TD (n)) $(TD 0 以上 n 未満の自然数をランダムに生成します)) 606 608 ) 607 609 )) 608 610 <br /> 609 611 610 612 $(SECTION データ型判定, $(SECBODY 611 613 $(TABLE 612 614 $(TR $(TH _isint) $(TD (a)) $(TD a が整数なら 1、でなければ 0))

Modified polemy/runtime.d from [641d49d7fd647519] to [6911dbe256744186].

5 5 * 6 6 * Runtime library for Polemy programming language. 7 7 */ 8 8 module polemy.runtime; 9 9 import polemy._common; 10 10 import polemy.layer; 11 11 import polemy.failure; 12 +import polemy.fresh; 12 13 import polemy.value; 13 14 import polemy.eval; 14 15 import std.stdio; 16 +import std.random; 15 17 16 18 /// enroll the native implementations of primitive functions 17 19 18 20 void enrollRuntimeLibrary( Evaluator e ) 19 21 { 20 22 // arithmetic operations 21 23 e.addPrimitive("+", ValueLayer, ................................................................................ 60 62 (Value v){return new IntValue(cast(FunValue)v !is null);} ); 61 63 e.addPrimitive("_isundefined", ValueLayer, 62 64 (Value v){return new IntValue(cast(UndefinedValue)v !is null);} ); 63 65 e.addPrimitive("_istable", ValueLayer, 64 66 (Value v){return new IntValue(cast(Table)v !is null);} ); 65 67 // table 66 68 e.addPrimitive(".", ValueLayer, (Table t, StrValue s){ 67 - return (t.has(s.data, ValueLayer) ? t.get(s.data, ValueLayer) : new UndefinedValue); 69 + if( t.has(s.data, ValueLayer) ) 70 + return t.get(s.data, ValueLayer); 71 + throw genex!RuntimeException(text("table do not have the field ",s)); 68 72 }); 69 73 e.addPrimitive(".?", ValueLayer, (Table t, StrValue s){ 70 74 return new IntValue(t.has(s.data, ValueLayer)); 71 75 }); 72 76 e.addPrimitive(".=", ValueLayer, (Table t, StrValue s, Value v){ 73 77 auto t2 = new Table(t, Table.Kind.NotPropagateSet); 74 78 t2.set(s.data, ValueLayer, v); 75 79 return t2; 76 80 }); 77 81 e.addPrimitive("{}", ValueLayer, (){ 78 82 return new Table; 79 83 }); 80 - // IO 84 + // IO and others 81 85 e.addPrimitive("print", ValueLayer, (Value a){ writeln(a); return new IntValue(0); }); 86 + e.addPrimitive("gensym", ValueLayer, (){ return new StrValue(freshVarName()); }); 87 + auto rand = Mt19937(unpredictableSeed); 88 + e.addPrimitive("rand", ValueLayer, (IntValue n){ 89 + return new IntValue( uniform(0,cast(int)n.data.toInt(),rand) ); 90 + }); 82 91 }