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