view core/src/luan/impl/LeExpr.java @ 576:4723d22062ce

remove LuanBit
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 13 Jul 2015 20:38:26 -0600
parents b48cfa14ba60
children 60c549d43988
line wrap: on
line source

package luan.impl;

import luan.Luan;
import luan.LuanFunction;
import luan.LuanException;
import luan.LuanElement;


final class LeExpr extends BinaryOpExpr {

	LeExpr(LuanElement el,Expr op1,Expr op2) {
		super(el,op1,op2);
	}

	@Override public Object eval(LuanStateImpl luan) throws LuanException {
		return le(luan);
	}

	private boolean le(LuanStateImpl luan) throws LuanException {
		Object o1 = op1.eval(luan);
		Object o2 = op2.eval(luan);
		if( o1 instanceof Number && o2 instanceof Number ) {
			Number n1 = (Number)o1;
			Number n2 = (Number)o2;
			return n1.doubleValue() <= n2.doubleValue();
		}
		if( o1 instanceof String && o2 instanceof String ) {
			String s1 = (String)o1;
			String s2 = (String)o2;
			return s1.compareTo(s2) <= 0;
		}
		luan.push(el,null);
		try {
			LuanFunction fn = luan.getBinHandler("__le",o1,o2);
			if( fn != null )
				return luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
			fn = luan.getBinHandler("__lt",o1,o2);
			if( fn != null )
				return !luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) );
			throw luan.exception( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
		} finally {
			luan.pop();
		}
	}
}