Mercurial Hosting > luan
comparison src/luan/impl/LuanCompiler.java @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | core/src/luan/impl/LuanCompiler.java@77e0c859c8a3 |
children | ba4daf107e07 |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 package luan.impl; | |
2 | |
3 import java.lang.ref.WeakReference; | |
4 import java.lang.reflect.InvocationTargetException; | |
5 import java.util.Map; | |
6 import java.util.HashMap; | |
7 import luan.LuanFunction; | |
8 import luan.LuanState; | |
9 import luan.LuanException; | |
10 import luan.LuanTable; | |
11 import luan.LuanJava; | |
12 import luan.modules.JavaLuan; | |
13 import luan.modules.PackageLuan; | |
14 | |
15 | |
16 public final class LuanCompiler { | |
17 private static final Map<String,WeakReference<Class>> map = new HashMap<String,WeakReference<Class>>(); | |
18 | |
19 public static LuanFunction compile(String sourceText,String sourceName,LuanTable env) throws LuanException { | |
20 Class fnClass = env==null ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env); | |
21 LuanJava java; | |
22 if( env == null ) { | |
23 java = new LuanJava(); | |
24 } else { | |
25 java = env.java; | |
26 if( java == null ) { | |
27 java = new LuanJava(); | |
28 env.java = java; | |
29 } | |
30 } | |
31 Closure closure; | |
32 try { | |
33 closure = (Closure)fnClass.getConstructor(LuanJava.class).newInstance(java); | |
34 } catch(NoSuchMethodException e) { | |
35 throw new RuntimeException(e); | |
36 } catch(InstantiationException e) { | |
37 throw new RuntimeException(e); | |
38 } catch(IllegalAccessException e) { | |
39 throw new RuntimeException(e); | |
40 } catch(InvocationTargetException e) { | |
41 throw new RuntimeException(e); | |
42 } | |
43 closure.upValues[0].o = JavaLuan.javaFn; | |
44 closure.upValues[1].o = PackageLuan.requireFn; | |
45 if( env != null ) closure.upValues[2].o = env; | |
46 return closure; | |
47 } | |
48 | |
49 private static synchronized Class getClass(String sourceText,String sourceName) throws LuanException { | |
50 String key = sourceName + "~~~" + sourceText; | |
51 WeakReference<Class> ref = map.get(key); | |
52 if( ref != null ) { | |
53 Class cls = ref.get(); | |
54 if( cls != null ) | |
55 return cls; | |
56 } | |
57 Class cls = getClass(sourceText,sourceName,null); | |
58 map.put(key,new WeakReference<Class>(cls)); | |
59 return cls; | |
60 } | |
61 | |
62 private static Class getClass(String sourceText,String sourceName,LuanTable env) throws LuanException { | |
63 LuanParser parser = new LuanParser(sourceText,sourceName); | |
64 parser.addVar( "java" ); | |
65 parser.addVar( "require" ); | |
66 if( env != null ) parser.addVar( "_ENV" ); | |
67 try { | |
68 return parser.RequiredModule(); | |
69 } catch(ParseException e) { | |
70 //e.printStackTrace(); | |
71 throw new LuanException( e.getFancyMessage() ); | |
72 } | |
73 } | |
74 | |
75 public static String toJava(String sourceText,String sourceName) throws LuanException { | |
76 LuanParser parser = new LuanParser(sourceText,sourceName); | |
77 parser.addVar( "java" ); | |
78 parser.addVar( "require" ); | |
79 try { | |
80 return parser.RequiredModuleSource(); | |
81 } catch(ParseException e) { | |
82 throw new LuanException( e.getFancyMessage() ); | |
83 } | |
84 } | |
85 | |
86 private LuanCompiler() {} // never | |
87 } |