Mercurial Hosting > luan
comparison src/luan/interp/LuanCompiler.java @ 128:f0a4abe58593
minor
git-svn-id: https://luan-java.googlecode.com/svn/trunk@129 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 08 Jun 2014 03:38:25 +0000 |
parents | 2428ecfed375 |
children | f99fd64291b3 |
comparison
equal
deleted
inserted
replaced
127:bcb3a09d0caf | 128:f0a4abe58593 |
---|---|
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 public static LuanFunction compileModule(LuanState luan,LuanSource src) throws LuanException { | 16 public static LuanFunction compileModule(LuanState luan,LuanSource src,boolean allowExpr) throws LuanException { |
17 UpValue.Getter envGetter = new UpValue.EnvGetter(); | 17 UpValue.Getter envGetter = 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); | |
25 final Closure c = new Closure((LuanStateImpl)luan,fnDef); | |
26 return new LuanFunction() { | |
27 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | |
28 Object rtn = c.call(luan,args); | |
29 if( rtn instanceof Object[] && ((Object[])rtn).length==0 ) | |
30 rtn = c.upValues()[0].get(); | |
31 return rtn; | |
32 } | |
33 }; | |
34 } | |
35 | |
36 public static LuanFunction compileGlobal(LuanState luan,LuanSource src,boolean allowExpr) throws LuanException { | |
37 UpValue.Getter envGetter = UpValue.globalGetter; | |
38 LuanParser parser = new LuanParser(src,envGetter); | |
39 FnDef fnDef = parse(luan,parser,allowExpr); | |
40 return new Closure((LuanStateImpl)luan,fnDef); | |
41 } | |
42 | |
43 private static FnDef parse(LuanState luan,LuanParser parser,boolean allowExpr) throws LuanException { | |
24 try { | 44 try { |
25 FnDef fnDef = parser.RequiredModule(); | 45 if( allowExpr ) { |
26 final Closure c = new Closure((LuanStateImpl)luan,fnDef); | 46 FnDef fnDef = parser.Expressions(); |
27 return new LuanFunction() { | 47 if( fnDef != null ) |
28 @Override public Object call(LuanState luan,Object[] args) throws LuanException { | 48 return fnDef; |
29 Object rtn = c.call(luan,args); | 49 } |
30 if( rtn instanceof Object[] && ((Object[])rtn).length==0 ) | 50 return parser.RequiredModule(); |
31 rtn = c.upValues()[0].get(); | |
32 return rtn; | |
33 } | |
34 }; | |
35 } catch(ParseException e) { | 51 } catch(ParseException e) { |
36 //e.printStackTrace(); | 52 //e.printStackTrace(); |
37 LuanElement le = new LuanSource.CompilerElement(src); | 53 LuanElement le = new LuanSource.CompilerElement(parser.source); |
38 throw luan.bit(le).exception( e.getFancyMessage() ); | |
39 } | |
40 } | |
41 | |
42 public static LuanFunction compileInteractive(LuanState luan,LuanSource src) throws LuanException { | |
43 UpValue.Getter envGetter = UpValue.globalGetter; | |
44 LuanParser parser = new LuanParser(src,envGetter); | |
45 try { | |
46 FnDef fnDef = parser.Expressions(); | |
47 if( fnDef == null ) | |
48 fnDef = parser.RequiredModule(); | |
49 return new Closure((LuanStateImpl)luan,fnDef); | |
50 } catch(ParseException e) { | |
51 //e.printStackTrace(); | |
52 LuanElement le = new LuanSource.CompilerElement(src); | |
53 throw luan.bit(le).exception( e.getFancyMessage() ); | 54 throw luan.bit(le).exception( e.getFancyMessage() ); |
54 } | 55 } |
55 } | 56 } |
56 | 57 |
57 public static LuanState newLuanState() { | 58 public static LuanState newLuanState() { |