comparison src/luan/interp/BinaryOpExpr.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 b7d7069fee58
children 2a35154aec14
comparison
equal deleted inserted replaced
34:0cdc1da466ee 35:e51906de0f11
1 package luan.interp; 1 package luan.interp;
2
3 import luan.Lua;
4 import luan.LuaTable;
5 import luan.LuaFunction;
6 import luan.LuaException;
2 7
3 8
4 abstract class BinaryOpExpr implements Expr { 9 abstract class BinaryOpExpr implements Expr {
5 final Expr op1; 10 final Expr op1;
6 final Expr op2; 11 final Expr op2;
7 12
8 BinaryOpExpr(Expr op1,Expr op2) { 13 BinaryOpExpr(Expr op1,Expr op2) {
9 this.op1 = op1; 14 this.op1 = op1;
10 this.op2 = op2; 15 this.op2 = op2;
11 } 16 }
17
18 static final LuaFunction getBinHandler(String op,Object o1,Object o2) throws LuaException {
19 LuaFunction f1 = Utils.getHandler(op,o1);
20 if( f1 != null )
21 return f1;
22 return Utils.getHandler(op,o2);
23 }
24
25 static final Object arithmetic(LuaStateImpl lua,String op,Object o1,Object o2) throws LuaException {
26 LuaFunction fn = getBinHandler(op,o1,o2);
27 if( fn != null )
28 return Utils.first(fn.call(lua,o1,o2));
29 String type = Lua.toNumber(o1)==null ? Lua.type(o1) : Lua.type(o2);
30 throw new LuaException("attempt to perform arithmetic on a "+type+" value");
31 }
12 } 32 }