Mercurial Hosting > luan
annotate core/src/luan/impl/$Luan.java @ 650:d658eab7bf4c
finish compiling operators
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 31 Mar 2016 22:43:51 -0600 |
parents | 37f0cf43f191 |
children | 140cc5191b7a |
rev | line source |
---|---|
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 package luan.impl; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
3 import java.util.Arrays; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 import java.util.List; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 import java.util.ArrayList; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 import luan.Luan; |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
7 import luan.LuanState; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 import luan.LuanTable; |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
9 import luan.LuanFunction; |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 import luan.LuanException; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 public final class $Luan { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 private $Luan() {} // never |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 private static List<Expressions> listExpressions = new ArrayList<Expressions>(); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 static int addExpressions(Expressions exp) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 int i = listExpressions.size(); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 listExpressions.add(exp); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 return i; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 public static Expressions getExpressions(int i) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 return listExpressions.get(i); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 public static Object first(Object obj) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 return Luan.first(obj); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
34 public static int len(LuanState luan,Object o) throws LuanException { |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 if( o instanceof String ) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 String s = (String)o; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 return s.length(); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 if( o instanceof byte[] ) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 byte[] a = (byte[])o; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 return a.length; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 if( o instanceof LuanTable ) { |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 LuanTable t = (LuanTable)o; |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 return t.length(luan); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 } |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 throw new LuanException( "attempt to get length of a " + Luan.type(o) + " value" ); |
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 } |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
49 |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
50 public static Object unm(LuanState luan,Object o) throws LuanException { |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
51 if( o instanceof Number ) |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
52 return -((Number)o).doubleValue(); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
53 if( o instanceof LuanTable ) { |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
54 LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
55 if( fn != null ) { |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
56 return Luan.first(fn.call(luan,new Object[]{o})); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
57 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
58 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
59 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
60 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
61 |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
62 public static boolean not(Object o) throws LuanException { |
649
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
63 return !Luan.checkBoolean(o); |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
64 } |
37f0cf43f191
UnaryExpr now compiled
Franklin Schmidt <fschmidt@gmail.com>
parents:
648
diff
changeset
|
65 |
650
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
66 private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
67 LuanFunction fn = Luan.getBinHandler(op,o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
68 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
69 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
70 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
71 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
72 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
73 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
74 public static Object pow(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
75 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
76 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
77 return arithmetic(luan,"__pow",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
78 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
79 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
80 public static Object mul(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
81 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
82 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
83 return arithmetic(luan,"__mul",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
84 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
85 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
86 public static Object div(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
87 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
88 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
89 return arithmetic(luan,"__div",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
90 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
91 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
92 public static Object mod(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
93 if( o1 instanceof Number && o2 instanceof Number ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
94 double d1 = ((Number)o1).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
95 double d2 = ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
96 return d1 - Math.floor(d1/d2)*d2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
97 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
98 return arithmetic(luan,"__mod",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
99 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
100 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
101 public static Object add(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
102 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
103 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
104 return arithmetic(luan,"__add",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
105 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
106 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
107 public static Object sub(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
108 if( o1 instanceof Number && o2 instanceof Number ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
109 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
110 return arithmetic(luan,"__sub",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
111 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
112 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
113 public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
114 LuanFunction fn = Luan.getBinHandler("__concat",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
115 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
116 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
117 String s1 = luan.toString(o1); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
118 String s2 = luan.toString(o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
119 return s1 + s2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
120 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
121 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
122 public static boolean eq(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
123 if( o1 == o2 || o1 != null && o1.equals(o2) ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
124 return true; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
125 if( o1 instanceof Number && o2 instanceof Number ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
126 Number n1 = (Number)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
127 Number n2 = (Number)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
128 return n1.doubleValue() == n2.doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
129 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
130 if( o1 instanceof byte[] && o2 instanceof byte[] ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
131 byte[] b1 = (byte[])o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
132 byte[] b2 = (byte[])o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
133 return Arrays.equals(b1,b2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
134 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
135 if( !(o1 instanceof LuanTable && o2 instanceof LuanTable) ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
136 return false; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
137 LuanTable t1 = (LuanTable)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
138 LuanTable t2 = (LuanTable)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
139 LuanTable mt1 = t1.getMetatable(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
140 LuanTable mt2 = t2.getMetatable(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
141 if( mt1==null || mt2==null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
142 return false; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
143 Object f = mt1.rawGet("__eq"); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
144 if( f == null || !f.equals(mt2.rawGet("__eq")) ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
145 return false; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
146 LuanFunction fn = Luan.checkFunction(f); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
147 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
148 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
149 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
150 public static boolean le(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
151 if( o1 instanceof Number && o2 instanceof Number ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
152 Number n1 = (Number)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
153 Number n2 = (Number)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
154 return n1.doubleValue() <= n2.doubleValue(); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
155 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
156 if( o1 instanceof String && o2 instanceof String ) { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
157 String s1 = (String)o1; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
158 String s2 = (String)o2; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
159 return s1.compareTo(s2) <= 0; |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
160 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
161 LuanFunction fn = Luan.getBinHandler("__le",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
162 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
163 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
164 fn = Luan.getBinHandler("__lt",o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
165 if( fn != null ) |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
166 return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
167 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
168 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
169 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
170 public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
171 return Luan.isLessThan(luan,o1,o2); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
172 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
173 |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
174 public static boolean cnd(Object o) throws LuanException { |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
175 return !(o == null || Boolean.FALSE.equals(o)); |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
176 } |
d658eab7bf4c
finish compiling operators
Franklin Schmidt <fschmidt@gmail.com>
parents:
649
diff
changeset
|
177 |
648
e387e4021afe
start compiler with len operator
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
178 } |