comparison core/src/luan/impl/LuanParser.java @ 664:71f8f5075df8

compile FnDef
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 07 Apr 2016 15:11:52 -0600
parents b438a47196bc
children 41f8fdbc3a0a
comparison
equal deleted inserted replaced
663:b438a47196bc 664:71f8f5075df8
175 } 175 }
176 176
177 private FnDef newFnDef(int start,StmtString stmt) { 177 private FnDef newFnDef(int start,StmtString stmt) {
178 if( !stmt.hasReturn ) 178 if( !stmt.hasReturn )
179 stmt = new StmtString( stmt.code + "return LuanFunction.NOTHING;\n" ); 179 stmt = new StmtString( stmt.code + "return LuanFunction.NOTHING;\n" );
180 return new FnDef( toExpressions(stmt), frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); 180 // return new FnDef( toExpressions(stmt), frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) );
181 return toFnDef( stmt, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) );
181 } 182 }
182 183
183 FnDef Expression() throws ParseException { 184 FnDef Expression() throws ParseException {
184 Spaces(In.NOTHING); 185 Spaces(In.NOTHING);
185 int start = parser.begin(); 186 int start = parser.begin();
276 } 277 }
277 278
278 private ExpString indexExpStr(ExpString exp1,ExpString exp2) { 279 private ExpString indexExpStr(ExpString exp1,ExpString exp2) {
279 String code = "luan.index(" + exp1.expr().code + "," + exp2.expr().code + ")"; 280 String code = "luan.index(" + exp1.expr().code + "," + exp2.expr().code + ")";
280 return new ExpString(code,true,false); 281 return new ExpString(code,true,false);
281 }
282
283 private ExpString constExpStr(String s) {
284 s = s
285 .replace("\\","\\\\")
286 .replace("\"","\\\"")
287 .replace("\n","\\n")
288 .replace("\r","\\r")
289 .replace("\t","\\t")
290 .replace("\b","\\b")
291 ;
292 return new ExpString( "\""+s+"\"", true,false);
293 } 282 }
294 283
295 private ExpString callExpStr(ExpString fn,ExpString args) { 284 private ExpString callExpStr(ExpString fn,ExpString args) {
296 String code = "Luan.checkFunction(" + fn.expr().code + ").call(luan,Luan.array(" + args.code + "))"; 285 String code = "Luan.checkFunction(" + fn.expr().code + ").call(luan,Luan.array(" + args.code + "))";
297 return new ExpString(code,false,true); 286 return new ExpString(code,false,true);
1274 if( s != null ) 1263 if( s != null )
1275 return parser.success(constExpStr(s)); 1264 return parser.success(constExpStr(s));
1276 return parser.failure(null); 1265 return parser.failure(null);
1277 } 1266 }
1278 1267
1268 private ExpString constExpStr(String s) {
1269 s = s
1270 .replace("\\","\\\\")
1271 .replace("\"","\\\"")
1272 .replace("\n","\\n")
1273 .replace("\r","\\r")
1274 .replace("\t","\\t")
1275 .replace("\b","\\b")
1276 ;
1277 return new ExpString( "\""+s+"\"", true,false);
1278 }
1279
1279 private boolean NilLiteral(In in) throws ParseException { 1280 private boolean NilLiteral(In in) throws ParseException {
1280 return Keyword("nil",in); 1281 return Keyword("nil",in);
1281 } 1282 }
1282 1283
1283 private Boolean BooleanLiteral(In in) throws ParseException { 1284 private Boolean BooleanLiteral(In in) throws ParseException {
1530 final boolean isExpr; 1531 final boolean isExpr;
1531 final boolean isStmt; 1532 final boolean isStmt;
1532 1533
1533 ExpString(Expressions exp) { 1534 ExpString(Expressions exp) {
1534 if( exp==null ) throw new NullPointerException(); 1535 if( exp==null ) throw new NullPointerException();
1535 int i = LuanImpl.addExpressions(exp); 1536 int i = LuanImpl.addObj(exp);
1536 code = "LuanImpl.getExpressions(" + i + ").eval(luan)"; 1537 code = "((Expressions)LuanImpl.getObj(" + i + ")).eval(luan)";
1537 isExpr = exp instanceof Expr; 1538 isExpr = exp instanceof Expr;
1538 isStmt = exp instanceof StmtExp; 1539 isStmt = exp instanceof StmtExp;
1539 } 1540 }
1540 1541
1541 ExpString(String code,boolean isExpr,boolean isStmt) { 1542 ExpString(String code,boolean isExpr,boolean isStmt) {
1575 } 1576 }
1576 1577
1577 private static class StmtString { 1578 private static class StmtString {
1578 final String code; 1579 final String code;
1579 boolean hasReturn = false; 1580 boolean hasReturn = false;
1580 /* 1581
1581 StmtString(Stmt stmt) {
1582 if( stmt==null ) throw new NullPointerException();
1583 int i = LuanImpl.addStmt(stmt);
1584 code = "LuanImpl.getStmt(" + i + ").eval(luan);\n";
1585 }
1586 */
1587 StmtString(String code) { 1582 StmtString(String code) {
1588 this.code = code; 1583 this.code = code;
1589 if( code.contains("LuanParser") ) throw new RuntimeException("\n"+code); 1584 if( code.contains("LuanParser") ) throw new RuntimeException("\n"+code);
1590 } 1585 }
1591
1592 Stmt toStmt() {
1593 String className = "EXP" + ++classCounter;
1594 String classCode = ""
1595 +"package luan.impl;\n"
1596 +"import luan.Luan;\n"
1597 +"import luan.LuanFunction;\n"
1598 +"import luan.LuanException;\n"
1599 +"import luan.modules.PackageLuan;\n"
1600 +"\n"
1601 +"public class " + className +" implements Stmt {\n"
1602 +" @Override public void eval(LuanStateImpl luan) throws LuanException {\n"
1603 +" Object t;\n"
1604 +" " + code
1605 +" }\n"
1606 +"}\n"
1607 ;
1608 //System.out.println(code);
1609 try {
1610 Class cls = LuanJavaCompiler.compile("luan.impl."+className,"code",classCode);
1611 return (Stmt)cls.newInstance();
1612 } catch(ClassNotFoundException e) {
1613 throw new RuntimeException(e);
1614 } catch(InstantiationException e) {
1615 throw new RuntimeException(e);
1616 } catch(IllegalAccessException e) {
1617 throw new RuntimeException(e);
1618 }
1619 }
1620 } 1586 }
1621 1587
1622 private static StmtString EMPTY_STMT = new StmtString(""); 1588 private static StmtString EMPTY_STMT = new StmtString("");
1623 1589
1624 private static Stmt stmt(StmtString stmtStr) { 1590 static FnDef toFnDef(StmtString stmt,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) {
1625 return stmtStr==null ? null : stmtStr.toStmt(); 1591 int i = LuanImpl.addObj(upValueGetters);
1626 }
1627
1628
1629 static Expressions toExpressions(StmtString stmt) {
1630 String className = "EXP" + ++classCounter; 1592 String className = "EXP" + ++classCounter;
1631 String classCode = "" 1593 String classCode = ""
1632 +"package luan.impl;\n" 1594 +"package luan.impl;\n"
1633 +"import luan.Luan;\n" 1595 +"import luan.Luan;\n"
1634 +"import luan.LuanFunction;\n" 1596 +"import luan.LuanFunction;\n"
1635 +"import luan.LuanException;\n" 1597 +"import luan.LuanException;\n"
1636 +"import luan.modules.PackageLuan;\n" 1598 +"import luan.modules.PackageLuan;\n"
1637 +"\n" 1599 +"\n"
1638 +"public class " + className +" implements Expressions {\n" 1600 +"public class " + className +" extends FnDef {\n"
1639 +" @Override public Object eval(LuanStateImpl luan) throws LuanException {\n" 1601 +" public "+className+"() {\n"
1602 +" super("+stackSize+","+numArgs+","+isVarArg+",(UpValue.Getter[])LuanImpl.getObj("+i+"));\n"
1603 +" }\n"
1604 +"\n"
1605 +" @Override public Object run(LuanStateImpl luan) throws LuanException {\n"
1640 +" Object t;\n" 1606 +" Object t;\n"
1641 +" " + stmt.code 1607 +" " + stmt.code
1642 +" }\n" 1608 +" }\n"
1643 +"}\n" 1609 +"}\n"
1644 ; 1610 ;
1645 try { 1611 try {
1646 Class cls = LuanJavaCompiler.compile("luan.impl."+className,"code",classCode); 1612 Class cls = LuanJavaCompiler.compile("luan.impl."+className,"code",classCode);
1647 return (Expressions)cls.newInstance(); 1613 return (FnDef)cls.newInstance();
1648 } catch(ClassNotFoundException e) { 1614 } catch(ClassNotFoundException e) {
1649 throw new RuntimeException(e); 1615 throw new RuntimeException(e);
1650 } catch(InstantiationException e) { 1616 } catch(InstantiationException e) {
1651 throw new RuntimeException(e); 1617 throw new RuntimeException(e);
1652 } catch(IllegalAccessException e) { 1618 } catch(IllegalAccessException e) {
1658 private static class SettableString { 1624 private static class SettableString {
1659 final String code; 1625 final String code;
1660 1626
1661 SettableString(Settable settable) { 1627 SettableString(Settable settable) {
1662 if( settable==null ) throw new NullPointerException(); 1628 if( settable==null ) throw new NullPointerException();
1663 int i = LuanImpl.addSettable(settable); 1629 int i = LuanImpl.addObj(settable);
1664 code = "LuanImpl.getSettable(" + i + ")"; 1630 code = "((Settable)LuanImpl.getObj(" + i + "))";
1665 } 1631 }
1666 1632
1667 SettableString(String code) { 1633 SettableString(String code) {
1668 this.code = code; 1634 this.code = code;
1669 } 1635 }