Mercurial Hosting > luan
comparison src/luan/Lua.java @ 1:2df768b926aa
start Luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@2 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 13 Nov 2012 05:54:32 +0000 |
parents | |
children | 4da26b11d12a |
comparison
equal
deleted
inserted
replaced
0:a89d06646ff3 | 1:2df768b926aa |
---|---|
1 package luan; | |
2 | |
3 | |
4 public class Lua { | |
5 | |
6 public static String toString(Object obj) { | |
7 if( obj == null ) | |
8 return "nil"; | |
9 return obj.toString(); | |
10 } | |
11 | |
12 public static String type(Object obj) { | |
13 if( obj == null ) | |
14 return "nil"; | |
15 if( obj instanceof String ) | |
16 return "string"; | |
17 if( obj instanceof Boolean ) | |
18 return "boolean"; | |
19 if( obj instanceof LuaNumber ) | |
20 return "number"; | |
21 return "userdata"; | |
22 } | |
23 | |
24 public static int length(Object obj) throws LuaException { | |
25 if( obj instanceof String ) { | |
26 String s = (String)obj; | |
27 return s.length(); | |
28 } | |
29 if( obj instanceof LuaTable ) { | |
30 LuaTable t = (LuaTable)obj; | |
31 return t.length(); | |
32 } | |
33 throw new LuaException( "attempt to get length of a " + type(obj) + " value" ); | |
34 } | |
35 | |
36 static LuaNumber toNumber(Object obj) throws LuaException { | |
37 if( obj instanceof LuaNumber ) | |
38 return (LuaNumber)obj; | |
39 if( obj instanceof String ) { | |
40 String s = (String)obj; | |
41 try { | |
42 return new LuaNumber( Double.parseDouble(s) ); | |
43 } catch(NumberFormatException e) {} | |
44 } | |
45 throw new LuaException( "attempt to perform arithmetic on a " + type(obj) + " value" ); | |
46 } | |
47 | |
48 static LuaNumber add(Object n1,Object n2) throws LuaException { | |
49 return new LuaNumber( toNumber(n1).n + toNumber(n2).n ); | |
50 } | |
51 | |
52 static LuaNumber sub(Object n1,Object n2) throws LuaException { | |
53 return new LuaNumber( toNumber(n1).n - toNumber(n2).n ); | |
54 } | |
55 | |
56 } |