Mercurial Hosting > luan
comparison src/luan/impl/LuanImpl.java @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | core/src/luan/impl/LuanImpl.java@41f791e4206d |
children | 894786a03d22 |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 package luan.impl; | |
2 | |
3 import java.util.Arrays; | |
4 import java.util.List; | |
5 import java.util.ArrayList; | |
6 import luan.Luan; | |
7 import luan.LuanState; | |
8 import luan.LuanTable; | |
9 import luan.LuanFunction; | |
10 import luan.LuanException; | |
11 import luan.modules.JavaLuan; | |
12 | |
13 | |
14 public final class LuanImpl { | |
15 private LuanImpl() {} // never | |
16 | |
17 public static int len(LuanState luan,Object o) throws LuanException { | |
18 if( o instanceof String ) { | |
19 String s = (String)o; | |
20 return s.length(); | |
21 } | |
22 if( o instanceof byte[] ) { | |
23 byte[] a = (byte[])o; | |
24 return a.length; | |
25 } | |
26 if( o instanceof LuanTable ) { | |
27 LuanTable t = (LuanTable)o; | |
28 return t.length(luan); | |
29 } | |
30 throw new LuanException( "attempt to get length of a " + Luan.type(o) + " value" ); | |
31 } | |
32 | |
33 public static Object unm(LuanState luan,Object o) throws LuanException { | |
34 if( o instanceof Number ) | |
35 return -((Number)o).doubleValue(); | |
36 if( o instanceof LuanTable ) { | |
37 LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o); | |
38 if( fn != null ) { | |
39 return Luan.first(fn.call(luan,new Object[]{o})); | |
40 } | |
41 } | |
42 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); | |
43 } | |
44 | |
45 private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException { | |
46 LuanFunction fn = Luan.getBinHandler(op,o1,o2); | |
47 if( fn != null ) | |
48 return Luan.first(fn.call(luan,new Object[]{o1,o2})); | |
49 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); | |
50 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); | |
51 } | |
52 | |
53 public static Object pow(LuanState luan,Object o1,Object o2) throws LuanException { | |
54 if( o1 instanceof Number && o2 instanceof Number ) | |
55 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() ); | |
56 return arithmetic(luan,"__pow",o1,o2); | |
57 } | |
58 | |
59 public static Object mul(LuanState luan,Object o1,Object o2) throws LuanException { | |
60 if( o1 instanceof Number && o2 instanceof Number ) | |
61 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue(); | |
62 return arithmetic(luan,"__mul",o1,o2); | |
63 } | |
64 | |
65 public static Object div(LuanState luan,Object o1,Object o2) throws LuanException { | |
66 if( o1 instanceof Number && o2 instanceof Number ) | |
67 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue(); | |
68 return arithmetic(luan,"__div",o1,o2); | |
69 } | |
70 | |
71 public static Object mod(LuanState luan,Object o1,Object o2) throws LuanException { | |
72 if( o1 instanceof Number && o2 instanceof Number ) { | |
73 double d1 = ((Number)o1).doubleValue(); | |
74 double d2 = ((Number)o2).doubleValue(); | |
75 return d1 - Math.floor(d1/d2)*d2; | |
76 } | |
77 return arithmetic(luan,"__mod",o1,o2); | |
78 } | |
79 | |
80 public static Object add(LuanState luan,Object o1,Object o2) throws LuanException { | |
81 if( o1 instanceof Number && o2 instanceof Number ) | |
82 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue(); | |
83 return arithmetic(luan,"__add",o1,o2); | |
84 } | |
85 | |
86 public static Object sub(LuanState luan,Object o1,Object o2) throws LuanException { | |
87 if( o1 instanceof Number && o2 instanceof Number ) | |
88 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); | |
89 return arithmetic(luan,"__sub",o1,o2); | |
90 } | |
91 | |
92 public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException { | |
93 LuanFunction fn = Luan.getBinHandler("__concat",o1,o2); | |
94 if( fn != null ) | |
95 return Luan.first(fn.call(luan,new Object[]{o1,o2})); | |
96 String s1 = luan.toString(o1); | |
97 String s2 = luan.toString(o2); | |
98 return s1 + s2; | |
99 } | |
100 | |
101 public static boolean eq(LuanState luan,Object o1,Object o2) throws LuanException { | |
102 if( o1 == o2 || o1 != null && o1.equals(o2) ) | |
103 return true; | |
104 if( o1 instanceof Number && o2 instanceof Number ) { | |
105 Number n1 = (Number)o1; | |
106 Number n2 = (Number)o2; | |
107 return n1.doubleValue() == n2.doubleValue(); | |
108 } | |
109 if( o1 instanceof byte[] && o2 instanceof byte[] ) { | |
110 byte[] b1 = (byte[])o1; | |
111 byte[] b2 = (byte[])o2; | |
112 return Arrays.equals(b1,b2); | |
113 } | |
114 if( !(o1 instanceof LuanTable && o2 instanceof LuanTable) ) | |
115 return false; | |
116 LuanTable t1 = (LuanTable)o1; | |
117 LuanTable t2 = (LuanTable)o2; | |
118 LuanTable mt1 = t1.getMetatable(); | |
119 LuanTable mt2 = t2.getMetatable(); | |
120 if( mt1==null || mt2==null ) | |
121 return false; | |
122 Object f = mt1.rawGet("__eq"); | |
123 if( f == null || !f.equals(mt2.rawGet("__eq")) ) | |
124 return false; | |
125 LuanFunction fn = Luan.checkFunction(f); | |
126 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); | |
127 } | |
128 | |
129 public static boolean le(LuanState luan,Object o1,Object o2) throws LuanException { | |
130 if( o1 instanceof Number && o2 instanceof Number ) { | |
131 Number n1 = (Number)o1; | |
132 Number n2 = (Number)o2; | |
133 return n1.doubleValue() <= n2.doubleValue(); | |
134 } | |
135 if( o1 instanceof String && o2 instanceof String ) { | |
136 String s1 = (String)o1; | |
137 String s2 = (String)o2; | |
138 return s1.compareTo(s2) <= 0; | |
139 } | |
140 LuanFunction fn = Luan.getBinHandler("__le",o1,o2); | |
141 if( fn != null ) | |
142 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); | |
143 fn = Luan.getBinHandler("__lt",o1,o2); | |
144 if( fn != null ) | |
145 return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) ); | |
146 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); | |
147 } | |
148 | |
149 public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException { | |
150 return Luan.isLessThan(luan,o1,o2); | |
151 } | |
152 | |
153 public static boolean cnd(Object o) throws LuanException { | |
154 return !(o == null || Boolean.FALSE.equals(o)); | |
155 } | |
156 | |
157 public static void nop(Object o) {} | |
158 | |
159 public static void put(LuanState luan,Object t,Object key,Object value) throws LuanException { | |
160 if( t instanceof LuanTable ) { | |
161 LuanTable tbl = (LuanTable)t; | |
162 tbl.put(luan,key,value); | |
163 return; | |
164 } | |
165 if( t != null && luan.java.ok ) | |
166 JavaLuan.__new_index(luan,t,key,value); | |
167 else | |
168 throw new LuanException( "attempt to index a " + Luan.type(t) + " value" ); | |
169 } | |
170 | |
171 public static Object pick(Object o,int i) { | |
172 if( i < 1 ) | |
173 throw new RuntimeException(); | |
174 if( !(o instanceof Object[]) ) | |
175 return null; | |
176 Object[] a = (Object[])o; | |
177 return i<a.length ? a[i] : null; | |
178 } | |
179 | |
180 public static Object[] varArgs(Object[] a,int i) { | |
181 if( i >= a.length ) | |
182 return LuanFunction.NOTHING; | |
183 Object[] rtn = new Object[a.length - i]; | |
184 System.arraycopy(a,i,rtn,0,rtn.length); | |
185 return rtn; | |
186 } | |
187 | |
188 public static Object[] concatArgs(Object o1,Object o2) { | |
189 if( o1 instanceof Object[] ) { | |
190 Object[] a1 = (Object[])o1; | |
191 if( o2 instanceof Object[] ) { | |
192 Object[] a2 = (Object[])o2; | |
193 Object[] rtn = new Object[a1.length+a2.length]; | |
194 System.arraycopy(a1,0,rtn,0,a1.length); | |
195 System.arraycopy(a2,0,rtn,a1.length,a2.length); | |
196 return rtn; | |
197 } else { | |
198 Object[] rtn = new Object[a1.length+1]; | |
199 System.arraycopy(a1,0,rtn,0,a1.length); | |
200 rtn[a1.length] = o2; | |
201 return rtn; | |
202 } | |
203 } else { | |
204 if( o2 instanceof Object[] ) { | |
205 Object[] a2 = (Object[])o2; | |
206 Object[] rtn = new Object[1+a2.length]; | |
207 rtn[0] = o1; | |
208 System.arraycopy(a2,0,rtn,1,a2.length); | |
209 return rtn; | |
210 } else { | |
211 Object[] rtn = new Object[2]; | |
212 rtn[0] = o1; | |
213 rtn[2] = o2; | |
214 return rtn; | |
215 } | |
216 } | |
217 } | |
218 | |
219 public static LuanTable table(Object[] a) { | |
220 LuanTable table = new LuanTable(); | |
221 int i = 0; | |
222 for( Object fld : a ) { | |
223 if( fld instanceof TableField ) { | |
224 TableField tblFld = (TableField)fld; | |
225 Object key = tblFld.key; | |
226 Object value = tblFld.value; | |
227 if( key != null && value != null ) | |
228 table.rawPut(key,value); | |
229 } else { | |
230 i++; | |
231 if( fld != null ) | |
232 table.rawPut(i,fld); | |
233 } | |
234 } | |
235 return table; | |
236 } | |
237 | |
238 public static Object first(Object[] a) { | |
239 return a.length==0 ? null : a[0]; | |
240 } | |
241 | |
242 public static String strconcat(String... a) { | |
243 StringBuilder sb = new StringBuilder(); | |
244 for( String s : a ) { | |
245 sb.append(s); | |
246 } | |
247 return sb.toString(); | |
248 } | |
249 | |
250 } |