Index: index.dd ================================================================== --- index.dd +++ index.dd @@ -601,10 +601,12 @@ $(SECTION 外部とのやりとり, $(SECBODY $(TABLE $(TR $(TH print) $(TD (a)) $(TD a を文字列化標準出力に改行付きで表示)) $(TR $(TH argv) $(TD ) $(TD スクリプトに渡された引数文字列のconsリスト)) + $(TR $(TH gensym) $(TD ()) $(TD エセgensym。変数名として他とかぶらなそうな文字列を返します)) + $(TR $(TH rand) $(TD (n)) $(TD 0 以上 n 未満の自然数をランダムに生成します)) ) ))
$(SECTION データ型判定, $(SECBODY Index: polemy/runtime.d ================================================================== --- polemy/runtime.d +++ polemy/runtime.d @@ -7,13 +7,15 @@ */ module polemy.runtime; import polemy._common; import polemy.layer; import polemy.failure; -import polemy.value; +import polemy.fresh; +import polemy.value; import polemy.eval; import std.stdio; +import std.random; /// enroll the native implementations of primitive functions void enrollRuntimeLibrary( Evaluator e ) { @@ -62,11 +64,13 @@ (Value v){return new IntValue(cast(UndefinedValue)v !is null);} ); e.addPrimitive("_istable", ValueLayer, (Value v){return new IntValue(cast(Table)v !is null);} ); // table e.addPrimitive(".", ValueLayer, (Table t, StrValue s){ - return (t.has(s.data, ValueLayer) ? t.get(s.data, ValueLayer) : new UndefinedValue); + if( t.has(s.data, ValueLayer) ) + return t.get(s.data, ValueLayer); + throw genex!RuntimeException(text("table do not have the field ",s)); }); e.addPrimitive(".?", ValueLayer, (Table t, StrValue s){ return new IntValue(t.has(s.data, ValueLayer)); }); e.addPrimitive(".=", ValueLayer, (Table t, StrValue s, Value v){ @@ -75,8 +79,13 @@ return t2; }); e.addPrimitive("{}", ValueLayer, (){ return new Table; }); - // IO + // IO and others e.addPrimitive("print", ValueLayer, (Value a){ writeln(a); return new IntValue(0); }); + e.addPrimitive("gensym", ValueLayer, (){ return new StrValue(freshVarName()); }); + auto rand = Mt19937(unpredictableSeed); + e.addPrimitive("rand", ValueLayer, (IntValue n){ + return new IntValue( uniform(0,cast(int)n.data.toInt(),rand) ); + }); }