| tom.rmadilo 2008-01-31, 7:49 pm |
|
On Jan 31, 2:12 am, Fredderic <my-name-h...@excite.com> wrote:
>
> For [while] and [for], the deal would be exactly the same, except that
> one extra step is required to return the last iteration result. You'd
> need to grab the result if the body completes naturally, and set it as
> the loops result value after failing the condition (which would have
> just set a new result value). Though there is some case for keeping
> the condition result value instead (as opposed to [if] and (proposed)
> [foreach] which return the result of body evaluation). So for those
> two, it'd probably be best to forget about fiddling with saving the
> body result, and just leave the interpreters result value as-is, just
> as with [foreach]. But that's a separate topic.
I think you're right, it would be easy to test, at least with
[foreach]
Starting at line 1811 of generic/tclCmdAH.c:
result = TclEvalObjEx(interp, bodyPtr, 0, iPtr->cmdFramePtr, objc-1);
if (result != TCL_OK) {
if (result == TCL_CONTINUE) {
result = TCL_OK;
} else if (result == TCL_BREAK) {
result = TCL_OK;
break;
} else if (result == TCL_ERROR) {
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
"\n (\"foreach\" body line %d)",
interp->errorLine));
break;
} else {
break;
}
}
This executes the body code of foreach (in the parent frame) and
either breaks or continues, after resetting the result code to TCL_OK.
Once out of this loop (not shown), the next code nullifies the Tcl
result:
if (result == TCL_OK) {
Tcl_ResetResult(interp);
}
But I think I made a mistake about one thing in a previous post. The
loop code/frame/level doesn't cross the boundary of a proc. If you use
a bare [break] or [continue], you get an error. But, if you use
[return -code break/continue],
you can get out of the current level and then out of the loop.
|