Mercurial Hosting > luan
comparison src/luan/interp/LengthExpr.java @ 2:4da26b11d12a
start interp
git-svn-id: https://luan-java.googlecode.com/svn/trunk@3 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 14 Nov 2012 08:53:25 +0000 |
parents | |
children | 7a2cdbc5767f |
comparison
equal
deleted
inserted
replaced
1:2df768b926aa | 2:4da26b11d12a |
---|---|
1 package luan.interp; | |
2 | |
3 import luan.Lua; | |
4 import luan.LuaNumber; | |
5 import luan.LuaTable; | |
6 import luan.LuaException; | |
7 | |
8 | |
9 final class LengthExpr extends UnaryOpExpr { | |
10 | |
11 LengthExpr(Expr op) { | |
12 super(op); | |
13 } | |
14 | |
15 @Override Object eval() throws LuaException { | |
16 return new LuaNumber( length(op.eval()) ); | |
17 } | |
18 | |
19 private static int length(Object obj) throws LuaException { | |
20 if( obj instanceof String ) { | |
21 String s = (String)obj; | |
22 return s.length(); | |
23 } | |
24 if( obj instanceof LuaTable ) { | |
25 LuaTable t = (LuaTable)obj; | |
26 return t.length(); | |
27 } | |
28 throw new LuaException( "attempt to get length of a " + Lua.type(obj) + " value" ); | |
29 } | |
30 } |