Mercurial Hosting > luan
comparison src/luan/impl/LuanImpl.java @ 1333:25746915a241
merge Luan and LuanState
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Feb 2019 22:33:40 -0700 |
parents | f41919741100 |
children | c88b486a9511 |
comparison
equal
deleted
inserted
replaced
1332:11b7e11f9ed5 | 1333:25746915a241 |
---|---|
2 | 2 |
3 import java.util.Arrays; | 3 import java.util.Arrays; |
4 import java.util.List; | 4 import java.util.List; |
5 import java.util.ArrayList; | 5 import java.util.ArrayList; |
6 import luan.Luan; | 6 import luan.Luan; |
7 import luan.LuanState; | |
8 import luan.LuanTable; | 7 import luan.LuanTable; |
9 import luan.LuanFunction; | 8 import luan.LuanFunction; |
10 import luan.LuanException; | 9 import luan.LuanException; |
11 import luan.modules.JavaLuan; | 10 import luan.modules.JavaLuan; |
12 | 11 |
28 return t.length(); | 27 return t.length(); |
29 } | 28 } |
30 throw new LuanException( "attempt to get length of a " + Luan.type(o) + " value" ); | 29 throw new LuanException( "attempt to get length of a " + Luan.type(o) + " value" ); |
31 } | 30 } |
32 | 31 |
33 public static Object unm(LuanState luan,Object o) throws LuanException { | 32 public static Object unm(Luan luan,Object o) throws LuanException { |
34 if( o instanceof Number ) | 33 if( o instanceof Number ) |
35 return -((Number)o).doubleValue(); | 34 return -((Number)o).doubleValue(); |
36 if( o instanceof LuanTable ) { | 35 if( o instanceof LuanTable ) { |
37 LuanFunction fn = luan.getHandlerFunction("__unm",(LuanTable)o); | 36 LuanFunction fn = luan.getHandlerFunction("__unm",(LuanTable)o); |
38 if( fn != null ) { | 37 if( fn != null ) { |
40 } | 39 } |
41 } | 40 } |
42 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); | 41 throw new LuanException("attempt to perform arithmetic on a "+Luan.type(o)+" value"); |
43 } | 42 } |
44 | 43 |
45 private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException { | 44 private static Object arithmetic(Luan luan,String op,Object o1,Object o2) throws LuanException { |
46 LuanFunction fn = luan.getBinHandler(op,o1,o2); | 45 LuanFunction fn = luan.getBinHandler(op,o1,o2); |
47 if( fn != null ) | 46 if( fn != null ) |
48 return Luan.first(fn.call(luan,new Object[]{o1,o2})); | 47 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
49 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); | 48 String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2); |
50 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); | 49 throw new LuanException("attempt to perform arithmetic on a "+type+" value"); |
51 } | 50 } |
52 | 51 |
53 public static Object pow(LuanState luan,Object o1,Object o2) throws LuanException { | 52 public static Object pow(Luan luan,Object o1,Object o2) throws LuanException { |
54 if( o1 instanceof Number && o2 instanceof Number ) | 53 if( o1 instanceof Number && o2 instanceof Number ) |
55 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() ); | 54 return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() ); |
56 return arithmetic(luan,"__pow",o1,o2); | 55 return arithmetic(luan,"__pow",o1,o2); |
57 } | 56 } |
58 | 57 |
59 public static Object mul(LuanState luan,Object o1,Object o2) throws LuanException { | 58 public static Object mul(Luan luan,Object o1,Object o2) throws LuanException { |
60 if( o1 instanceof Number && o2 instanceof Number ) | 59 if( o1 instanceof Number && o2 instanceof Number ) |
61 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue(); | 60 return ((Number)o1).doubleValue() * ((Number)o2).doubleValue(); |
62 return arithmetic(luan,"__mul",o1,o2); | 61 return arithmetic(luan,"__mul",o1,o2); |
63 } | 62 } |
64 | 63 |
65 public static Object div(LuanState luan,Object o1,Object o2) throws LuanException { | 64 public static Object div(Luan luan,Object o1,Object o2) throws LuanException { |
66 if( o1 instanceof Number && o2 instanceof Number ) | 65 if( o1 instanceof Number && o2 instanceof Number ) |
67 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue(); | 66 return ((Number)o1).doubleValue() / ((Number)o2).doubleValue(); |
68 return arithmetic(luan,"__div",o1,o2); | 67 return arithmetic(luan,"__div",o1,o2); |
69 } | 68 } |
70 | 69 |
71 public static Object mod(LuanState luan,Object o1,Object o2) throws LuanException { | 70 public static Object mod(Luan luan,Object o1,Object o2) throws LuanException { |
72 if( o1 instanceof Number && o2 instanceof Number ) { | 71 if( o1 instanceof Number && o2 instanceof Number ) { |
73 double d1 = ((Number)o1).doubleValue(); | 72 double d1 = ((Number)o1).doubleValue(); |
74 double d2 = ((Number)o2).doubleValue(); | 73 double d2 = ((Number)o2).doubleValue(); |
75 return d1 - Math.floor(d1/d2)*d2; | 74 return d1 - Math.floor(d1/d2)*d2; |
76 } | 75 } |
77 return arithmetic(luan,"__mod",o1,o2); | 76 return arithmetic(luan,"__mod",o1,o2); |
78 } | 77 } |
79 | 78 |
80 public static Object add(LuanState luan,Object o1,Object o2) throws LuanException { | 79 public static Object add(Luan luan,Object o1,Object o2) throws LuanException { |
81 if( o1 instanceof Number && o2 instanceof Number ) | 80 if( o1 instanceof Number && o2 instanceof Number ) |
82 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue(); | 81 return ((Number)o1).doubleValue() + ((Number)o2).doubleValue(); |
83 return arithmetic(luan,"__add",o1,o2); | 82 return arithmetic(luan,"__add",o1,o2); |
84 } | 83 } |
85 | 84 |
86 public static Object sub(LuanState luan,Object o1,Object o2) throws LuanException { | 85 public static Object sub(Luan luan,Object o1,Object o2) throws LuanException { |
87 if( o1 instanceof Number && o2 instanceof Number ) | 86 if( o1 instanceof Number && o2 instanceof Number ) |
88 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); | 87 return ((Number)o1).doubleValue() - ((Number)o2).doubleValue(); |
89 return arithmetic(luan,"__sub",o1,o2); | 88 return arithmetic(luan,"__sub",o1,o2); |
90 } | 89 } |
91 | 90 |
92 public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException { | 91 public static Object concat(Luan luan,Object o1,Object o2) throws LuanException { |
93 LuanFunction fn = luan.getBinHandler("__concat",o1,o2); | 92 LuanFunction fn = luan.getBinHandler("__concat",o1,o2); |
94 if( fn != null ) | 93 if( fn != null ) |
95 return Luan.first(fn.call(luan,new Object[]{o1,o2})); | 94 return Luan.first(fn.call(luan,new Object[]{o1,o2})); |
96 String s1 = luan.toString(o1); | 95 String s1 = luan.toString(o1); |
97 String s2 = luan.toString(o2); | 96 String s2 = luan.toString(o2); |
98 return s1 + s2; | 97 return s1 + s2; |
99 } | 98 } |
100 | 99 |
101 public static boolean eq(LuanState luan,Object o1,Object o2) throws LuanException { | 100 public static boolean eq(Luan luan,Object o1,Object o2) throws LuanException { |
102 if( o1 == o2 || o1 != null && o1.equals(o2) ) | 101 if( o1 == o2 || o1 != null && o1.equals(o2) ) |
103 return true; | 102 return true; |
104 if( o1 instanceof Number && o2 instanceof Number ) { | 103 if( o1 instanceof Number && o2 instanceof Number ) { |
105 Number n1 = (Number)o1; | 104 Number n1 = (Number)o1; |
106 Number n2 = (Number)o2; | 105 Number n2 = (Number)o2; |
124 return false; | 123 return false; |
125 LuanFunction fn = Luan.checkFunction(f); | 124 LuanFunction fn = Luan.checkFunction(f); |
126 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); | 125 return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); |
127 } | 126 } |
128 | 127 |
129 public static boolean le(LuanState luan,Object o1,Object o2) throws LuanException { | 128 public static boolean le(Luan luan,Object o1,Object o2) throws LuanException { |
130 if( o1 instanceof Number && o2 instanceof Number ) { | 129 if( o1 instanceof Number && o2 instanceof Number ) { |
131 Number n1 = (Number)o1; | 130 Number n1 = (Number)o1; |
132 Number n2 = (Number)o2; | 131 Number n2 = (Number)o2; |
133 return n1.doubleValue() <= n2.doubleValue(); | 132 return n1.doubleValue() <= n2.doubleValue(); |
134 } | 133 } |
144 if( fn != null ) | 143 if( fn != null ) |
145 return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) ); | 144 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) ); | 145 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); |
147 } | 146 } |
148 | 147 |
149 public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException { | 148 public static boolean lt(Luan luan,Object o1,Object o2) throws LuanException { |
150 return luan.isLessThan(o1,o2); | 149 return luan.isLessThan(o1,o2); |
151 } | 150 } |
152 | 151 |
153 public static boolean cnd(Object o) throws LuanException { | 152 public static boolean cnd(Object o) throws LuanException { |
154 return !(o == null || Boolean.FALSE.equals(o)); | 153 return !(o == null || Boolean.FALSE.equals(o)); |
155 } | 154 } |
156 | 155 |
157 public static void nop(Object o) {} | 156 public static void nop(Object o) {} |
158 | 157 |
159 public static void put(LuanState luan,Object t,Object key,Object value) throws LuanException { | 158 public static void put(Luan luan,Object t,Object key,Object value) throws LuanException { |
160 if( t instanceof LuanTable ) { | 159 if( t instanceof LuanTable ) { |
161 LuanTable tbl = (LuanTable)t; | 160 LuanTable tbl = (LuanTable)t; |
162 tbl.put(key,value); | 161 tbl.put(key,value); |
163 return; | 162 return; |
164 } | 163 } |
227 return rtn; | 226 return rtn; |
228 } | 227 } |
229 } | 228 } |
230 } | 229 } |
231 | 230 |
232 public static LuanTable table(LuanState luan,Object[] a) throws LuanException { | 231 public static LuanTable table(Luan luan,Object[] a) throws LuanException { |
233 LuanTable table = new LuanTable(luan); | 232 LuanTable table = new LuanTable(luan); |
234 int i = 0; | 233 int i = 0; |
235 for( Object fld : a ) { | 234 for( Object fld : a ) { |
236 if( fld instanceof TableField ) { | 235 if( fld instanceof TableField ) { |
237 TableField tblFld = (TableField)fld; | 236 TableField tblFld = (TableField)fld; |