Mercurial Hosting > luan
comparison src/luan/Luan.java @ 1335:e0cf0d108a77
major cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Feb 2019 03:10:45 -0700 |
parents | c88b486a9511 |
children | 8b61c8c4e07a |
comparison
equal
deleted
inserted
replaced
1334:c88b486a9511 | 1335:e0cf0d108a77 |
---|---|
21 public final class Luan implements LuanCloneable { | 21 public final class Luan implements LuanCloneable { |
22 private static final Logger logger = LoggerFactory.getLogger(Luan.class); | 22 private static final Logger logger = LoggerFactory.getLogger(Luan.class); |
23 | 23 |
24 private final List<LuanClosure> stack = new ArrayList<LuanClosure>(); | 24 private final List<LuanClosure> stack = new ArrayList<LuanClosure>(); |
25 private Map registry; | 25 private Map registry; |
26 public boolean isLocked = false; | 26 private boolean isLocked = false; |
27 | 27 |
28 public interface OnClose extends Closeable { | 28 public interface OnClose extends Closeable { |
29 public void onClose(Closeable c); | 29 public void onClose(Closeable c); |
30 } | 30 } |
31 public OnClose onClose; | 31 public OnClose onClose; |
55 int n = stack.size(); | 55 int n = stack.size(); |
56 return n < i ? null : stack.get(n-i); | 56 return n < i ? null : stack.get(n-i); |
57 } | 57 } |
58 | 58 |
59 void push(LuanClosure closure) { | 59 void push(LuanClosure closure) { |
60 if( isLocked ) | |
61 throw new RuntimeException(this+" is locked "+closure); | |
60 stack.add(closure); | 62 stack.add(closure); |
61 } | 63 } |
62 | 64 |
63 void pop() { | 65 void pop() { |
64 stack.remove(stack.size()-1); | 66 stack.remove(stack.size()-1); |
72 if( onClose != null ) | 74 if( onClose != null ) |
73 onClose.onClose(c); | 75 onClose.onClose(c); |
74 } | 76 } |
75 | 77 |
76 public Object eval(String cmd,Object... args) throws LuanException { | 78 public Object eval(String cmd,Object... args) throws LuanException { |
77 return Luan.load(cmd,"eval").call(this,args); | 79 return load(cmd,"eval").call(args); |
78 } | 80 } |
79 | 81 |
80 public Object require(String modName) throws LuanException { | 82 public Object require(String modName) throws LuanException { |
81 return PackageLuan.require(this,modName); | 83 return PackageLuan.require(this,modName); |
82 } | 84 } |
104 return JavaLuan.__index(this,obj,key); | 106 return JavaLuan.__index(this,obj,key); |
105 throw new LuanException("attempt to index a " + Luan.type(obj) + " value" ); | 107 throw new LuanException("attempt to index a " + Luan.type(obj) + " value" ); |
106 } | 108 } |
107 | 109 |
108 | 110 |
109 public boolean isLessThan(Object o1,Object o2) throws LuanException { | 111 public static boolean isLessThan(Object o1,Object o2) throws LuanException { |
110 if( o1 instanceof Number && o2 instanceof Number ) { | 112 if( o1 instanceof Number && o2 instanceof Number ) { |
111 Number n1 = (Number)o1; | 113 Number n1 = (Number)o1; |
112 Number n2 = (Number)o2; | 114 Number n2 = (Number)o2; |
113 return n1.doubleValue() < n2.doubleValue(); | 115 return n1.doubleValue() < n2.doubleValue(); |
114 } | 116 } |
117 String s2 = (String)o2; | 119 String s2 = (String)o2; |
118 return s1.compareTo(s2) < 0; | 120 return s1.compareTo(s2) < 0; |
119 } | 121 } |
120 LuanFunction fn = getBinHandler("__lt",o1,o2); | 122 LuanFunction fn = getBinHandler("__lt",o1,o2); |
121 if( fn != null ) | 123 if( fn != null ) |
122 return Luan.checkBoolean( Luan.first(fn.call(this,new Object[]{o1,o2})) ); | 124 return Luan.checkBoolean( Luan.first(fn.call(o1,o2)) ); |
123 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); | 125 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); |
124 } | 126 } |
125 | 127 |
126 public static LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException { | 128 public static LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException { |
127 if( o1 instanceof LuanTable ) { | 129 if( o1 instanceof LuanTable ) { |
179 } | 181 } |
180 | 182 |
181 public static void doFile(String uri,String... args) throws LuanException { | 183 public static void doFile(String uri,String... args) throws LuanException { |
182 Luan luan = new Luan(); | 184 Luan luan = new Luan(); |
183 LuanFunction fn = (LuanFunction)BasicLuan.load_file(luan,uri); | 185 LuanFunction fn = (LuanFunction)BasicLuan.load_file(luan,uri); |
184 fn.call(luan,args); | 186 fn.call((Object[])args); |
185 } | 187 } |
186 | 188 |
187 public static Object first(Object obj) { | 189 public static Object first(Object obj) { |
188 if( !(obj instanceof Object[]) ) | 190 if( !(obj instanceof Object[]) ) |
189 return obj; | 191 return obj; |
274 if( obj instanceof LuanFunction ) | 276 if( obj instanceof LuanFunction ) |
275 return (LuanFunction)obj; | 277 return (LuanFunction)obj; |
276 throw new LuanException("attempt to call a " + Luan.type(obj) + " value" ); | 278 throw new LuanException("attempt to call a " + Luan.type(obj) + " value" ); |
277 } | 279 } |
278 | 280 |
279 public static LuanFunction load(String text,String sourceName,LuanTable env) | 281 public LuanFunction load(String text,String sourceName,LuanTable env) |
280 throws LuanException | 282 throws LuanException |
281 { | 283 { |
282 return LuanCompiler.compile(text,sourceName,env); | 284 return LuanCompiler.compile(this,text,sourceName,env); |
283 } | 285 } |
284 | 286 |
285 public static LuanFunction load(String text,String sourceName) | 287 public LuanFunction load(String text,String sourceName) |
286 throws LuanException | 288 throws LuanException |
287 { | 289 { |
288 return load(text,sourceName,null); | 290 return load(text,sourceName,null); |
289 } | 291 } |
290 | 292 |