I've just implemented this:

proc A (x:int) {
  proc B (y:int) {
    println$ "B start";
    return from A;  // <<-------------
    println$ "B again BAD";
  }
  println$ "Here is A";
  B 2;
  println$ "Here is A BAD";
  return;
_endof_A:>
  println$ "Fast exit from A";
}

A 22;

Basically "return from p" terminates the current
procedure and all parents up to the named procedure's
most recent stack frame, and is equivalent to doing a non-local
goto to the end of the procedure p.

In the last commit you have to manually put the sequence

        return;
_endof_X:>

at the end of the procedure you want to return from. I will remove that
requirement shortly.

This features provides a way to shortcut a loop or other algorithm,
when you find a result. Like goto, the target must be visible and
it must be active (or all hell breaks loose).  You can put this instruction
in a closure but the parent must be active when the closure is invoked,
i.e. there has to be an active call chain to pop off the "stack".

I don't think you can goto out of a procedure nested inside a function
nested in a procedure.

I will also do a functional equivalent if I can, it's basically the same
except it returns a value. This would be done by returning a magic
variable after the target label, and assigning it at the return point,
however there's a minor problem with this implementation 
that it technically breaks the rule that functions can't have side effects
by assigning values outside their own scope. At present this isn't
checked by Felix so it will work, but if a check is later added some
special handling may be needed to cope with fast exits.

In both cases, this construction is "better" than the alternatives

(a) throw an exception
 
(b) return a union all the way up the call chain, which must then
      be decoded after every application to detect a fast exit.

Note (b) could never work for procedures since they don't return values.
Generally (b) would be a mess to implement. Exceptions can't be used
for procedures anyhow, since they don't have machine stack frames.

--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to