comparison core/src/luan/impl/ReturnStmt.java @ 615:abc3198c86dd

fix tail recursion bug; add Hosting.exists();
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 17 Dec 2015 01:53:48 -0700
parents 4723d22062ce
children 859c0dedc8b6
comparison
equal deleted inserted replaced
614:9ecf5673fb6d 615:abc3198c86dd
6 import luan.LuanElement; 6 import luan.LuanElement;
7 7
8 8
9 final class ReturnStmt extends CodeImpl implements Stmt { 9 final class ReturnStmt extends CodeImpl implements Stmt {
10 private final Expressions expressions; 10 private final Expressions expressions;
11 private final Expr tailFnExpr;
12 boolean throwReturnException = true; 11 boolean throwReturnException = true;
13 12
14 ReturnStmt(LuanElement el,Expressions expressions) { 13 ReturnStmt(LuanElement el,Expressions expressions) {
15 super(el); 14 super(el);
16 if( expressions instanceof FnCall ) { // tail call 15 this.expressions = expressions;
17 FnCall fnCall = (FnCall)expressions;
18 this.expressions = fnCall.args;
19 this.tailFnExpr = fnCall.fnExpr;
20 } else {
21 this.expressions = expressions;
22 this.tailFnExpr = null;
23 }
24 } 16 }
25 17
26 @Override public void eval(LuanStateImpl luan) throws LuanException { 18 @Override public void eval(LuanStateImpl luan) throws LuanException {
27 luan.returnValues = expressions.eval(luan); 19 if( !(expressions instanceof FnCall) ) {
28 if( tailFnExpr != null ) { 20 luan.returnValues = expressions.eval(luan);
29 LuanElement elTail = tailFnExpr.el(); 21 } else { // tail call
30 luan.push(elTail,elTail.text()); 22 FnCall tailFnCall = (FnCall)expressions;
31 try { 23 LuanFunction tailFn = luan.checkFunction( tailFnCall.fnExpr.eval(luan) );
32 LuanFunction tailFn = luan.checkFunction( tailFnExpr.eval(luan) ); 24 luan.returnValues = tailFnCall.args.eval(luan);
33 if( tailFn instanceof Closure ) { 25 if( tailFn instanceof Closure ) {
34 luan.tailFn = (Closure)tailFn; 26 luan.tailFn = (Closure)tailFn;
35 } else { 27 } else {
28 luan.push(el,tailFnCall.fnName);
29 try {
36 luan.returnValues = tailFn.call(luan,Luan.array(luan.returnValues)); 30 luan.returnValues = tailFn.call(luan,Luan.array(luan.returnValues));
31 } finally {
32 luan.pop();
37 } 33 }
38 } finally {
39 luan.pop();
40 } 34 }
41 } 35 }
42 if( throwReturnException ) 36 if( throwReturnException )
43 throw new ReturnException(); 37 throw new ReturnException();
44 } 38 }