comparison src/luan/impl/LuanCompiler.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents 25746915a241
children 8d95711f6615
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
2 2
3 import java.lang.ref.WeakReference; 3 import java.lang.ref.WeakReference;
4 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.InvocationTargetException;
5 import java.util.Map; 5 import java.util.Map;
6 import java.util.HashMap; 6 import java.util.HashMap;
7 import luan.Luan;
7 import luan.LuanFunction; 8 import luan.LuanFunction;
8 import luan.LuanException; 9 import luan.LuanException;
9 import luan.LuanTable; 10 import luan.LuanTable;
10 import luan.LuanClosure; 11 import luan.LuanClosure;
11 import luan.modules.JavaLuan; 12 import luan.modules.JavaLuan;
13 14
14 15
15 public final class LuanCompiler { 16 public final class LuanCompiler {
16 private static final Map<String,WeakReference<Class>> map = new HashMap<String,WeakReference<Class>>(); 17 private static final Map<String,WeakReference<Class>> map = new HashMap<String,WeakReference<Class>>();
17 18
18 public static LuanFunction compile(String sourceText,String sourceName,LuanTable env) throws LuanException { 19 public static LuanFunction compile(Luan luan,String sourceText,String sourceName,LuanTable env) throws LuanException {
19 Class fnClass = env==null ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env); 20 Class fnClass = env==null ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env);
20 boolean javaOk = false; 21 boolean javaOk = false;
21 if( env != null && env.closure != null ) 22 if( env != null && env.closure != null )
22 javaOk = env.closure.javaOk; 23 javaOk = env.closure.javaOk;
23 LuanClosure closure; 24 LuanClosure closure;
24 try { 25 try {
25 closure = (LuanClosure)fnClass.getConstructor(Boolean.TYPE,String.class).newInstance(javaOk,sourceName); 26 closure = (LuanClosure)fnClass.getConstructor(Luan.class,Boolean.TYPE,String.class).newInstance(luan,javaOk,sourceName);
26 } catch(NoSuchMethodException e) { 27 } catch(NoSuchMethodException e) {
27 throw new RuntimeException(e); 28 throw new RuntimeException(e);
28 } catch(InstantiationException e) { 29 } catch(InstantiationException e) {
29 throw new RuntimeException(e); 30 throw new RuntimeException(e);
30 } catch(IllegalAccessException e) { 31 } catch(IllegalAccessException e) {
31 throw new RuntimeException(e); 32 throw new RuntimeException(e);
32 } catch(InvocationTargetException e) { 33 } catch(InvocationTargetException e) {
33 throw new RuntimeException(e); 34 throw new RuntimeException(e);
34 } 35 }
35 closure.upValues[0].o = JavaLuan.javaFn; 36 closure.upValues[0].o = JavaLuan.javaFn(luan);
36 closure.upValues[1].o = PackageLuan.requireFn; 37 closure.upValues[1].o = PackageLuan.requireFn(luan);
37 if( env != null ) { 38 if( env != null ) {
38 closure.upValues[2].o = env; 39 closure.upValues[2].o = env;
39 env.closure = closure; 40 env.closure = closure;
40 } 41 }
41 return closure; 42 return closure;