Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/impl/LuanCompiler.java Fri Aug 26 14:36:40 2016 -0600 @@ -0,0 +1,87 @@ +package luan.impl; + +import java.lang.ref.WeakReference; +import java.lang.reflect.InvocationTargetException; +import java.util.Map; +import java.util.HashMap; +import luan.LuanFunction; +import luan.LuanState; +import luan.LuanException; +import luan.LuanTable; +import luan.LuanJava; +import luan.modules.JavaLuan; +import luan.modules.PackageLuan; + + +public final class LuanCompiler { + private static final Map<String,WeakReference<Class>> map = new HashMap<String,WeakReference<Class>>(); + + public static LuanFunction compile(String sourceText,String sourceName,LuanTable env) throws LuanException { + Class fnClass = env==null ? getClass(sourceText,sourceName) : getClass(sourceText,sourceName,env); + LuanJava java; + if( env == null ) { + java = new LuanJava(); + } else { + java = env.java; + if( java == null ) { + java = new LuanJava(); + env.java = java; + } + } + Closure closure; + try { + closure = (Closure)fnClass.getConstructor(LuanJava.class).newInstance(java); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } catch(InstantiationException e) { + throw new RuntimeException(e); + } catch(IllegalAccessException e) { + throw new RuntimeException(e); + } catch(InvocationTargetException e) { + throw new RuntimeException(e); + } + closure.upValues[0].o = JavaLuan.javaFn; + closure.upValues[1].o = PackageLuan.requireFn; + if( env != null ) closure.upValues[2].o = env; + return closure; + } + + private static synchronized Class getClass(String sourceText,String sourceName) throws LuanException { + String key = sourceName + "~~~" + sourceText; + WeakReference<Class> ref = map.get(key); + if( ref != null ) { + Class cls = ref.get(); + if( cls != null ) + return cls; + } + Class cls = getClass(sourceText,sourceName,null); + map.put(key,new WeakReference<Class>(cls)); + return cls; + } + + private static Class getClass(String sourceText,String sourceName,LuanTable env) throws LuanException { + LuanParser parser = new LuanParser(sourceText,sourceName); + parser.addVar( "java" ); + parser.addVar( "require" ); + if( env != null ) parser.addVar( "_ENV" ); + try { + return parser.RequiredModule(); + } catch(ParseException e) { +//e.printStackTrace(); + throw new LuanException( e.getFancyMessage() ); + } + } + + public static String toJava(String sourceText,String sourceName) throws LuanException { + LuanParser parser = new LuanParser(sourceText,sourceName); + parser.addVar( "java" ); + parser.addVar( "require" ); + try { + return parser.RequiredModuleSource(); + } catch(ParseException e) { + throw new LuanException( e.getFancyMessage() ); + } + } + + private LuanCompiler() {} // never +}