comparison src/luan/modules/BasicLuan.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
7 import java.util.Map; 7 import java.util.Map;
8 import java.util.List; 8 import java.util.List;
9 import java.util.ArrayList; 9 import java.util.ArrayList;
10 import java.util.Arrays; 10 import java.util.Arrays;
11 import luan.Luan; 11 import luan.Luan;
12 import luan.LuanState;
13 import luan.LuanTable; 12 import luan.LuanTable;
14 import luan.LuanFunction; 13 import luan.LuanFunction;
15 import luan.LuanException; 14 import luan.LuanException;
16 import luan.LuanMethod; 15 import luan.LuanMethod;
17 import luan.modules.parsers.LuanToString; 16 import luan.modules.parsers.LuanToString;
29 Utils.checkNotNull(text); 28 Utils.checkNotNull(text);
30 Utils.checkNotNull(sourceName,1); 29 Utils.checkNotNull(sourceName,1);
31 return Luan.load(text,sourceName,env); 30 return Luan.load(text,sourceName,env);
32 } 31 }
33 32
34 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { 33 public static LuanFunction load_file(Luan luan,String fileName) throws LuanException {
35 if( fileName == null ) { 34 if( fileName == null ) {
36 fileName = "stdin:"; 35 fileName = "stdin:";
37 } else if( fileName.indexOf(':') == -1 ) { 36 } else if( fileName.indexOf(':') == -1 ) {
38 fileName = "file:" + fileName; 37 fileName = "file:" + fileName;
39 } 38 }
53 return new LuanFunction() { 52 return new LuanFunction() {
54 List<Object> list = t.asList(); 53 List<Object> list = t.asList();
55 int i = 0; 54 int i = 0;
56 final int size = list.size(); 55 final int size = list.size();
57 56
58 @Override public Object[] call(LuanState luan,Object[] args) { 57 @Override public Object[] call(Luan luan,Object[] args) {
59 if( i >= size ) 58 if( i >= size )
60 return LuanFunction.NOTHING; 59 return LuanFunction.NOTHING;
61 Object val = list.get(i++); 60 Object val = list.get(i++);
62 return new Object[]{i,val}; 61 return new Object[]{i,val};
63 } 62 }
102 return t.rawLength(); 101 return t.rawLength();
103 } 102 }
104 throw new LuanException( "bad argument #1 to 'raw_len' (table or string expected)" ); 103 throw new LuanException( "bad argument #1 to 'raw_len' (table or string expected)" );
105 } 104 }
106 105
107 public static String to_string(LuanState luan,Object v) throws LuanException { 106 public static String to_string(Luan luan,Object v) throws LuanException {
108 return luan.toString(v); 107 return luan.toString(v);
109 } 108 }
110 109
111 public static LuanTable new_error(LuanState luan,Object msg) throws LuanException { 110 public static LuanTable new_error(Luan luan,Object msg) throws LuanException {
112 String s = luan.toString(msg); 111 String s = luan.toString(msg);
113 LuanTable tbl = new LuanException(s).table(luan); 112 LuanTable tbl = new LuanException(s).table(luan);
114 tbl.rawPut( "message", msg ); 113 tbl.rawPut( "message", msg );
115 return tbl; 114 return tbl;
116 } 115 }
136 if( step == 0.0 ) 135 if( step == 0.0 )
137 throw new LuanException("bad argument #3 (step may not be zero)"); 136 throw new LuanException("bad argument #3 (step may not be zero)");
138 return new LuanFunction() { 137 return new LuanFunction() {
139 double v = from; 138 double v = from;
140 139
141 @Override public Object call(LuanState luan,Object[] args) { 140 @Override public Object call(Luan luan,Object[] args) {
142 if( step > 0.0 && v > to || step < 0.0 && v < to ) 141 if( step > 0.0 && v > to || step < 0.0 && v < to )
143 return LuanFunction.NOTHING; 142 return LuanFunction.NOTHING;
144 double rtn = v; 143 double rtn = v;
145 v += step; 144 v += step;
146 return rtn; 145 return rtn;
150 149
151 public static LuanFunction values(final Object... args) throws LuanException { 150 public static LuanFunction values(final Object... args) throws LuanException {
152 return new LuanFunction() { 151 return new LuanFunction() {
153 int i = 0; 152 int i = 0;
154 153
155 @Override public Object call(LuanState luan,Object[] unused) { 154 @Override public Object call(Luan luan,Object[] unused) {
156 if( i >= args.length ) 155 if( i >= args.length )
157 return LuanFunction.NOTHING; 156 return LuanFunction.NOTHING;
158 Object val = args[i++]; 157 Object val = args[i++];
159 return new Object[]{i,val}; 158 return new Object[]{i,val};
160 } 159 }
163 162
164 private LuanFunction fn(Object obj) { 163 private LuanFunction fn(Object obj) {
165 return obj instanceof LuanFunction ? (LuanFunction)obj : null; 164 return obj instanceof LuanFunction ? (LuanFunction)obj : null;
166 } 165 }
167 166
168 public static Object try_(LuanState luan,LuanTable blocks,Object... args) throws LuanException { 167 public static Object try_(Luan luan,LuanTable blocks,Object... args) throws LuanException {
169 Utils.checkNotNull(blocks); 168 Utils.checkNotNull(blocks);
170 Object obj = blocks.get(1); 169 Object obj = blocks.get(1);
171 if( obj == null ) 170 if( obj == null )
172 throw new LuanException("missing 'try' value"); 171 throw new LuanException("missing 'try' value");
173 if( !(obj instanceof LuanFunction) ) 172 if( !(obj instanceof LuanFunction) )
197 if( finallyFn != null ) 196 if( finallyFn != null )
198 finallyFn.call(luan); 197 finallyFn.call(luan);
199 } 198 }
200 } 199 }
201 200
202 @LuanMethod public static Object[] pcall(LuanState luan,LuanFunction f,Object... args) { 201 @LuanMethod public static Object[] pcall(Luan luan,LuanFunction f,Object... args) {
203 try { 202 try {
204 Object[] r = Luan.array(f.call(luan,args)); 203 Object[] r = Luan.array(f.call(luan,args));
205 Object[] rtn = new Object[r.length+1]; 204 Object[] rtn = new Object[r.length+1];
206 rtn[0] = true; 205 rtn[0] = true;
207 for( int i=0; i<r.length; i++ ) { 206 for( int i=0; i<r.length; i++ ) {