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; 36c517dfc4 2010-11-23 kinaba: import polemy.value; 36c517dfc4 2010-11-23 kinaba: import polemy.eval; 36c517dfc4 2010-11-23 kinaba: import std.stdio; 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: { 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("+", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("-", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("*", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("/", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data / rhs.data);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("%", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs.data);} ); 5e924caac9 2010-11-23 kinaba: e.addPrimitive("~", ValueLayer, (Value lhs, Value rhs){return new StrValue(lhs.toString ~ rhs.toString);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("||", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 || rhs.data!=0);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("&&", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 && rhs.data!=0);} ); 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);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("print", ValueLayer, (Value a){ 36c517dfc4 2010-11-23 kinaba: writeln(a); 36c517dfc4 2010-11-23 kinaba: return new IntValue(0); 36c517dfc4 2010-11-23 kinaba: }); 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: // [TODO] fill positional information 36c517dfc4 2010-11-23 kinaba: return toRun.invoke(ValueLayer, toRun.definitionContext(), null); 36c517dfc4 2010-11-23 kinaba: }); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("_isint", ValueLayer, (Value v){return new IntValue(cast(IntValue)v !is null);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("_isstr", ValueLayer, (Value v){return new IntValue(cast(StrValue)v !is null);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("_isfun", ValueLayer, (Value v){return new IntValue(cast(FunValue)v !is null);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("_isundefined", ValueLayer, (Value v){return new IntValue(cast(UndefinedValue)v !is null);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive("_istable", ValueLayer, (Value v){return new IntValue(cast(Table)v !is null);} ); 36c517dfc4 2010-11-23 kinaba: e.addPrimitive(".", ValueLayer, (Table t, StrValue s){ 36c517dfc4 2010-11-23 kinaba: return (t.has(s.data, ValueLayer) ? t.get(s.data, ValueLayer) : new UndefinedValue); 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; 36c517dfc4 2010-11-23 kinaba: }); 36c517dfc4 2010-11-23 kinaba: }