Mercurial Hosting > luan
view core/src/luan/impl/LuanCompiler.java @ 701:d0280c7fdc3a
add Io.ip()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 04 May 2016 17:49:41 -0600 |
parents | b620b8e1010f |
children | 6e6e9e73abaa |
line wrap: on
line source
package luan.impl; import java.util.Map; import java.lang.reflect.InvocationTargetException; 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 LuanCompiler() {} // never public static LuanFunction compile(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" ); Class fnClass; try { fnClass = parser.RequiredModule(); } catch(ParseException e) { //e.printStackTrace(); throw new LuanException( e.getFancyMessage() ); } 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; } public static String toJava(String sourceName,String sourceText) throws LuanException { LuanParser parser = new LuanParser(sourceName,sourceText); parser.addVar( "java" ); parser.addVar( "require" ); try { return parser.RequiredModuleSource(); } catch(ParseException e) { throw new LuanException( e.getFancyMessage() ); } } }