Artifact Content
Not logged in

Artifact 6af5a0863b67aa85a9f4f2b89998be1f07228b71


/**
 * Authors: k.inaba
 * License: NYSL 0.9982 http://www.kmonos.net/nysl/
 *
 * Runtime library for Polemy programming language.
 */
module polemy.runtime;
import polemy._common;
import polemy.layer;
import polemy.value;
import polemy.eval;
import std.stdio;

/// enroll the native implementations of primitive functions

void enrollRuntimeLibrary( Evaluator e )
{
	e.addPrimitive("+",  ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} );
	e.addPrimitive("-",  ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} );
	e.addPrimitive("*",  ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} );
	e.addPrimitive("/",  ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data / rhs.data);} );
	e.addPrimitive("%",  ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs.data);} );
	e.addPrimitive("~",  ValueLayer, (Value lhs, Value rhs){return new StrValue(lhs.toString ~ rhs.toString);} );
	e.addPrimitive("||", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 || rhs.data!=0);} );
	e.addPrimitive("&&", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 && rhs.data!=0);} );
	e.addPrimitive("<",  ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs < rhs);} );
	e.addPrimitive(">",  ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs > rhs);} );
	e.addPrimitive("<=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs <= rhs);} );
	e.addPrimitive(">=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs >= rhs);} );
	e.addPrimitive("==", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs == rhs);} );
	e.addPrimitive("!=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs != rhs);} );
	e.addPrimitive("print", ValueLayer, (Value a){
		writeln(a);
		return new IntValue(0);
	});
	e.addPrimitive("if", ValueLayer, (IntValue x, FunValue ft, FunValue fe){
		auto toRun = (x.data==0 ? fe : ft);
		// [TODO] fill positional information
		return toRun.invoke(ValueLayer, toRun.definitionContext(), null);
	});
	e.addPrimitive("_isint", ValueLayer, (Value v){return new IntValue(cast(IntValue)v !is null);} );
	e.addPrimitive("_isstr", ValueLayer, (Value v){return new IntValue(cast(StrValue)v !is null);} );
	e.addPrimitive("_isfun", ValueLayer, (Value v){return new IntValue(cast(FunValue)v !is null);} );
	e.addPrimitive("_isundefined", ValueLayer, (Value v){return new IntValue(cast(UndefinedValue)v !is null);} );
	e.addPrimitive("_istable", ValueLayer, (Value v){return new IntValue(cast(Table)v !is null);} );
	e.addPrimitive(".", ValueLayer, (Table t, StrValue s){
		return (t.has(s.data, ValueLayer) ? t.get(s.data, ValueLayer) : new UndefinedValue);
	});
	e.addPrimitive(".?", ValueLayer, (Table t, StrValue s){
		return new IntValue(t.has(s.data, ValueLayer));
	});
	e.addPrimitive(".=", ValueLayer, (Table t, StrValue s, Value v){
		auto t2 = new Table(t, Table.Kind.NotPropagateSet);
		t2.set(s.data, ValueLayer, v);
		return t2;
	});
	e.addPrimitive("{}", ValueLayer, (){
		return new Table;
	});
}