comparison src/luan/interp/LeExpr.java @ 35:e51906de0f11

implement metatables git-svn-id: https://luan-java.googlecode.com/svn/trunk@36 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 18 Dec 2012 07:05:58 +0000
parents 5cf15507d77e
children 8a57ebfdfd78
comparison
equal deleted inserted replaced
34:0cdc1da466ee 35:e51906de0f11
1 package luan.interp; 1 package luan.interp;
2 2
3 import luan.Lua; 3 import luan.Lua;
4 import luan.LuaNumber; 4 import luan.LuaNumber;
5 import luan.LuaFunction;
5 import luan.LuaException; 6 import luan.LuaException;
6 7
7 8
8 final class LeExpr extends BinaryOpExpr { 9 final class LeExpr extends BinaryOpExpr {
9 10
10 LeExpr(Expr op1,Expr op2) { 11 LeExpr(Expr op1,Expr op2) {
11 super(op1,op2); 12 super(op1,op2);
12 } 13 }
13 14
14 @Override public Object eval(LuaStateImpl lua) throws LuaException { 15 @Override public Object eval(LuaStateImpl lua) throws LuaException {
15 Object v1 = op1.eval(lua); 16 Object o1 = op1.eval(lua);
16 Object v2 = op2.eval(lua); 17 Object o2 = op2.eval(lua);
17 if( v1 instanceof LuaNumber && v2 instanceof LuaNumber ) { 18 if( o1 instanceof LuaNumber && o2 instanceof LuaNumber ) {
18 LuaNumber n1 = (LuaNumber)v1; 19 LuaNumber n1 = (LuaNumber)o1;
19 LuaNumber n2 = (LuaNumber)v2; 20 LuaNumber n2 = (LuaNumber)o2;
20 return n1.compareTo(n2) <= 0; 21 return n1.compareTo(n2) <= 0;
21 } 22 }
22 if( v1 instanceof String && v2 instanceof String ) { 23 if( o1 instanceof String && o2 instanceof String ) {
23 String s1 = (String)v1; 24 String s1 = (String)o1;
24 String s2 = (String)v2; 25 String s2 = (String)o2;
25 return s1.compareTo(s2) <= 0; 26 return s1.compareTo(s2) <= 0;
26 } 27 }
27 throw new LuaException( "attempt to compare " + Lua.type(v1) + " with " + Lua.type(v2) ); 28 LuaFunction fn = getBinHandler("__le",o1,o2);
29 if( fn != null )
30 return Lua.toBoolean( Utils.first(fn.call(lua,o1,o2)) );
31 fn = getBinHandler("__lt",o1,o2);
32 if( fn != null )
33 return !Lua.toBoolean( Utils.first(fn.call(lua,o2,o1)) );
34 throw new LuaException( "attempt to compare " + Lua.type(o1) + " with " + Lua.type(o2) );
28 } 35 }
29 } 36 }