comparison core/src/luan/impl/LuanParser.java @ 665:41f8fdbc3a0a

compile modules
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 07 Apr 2016 17:06:22 -0600
parents 71f8f5075df8
children 2f449ccf54d2
comparison
equal deleted inserted replaced
664:71f8f5075df8 665:41f8fdbc3a0a
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) );
181 return toFnDef( stmt, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); 180 return toFnDef( stmt, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) );
181 }
182
183 private ExpString newFnExpStr(int start,StmtString stmt) {
184 if( !stmt.hasReturn )
185 stmt = new StmtString( stmt.code + "return LuanFunction.NOTHING;\n" );
186 return toFnExpStr( stmt, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) );
182 } 187 }
183 188
184 FnDef Expression() throws ParseException { 189 FnDef Expression() throws ParseException {
185 Spaces(In.NOTHING); 190 Spaces(In.NOTHING);
186 int start = parser.begin(); 191 int start = parser.begin();
187 ExpString expr = ExprZ(In.NOTHING); 192 ExpString expr = ExprZ(In.NOTHING);
188 if( expr != null && parser.endOfInput() ) { 193 if( expr != null && parser.endOfInput() ) {
189 String code = "return " + expr.code + ";\n"; 194 String code = "return " + expr.code + ";\n";
190 StmtString stmt = new StmtString(code); 195 StmtString stmt = new StmtString(code);
196 stmt.hasReturn = true;
191 return parser.success(newFnDef(start,stmt)); 197 return parser.success(newFnDef(start,stmt));
192 } 198 }
193 return parser.failure(null); 199 return parser.failure(null);
194 } 200 }
195 201
351 if( exp==null ) 357 if( exp==null )
352 return parser.failure(null); 358 return parser.failure(null);
353 var = indexVar( var.exp(), exp ); 359 var = indexVar( var.exp(), exp );
354 } 360 }
355 361
356 FnDef fnDef = RequiredFunction(In.NOTHING); 362 ExpString fnDef = RequiredFunction(In.NOTHING);
357 return parser.success( var.set(new ExpString(fnDef)) ); 363 return parser.success( var.set(fnDef) );
358 } 364 }
359 365
360 private StmtString LocalFunctionStmt() throws ParseException { 366 private StmtString LocalFunctionStmt() throws ParseException {
361 parser.begin(); 367 parser.begin();
362 if( !(Keyword("local",In.NOTHING) && Keyword("function",In.NOTHING)) ) 368 if( !(Keyword("local",In.NOTHING) && Keyword("function",In.NOTHING)) )
363 return parser.failure(null); 369 return parser.failure(null);
364 String name = RequiredName(In.NOTHING); 370 String name = RequiredName(In.NOTHING);
365 addSymbol( name ); 371 addSymbol( name );
366 FnDef fnDef = RequiredFunction(In.NOTHING); 372 ExpString fnDef = RequiredFunction(In.NOTHING);
367 Settable s = new SetLocalVar(symbolsSize()-1); 373 Settable s = new SetLocalVar(symbolsSize()-1);
368 String code = new SettableString(s).code + ".set(luan," + new ExpString(fnDef).code + ");\n"; 374 String code = new SettableString(s).code + ".set(luan," + fnDef.code + ");\n";
369 return parser.success( new StmtString(code) ); 375 return parser.success( new StmtString(code) );
370 } 376 }
371 377
372 private StmtString BreakStmt() throws ParseException { 378 private StmtString BreakStmt() throws ParseException {
373 parser.begin(); 379 parser.begin();
826 return parser.success(exp1); 832 return parser.success(exp1);
827 } 833 }
828 834
829 private ExpString SingleExpr(In in) throws ParseException { 835 private ExpString SingleExpr(In in) throws ParseException {
830 parser.begin(); 836 parser.begin();
831 Expressions exp = FunctionExpr(in); 837 ExpString es = FunctionExpr(in);
832 if( exp != null )
833 return parser.success(new ExpString(exp));
834 ExpString es = VarExp(in);
835 if( es != null ) 838 if( es != null )
836 return parser.success(es); 839 return parser.success(es);
837 exp = VarArgs(in); 840 es = VarExp(in);
841 if( es != null )
842 return parser.success(es);
843 Expressions exp = VarArgs(in);
838 if( exp != null ) 844 if( exp != null )
839 return parser.success(new ExpString(exp)); 845 return parser.success(new ExpString(exp));
840 return parser.failure(null); 846 return parser.failure(null);
841 } 847 }
842 848
843 private Expr FunctionExpr(In in) throws ParseException { 849 private ExpString FunctionExpr(In in) throws ParseException {
844 if( !Keyword("function",in) ) 850 if( !Keyword("function",in) )
845 return null; 851 return null;
846 return RequiredFunction(in); 852 return RequiredFunction(in);
847 } 853 }
848 854
849 private FnDef RequiredFunction(In in) throws ParseException { 855 private ExpString RequiredFunction(In in) throws ParseException {
850 int start = parser.begin(); 856 int start = parser.begin();
851 RequiredMatch('('); 857 RequiredMatch('(');
852 In inParens = in.parens(); 858 In inParens = in.parens();
853 Spaces(inParens); 859 Spaces(inParens);
854 frame = new Frame(frame); 860 frame = new Frame(frame);
868 } 874 }
869 RequiredMatch(')'); 875 RequiredMatch(')');
870 Spaces(in); 876 Spaces(in);
871 StmtString block = RequiredBlock(); 877 StmtString block = RequiredBlock();
872 RequiredKeyword("end",in); 878 RequiredKeyword("end",in);
873 FnDef fnDef = newFnDef(start,block); 879 ExpString fnDef = newFnExpStr(start,block);
874 frame = frame.parent; 880 frame = frame.parent;
875 return parser.success(fnDef); 881 return parser.success(fnDef);
876 } 882 }
877 883
878 private VarArgs VarArgs(In in) throws ParseException { 884 private VarArgs VarArgs(In in) throws ParseException {
1585 } 1591 }
1586 } 1592 }
1587 1593
1588 private static StmtString EMPTY_STMT = new StmtString(""); 1594 private static StmtString EMPTY_STMT = new StmtString("");
1589 1595
1590 static FnDef toFnDef(StmtString stmt,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) { 1596 private static FnDef toFnDef(StmtString stmt,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) {
1591 int i = LuanImpl.addObj(upValueGetters); 1597 int i = LuanImpl.addObj(upValueGetters);
1592 String className = "EXP" + ++classCounter; 1598 String className = "EXP" + ++classCounter;
1593 String classCode = "" 1599 String classCode = ""
1594 +"package luan.impl;\n" 1600 +"package luan.impl;\n"
1595 +"import luan.Luan;\n" 1601 +"import luan.Luan;\n"
1607 +" " + stmt.code 1613 +" " + stmt.code
1608 +" }\n" 1614 +" }\n"
1609 +"}\n" 1615 +"}\n"
1610 ; 1616 ;
1611 try { 1617 try {
1618 //System.out.println(classCode);
1612 Class cls = LuanJavaCompiler.compile("luan.impl."+className,"code",classCode); 1619 Class cls = LuanJavaCompiler.compile("luan.impl."+className,"code",classCode);
1613 return (FnDef)cls.newInstance(); 1620 return (FnDef)cls.newInstance();
1614 } catch(ClassNotFoundException e) { 1621 } catch(ClassNotFoundException e) {
1615 throw new RuntimeException(e); 1622 throw new RuntimeException(e);
1616 } catch(InstantiationException e) { 1623 } catch(InstantiationException e) {
1618 } catch(IllegalAccessException e) { 1625 } catch(IllegalAccessException e) {
1619 throw new RuntimeException(e); 1626 throw new RuntimeException(e);
1620 } 1627 }
1621 } 1628 }
1622 1629
1630 private ExpString toFnExpStr(StmtString stmt,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) {
1631 int i = LuanImpl.addObj(upValueGetters);
1632 String classCode = ""
1633 +"\n"
1634 +"new FnDef("+stackSize+","+numArgs+","+isVarArg+",(UpValue.Getter[])LuanImpl.getObj("+i+")) {\n"
1635 +" @Override public Object run(LuanStateImpl luan) throws LuanException {\n"
1636 +" Object t;\n"
1637 +" " + stmt.code
1638 +" }\n"
1639 +"}.eval(luan)\n"
1640 ;
1641 return new ExpString(classCode,true,false);
1642 }
1643
1623 1644
1624 private static class SettableString { 1645 private static class SettableString {
1625 final String code; 1646 final String code;
1626 1647
1627 SettableString(Settable settable) { 1648 SettableString(Settable settable) {