2134cd44cc 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: /** 36c517dfc4 2010-11-23 kinaba: * Authors: k.inaba 36c517dfc4 2010-11-23 kinaba: * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 36c517dfc4 2010-11-23 kinaba: * 36c517dfc4 2010-11-23 kinaba: * Runtime library for Polemy programming language. 36c517dfc4 2010-11-23 kinaba: */ 36c517dfc4 2010-11-23 kinaba: module polemy.runtime; 36c517dfc4 2010-11-23 kinaba: import polemy._common; 36c517dfc4 2010-11-23 kinaba: import polemy.layer; 2134cd44cc 2010-11-23 kinaba: import polemy.failure; c75f0d5f1e 2010-11-24 kinaba: import polemy.fresh; 36c517dfc4 2010-11-23 kinaba: import polemy.value; 36c517dfc4 2010-11-23 kinaba: import polemy.eval; 36c517dfc4 2010-11-23 kinaba: import std.stdio; c75f0d5f1e 2010-11-24 kinaba: import std.random; 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: /// enroll the native implementations of primitive functions 36c517dfc4 2010-11-23 kinaba: 36c517dfc4 2010-11-23 kinaba: void enrollRuntimeLibrary( Evaluator e ) 36c517dfc4 2010-11-23 kinaba: { 2134cd44cc 2010-11-23 kinaba: // arithmetic operations 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("+", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} ); 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("-", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} ); 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("*", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} ); 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("/", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (IntValue lhs, IntValue rhs){ 2134cd44cc 2010-11-23 kinaba: if( rhs.data == 0 ) 2134cd44cc 2010-11-23 kinaba: throw genex!RuntimeException("division by 0"); 2134cd44cc 2010-11-23 kinaba: return new IntValue(lhs.data / rhs.data); 2134cd44cc 2010-11-23 kinaba: }); 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("%", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs.data);} ); 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("||", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 || rhs.data!=0);} ); 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("&&", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 && rhs.data!=0);} ); 2134cd44cc 2010-11-23 kinaba: // string operation(s) 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("~", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (Value lhs, Value rhs){return new StrValue(lhs.toString ~ rhs.toString);} ); 2134cd44cc 2010-11-23 kinaba: // comparison 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("<", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs < rhs);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive(">", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs > rhs);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("<=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs <= rhs);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive(">=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs >= rhs);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("==", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs == rhs);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("!=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs != rhs);} ); 2134cd44cc 2010-11-23 kinaba: // control flow 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("if", ValueLayer, (IntValue x, FunValue ft, FunValue fe){ 36c517dfc4 2010-11-23 kinaba: auto toRun = (x.data==0 ? fe : ft); 36c517dfc4 2010-11-23 kinaba: return toRun.invoke(ValueLayer, toRun.definitionContext(), null); 36c517dfc4 2010-11-23 kinaba: }); 2134cd44cc 2010-11-23 kinaba: // type test 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("_isint", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (Value v){return new IntValue(cast(IntValue)v !is null);} ); 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("_isstr", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (Value v){return new IntValue(cast(StrValue)v !is null);} ); 2134cd44cc 2010-11-23 kinaba: e.addPrimitive("_isfun", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (Value v){return new IntValue(cast(FunValue)v !is null);} ); 005474ba5b 2010-11-27 kinaba: e.addPrimitive("_isbot", ValueLayer, 203e4cb208 2010-11-27 kinaba: (Value v){return new IntValue(cast(BottomValue)v !is null);} ); 005474ba5b 2010-11-27 kinaba: e.addPrimitive("_istbl", ValueLayer, 2134cd44cc 2010-11-23 kinaba: (Value v){return new IntValue(cast(Table)v !is null);} ); 2134cd44cc 2010-11-23 kinaba: // table 36c517dfc4 2010-11-23 kinaba: e.addPrimitive(".", ValueLayer, (Table t, StrValue s){ c75f0d5f1e 2010-11-24 kinaba: if( t.has(s.data, ValueLayer) ) c75f0d5f1e 2010-11-24 kinaba: return t.get(s.data, ValueLayer); c75f0d5f1e 2010-11-24 kinaba: throw genex!RuntimeException(text("table do not have the field ",s)); 36c517dfc4 2010-11-23 kinaba: }); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive(".?", ValueLayer, (Table t, StrValue s){ 36c517dfc4 2010-11-23 kinaba: return new IntValue(t.has(s.data, ValueLayer)); 36c517dfc4 2010-11-23 kinaba: }); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive(".=", ValueLayer, (Table t, StrValue s, Value v){ 36c517dfc4 2010-11-23 kinaba: auto t2 = new Table(t, Table.Kind.NotPropagateSet); 36c517dfc4 2010-11-23 kinaba: t2.set(s.data, ValueLayer, v); 36c517dfc4 2010-11-23 kinaba: return t2; 36c517dfc4 2010-11-23 kinaba: }); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("{}", ValueLayer, (){ 36c517dfc4 2010-11-23 kinaba: return new Table; 2134cd44cc 2010-11-23 kinaba: }); c75f0d5f1e 2010-11-24 kinaba: // IO and others 474c4facf0 2010-11-25 kinaba: e.addPrimitive("print", ValueLayer, (Value a){ writeln(a); return a; }); c75f0d5f1e 2010-11-24 kinaba: e.addPrimitive("gensym", ValueLayer, (){ return new StrValue(freshVarName()); }); c75f0d5f1e 2010-11-24 kinaba: auto rand = Mt19937(unpredictableSeed); c75f0d5f1e 2010-11-24 kinaba: e.addPrimitive("rand", ValueLayer, (IntValue n){ c75f0d5f1e 2010-11-24 kinaba: return new IntValue( uniform(0,cast(int)n.data.toInt(),rand) ); c75f0d5f1e 2010-11-24 kinaba: }); 36c517dfc4 2010-11-23 kinaba: }