Version française
Home     About     Download     Resources     Contact us    
Browse thread
break and continue for OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: luc.maranget@i...
Subject: Re: [Caml-list] break and continue for OCaml
> I have the first cut of patch to implement break and continue inside
> for and while loops for OCaml.
> 
> http://sparcs.kaist.ac.kr/~tinuviel/devel/ocaml/
> 
> Patch is against OCaml 3.10.2. All the meat is in bytecomp/bytegen.ml.
> Currently bytecode only. I am working on natvie code and error
> handling when break is used outside loop etc but I thought "releasing
> early" could be useful.
> 
> What do you think?


I think that you can implement break/continue without altering
the lambda-code (file bytecomp/lambda.mli) by using the existing
'static exception' mechanism :

...
| Lstaticraise of (int * lambda list)
| Lstaticcatch of lambda * (int * Ident.t list) * lambda
...

with the following pretty printing convention (cf. option -dlambda)

 (exit i) stands for 'Lstaticraise (i,[])'
 (catch e1 with (i) e2) stands for 'Lstaticcatch (e1,(i,[]),e2)'


Then you have for instance
while e1 do e2 done ->

(catch
  (while (e1) (catch  e2 with (icont) ())
with (ibreak) ())

In the loop body e2, break is compiled into (exit ibreak)
and coninue into (exit icont).


It is a bit complicated, but then you get native code compilation for
free.

--
Luc
PS> I make no statement on whether break/continue
    should be added to ocaml!

    But..

      1. Exceptions are already here and they express more flexible
         control flow modifications.

      2. Adding keywords is something that is not easily accepted,
         because it breaks existing code.