break and continue within try block do not work in reference implementation

46 views
Skip to first unread message

cam

unread,
Apr 11, 2013, 5:19:45 PM4/11/13
to utah-compiler...@googlegroups.com
Just FYI... break and continue within try block do not work in either lir or cps reference implementations.
Examples:

#works with reference implementation
for i in (1, 2, 3, 4):
  print(i)
  break # or continue

print("done")

#does not work with reference implementation
for i in (1, 2, 3, 4, 5):
  print(i)
  try:
    continue # or break
  except:
    print("caught exception")
    break

cam

unread,
Apr 12, 2013, 1:51:05 PM4/12/13
to utah-compiler...@googlegroups.com
continue and break seem to fail inside a try block for the reference lir and cps implementations because break and continue are passed an extra parameter.
Correct me if I am wrong, but to reduce the parameters breaks $old-break (because of call/ec), so it seems best just to emit an extra param for the break/continue lambdas as shown here:
# LIR break inside try (same problem for continue - not shown)
((lambda (continue)
   (begin
     ((lambda (break)
        ...
        (begin
          (break (void))))) ; <= ERROR: arity mismatch - (void) is the offender
     ...
     (lambda () ; break takes no param fix => (lambda (_)
       (begin
         (set! $current-handler
               $old-handler)
         ($old-break (void))))) 
_____________________________________________________________________________________
# CPS break inside try (same problem for continue - not shown)
((lambda (break k1)
   ((lambda (f cc) (f (lambda (x _) (cc x)) cc))
    (lambda ($ec19 k2)
      ...
      ((lambda (k5)
         (break (void) k5)) ; ERROR: arity mismatch - (void) is the offender
       k4)
      (k4 (void)))))
 ...
 (lambda (k3) ; break only takes 1 param, fix => (lambda (_ k3)
   (set-then!
    $current-handler
    $old-handler
    ($old-break (void) k3)))) ; this void param is required, so can't reduce params

cam

unread,
Apr 12, 2013, 2:10:45 PM4/12/13
to utah-compiler...@googlegroups.com
I think this approach still has issues.  Nvmd.

cam

unread,
Apr 12, 2013, 2:25:22 PM4/12/13
to utah-compiler...@googlegroups.com
Actually, the below approach does match python3 output, so I suppose that means it works.

Matt Might

unread,
Apr 15, 2013, 1:22:37 AM4/15/13
to cam, utah-compiler...@googlegroups.com
I fixed this one too now. It was a bug in pydesugar (not in the header files).

Here's what I'd done wrong:

If you look at at hir-header.rkt, I desugared try into call/ec so that
break and continue don't take parameters.

But, the CPS transform of call/ec assumes that exactly one value will
be passed to every continuation, so I modified it so that they take
(and ignore) a parameter.

Then, I forgot to change the *rebindings* of break and continue to
also accept one parameter when I copy/pasted from hir-header.rkt into
the desugarer itself.

Oops.

Since I screwed that up, I'll have to be lenient in grading too. ;)
> --
> You received this message because you are subscribed to the Google Groups
> "Utah Compilers, Spring 2013" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to utah-compilers-spri...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages