Differences From Artifact [641d49d7fd647519]:
- File        
polemy/runtime.d
- 2010-11-23 13:55:15 - part of checkin [2134cd44cc] on branch trunk - further clean-up for polemy2d (user: kinaba) [annotate]
 
 
To Artifact [6911dbe256744186]:
- File        
polemy/runtime.d
- 2010-11-24 12:32:01 - part of checkin [c75f0d5f1e] on branch trunk - Enriched the runtime. a.b is now runtime error, not undefined value if the field b does not exist. Added rand(n) and gensym(). (user: kinaba) [annotate]
 
 
     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   }