view core/src/luan/impl/UnmExpr.java @ 446:bbad2d06f728

remove automatic conversion from string to number
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 May 2015 16:21:17 -0600
parents d55e873e1f0d
children b48cfa14ba60
line wrap: on
line source

package luan.impl;

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


// unary minus
final class UnmExpr extends UnaryOpExpr {

	UnmExpr(LuanSource.Element se,Expr op) {
		super(se,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(se);
		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");
	}
}