Mercurial Hosting > luan
changeset 681:f1c935be546d
improve stack trace
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Apr 2016 20:10:38 -0600 |
parents | ecd436959855 |
children | 0c334975d526 |
files | core/src/luan/impl/Closure.java core/src/luan/impl/LuanParser.java |
diffstat | 2 files changed, 38 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
diff -r ecd436959855 -r f1c935be546d core/src/luan/impl/Closure.java --- a/core/src/luan/impl/Closure.java Thu Apr 14 16:38:30 2016 -0600 +++ b/core/src/luan/impl/Closure.java Thu Apr 14 20:10:38 2016 -0600 @@ -37,6 +37,8 @@ luan.java = java; try { return doCall(luan,args); + } catch(StackOverflowError e) { + throw new LuanException( "stack overflow" ); } finally { luan.java = old; }
diff -r ecd436959855 -r f1c935be546d core/src/luan/impl/LuanParser.java --- a/core/src/luan/impl/LuanParser.java Thu Apr 14 16:38:30 2016 -0600 +++ b/core/src/luan/impl/LuanParser.java Thu Apr 14 20:10:38 2016 -0600 @@ -245,8 +245,8 @@ return toFnClass( stmt, frame.upValueSymbols ); } - private Expr newFnExpStr(Stmts stmt) { - return toFnExpStr( stmt, frame.upValueSymbols ); + private Expr newFnExp(Stmts stmt,String name) { + return toFnExp( stmt, frame.upValueSymbols, name ); } Class Expression() throws ParseException { @@ -436,16 +436,18 @@ return parser.failure(null); parser.currentIndex(); - Var var = nameVar(RequiredName()); + String name = RequiredName(); + Var var = nameVar(name); while( parser.match( '.' ) ) { Spaces(); - Expr exp = NameExpr(); - if( exp==null ) +// Expr exp = NameExpr(); + name = Name(); + if( name==null ) return parser.failure(null); - var = indexVar( var.exp(), exp ); + var = indexVar( var.exp(), constExpStr(name) ); } - Expr fnDef = RequiredFunction(In.NOTHING); + Expr fnDef = RequiredFunction(name); return parser.success( var.set(fnDef) ); } @@ -456,7 +458,7 @@ Stmts stmt = new Stmts(); String name = RequiredName(); stmt.addAll( addSymbol(name,null) ); - Expr fnDef = RequiredFunction(In.NOTHING); + Expr fnDef = RequiredFunction(name); stmt.addAll( nameVar(name).set(fnDef) ); return parser.success( stmt ); } @@ -483,7 +485,7 @@ if( !Keyword("in") ) return parser.failure(null); Expr expr = RequiredExpr(In.NOTHING).single(); - RequiredKeyword("do",In.NOTHING); + RequiredKeyword("do"); String fnVar = "fn"+ ++forCounter; Expr fnExp = new Expr(null,false); @@ -500,7 +502,7 @@ stmt.addAll( nameVar(names.get(0)).exp() ); stmt.add( "==null ) break; " ); Stmts loop = RequiredLoopBlock(); - RequiredKeyword("end",In.NOTHING); + RequiredKeyword("end"); stmt.addAll( loop ); stmt.add( "} " ); popSymbols( symbolsSize() - stackStart ); @@ -512,7 +514,7 @@ if( !Keyword("do") ) return parser.failure(null); Stmts stmt = RequiredBlock(); - RequiredKeyword("end",In.NOTHING); + RequiredKeyword("end"); return parser.success(stmt); } @@ -579,9 +581,9 @@ if( !Keyword("while") ) return parser.failure(null); Expr cnd = RequiredExpr(In.NOTHING).single(); - RequiredKeyword("do",In.NOTHING); + RequiredKeyword("do"); Stmts loop = RequiredLoopBlock(); - RequiredKeyword("end",In.NOTHING); + RequiredKeyword("end"); Stmts stmt = new Stmts(); stmt.add( "while( Luan.checkBoolean(" ); stmt.addAll( cnd ); @@ -596,7 +598,7 @@ if( !Keyword("repeat") ) return parser.failure(null); Stmts loop =RequiredLoopBlock(); - RequiredKeyword("until",In.NOTHING); + RequiredKeyword("until"); Expr cnd = RequiredExpr(In.NOTHING).single(); Stmts stmt = new Stmts(); stmt.add( "do { " ); @@ -622,7 +624,7 @@ Expr cnd; Stmts block; cnd = RequiredExpr(In.NOTHING).single(); - RequiredKeyword("then",In.NOTHING); + RequiredKeyword("then"); block = RequiredBlock(); stmt.add( "if( Luan.checkBoolean(" ); stmt.addAll( cnd ); @@ -630,7 +632,7 @@ stmt.addAll( block ); while( Keyword("elseif") ) { cnd = RequiredExpr(In.NOTHING).single(); - RequiredKeyword("then",In.NOTHING); + RequiredKeyword("then"); block = RequiredBlock(); stmt.add( "} else if( Luan.checkBoolean(" ); stmt.addAll( cnd ); @@ -642,7 +644,7 @@ stmt.add( "} else { " ); stmt.addAll( block ); } - RequiredKeyword("end",In.NOTHING); + RequiredKeyword("end"); stmt.add( "} " ); return parser.success( stmt ); } @@ -1029,7 +1031,7 @@ private Expr SingleExpr(In in) throws ParseException { parser.begin(); - Expr exp = FunctionExpr(in); + Expr exp = FunctionExpr(); if( exp != null ) return parser.success(exp); exp = VarExp(in); @@ -1041,13 +1043,13 @@ return parser.failure(null); } - private Expr FunctionExpr(In in) throws ParseException { + private Expr FunctionExpr() throws ParseException { if( !Keyword("function") ) return null; - return RequiredFunction(in); + return RequiredFunction(null); } - private Expr RequiredFunction(In in) throws ParseException { + private Expr RequiredFunction(String name) throws ParseException { parser.begin(); RequiredMatch('('); Spaces(); @@ -1076,8 +1078,8 @@ Stmts block = RequiredBlock(); stmt.addAll( block ); stmt.hasReturn = block.hasReturn; - Expr fnDef = newFnExpStr(stmt); - RequiredKeyword("end",in); + Expr fnDef = newFnExp(stmt,name); + RequiredKeyword("end"); frame = frame.parent; return parser.success(fnDef); } @@ -1409,7 +1411,7 @@ throw parser.exception("'"+s+"' expected"); } - private void RequiredKeyword(String keyword,In in) throws ParseException { + private void RequiredKeyword(String keyword) throws ParseException { if( !Keyword(keyword) ) throw parser.exception("'"+keyword+"' expected"); } @@ -1905,7 +1907,7 @@ ; } - private Expr toFnExpStr(Stmts stmt,List<UpSym> upValueSymbols) { + private Expr toFnExp(Stmts stmt,List<UpSym> upValueSymbols,String name) { stmt.addNewLines(); if( !stmt.hasReturn ) stmt.add( "return LuanFunction.NOTHING; " ); @@ -1916,6 +1918,15 @@ + init(upValueSymbols) +"} " +"@Override public Object doCall(LuanState luan,Object[] args) throws LuanException { " + ); + if( name != null ) { + exp.add( "" + +"return _" + name + "(luan,args); " + +"} " + +"private Object _" + name + "(LuanState luan,Object[] args) throws LuanException { " + ); + } + exp.add( "" +"Object t; " +"Object[] a; " );