@@ -8,11 +8,13 @@ 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 ) @@ -63,9 +65,11 @@ 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)); }); @@ -76,7 +80,12 @@ }); 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) ); + }); }