Mercurial Hosting > luan
view core/src/luan/impl/LuanCompiler.java @ 667:08966099aa6d
implement Closure directly
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 08 Apr 2016 07:00:17 -0600 |
parents | 37f0cf43f191 |
children | 58ebfec6178b |
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.modules.JavaLuan; import luan.modules.PackageLuan; public final class LuanCompiler { private LuanCompiler() {} // never public static LuanFunction compile(LuanState luan,String sourceName,String sourceText,LuanTable env,boolean allowExpr) throws LuanException { LuanParser parser = new LuanParser(sourceName,sourceText,env); parser.addVar( "java", JavaLuan.javaFn ); parser.addVar( "require", PackageLuan.requireFn ); Class fnClass = parse(parser,allowExpr); final LuanStateImpl luanImpl = (LuanStateImpl)luan; try { return (LuanFunction)fnClass.getConstructor(LuanStateImpl.class).newInstance(luanImpl); } 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); } } private static Class parse(LuanParser parser,boolean allowExpr) throws LuanException { try { if( allowExpr ) { Class fnClass = parser.Expression(); if( fnClass != null ) return fnClass; } return parser.RequiredModule(); } catch(ParseException e) { e.printStackTrace(); throw new LuanException( e.getFancyMessage() ); } } public static LuanState newLuanState() { return new LuanStateImpl(); } }