Mercurial Hosting > luan
changeset 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 |
files | core/src/luan/impl/LuanCompiler.java core/src/luan/impl/LuanParser.java core/src/luan/modules/luan_to_java.luan |
diffstat | 3 files changed, 58 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/core/src/luan/impl/LuanCompiler.java Wed Apr 13 14:23:06 2016 -0600 +++ b/core/src/luan/impl/LuanCompiler.java Wed Apr 13 16:24:48 2016 -0600 @@ -61,4 +61,16 @@ throw new LuanException( e.getFancyMessage() ); } } + + public static String toJava(String sourceName,String sourceText) throws LuanException { + LuanParser parser = new LuanParser(sourceName,sourceText); + parser.addVar( "java" ); + parser.addVar( "require" ); + try { + return parser.RequiredModuleSource(); + } catch(ParseException e) { + throw new LuanException( e.getFancyMessage() ); + } + } + }
--- a/core/src/luan/impl/LuanParser.java Wed Apr 13 14:23:06 2016 -0600 +++ b/core/src/luan/impl/LuanParser.java Wed Apr 13 16:24:48 2016 -0600 @@ -243,43 +243,49 @@ return t; } - private Class newFnClass(int start,Stmts stmt) { - if( !stmt.hasReturn ) - stmt.add( "return LuanFunction.NOTHING;\n" ); + private Class newFnClass(Stmts stmt) { return toFnClass( stmt, frame.upValueSymbols ); } - private Expr newFnExpStr(int start,Stmts stmt) { - if( !stmt.hasReturn ) - stmt.add( "return LuanFunction.NOTHING;\n" ); + private Expr newFnExpStr(Stmts stmt) { return toFnExpStr( stmt, frame.upValueSymbols ); } Class Expression() throws ParseException { Spaces(In.NOTHING); - int start = parser.begin(); + parser.begin(); Expr expr = ExprZ(In.NOTHING); if( expr != null && parser.endOfInput() ) { top.add( "return " ); top.addAll( expr ); top.add( ";\n" ); top.hasReturn = true; - return parser.success(newFnClass(start,top)); + return parser.success(newFnClass(top)); } return parser.failure(null); } Class RequiredModule() throws ParseException { + GetRequiredModule(); + return newFnClass(top); + } + + String RequiredModuleSource() throws ParseException { + GetRequiredModule(); + return toFnString( top, frame.upValueSymbols ); + } + + void GetRequiredModule() throws ParseException { Spaces(In.NOTHING); - int start = parser.begin(); + parser.begin(); frame.isVarArg = true; top.add( "final Object[] varArgs = LuanImpl.varArgs(args,0);\n" ); Stmts block = RequiredBlock(); top.addAll( block ); top.hasReturn = block.hasReturn; - if( parser.endOfInput() ) - return parser.success(newFnClass(start,top)); - throw parser.exception(); + if( !parser.endOfInput() ) + throw parser.exception(); + parser.success(); } private Stmts RequiredBlock() throws ParseException { @@ -1041,7 +1047,7 @@ } private Expr RequiredFunction(In in) throws ParseException { - int start = parser.begin(); + parser.begin(); RequiredMatch('('); In inParens = in.parens(); Spaces(inParens); @@ -1071,7 +1077,7 @@ stmt.addAll( block ); stmt.hasReturn = block.hasReturn; RequiredKeyword("end",in); - Expr fnDef = newFnExpStr(start,stmt); + Expr fnDef = newFnExpStr(stmt); frame = frame.parent; return parser.success(fnDef); } @@ -1837,7 +1843,25 @@ private Class toFnClass(Stmts stmts,List<UpSym> upValueSymbols) { String className = "EXP" + classCounter.incrementAndGet(); - String classCode = "" + String classCode = toFnString(stmts,upValueSymbols,className); + try { +//System.out.println(parser.sourceName); +//System.out.println(classCode); + return LuanJavaCompiler.compile("luan.impl."+className,parser.sourceName,classCode); + } catch(ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + private String toFnString(Stmts stmts,List<UpSym> upValueSymbols) { + String className = "EXP" + classCounter.incrementAndGet(); + return toFnString(stmts,upValueSymbols,className); + } + + private String toFnString(Stmts stmts,List<UpSym> upValueSymbols,String className) { + if( !stmts.hasReturn ) + stmts.add( "return LuanFunction.NOTHING;\n" ); + return "" +"package luan.impl;\n" +"import luan.Luan;\n" +"import luan.LuanFunction;\n" @@ -1859,16 +1883,11 @@ +" }\n" +"}\n" ; - try { -//System.out.println(parser.sourceName); -//System.out.println(classCode); - return LuanJavaCompiler.compile("luan.impl."+className,parser.sourceName,classCode); - } catch(ClassNotFoundException e) { - throw new RuntimeException(e); - } } private static Expr toFnExpStr(Stmts stmt,List<UpSym> upValueSymbols) { + if( !stmt.hasReturn ) + stmt.add( "return LuanFunction.NOTHING;\n" ); Expr exp = new Expr(Val.SINGLE,false); exp.add( "" +"\n"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/luan/modules/luan_to_java.luan Wed Apr 13 16:24:48 2016 -0600 @@ -0,0 +1,5 @@ +java() +local LuanCompiler = require "java:luan.impl.LuanCompiler" +local Io = require "luan:Io" + +Io.stdout.write( LuanCompiler.toJava( "stdin", Io.stdin.read_text() ) )