comparison core/src/luan/impl/IndexExpr.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/IndexExpr.java@4eaee12f6c65
children d55e873e1f0d
comparison
equal deleted inserted replaced
170:7c792a328a83 171:3dcb0f9bee82
1 package luan.impl;
2
3 import luan.Luan;
4 import luan.LuanException;
5 import luan.LuanTable;
6 import luan.LuanFunction;
7 import luan.LuanSource;
8
9
10 final class IndexExpr extends BinaryOpExpr {
11
12 IndexExpr(LuanSource.Element se,Expr op1,Expr op2) {
13 super(se,op1,op2);
14 }
15
16 @Override public Object eval(LuanStateImpl luan) throws LuanException {
17 return index(luan,op1.eval(luan),op2.eval(luan));
18 }
19
20 private Object index(LuanStateImpl luan,Object t,Object key) throws LuanException {
21 Object h;
22 if( t instanceof LuanTable ) {
23 LuanTable tbl = (LuanTable)t;
24 Object value = tbl.get(key);
25 if( value != null )
26 return value;
27 h = luan.getHandler("__index",t);
28 if( h==null )
29 return null;
30 } else {
31 h = luan.getHandler("__index",t);
32 if( h==null )
33 throw luan.bit(op1.se()).exception( "attempt to index '"+op1.se().text()+"' (a " + Luan.type(t) + " value)" );
34 }
35 if( h instanceof LuanFunction ) {
36 LuanFunction fn = (LuanFunction)h;
37 return Luan.first(luan.bit(se).call(fn,"__index",new Object[]{t,key}));
38 }
39 return index(luan,h,key);
40 }
41 }