diff src/luan/impl/LeExpr.java @ 166:4eaee12f6c65

move luan/interp to impl git-svn-id: https://luan-java.googlecode.com/svn/trunk@167 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 04:17:38 +0000
parents src/luan/interp/LeExpr.java@f5af13062b10
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/impl/LeExpr.java	Sun Jun 22 04:17:38 2014 +0000
@@ -0,0 +1,38 @@
+package luan.impl;
+
+import luan.Luan;
+import luan.LuanFunction;
+import luan.LuanException;
+import luan.LuanSource;
+import luan.LuanBit;
+
+
+final class LeExpr extends BinaryOpExpr {
+
+	LeExpr(LuanSource.Element se,Expr op1,Expr op2) {
+		super(se,op1,op2);
+	}
+
+	@Override public Object eval(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;
+		}
+		LuanBit bit = luan.bit(se);
+		LuanFunction fn = bit.getBinHandler("__le",o1,o2);
+		if( fn != null )
+			return Luan.toBoolean( Luan.first(bit.call(fn,"__le",new Object[]{o1,o2})) );
+		fn = bit.getBinHandler("__lt",o1,o2);
+		if( fn != null )
+			return !Luan.toBoolean( Luan.first(bit.call(fn,"__lt",new Object[]{o2,o1})) );
+		throw bit.exception( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
+	}
+}