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