Mercurial Hosting > luan
comparison src/luan/modules/BasicLuan.java @ 1563:8fbcc4747091
remove LuanFunction.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 09 Nov 2020 01:37:57 -0700 |
parents | b89212fd04b5 |
children | c922446f53aa |
comparison
equal
deleted
inserted
replaced
1562:b89212fd04b5 | 1563:8fbcc4747091 |
---|---|
13 import luan.Luan; | 13 import luan.Luan; |
14 import luan.LuanTable; | 14 import luan.LuanTable; |
15 import luan.LuanFunction; | 15 import luan.LuanFunction; |
16 import luan.LuanException; | 16 import luan.LuanException; |
17 import luan.LuanCloner; | 17 import luan.LuanCloner; |
18 import luan.LuanCloneable; | |
18 import luan.LuanImmutabler; | 19 import luan.LuanImmutabler; |
19 import luan.modules.parsers.LuanToString; | 20 import luan.modules.parsers.LuanToString; |
20 | 21 |
21 | 22 |
22 public final class BasicLuan { | 23 public final class BasicLuan { |
43 if( src == null ) | 44 if( src == null ) |
44 return null; | 45 return null; |
45 return load(luan,src,fileName,null); | 46 return load(luan,src,fileName,null); |
46 } | 47 } |
47 */ | 48 */ |
48 public static LuanFunction pairs(final LuanTable t) throws LuanException { | 49 public static LuanFunction pairs(Luan luan,final LuanTable t) throws LuanException { |
49 Utils.checkNotNull(t); | 50 Utils.checkNotNull(t); |
50 return t.pairs(); | 51 return t.pairs(luan); |
51 } | 52 } |
52 | 53 |
53 private static class Ipairs extends LuanFunction { | 54 private static class Ipairs extends LuanFunction implements LuanCloneable, Cloneable { |
54 List<Object> list; | 55 List<Object> list; |
55 int i = 0; | 56 int i = 0; |
56 final int size; | 57 final int size; |
57 | 58 |
58 Ipairs(LuanTable t) { | 59 Ipairs(LuanTable t) { |
59 super(true); | |
60 list = t.asList(); | 60 list = t.asList(); |
61 size = list.size(); | 61 size = list.size(); |
62 } | 62 } |
63 | 63 |
64 @Override public Object[] call(Object[] args) { | 64 @Override public Object[] call(Luan luan,Object[] args) { |
65 if( i >= size ) | 65 if( i >= size ) |
66 return LuanFunction.NOTHING; | 66 return LuanFunction.NOTHING; |
67 Object val = list.get(i++); | 67 Object val = list.get(i++); |
68 return new Object[]{i,val}; | 68 return new Object[]{i,val}; |
69 } | 69 } |
70 | 70 |
71 @Override protected void completeClone(LuanFunction dc,LuanCloner cloner) { | 71 @Override public final Ipairs shallowClone() { |
72 try { | |
73 return (Ipairs)clone(); | |
74 } catch(CloneNotSupportedException e) { | |
75 throw new RuntimeException(e); | |
76 } | |
77 } | |
78 | |
79 @Override public final void deepenClone(LuanCloneable dc,LuanCloner cloner) { | |
72 Ipairs clone = (Ipairs)dc; | 80 Ipairs clone = (Ipairs)dc; |
73 clone.list = (List)cloner.clone(list); | 81 clone.list = (List)cloner.clone(list); |
74 super.completeClone(dc,cloner); | |
75 } | 82 } |
76 | 83 |
77 @Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException { | 84 @Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException { |
78 throw new LuanException("ipairs cannot be made immutable"); | 85 throw new LuanException("ipairs cannot be made immutable"); |
79 } | 86 } |
122 return t.rawLength(); | 129 return t.rawLength(); |
123 } | 130 } |
124 throw new LuanException( "bad argument #1 to 'raw_len' (table or string expected)" ); | 131 throw new LuanException( "bad argument #1 to 'raw_len' (table or string expected)" ); |
125 } | 132 } |
126 | 133 |
127 public static String to_string(Object v) throws LuanException { | 134 public static String to_string(Luan luan,Object v) throws LuanException { |
128 return Luan.luanToString(v); | 135 return luan.luanToString(v); |
129 } | 136 } |
130 | 137 |
131 public static LuanTable new_error(Luan luan,Object msg) throws LuanException { | 138 public static LuanTable new_error(Luan luan,Object msg) throws LuanException { |
132 String s = Luan.luanToString(msg); | 139 String s = luan.luanToString(msg); |
133 LuanTable tbl = new LuanException(s).table(luan); | 140 LuanTable tbl = new LuanException(s).table(luan); |
134 tbl.rawPut( "message", msg ); | 141 tbl.rawPut( "message", msg ); |
135 return tbl; | 142 return tbl; |
136 } | 143 } |
137 | 144 |
153 | 160 |
154 public static LuanFunction range(final double from,final double to,Double stepV) throws LuanException { | 161 public static LuanFunction range(final double from,final double to,Double stepV) throws LuanException { |
155 final double step = stepV==null ? 1.0 : stepV; | 162 final double step = stepV==null ? 1.0 : stepV; |
156 if( step == 0.0 ) | 163 if( step == 0.0 ) |
157 throw new LuanException("bad argument #3 (step may not be zero)"); | 164 throw new LuanException("bad argument #3 (step may not be zero)"); |
158 return new LuanFunction(false) { | 165 return new LuanFunction() { |
159 double v = from; | 166 double v = from; |
160 | 167 |
161 @Override public Object call(Object[] args) { | 168 @Override public Object call(Luan luan,Object[] args) { |
162 if( step > 0.0 && v > to || step < 0.0 && v < to ) | 169 if( step > 0.0 && v > to || step < 0.0 && v < to ) |
163 return LuanFunction.NOTHING; | 170 return LuanFunction.NOTHING; |
164 double rtn = v; | 171 double rtn = v; |
165 v += step; | 172 v += step; |
166 return rtn; | 173 return rtn; |
167 } | 174 } |
168 }; | 175 }; |
169 } | 176 } |
170 | 177 |
171 private static class Values extends LuanFunction { | 178 private static class Values extends LuanFunction implements LuanCloneable, Cloneable { |
172 Object[] args; | 179 Object[] args; |
173 int i = 0; | 180 int i = 0; |
174 | 181 |
175 Values(Object[] args) { | 182 Values(Object[] args) { |
176 super(true); | |
177 this.args = args; | 183 this.args = args; |
178 } | 184 } |
179 | 185 |
180 @Override public Object[] call(Object[] x) { | 186 @Override public Object[] call(Luan luan,Object[] x) { |
181 if( i >= args.length ) | 187 if( i >= args.length ) |
182 return LuanFunction.NOTHING; | 188 return LuanFunction.NOTHING; |
183 Object val = args[i++]; | 189 Object val = args[i++]; |
184 return new Object[]{i,val}; | 190 return new Object[]{i,val}; |
185 } | 191 } |
186 | 192 |
187 @Override protected void completeClone(LuanFunction dc,LuanCloner cloner) { | 193 @Override public final Values shallowClone() { |
194 try { | |
195 return (Values)clone(); | |
196 } catch(CloneNotSupportedException e) { | |
197 throw new RuntimeException(e); | |
198 } | |
199 } | |
200 | |
201 @Override public final void deepenClone(LuanCloneable dc,LuanCloner cloner) { | |
188 Values clone = (Values)dc; | 202 Values clone = (Values)dc; |
189 clone.args = (Object[])cloner.clone(args); | 203 clone.args = (Object[])cloner.clone(args); |
190 super.completeClone(dc,cloner); | |
191 } | 204 } |
192 | 205 |
193 @Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException { | 206 @Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException { |
194 throw new LuanException("values cannot be made immutable"); | 207 throw new LuanException("values cannot be made immutable"); |
195 } | 208 } |
216 } else { | 229 } else { |
217 return obj.hashCode(); | 230 return obj.hashCode(); |
218 } | 231 } |
219 } | 232 } |
220 | 233 |
221 public static String stringify(Luan luan,Object obj,LuanTable options,LuanTable subOptions) throws LuanException { | 234 public static String stringify(Object obj,LuanTable options,LuanTable subOptions) throws LuanException { |
222 LuanToString lts = new LuanToString(luan,options,subOptions); | 235 LuanToString lts = new LuanToString(options,subOptions); |
223 return lts.toString(obj); | 236 return lts.toString(obj); |
224 } | 237 } |
225 | 238 |
226 public static String json_string(Object obj,LuanTable options) throws LuanException { | 239 public static String json_string(Object obj,LuanTable options) throws LuanException { |
227 JsonToString jts = new JsonToString(); | 240 JsonToString jts = new JsonToString(); |