comparison core/src/luan/impl/LeExpr.java @ 171:3dcb0f9bee82

add core component git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 05:41:22 +0000
parents src/luan/impl/LeExpr.java@4eaee12f6c65
children f2c6c22cddc0
comparison
equal deleted inserted replaced
170:7c792a328a83 171:3dcb0f9bee82
1 package luan.impl;
2
3 import luan.Luan;
4 import luan.LuanFunction;
5 import luan.LuanException;
6 import luan.LuanSource;
7 import luan.LuanBit;
8
9
10 final class LeExpr extends BinaryOpExpr {
11
12 LeExpr(LuanSource.Element se,Expr op1,Expr op2) {
13 super(se,op1,op2);
14 }
15
16 @Override public Object eval(LuanStateImpl luan) throws LuanException {
17 Object o1 = op1.eval(luan);
18 Object o2 = op2.eval(luan);
19 if( o1 instanceof Number && o2 instanceof Number ) {
20 Number n1 = (Number)o1;
21 Number n2 = (Number)o2;
22 return n1.doubleValue() <= n2.doubleValue();
23 }
24 if( o1 instanceof String && o2 instanceof String ) {
25 String s1 = (String)o1;
26 String s2 = (String)o2;
27 return s1.compareTo(s2) <= 0;
28 }
29 LuanBit bit = luan.bit(se);
30 LuanFunction fn = bit.getBinHandler("__le",o1,o2);
31 if( fn != null )
32 return Luan.toBoolean( Luan.first(bit.call(fn,"__le",new Object[]{o1,o2})) );
33 fn = bit.getBinHandler("__lt",o1,o2);
34 if( fn != null )
35 return !Luan.toBoolean( Luan.first(bit.call(fn,"__lt",new Object[]{o2,o1})) );
36 throw bit.exception( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
37 }
38 }