Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
666:2f449ccf54d2 | 667:08966099aa6d |
---|---|
1 package luan.impl; | 1 package luan.impl; |
2 | 2 |
3 import java.util.Map; | |
4 import java.lang.reflect.InvocationTargetException; | |
3 import luan.LuanFunction; | 5 import luan.LuanFunction; |
4 import luan.LuanState; | 6 import luan.LuanState; |
5 import luan.LuanException; | 7 import luan.LuanException; |
6 import luan.LuanTable; | 8 import luan.LuanTable; |
7 import luan.modules.JavaLuan; | 9 import luan.modules.JavaLuan; |
8 import luan.modules.PackageLuan; | 10 import luan.modules.PackageLuan; |
9 import java.util.Map; | |
10 | 11 |
11 | 12 |
12 public final class LuanCompiler { | 13 public final class LuanCompiler { |
13 private LuanCompiler() {} // never | 14 private LuanCompiler() {} // never |
14 | 15 |
15 public static LuanFunction compile(LuanState luan,String sourceName,String sourceText,LuanTable env,boolean allowExpr) throws LuanException { | 16 public static LuanFunction compile(LuanState luan,String sourceName,String sourceText,LuanTable env,boolean allowExpr) throws LuanException { |
16 LuanParser parser = new LuanParser(sourceName,sourceText,env); | 17 LuanParser parser = new LuanParser(sourceName,sourceText,env); |
17 parser.addVar( "java", JavaLuan.javaFn ); | 18 parser.addVar( "java", JavaLuan.javaFn ); |
18 parser.addVar( "require", PackageLuan.requireFn ); | 19 parser.addVar( "require", PackageLuan.requireFn ); |
19 FnDef fnDef = parse(parser,allowExpr); | 20 Class fnClass = parse(parser,allowExpr); |
20 final LuanStateImpl luanImpl = (LuanStateImpl)luan; | 21 final LuanStateImpl luanImpl = (LuanStateImpl)luan; |
21 return new Closure(luanImpl,fnDef); | 22 try { |
23 return (LuanFunction)fnClass.getConstructor(LuanStateImpl.class).newInstance(luanImpl); | |
24 } catch(NoSuchMethodException e) { | |
25 throw new RuntimeException(e); | |
26 } catch(InstantiationException e) { | |
27 throw new RuntimeException(e); | |
28 } catch(IllegalAccessException e) { | |
29 throw new RuntimeException(e); | |
30 } catch(InvocationTargetException e) { | |
31 throw new RuntimeException(e); | |
32 } | |
22 } | 33 } |
23 | 34 |
24 private static FnDef parse(LuanParser parser,boolean allowExpr) throws LuanException { | 35 private static Class parse(LuanParser parser,boolean allowExpr) throws LuanException { |
25 try { | 36 try { |
26 if( allowExpr ) { | 37 if( allowExpr ) { |
27 FnDef fnDef = parser.Expression(); | 38 Class fnClass = parser.Expression(); |
28 if( fnDef != null ) | 39 if( fnClass != null ) |
29 return fnDef; | 40 return fnClass; |
30 } | 41 } |
31 return parser.RequiredModule(); | 42 return parser.RequiredModule(); |
32 } catch(ParseException e) { | 43 } catch(ParseException e) { |
33 e.printStackTrace(); | 44 e.printStackTrace(); |
34 throw new LuanException( e.getFancyMessage() ); | 45 throw new LuanException( e.getFancyMessage() ); |