comparison src/luan/interp/LuanCompiler.java @ 108:3c404a296995

make Package module more standard; return _ENV by default; add "import" statement; git-svn-id: https://luan-java.googlecode.com/svn/trunk@109 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 23 May 2014 03:21:54 +0000
parents 754e6030c029
children 2428ecfed375
comparison
equal deleted inserted replaced
107:dbf459397217 108:3c404a296995
5 import luan.LuanException; 5 import luan.LuanException;
6 import luan.LuanSource; 6 import luan.LuanSource;
7 import luan.LuanElement; 7 import luan.LuanElement;
8 import luan.LuanTable; 8 import luan.LuanTable;
9 import luan.parser.ParseException; 9 import luan.parser.ParseException;
10 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,LuanSource src,LuanTable env) throws LuanException { 16 public static LuanFunction compileModule(LuanState luan,LuanSource src) throws LuanException {
16 UpValue.Getter envGetter = env!=null ? new UpValue.ValueGetter(env) : new UpValue.EnvGetter(); 17 UpValue.Getter envGetter = new UpValue.EnvGetter();
18 LuanParser parser = new LuanParser(src,envGetter);
19 for( Map.Entry<Object,Object> entry : luan.global() ) {
20 Object key = entry.getKey();
21 if( key instanceof String )
22 parser.addVar( (String)key, entry.getValue() );
23 }
17 try { 24 try {
18 FnDef fnDef = LuanParser.parse(src,envGetter); 25 FnDef fnDef = parser.RequiredModule();
26 final Closure c = new Closure((LuanStateImpl)luan,fnDef);
27 return new LuanFunction() {
28 public Object[] call(LuanState luan,Object[] args) throws LuanException {
29 Object[] rtn = c.call(luan,args);
30 return rtn.length==0 ? new Object[]{c.upValues()[0].get()} : rtn;
31 }
32 };
33 } catch(ParseException e) {
34 //e.printStackTrace();
35 LuanElement le = new LuanSource.CompilerElement(src);
36 throw luan.bit(le).exception( e.getFancyMessage() );
37 }
38 }
39
40 public static LuanFunction compileInteractive(LuanState luan,LuanSource src) throws LuanException {
41 UpValue.Getter envGetter = UpValue.globalGetter;
42 LuanParser parser = new LuanParser(src,envGetter);
43 try {
44 FnDef fnDef = parser.Expressions();
45 if( fnDef == null )
46 fnDef = parser.RequiredModule();
19 return new Closure((LuanStateImpl)luan,fnDef); 47 return new Closure((LuanStateImpl)luan,fnDef);
20 } catch(ParseException e) { 48 } catch(ParseException e) {
21 //e.printStackTrace(); 49 //e.printStackTrace();
22 LuanElement le = new LuanSource.CompilerElement(src); 50 LuanElement le = new LuanSource.CompilerElement(src);
23 throw luan.bit(le).exception( e.getFancyMessage() ); 51 throw luan.bit(le).exception( e.getFancyMessage() );