comparison src/luan/interp/UnmExpr.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 2a35154aec14
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 // unary minus 9 // unary minus
9 final class UnmExpr extends UnaryOpExpr { 10 final class UnmExpr extends UnaryOpExpr {
11 UnmExpr(Expr op) { 12 UnmExpr(Expr op) {
12 super(op); 13 super(op);
13 } 14 }
14 15
15 @Override public Object eval(LuaStateImpl lua) throws LuaException { 16 @Override public Object eval(LuaStateImpl lua) throws LuaException {
16 double n = Lua.checkNumber(op.eval(lua)).value(); 17 Object o = op.eval(lua);
17 return new LuaNumber( -n ); 18 LuaNumber n = Lua.toNumber(o);
19 if( n != null )
20 return new LuaNumber( -n.value() );
21 LuaFunction fn = Utils.getHandler("__unm",o);
22 if( fn != null )
23 return Utils.first(fn.call(lua,o));
24 throw new LuaException("attempt to perform arithmetic on a "+Lua.type(o)+" value");
18 } 25 }
19 } 26 }