Mercurial Hosting > luan
comparison src/luan/Lua.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 | 2df768b926aa |
children | 7a2cdbc5767f |
comparison
equal
deleted
inserted
replaced
1:2df768b926aa | 2:4da26b11d12a |
---|---|
1 package luan; | 1 package luan; |
2 | 2 |
3 | 3 |
4 public class Lua { | 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 | 5 |
12 public static String type(Object obj) { | 6 public static String type(Object obj) { |
13 if( obj == null ) | 7 if( obj == null ) |
14 return "nil"; | 8 return "nil"; |
15 if( obj instanceof String ) | 9 if( obj instanceof String ) |
19 if( obj instanceof LuaNumber ) | 13 if( obj instanceof LuaNumber ) |
20 return "number"; | 14 return "number"; |
21 return "userdata"; | 15 return "userdata"; |
22 } | 16 } |
23 | 17 |
24 public static int length(Object obj) throws LuaException { | 18 public static String toString(Object obj) { |
25 if( obj instanceof String ) { | 19 if( obj == null ) |
26 String s = (String)obj; | 20 return "nil"; |
27 return s.length(); | 21 return obj.toString(); |
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 } | 22 } |
35 | 23 |
36 static LuaNumber toNumber(Object obj) throws LuaException { | 24 public static LuaNumber toNumber(Object obj) throws LuaException { |
37 if( obj instanceof LuaNumber ) | 25 if( obj instanceof LuaNumber ) |
38 return (LuaNumber)obj; | 26 return (LuaNumber)obj; |
39 if( obj instanceof String ) { | 27 if( obj instanceof String ) { |
40 String s = (String)obj; | 28 String s = (String)obj; |
41 try { | 29 try { |
43 } catch(NumberFormatException e) {} | 31 } catch(NumberFormatException e) {} |
44 } | 32 } |
45 throw new LuaException( "attempt to perform arithmetic on a " + type(obj) + " value" ); | 33 throw new LuaException( "attempt to perform arithmetic on a " + type(obj) + " value" ); |
46 } | 34 } |
47 | 35 |
48 static LuaNumber add(Object n1,Object n2) throws LuaException { | 36 public static LuaFunction toFunction(Object obj) throws LuaException { |
49 return new LuaNumber( toNumber(n1).n + toNumber(n2).n ); | 37 if( obj instanceof LuaFunction ) |
50 } | 38 return (LuaFunction)obj; |
51 | 39 throw new LuaException( "attempt to call a " + type(obj) + " value" ); |
52 static LuaNumber sub(Object n1,Object n2) throws LuaException { | |
53 return new LuaNumber( toNumber(n1).n - toNumber(n2).n ); | |
54 } | 40 } |
55 | 41 |
56 } | 42 } |