File Annotation
Not logged in
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;
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: {
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);} );
2134cd44cc 2010-11-23        kinaba: 	e.addPrimitive("_isundefined", ValueLayer,
2134cd44cc 2010-11-23        kinaba: 		(Value v){return new IntValue(cast(UndefinedValue)v !is null);} );
2134cd44cc 2010-11-23        kinaba: 	e.addPrimitive("_istable", 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){
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: 	});
2134cd44cc 2010-11-23        kinaba: 	// IO
2134cd44cc 2010-11-23        kinaba: 	e.addPrimitive("print", ValueLayer, (Value a){ writeln(a); return new IntValue(0); });
36c517dfc4 2010-11-23        kinaba: }