Mercurial Hosting > luan
comparison src/luan/interp/LuanCompiler.java @ 150:f35c50027985
make load() more consistent
git-svn-id: https://luan-java.googlecode.com/svn/trunk@151 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 16 Jun 2014 10:11:48 +0000 |
parents | f99fd64291b3 |
children | cced1c4d3575 |
comparison
equal
deleted
inserted
replaced
149:f99fd64291b3 | 150:f35c50027985 |
---|---|
11 | 11 |
12 | 12 |
13 public final class LuanCompiler { | 13 public final class LuanCompiler { |
14 private LuanCompiler() {} // never | 14 private LuanCompiler() {} // never |
15 | 15 |
16 private static LuanFunction compile(LuanState luan,LuanSource src,boolean allowExpr) throws LuanException { | 16 public static LuanFunction compile(LuanState luan,LuanSource src,LuanTable env,boolean allowExpr) throws LuanException { |
17 UpValue.Getter envGetter = new UpValue.EnvGetter(); | 17 UpValue.Getter envGetter = env!=null ? new UpValue.ValueGetter(env) : new UpValue.EnvGetter(); |
18 LuanParser parser = new LuanParser(src,envGetter); | 18 LuanParser parser = new LuanParser(src,envGetter); |
19 for( Map.Entry<Object,Object> entry : luan.global() ) { | 19 for( Map.Entry<Object,Object> entry : luan.global() ) { |
20 Object key = entry.getKey(); | 20 Object key = entry.getKey(); |
21 if( key instanceof String ) | 21 if( key instanceof String ) |
22 parser.addVar( (String)key, entry.getValue() ); | 22 parser.addVar( (String)key, entry.getValue() ); |
23 } | 23 } |
24 FnDef fnDef = parse(luan,parser,allowExpr); | 24 FnDef fnDef = parse(luan,parser,allowExpr); |
25 if( env != null ) | |
26 return new Closure((LuanStateImpl)luan,fnDef); | |
25 final Closure c = new Closure((LuanStateImpl)luan,fnDef); | 27 final Closure c = new Closure((LuanStateImpl)luan,fnDef); |
26 return new LuanFunction() { | 28 return new LuanFunction() { |
27 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | 29 @Override public Object call(LuanState luan,Object[] args) throws LuanException { |
28 Object rtn = c.call(luan,args); | 30 Object rtn = c.call(luan,args); |
29 if( rtn instanceof Object[] && ((Object[])rtn).length==0 ) | 31 if( rtn instanceof Object[] && ((Object[])rtn).length==0 ) |
30 rtn = c.upValues()[0].get(); | 32 rtn = c.upValues()[0].get(); |
31 return rtn; | 33 return rtn; |
32 } | 34 } |
33 }; | 35 }; |
34 } | |
35 | |
36 public static LuanFunction compile(LuanState luan,LuanSource src,LuanTable env,boolean allowExpr) throws LuanException { | |
37 if( env==null ) | |
38 return compile(luan,src,allowExpr); | |
39 UpValue.Getter envGetter = new UpValue.ValueGetter(env); | |
40 LuanParser parser = new LuanParser(src,envGetter); | |
41 FnDef fnDef = parse(luan,parser,allowExpr); | |
42 return new Closure((LuanStateImpl)luan,fnDef); | |
43 } | 36 } |
44 | 37 |
45 private static FnDef parse(LuanState luan,LuanParser parser,boolean allowExpr) throws LuanException { | 38 private static FnDef parse(LuanState luan,LuanParser parser,boolean allowExpr) throws LuanException { |
46 try { | 39 try { |
47 if( allowExpr ) { | 40 if( allowExpr ) { |