diff 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
line wrap: on
line diff
--- a/src/luan/interp/BinaryOpExpr.java	Sun Dec 16 09:23:56 2012 +0000
+++ b/src/luan/interp/BinaryOpExpr.java	Tue Dec 18 07:05:58 2012 +0000
@@ -1,5 +1,10 @@
 package luan.interp;
 
+import luan.Lua;
+import luan.LuaTable;
+import luan.LuaFunction;
+import luan.LuaException;
+
 
 abstract class BinaryOpExpr implements Expr {
 	final Expr op1;
@@ -9,4 +14,19 @@
 		this.op1 = op1;
 		this.op2 = op2;
 	}
+
+	static final LuaFunction getBinHandler(String op,Object o1,Object o2) throws LuaException {
+		LuaFunction f1 = Utils.getHandler(op,o1);
+		if( f1 != null )
+			return f1;
+		return Utils.getHandler(op,o2);
+	}
+
+	static final Object arithmetic(LuaStateImpl lua,String op,Object o1,Object o2) throws LuaException {
+		LuaFunction fn = getBinHandler(op,o1,o2);
+		if( fn != null )
+			return Utils.first(fn.call(lua,o1,o2));
+		String type = Lua.toNumber(o1)==null ? Lua.type(o1) : Lua.type(o2);
+		throw new LuaException("attempt to perform arithmetic on a "+type+" value");
+	}
 }