Mercurial Hosting > luan
comparison core/src/luan/impl/LuanParser.java @ 678:49f3d290bebd
add luan_to_java
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 13 Apr 2016 16:24:48 -0600 |
parents | 5c85d38659db |
children | 43522473599d |
comparison
equal
deleted
inserted
replaced
677:5c85d38659db | 678:49f3d290bebd |
---|---|
241 if( t==null ) | 241 if( t==null ) |
242 throw parser.exception(msg); | 242 throw parser.exception(msg); |
243 return t; | 243 return t; |
244 } | 244 } |
245 | 245 |
246 private Class newFnClass(int start,Stmts stmt) { | 246 private Class newFnClass(Stmts stmt) { |
247 if( !stmt.hasReturn ) | |
248 stmt.add( "return LuanFunction.NOTHING;\n" ); | |
249 return toFnClass( stmt, frame.upValueSymbols ); | 247 return toFnClass( stmt, frame.upValueSymbols ); |
250 } | 248 } |
251 | 249 |
252 private Expr newFnExpStr(int start,Stmts stmt) { | 250 private Expr newFnExpStr(Stmts stmt) { |
253 if( !stmt.hasReturn ) | |
254 stmt.add( "return LuanFunction.NOTHING;\n" ); | |
255 return toFnExpStr( stmt, frame.upValueSymbols ); | 251 return toFnExpStr( stmt, frame.upValueSymbols ); |
256 } | 252 } |
257 | 253 |
258 Class Expression() throws ParseException { | 254 Class Expression() throws ParseException { |
259 Spaces(In.NOTHING); | 255 Spaces(In.NOTHING); |
260 int start = parser.begin(); | 256 parser.begin(); |
261 Expr expr = ExprZ(In.NOTHING); | 257 Expr expr = ExprZ(In.NOTHING); |
262 if( expr != null && parser.endOfInput() ) { | 258 if( expr != null && parser.endOfInput() ) { |
263 top.add( "return " ); | 259 top.add( "return " ); |
264 top.addAll( expr ); | 260 top.addAll( expr ); |
265 top.add( ";\n" ); | 261 top.add( ";\n" ); |
266 top.hasReturn = true; | 262 top.hasReturn = true; |
267 return parser.success(newFnClass(start,top)); | 263 return parser.success(newFnClass(top)); |
268 } | 264 } |
269 return parser.failure(null); | 265 return parser.failure(null); |
270 } | 266 } |
271 | 267 |
272 Class RequiredModule() throws ParseException { | 268 Class RequiredModule() throws ParseException { |
269 GetRequiredModule(); | |
270 return newFnClass(top); | |
271 } | |
272 | |
273 String RequiredModuleSource() throws ParseException { | |
274 GetRequiredModule(); | |
275 return toFnString( top, frame.upValueSymbols ); | |
276 } | |
277 | |
278 void GetRequiredModule() throws ParseException { | |
273 Spaces(In.NOTHING); | 279 Spaces(In.NOTHING); |
274 int start = parser.begin(); | 280 parser.begin(); |
275 frame.isVarArg = true; | 281 frame.isVarArg = true; |
276 top.add( "final Object[] varArgs = LuanImpl.varArgs(args,0);\n" ); | 282 top.add( "final Object[] varArgs = LuanImpl.varArgs(args,0);\n" ); |
277 Stmts block = RequiredBlock(); | 283 Stmts block = RequiredBlock(); |
278 top.addAll( block ); | 284 top.addAll( block ); |
279 top.hasReturn = block.hasReturn; | 285 top.hasReturn = block.hasReturn; |
280 if( parser.endOfInput() ) | 286 if( !parser.endOfInput() ) |
281 return parser.success(newFnClass(start,top)); | 287 throw parser.exception(); |
282 throw parser.exception(); | 288 parser.success(); |
283 } | 289 } |
284 | 290 |
285 private Stmts RequiredBlock() throws ParseException { | 291 private Stmts RequiredBlock() throws ParseException { |
286 Stmts stmts = new Stmts(); | 292 Stmts stmts = new Stmts(); |
287 int stackStart = symbolsSize(); | 293 int stackStart = symbolsSize(); |
1039 return null; | 1045 return null; |
1040 return RequiredFunction(in); | 1046 return RequiredFunction(in); |
1041 } | 1047 } |
1042 | 1048 |
1043 private Expr RequiredFunction(In in) throws ParseException { | 1049 private Expr RequiredFunction(In in) throws ParseException { |
1044 int start = parser.begin(); | 1050 parser.begin(); |
1045 RequiredMatch('('); | 1051 RequiredMatch('('); |
1046 In inParens = in.parens(); | 1052 In inParens = in.parens(); |
1047 Spaces(inParens); | 1053 Spaces(inParens); |
1048 frame = new Frame(frame); | 1054 frame = new Frame(frame); |
1049 Stmts stmt = new Stmts(); | 1055 Stmts stmt = new Stmts(); |
1069 Spaces(in); | 1075 Spaces(in); |
1070 Stmts block = RequiredBlock(); | 1076 Stmts block = RequiredBlock(); |
1071 stmt.addAll( block ); | 1077 stmt.addAll( block ); |
1072 stmt.hasReturn = block.hasReturn; | 1078 stmt.hasReturn = block.hasReturn; |
1073 RequiredKeyword("end",in); | 1079 RequiredKeyword("end",in); |
1074 Expr fnDef = newFnExpStr(start,stmt); | 1080 Expr fnDef = newFnExpStr(stmt); |
1075 frame = frame.parent; | 1081 frame = frame.parent; |
1076 return parser.success(fnDef); | 1082 return parser.success(fnDef); |
1077 } | 1083 } |
1078 | 1084 |
1079 private Expr VarArgs(In in) throws ParseException { | 1085 private Expr VarArgs(In in) throws ParseException { |
1835 boolean hasReturn = false; | 1841 boolean hasReturn = false; |
1836 } | 1842 } |
1837 | 1843 |
1838 private Class toFnClass(Stmts stmts,List<UpSym> upValueSymbols) { | 1844 private Class toFnClass(Stmts stmts,List<UpSym> upValueSymbols) { |
1839 String className = "EXP" + classCounter.incrementAndGet(); | 1845 String className = "EXP" + classCounter.incrementAndGet(); |
1840 String classCode = "" | 1846 String classCode = toFnString(stmts,upValueSymbols,className); |
1847 try { | |
1848 //System.out.println(parser.sourceName); | |
1849 //System.out.println(classCode); | |
1850 return LuanJavaCompiler.compile("luan.impl."+className,parser.sourceName,classCode); | |
1851 } catch(ClassNotFoundException e) { | |
1852 throw new RuntimeException(e); | |
1853 } | |
1854 } | |
1855 | |
1856 private String toFnString(Stmts stmts,List<UpSym> upValueSymbols) { | |
1857 String className = "EXP" + classCounter.incrementAndGet(); | |
1858 return toFnString(stmts,upValueSymbols,className); | |
1859 } | |
1860 | |
1861 private String toFnString(Stmts stmts,List<UpSym> upValueSymbols,String className) { | |
1862 if( !stmts.hasReturn ) | |
1863 stmts.add( "return LuanFunction.NOTHING;\n" ); | |
1864 return "" | |
1841 +"package luan.impl;\n" | 1865 +"package luan.impl;\n" |
1842 +"import luan.Luan;\n" | 1866 +"import luan.Luan;\n" |
1843 +"import luan.LuanFunction;\n" | 1867 +"import luan.LuanFunction;\n" |
1844 +"import luan.LuanState;\n" | 1868 +"import luan.LuanState;\n" |
1845 +"import luan.LuanJava;\n" | 1869 +"import luan.LuanJava;\n" |
1857 +" Object[] a;\n" | 1881 +" Object[] a;\n" |
1858 +" " + stmts | 1882 +" " + stmts |
1859 +" }\n" | 1883 +" }\n" |
1860 +"}\n" | 1884 +"}\n" |
1861 ; | 1885 ; |
1862 try { | |
1863 //System.out.println(parser.sourceName); | |
1864 //System.out.println(classCode); | |
1865 return LuanJavaCompiler.compile("luan.impl."+className,parser.sourceName,classCode); | |
1866 } catch(ClassNotFoundException e) { | |
1867 throw new RuntimeException(e); | |
1868 } | |
1869 } | 1886 } |
1870 | 1887 |
1871 private static Expr toFnExpStr(Stmts stmt,List<UpSym> upValueSymbols) { | 1888 private static Expr toFnExpStr(Stmts stmt,List<UpSym> upValueSymbols) { |
1889 if( !stmt.hasReturn ) | |
1890 stmt.add( "return LuanFunction.NOTHING;\n" ); | |
1872 Expr exp = new Expr(Val.SINGLE,false); | 1891 Expr exp = new Expr(Val.SINGLE,false); |
1873 exp.add( "" | 1892 exp.add( "" |
1874 +"\n" | 1893 +"\n" |
1875 +"new Closure("+upValueSymbols.size()+",java) {\n" | 1894 +"new Closure("+upValueSymbols.size()+",java) {\n" |
1876 +"{\n" | 1895 +"{\n" |