comparison src/luan/modules/BasicLuan.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents c88b486a9511
children b3c4fcf29a53
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
10 import java.util.Arrays; 10 import java.util.Arrays;
11 import luan.Luan; 11 import luan.Luan;
12 import luan.LuanTable; 12 import luan.LuanTable;
13 import luan.LuanFunction; 13 import luan.LuanFunction;
14 import luan.LuanException; 14 import luan.LuanException;
15 import luan.LuanMethod; 15 import luan.LuanCloner;
16 import luan.modules.parsers.LuanToString; 16 import luan.modules.parsers.LuanToString;
17 17
18 18
19 public final class BasicLuan { 19 public final class BasicLuan {
20 20
21 public static String type(Object obj) { 21 public static String type(Object obj) {
22 return Luan.type(obj); 22 return Luan.type(obj);
23 } 23 }
24 24
25 public static LuanFunction load(String text,String sourceName,LuanTable env) 25 public static LuanFunction load(Luan luan,String text,String sourceName,LuanTable env)
26 throws LuanException 26 throws LuanException
27 { 27 {
28 Utils.checkNotNull(text); 28 Utils.checkNotNull(text);
29 Utils.checkNotNull(sourceName,1); 29 Utils.checkNotNull(sourceName,1);
30 return Luan.load(text,sourceName,env); 30 return luan.load(text,sourceName,env);
31 } 31 }
32 32
33 public static LuanFunction load_file(Luan luan,String fileName) throws LuanException { 33 public static LuanFunction load_file(Luan luan,String fileName) throws LuanException {
34 if( fileName == null ) { 34 if( fileName == null ) {
35 fileName = "stdin:"; 35 fileName = "stdin:";
37 fileName = "file:" + fileName; 37 fileName = "file:" + fileName;
38 } 38 }
39 String src = PackageLuan.read(luan,fileName); 39 String src = PackageLuan.read(luan,fileName);
40 if( src == null ) 40 if( src == null )
41 return null; 41 return null;
42 return load(src,fileName,null); 42 return load(luan,src,fileName,null);
43 } 43 }
44 44
45 public static LuanFunction pairs(final LuanTable t) throws LuanException { 45 public static LuanFunction pairs(final LuanTable t) throws LuanException {
46 Utils.checkNotNull(t); 46 Utils.checkNotNull(t);
47 return t.pairs(); 47 return t.pairs();
48 } 48 }
49 49
50 public static LuanFunction ipairs(final LuanTable t) throws LuanException { 50 private static class Ipairs extends LuanFunction {
51 List<Object> list;
52 int i = 0;
53 final int size;
54
55 Ipairs(LuanTable t) {
56 super(true);
57 list = t.asList();
58 size = list.size();
59 }
60
61 @Override public Object[] call(Object[] args) {
62 if( i >= size )
63 return LuanFunction.NOTHING;
64 Object val = list.get(i++);
65 return new Object[]{i,val};
66 }
67
68 @Override protected void completeClone(LuanFunction dc,LuanCloner cloner) {
69 Ipairs clone = (Ipairs)dc;
70 clone.list = (List)cloner.clone(list);
71 super.completeClone(dc,cloner);
72 }
73 }
74
75 public static LuanFunction ipairs(LuanTable t) throws LuanException {
51 Utils.checkNotNull(t); 76 Utils.checkNotNull(t);
52 return new LuanFunction() { 77 return new Ipairs(t);
53 List<Object> list = t.asList();
54 int i = 0;
55 final int size = list.size();
56
57 @Override public Object[] call(Luan luan,Object[] args) {
58 if( i >= size )
59 return LuanFunction.NOTHING;
60 Object val = list.get(i++);
61 return new Object[]{i,val};
62 }
63 };
64 } 78 }
65 79
66 public static Object get_metatable(LuanTable table) throws LuanException { 80 public static Object get_metatable(LuanTable table) throws LuanException {
67 Utils.checkNotNull(table); 81 Utils.checkNotNull(table);
68 LuanTable metatable = table.getMetatable(); 82 LuanTable metatable = table.getMetatable();
132 146
133 public static LuanFunction range(final double from,final double to,Double stepV) throws LuanException { 147 public static LuanFunction range(final double from,final double to,Double stepV) throws LuanException {
134 final double step = stepV==null ? 1.0 : stepV; 148 final double step = stepV==null ? 1.0 : stepV;
135 if( step == 0.0 ) 149 if( step == 0.0 )
136 throw new LuanException("bad argument #3 (step may not be zero)"); 150 throw new LuanException("bad argument #3 (step may not be zero)");
137 return new LuanFunction() { 151 return new LuanFunction(false) {
138 double v = from; 152 double v = from;
139 153
140 @Override public Object call(Luan luan,Object[] args) { 154 @Override public Object call(Object[] args) {
141 if( step > 0.0 && v > to || step < 0.0 && v < to ) 155 if( step > 0.0 && v > to || step < 0.0 && v < to )
142 return LuanFunction.NOTHING; 156 return LuanFunction.NOTHING;
143 double rtn = v; 157 double rtn = v;
144 v += step; 158 v += step;
145 return rtn; 159 return rtn;
146 } 160 }
147 }; 161 };
148 } 162 }
149 163
164 private static class Values extends LuanFunction {
165 Object[] args;
166 int i = 0;
167
168 Values(Object[] args) {
169 super(true);
170 this.args = args;
171 }
172
173 @Override public Object[] call(Object[] args) {
174 if( i >= args.length )
175 return LuanFunction.NOTHING;
176 Object val = args[i++];
177 return new Object[]{i,val};
178 }
179
180 @Override protected void completeClone(LuanFunction dc,LuanCloner cloner) {
181 Values clone = (Values)dc;
182 clone.args = (Object[])cloner.clone(args);
183 super.completeClone(dc,cloner);
184 }
185 }
186
150 public static LuanFunction values(final Object... args) throws LuanException { 187 public static LuanFunction values(final Object... args) throws LuanException {
151 return new LuanFunction() { 188 return new Values(args);
152 int i = 0;
153
154 @Override public Object call(Luan luan,Object[] unused) {
155 if( i >= args.length )
156 return LuanFunction.NOTHING;
157 Object val = args[i++];
158 return new Object[]{i,val};
159 }
160 };
161 } 189 }
162 190
163 private LuanFunction fn(Object obj) { 191 private LuanFunction fn(Object obj) {
164 return obj instanceof LuanFunction ? (LuanFunction)obj : null; 192 return obj instanceof LuanFunction ? (LuanFunction)obj : null;
165 } 193 }
166 194
167 public static Object try_(Luan luan,LuanTable blocks,Object... args) throws LuanException { 195 public static Object try_(LuanTable blocks,Object... args) throws LuanException {
168 Utils.checkNotNull(blocks); 196 Utils.checkNotNull(blocks);
169 Object obj = blocks.get(1); 197 Object obj = blocks.get(1);
170 if( obj == null ) 198 if( obj == null )
171 throw new LuanException("missing 'try' value"); 199 throw new LuanException("missing 'try' value");
172 if( !(obj instanceof LuanFunction) ) 200 if( !(obj instanceof LuanFunction) )
185 if( !(obj instanceof LuanFunction) ) 213 if( !(obj instanceof LuanFunction) )
186 throw new LuanException("bad 'finally' value (function expected, got "+Luan.type(obj)+")"); 214 throw new LuanException("bad 'finally' value (function expected, got "+Luan.type(obj)+")");
187 finallyFn = (LuanFunction)obj; 215 finallyFn = (LuanFunction)obj;
188 } 216 }
189 try { 217 try {
190 return tryFn.call(luan,args); 218 return tryFn.call(args);
191 } catch(LuanException e) { 219 } catch(LuanException e) {
192 if( catchFn == null ) 220 if( catchFn == null )
193 throw e; 221 throw e;
194 return catchFn.call(luan,new Object[]{e.table(luan)}); 222 return catchFn.call(e.table(blocks.luan()));
195 } finally { 223 } finally {
196 if( finallyFn != null ) 224 if( finallyFn != null )
197 finallyFn.call(luan); 225 finallyFn.call();
198 } 226 }
199 } 227 }
200 228
201 @LuanMethod public static Object[] pcall(Luan luan,LuanFunction f,Object... args) { 229 public static Object[] pcall(LuanFunction f,Object... args) {
202 try { 230 try {
203 Object[] r = Luan.array(f.call(luan,args)); 231 Object[] r = Luan.array(f.call(args));
204 Object[] rtn = new Object[r.length+1]; 232 Object[] rtn = new Object[r.length+1];
205 rtn[0] = true; 233 rtn[0] = true;
206 for( int i=0; i<r.length; i++ ) { 234 for( int i=0; i<r.length; i++ ) {
207 rtn[i+1] = r[i]; 235 rtn[i+1] = r[i];
208 } 236 }
209 return rtn; 237 return rtn;
210 } catch(LuanException e) { 238 } catch(LuanException e) {
211 return new Object[]{false,e.table(luan)}; 239 return new Object[]{false,e.table(f.luan())};
212 } 240 }
213 } 241 }
214 242
215 public static String number_type(Number v) throws LuanException { 243 public static String number_type(Number v) throws LuanException {
216 Utils.checkNotNull(v); 244 Utils.checkNotNull(v);