Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
649:37f0cf43f191 | 650:d658eab7bf4c |
---|---|
1 package luan.impl; | 1 package luan.impl; |
2 | 2 |
3 import java.util.Arrays; | |
3 import java.util.List; | 4 import java.util.List; |
4 import java.util.ArrayList; | 5 import java.util.ArrayList; |
5 import luan.Luan; | 6 import luan.Luan; |
6 import luan.LuanState; | 7 import luan.LuanState; |
7 import luan.LuanTable; | 8 import luan.LuanTable; |
56 } | 57 } |
57 } | 58 } |
58 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); | 59 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); |
59 } | 60 } |
60 | 61 |
61 public static Boolean not(Object o) throws LuanException { | 62 public static boolean not(Object o) throws LuanException { |
62 return !Luan.checkBoolean(o); | 63 return !Luan.checkBoolean(o); |
63 } | 64 } |
64 | 65 |
66 private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException { | |
67 LuanFunction fn = Luan.getBinHandler(op,o1,o2); | |
68 if( fn != null ) | |
69 return Luan.first(fn.call(luan,new Object[]{o1,o2})); | |
70 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); | |
71 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); | |
72 } | |
73 | |
74 public static Object pow(LuanState luan,Object o1,Object o2) throws LuanException { | |
75 if( o1 instanceof Number && o2 instanceof Number ) | |
76 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() ); | |
77 return arithmetic(luan,"__pow",o1,o2); | |
78 } | |
79 | |
80 public static Object mul(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,"__mul",o1,o2); | |
84 } | |
85 | |
86 public static Object div(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,"__div",o1,o2); | |
90 } | |
91 | |
92 public static Object mod(LuanState luan,Object o1,Object o2) throws LuanException { | |
93 if( o1 instanceof Number && o2 instanceof Number ) { | |
94 double d1 = ((Number)o1).doubleValue(); | |
95 double d2 = ((Number)o2).doubleValue(); | |
96 return d1 - Math.floor(d1/d2)*d2; | |
97 } | |
98 return arithmetic(luan,"__mod",o1,o2); | |
99 } | |
100 | |
101 public static Object add(LuanState luan,Object o1,Object o2) throws LuanException { | |
102 if( o1 instanceof Number && o2 instanceof Number ) | |
103 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue(); | |
104 return arithmetic(luan,"__add",o1,o2); | |
105 } | |
106 | |
107 public static Object sub(LuanState luan,Object o1,Object o2) throws LuanException { | |
108 if( o1 instanceof Number && o2 instanceof Number ) | |
109 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); | |
110 return arithmetic(luan,"__sub",o1,o2); | |
111 } | |
112 | |
113 public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException { | |
114 LuanFunction fn = Luan.getBinHandler("__concat",o1,o2); | |
115 if( fn != null ) | |
116 return Luan.first(fn.call(luan,new Object[]{o1,o2})); | |
117 String s1 = luan.toString(o1); | |
118 String s2 = luan.toString(o2); | |
119 return s1 + s2; | |
120 } | |
121 | |
122 public static boolean eq(LuanState luan,Object o1,Object o2) throws LuanException { | |
123 if( o1 == o2 || o1 != null && o1.equals(o2) ) | |
124 return true; | |
125 if( o1 instanceof Number && o2 instanceof Number ) { | |
126 Number n1 = (Number)o1; | |
127 Number n2 = (Number)o2; | |
128 return n1.doubleValue() == n2.doubleValue(); | |
129 } | |
130 if( o1 instanceof byte[] && o2 instanceof byte[] ) { | |
131 byte[] b1 = (byte[])o1; | |
132 byte[] b2 = (byte[])o2; | |
133 return Arrays.equals(b1,b2); | |
134 } | |
135 if( !(o1 instanceof LuanTable && o2 instanceof LuanTable) ) | |
136 return false; | |
137 LuanTable t1 = (LuanTable)o1; | |
138 LuanTable t2 = (LuanTable)o2; | |
139 LuanTable mt1 = t1.getMetatable(); | |
140 LuanTable mt2 = t2.getMetatable(); | |
141 if( mt1==null || mt2==null ) | |
142 return false; | |
143 Object f = mt1.rawGet("__eq"); | |
144 if( f == null || !f.equals(mt2.rawGet("__eq")) ) | |
145 return false; | |
146 LuanFunction fn = Luan.checkFunction(f); | |
147 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); | |
148 } | |
149 | |
150 public static boolean le(LuanState luan,Object o1,Object o2) throws LuanException { | |
151 if( o1 instanceof Number && o2 instanceof Number ) { | |
152 Number n1 = (Number)o1; | |
153 Number n2 = (Number)o2; | |
154 return n1.doubleValue() <= n2.doubleValue(); | |
155 } | |
156 if( o1 instanceof String && o2 instanceof String ) { | |
157 String s1 = (String)o1; | |
158 String s2 = (String)o2; | |
159 return s1.compareTo(s2) <= 0; | |
160 } | |
161 LuanFunction fn = Luan.getBinHandler("__le",o1,o2); | |
162 if( fn != null ) | |
163 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); | |
164 fn = Luan.getBinHandler("__lt",o1,o2); | |
165 if( fn != null ) | |
166 return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) ); | |
167 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); | |
168 } | |
169 | |
170 public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException { | |
171 return Luan.isLessThan(luan,o1,o2); | |
172 } | |
173 | |
174 public static boolean cnd(Object o) throws LuanException { | |
175 return !(o == null || Boolean.FALSE.equals(o)); | |
176 } | |
177 | |
65 } | 178 } |