Mercurial Hosting > luan
changeset 657:8081713bf7d9
compile literals
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 05 Apr 2016 17:37:54 -0600 |
parents | 471be4752b9e |
children | e038905512d3 |
files | core/src/luan/impl/LuanParser.java |
diffstat | 1 files changed, 34 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/core/src/luan/impl/LuanParser.java Tue Apr 05 16:38:51 2016 -0600 +++ b/core/src/luan/impl/LuanParser.java Tue Apr 05 17:37:54 2016 -0600 @@ -267,8 +267,16 @@ return new ExpString(code,true,false); } - private ExpString constExpStr(Object obj) { - return new ExpString(new ConstExpr(obj)); + private ExpString constExpStr(String s) { + s = s + .replace("\\","\\\\") + .replace("\"","\\\"") + .replace("\n","\\n") + .replace("\r","\\r") + .replace("\t","\\t") + .replace("\b","\\b") + ; + return new ExpString( "\""+s+"\"", true,false); } private ExpString callExpStr(ExpString fn,ExpString args) { @@ -280,12 +288,12 @@ ExpString exprs = TemplateExpressions(In.NOTHING); if( exprs == null ) return null; - ExpString requireCall = callExpStr( constExpStr(PackageLuan.requireFn), constExpStr("luan:Io") ); + String code = "PackageLuan.require(luan,\"luan:Io\")"; + ExpString requireCall = new ExpString(code,true,false); ExpString stdoutExp = indexExpStr( requireCall.expr(), constExpStr("stdout") ); ExpString writeExp = indexExpStr( stdoutExp, constExpStr("write") ); ExpString writeCall = callExpStr( writeExp, exprs ); - String code = writeCall.code + ";\n"; - return new StmtString(code); + return new StmtString( writeCall.code + ";\n" ); } private ExpString TemplateExpressions(In in) throws ParseException { @@ -796,10 +804,9 @@ private ExpString PowExpr(In in) throws ParseException { parser.begin(); - Expressions exp = SingleExpr(in); - if( exp==null ) + ExpString exp1 = SingleExpr(in); + if( exp1==null ) return parser.failure(null); - ExpString exp1 = new ExpString(exp); if( parser.match('^') ) { Spaces(in); ExpString exp2 = required(PowExpr(in)); @@ -809,18 +816,17 @@ return parser.success(exp1); } - private Expressions SingleExpr(In in) throws ParseException { + private ExpString SingleExpr(In in) throws ParseException { parser.begin(); - Expressions exp; - exp = FunctionExpr(in); + Expressions exp = FunctionExpr(in); if( exp != null ) - return parser.success(exp); - exp = exp(VarExp(in)); - if( exp != null ) - return parser.success(exp); + return parser.success(new ExpString(exp)); + ExpString es = VarExp(in); + if( es != null ) + return parser.success(es); exp = VarArgs(in); if( exp != null ) - return parser.success(exp); + return parser.success(new ExpString(exp)); return parser.failure(null); } @@ -1069,7 +1075,7 @@ } String s = StringLiteral(in); if( s != null ) { - builder.add( new ExpString(new ConstExpr(s)) ); + builder.add( constExpStr(s) ); return parser.success(); } /* @@ -1208,13 +1214,17 @@ private ExpString Literal(In in) throws ParseException { parser.begin(); if( NilLiteral(in) ) - return parser.success(constExpStr(null)); + return parser.success(new ExpString("null",true,false)); Boolean b = BooleanLiteral(in); if( b != null ) - return parser.success(constExpStr(b)); + return parser.success(new ExpString(b.toString(),true,false)); Number n = NumberLiteral(in); - if( n != null ) - return parser.success(constExpStr(n)); + if( n != null ) { + String s = n.toString(); + if( n instanceof Long ) + s += "L"; + return parser.success(new ExpString(s,true,false)); + } String s = StringLiteral(in); if( s != null ) return parser.success(constExpStr(s)); @@ -1501,6 +1511,7 @@ +"import luan.Luan;\n" +"import luan.LuanFunction;\n" +"import luan.LuanException;\n" + +"import luan.modules.PackageLuan;\n" +"\n" +"public class " + className +" implements " + superClass + " {\n" +" @Override public Object eval(LuanStateImpl luan) throws LuanException {\n" @@ -1551,15 +1562,7 @@ } } } -/* - private static List<Expressions> expList(List<ExpString> esList) { - List<Expressions> list = new ArrayList<Expressions>(); - for( ExpString es : esList ) { - list.add(exp(es)); - } - return list; - } -*/ + private static class StmtString { final String code; @@ -1581,6 +1584,7 @@ +"import luan.Luan;\n" +"import luan.LuanFunction;\n" +"import luan.LuanException;\n" + +"import luan.modules.PackageLuan;\n" +"\n" +"public class " + className +" implements Stmt {\n" +" @Override public void eval(LuanStateImpl luan) throws LuanException {\n"