Mercurial Hosting > luan
comparison core/src/luan/impl/LuanParser.java @ 667:08966099aa6d
implement Closure directly
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 08 Apr 2016 07:00:17 -0600 |
parents | 2f449ccf54d2 |
children | 7780cafca27f |
comparison
equal
deleted
inserted
replaced
666:2f449ccf54d2 | 667:08966099aa6d |
---|---|
172 if( exprs instanceof Expr ) | 172 if( exprs instanceof Expr ) |
173 return (Expr)exprs; | 173 return (Expr)exprs; |
174 return new ExpressionsExpr(exprs); | 174 return new ExpressionsExpr(exprs); |
175 } | 175 } |
176 | 176 |
177 private FnDef newFnDef(int start,StmtString stmt) { | 177 private Class newFnClass(int start,StmtString stmt) { |
178 if( !stmt.hasReturn ) | 178 if( !stmt.hasReturn ) |
179 stmt.list.add( "return LuanFunction.NOTHING;\n" ); | 179 stmt.list.add( "return LuanFunction.NOTHING;\n" ); |
180 return toFnDef( stmt, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); | 180 return toFnClass( stmt, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); |
181 } | 181 } |
182 | 182 |
183 private ExpString newFnExpStr(int start,StmtString stmt) { | 183 private ExpString newFnExpStr(int start,StmtString stmt) { |
184 if( !stmt.hasReturn ) | 184 if( !stmt.hasReturn ) |
185 stmt.list.add( "return LuanFunction.NOTHING;\n" ); | 185 stmt.list.add( "return LuanFunction.NOTHING;\n" ); |
186 return toFnExpStr( stmt, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); | 186 return toFnExpStr( stmt, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); |
187 } | 187 } |
188 | 188 |
189 FnDef Expression() throws ParseException { | 189 Class Expression() throws ParseException { |
190 Spaces(In.NOTHING); | 190 Spaces(In.NOTHING); |
191 int start = parser.begin(); | 191 int start = parser.begin(); |
192 ExpString expr = ExprZ(In.NOTHING); | 192 ExpString expr = ExprZ(In.NOTHING); |
193 if( expr != null && parser.endOfInput() ) { | 193 if( expr != null && parser.endOfInput() ) { |
194 StmtString stmt = new StmtString(); | 194 StmtString stmt = new StmtString(); |
195 stmt.list.add( "return " ); | 195 stmt.list.add( "return " ); |
196 stmt.list.addAll( expr.list ); | 196 stmt.list.addAll( expr.list ); |
197 stmt.list.add( ";\n" ); | 197 stmt.list.add( ";\n" ); |
198 stmt.hasReturn = true; | 198 stmt.hasReturn = true; |
199 return parser.success(newFnDef(start,stmt)); | 199 return parser.success(newFnClass(start,stmt)); |
200 } | 200 } |
201 return parser.failure(null); | 201 return parser.failure(null); |
202 } | 202 } |
203 | 203 |
204 FnDef RequiredModule() throws ParseException { | 204 Class RequiredModule() throws ParseException { |
205 Spaces(In.NOTHING); | 205 Spaces(In.NOTHING); |
206 int start = parser.begin(); | 206 int start = parser.begin(); |
207 frame.isVarArg = true; | 207 frame.isVarArg = true; |
208 StmtString stmt = RequiredBlock(); | 208 StmtString stmt = RequiredBlock(); |
209 if( parser.endOfInput() ) | 209 if( parser.endOfInput() ) |
210 return parser.success(newFnDef(start,stmt)); | 210 return parser.success(newFnClass(start,stmt)); |
211 throw parser.exception(); | 211 throw parser.exception(); |
212 } | 212 } |
213 | 213 |
214 private StmtString RequiredBlock() throws ParseException { | 214 private StmtString RequiredBlock() throws ParseException { |
215 StmtString stmts = new StmtString(); | 215 StmtString stmts = new StmtString(); |
1755 private static class StmtString { | 1755 private static class StmtString { |
1756 final List list = new ArrayList(); | 1756 final List list = new ArrayList(); |
1757 boolean hasReturn = false; | 1757 boolean hasReturn = false; |
1758 } | 1758 } |
1759 | 1759 |
1760 private static FnDef toFnDef(StmtString stmt,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) { | 1760 private static Class toFnClass(StmtString stmt,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) { |
1761 StringBuilder sb = new StringBuilder(); | 1761 StringBuilder sb = new StringBuilder(); |
1762 for( Object o : stmt.list ) { | 1762 for( Object o : stmt.list ) { |
1763 if( o instanceof List ) throw new RuntimeException(); | 1763 if( o instanceof List ) throw new RuntimeException(); |
1764 if( o instanceof ExpString ) throw new RuntimeException(); | 1764 if( o instanceof ExpString ) throw new RuntimeException(); |
1765 if( o instanceof StmtString ) throw new RuntimeException(); | 1765 if( o instanceof StmtString ) throw new RuntimeException(); |
1775 +"import luan.Luan;\n" | 1775 +"import luan.Luan;\n" |
1776 +"import luan.LuanFunction;\n" | 1776 +"import luan.LuanFunction;\n" |
1777 +"import luan.LuanException;\n" | 1777 +"import luan.LuanException;\n" |
1778 +"import luan.modules.PackageLuan;\n" | 1778 +"import luan.modules.PackageLuan;\n" |
1779 +"\n" | 1779 +"\n" |
1780 +"public class " + className +" extends FnDef {\n" | 1780 +"public class " + className +" extends Closure {\n" |
1781 +" public "+className+"() {\n" | 1781 +" public "+className+"(LuanStateImpl luan) throws LuanException {\n" |
1782 +" super("+stackSize+","+numArgs+","+isVarArg+",(UpValue.Getter[])LuanImpl.getObj("+i+"));\n" | 1782 +" super(luan,"+stackSize+","+numArgs+","+isVarArg+",(UpValue.Getter[])LuanImpl.getObj("+i+"));\n" |
1783 +" }\n" | 1783 +" }\n" |
1784 +"\n" | 1784 +"\n" |
1785 +" @Override public Object run(LuanStateImpl luan) throws LuanException {\n" | 1785 +" @Override public Object run(LuanStateImpl luan) throws LuanException {\n" |
1786 +" Object t;\n" | 1786 +" Object t;\n" |
1787 +" " + code | 1787 +" " + code |
1788 +" }\n" | 1788 +" }\n" |
1789 +"}\n" | 1789 +"}\n" |
1790 ; | 1790 ; |
1791 try { | 1791 try { |
1792 //System.out.println(classCode); | 1792 //System.out.println(classCode); |
1793 Class cls = LuanJavaCompiler.compile("luan.impl."+className,"code",classCode); | 1793 return LuanJavaCompiler.compile("luan.impl."+className,"code",classCode); |
1794 return (FnDef)cls.newInstance(); | |
1795 } catch(ClassNotFoundException e) { | 1794 } catch(ClassNotFoundException e) { |
1796 throw new RuntimeException(e); | |
1797 } catch(InstantiationException e) { | |
1798 throw new RuntimeException(e); | |
1799 } catch(IllegalAccessException e) { | |
1800 throw new RuntimeException(e); | 1795 throw new RuntimeException(e); |
1801 } | 1796 } |
1802 } | 1797 } |
1803 | 1798 |
1804 private ExpString toFnExpStr(StmtString stmt,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) { | 1799 private ExpString toFnExpStr(StmtString stmt,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) { |
1805 int i = LuanImpl.addObj(upValueGetters); | 1800 int i = LuanImpl.addObj(upValueGetters); |
1806 ExpString exp = new ExpString(true,false); | 1801 ExpString exp = new ExpString(true,false); |
1807 exp.list.add( "" | 1802 exp.list.add( "" |
1808 +"\n" | 1803 +"\n" |
1809 +"new FnDef("+stackSize+","+numArgs+","+isVarArg+",(UpValue.Getter[])LuanImpl.getObj("+i+")) {\n" | 1804 +"new Closure(luan,"+stackSize+","+numArgs+","+isVarArg+",(UpValue.Getter[])LuanImpl.getObj("+i+")) {\n" |
1810 +" @Override public Object run(LuanStateImpl luan) throws LuanException {\n" | 1805 +" @Override public Object run(LuanStateImpl luan) throws LuanException {\n" |
1811 +" Object t;\n" | 1806 +" Object t;\n" |
1812 +" " | 1807 +" " |
1813 ); | 1808 ); |
1814 exp.list.addAll( stmt.list ); | 1809 exp.list.addAll( stmt.list ); |
1815 exp.list.add( "" | 1810 exp.list.add( "" |
1816 +" }\n" | 1811 +" }\n" |
1817 +"}.eval(luan)\n" | 1812 +"}\n" |
1818 ); | 1813 ); |
1819 return exp; | 1814 return exp; |
1820 } | 1815 } |
1821 | 1816 |
1822 | 1817 |