view core/src/luan/impl/UnmExpr.java @ 460:b48cfa14ba60

improve stack trace
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 May 2015 14:32:29 -0600
parents bbad2d06f728
children 4723d22062ce
line wrap: on
line source

package luan.impl;

import luan.Luan;
import luan.LuanFunction;
import luan.LuanException;
import luan.LuanElement;
import luan.LuanBit;
import luan.LuanTable;


// unary minus
final class UnmExpr extends UnaryOpExpr {

	UnmExpr(LuanElement el,Expr op) {
		super(el,op);
	}

	@Override public Object eval(LuanStateImpl luan) throws LuanException {
		Object o = op.eval(luan);
		if( o instanceof Number )
			return -((Number)o).doubleValue();
		LuanBit bit = luan.bit(el);
		if( o instanceof LuanTable ) {
			LuanFunction fn = bit.getHandlerFunction("__unm",(LuanTable)o);
			if( fn != null ) {
				return Luan.first(bit.call(fn,"__unm",new Object[]{o}));
			}
		}
		throw bit.exception("attempt to perform arithmetic on a "+Luan.type(o)+" value");
	}
}