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 * Runtime library for Polemy programming language. 6 * Runtime library for Polemy programming language.
7 */ 7 */
8 module polemy.runtime; 8 module polemy.runtime;
9 import polemy._common; 9 import polemy._common;
10 import polemy.layer; 10 import polemy.layer;
11 import polemy.failure; 11 import polemy.failure;
> 12 import polemy.fresh;
12 import polemy.value; 13 import polemy.value;
13 import polemy.eval; 14 import polemy.eval;
14 import std.stdio; 15 import std.stdio;
> 16 import std.random;
15 17
16 /// enroll the native implementations of primitive functions 18 /// enroll the native implementations of primitive functions
17 19
18 void enrollRuntimeLibrary( Evaluator e ) 20 void enrollRuntimeLibrary( Evaluator e )
19 { 21 {
20 // arithmetic operations 22 // arithmetic operations
21 e.addPrimitive("+", ValueLayer, 23 e.addPrimitive("+", ValueLayer,
................................................................................................................................................................................
60 (Value v){return new IntValue(cast(FunValue)v !is null);} ); 62 (Value v){return new IntValue(cast(FunValue)v !is null);} );
61 e.addPrimitive("_isundefined", ValueLayer, 63 e.addPrimitive("_isundefined", ValueLayer,
62 (Value v){return new IntValue(cast(UndefinedValue)v !is null);} 64 (Value v){return new IntValue(cast(UndefinedValue)v !is null);}
63 e.addPrimitive("_istable", ValueLayer, 65 e.addPrimitive("_istable", ValueLayer,
64 (Value v){return new IntValue(cast(Table)v !is null);} ); 66 (Value v){return new IntValue(cast(Table)v !is null);} );
65 // table 67 // table
66 e.addPrimitive(".", ValueLayer, (Table t, StrValue s){ 68 e.addPrimitive(".", ValueLayer, (Table t, StrValue s){
67 return (t.has(s.data, ValueLayer) ? t.get(s.data, ValueLayer) : | 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 "
68 }); 72 });
69 e.addPrimitive(".?", ValueLayer, (Table t, StrValue s){ 73 e.addPrimitive(".?", ValueLayer, (Table t, StrValue s){
70 return new IntValue(t.has(s.data, ValueLayer)); 74 return new IntValue(t.has(s.data, ValueLayer));
71 }); 75 });
72 e.addPrimitive(".=", ValueLayer, (Table t, StrValue s, Value v){ 76 e.addPrimitive(".=", ValueLayer, (Table t, StrValue s, Value v){
73 auto t2 = new Table(t, Table.Kind.NotPropagateSet); 77 auto t2 = new Table(t, Table.Kind.NotPropagateSet);
74 t2.set(s.data, ValueLayer, v); 78 t2.set(s.data, ValueLayer, v);
75 return t2; 79 return t2;
76 }); 80 });
77 e.addPrimitive("{}", ValueLayer, (){ 81 e.addPrimitive("{}", ValueLayer, (){
78 return new Table; 82 return new Table;
79 }); 83 });
80 // IO | 84 // IO and others
81 e.addPrimitive("print", ValueLayer, (Value a){ writeln(a); return new In 85 e.addPrimitive("print", ValueLayer, (Value a){ writeln(a); return new In
> 86 e.addPrimitive("gensym", ValueLayer, (){ return new StrValue(freshVarNam
> 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 }