Mercurial Hosting > luan
comparison src/luan/impl/LuanCompiler.java @ 166:4eaee12f6c65
move luan/interp to impl
git-svn-id: https://luan-java.googlecode.com/svn/trunk@167 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 22 Jun 2014 04:17:38 +0000 |
parents | src/luan/interp/LuanCompiler.java@94bbc4cbc106 |
children |
comparison
equal
deleted
inserted
replaced
165:94bbc4cbc106 | 166:4eaee12f6c65 |
---|---|
1 package luan.impl; | |
2 | |
3 import luan.LuanFunction; | |
4 import luan.LuanState; | |
5 import luan.LuanException; | |
6 import luan.LuanSource; | |
7 import luan.LuanElement; | |
8 import luan.LuanTable; | |
9 import java.util.Map; | |
10 | |
11 | |
12 public final class LuanCompiler { | |
13 private LuanCompiler() {} // never | |
14 | |
15 public static LuanFunction compile(LuanState luan,LuanSource src,LuanTable env,boolean allowExpr) throws LuanException { | |
16 UpValue.Getter envGetter = env!=null ? new UpValue.ValueGetter(env) : new UpValue.EnvGetter(); | |
17 LuanParser parser = new LuanParser(src,envGetter); | |
18 for( Map.Entry<Object,Object> entry : luan.global() ) { | |
19 Object key = entry.getKey(); | |
20 if( key instanceof String ) | |
21 parser.addVar( (String)key, entry.getValue() ); | |
22 } | |
23 FnDef fnDef = parse(luan,parser,allowExpr); | |
24 if( env != null ) | |
25 return new Closure((LuanStateImpl)luan,fnDef); | |
26 final Closure c = new Closure((LuanStateImpl)luan,fnDef); | |
27 return new LuanFunction() { | |
28 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | |
29 Object rtn = c.call(luan,args); | |
30 if( rtn instanceof Object[] && ((Object[])rtn).length==0 ) | |
31 rtn = c.upValues()[0].get(); | |
32 return rtn; | |
33 } | |
34 }; | |
35 } | |
36 | |
37 private static FnDef parse(LuanState luan,LuanParser parser,boolean allowExpr) throws LuanException { | |
38 try { | |
39 if( allowExpr ) { | |
40 FnDef fnDef = parser.Expression(); | |
41 if( fnDef != null ) | |
42 return fnDef; | |
43 } | |
44 return parser.RequiredModule(); | |
45 } catch(ParseException e) { | |
46 //e.printStackTrace(); | |
47 LuanElement le = new LuanSource.CompilerElement(parser.source); | |
48 throw luan.bit(le).exception( e.getFancyMessage() ); | |
49 } | |
50 } | |
51 | |
52 public static LuanState newLuanState() { | |
53 return new LuanStateImpl(); | |
54 } | |
55 } |