diff core/src/luan/impl/FnCall.java @ 171:3dcb0f9bee82

add core component git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 05:41:22 +0000
parents src/luan/impl/FnCall.java@4eaee12f6c65
children d55e873e1f0d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/luan/impl/FnCall.java	Sun Jun 22 05:41:22 2014 +0000
@@ -0,0 +1,39 @@
+package luan.impl;
+
+import luan.Luan;
+import luan.LuanFunction;
+import luan.LuanException;
+import luan.LuanSource;
+
+
+final class FnCall extends CodeImpl implements Expressions {
+	final Expr fnExpr;
+	final Expressions args;
+	final String fnName;
+
+	FnCall(LuanSource.Element se,Expr fnExpr,Expressions args) {
+		super(se);
+		this.fnExpr = fnExpr;
+		this.args = args;
+		this.fnName = fnExpr.se().text();
+	}
+
+	@Override public Object eval(LuanStateImpl luan) throws LuanException {
+		return call( luan, fnExpr.eval(luan) );
+	}
+
+	private Object call(LuanStateImpl luan,Object o) throws LuanException {
+		if( o instanceof LuanFunction ) {
+			LuanFunction fn = (LuanFunction)o;
+			return luan.bit(se).call( fn, fnName, Luan.array(args.eval(luan)) );
+		}
+		Object h = luan.getHandler("__call",o);
+		if( h != null )
+			return call(luan,h);
+		throw luan.bit(fnExpr.se()).exception( "attempt to call '"+fnExpr.se().text()+"' (a " + Luan.type(o) + " value)" );
+	}
+
+	@Override public String toString() {
+		return "(FnCall "+fnName+" "+fnExpr+" "+args+")";
+	}
+}