/* Copyright (C) 2006 Chris Double.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
 
 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 Based on Scheme code for bshift and breset from:
 http://groups.google.com/group/comp.lang.scheme/msg/9f0d61da01540816
*/
function callcc(func) {
  var k = new Continuation();
  return func(k);
}

/* The function 'f' will be called with 'rc' as
   an argument. 'rc' is the reset marker and 
   identifies which nested 'reset' is used when using
   bshift. */
function breset(f) {
  var result = callcc(function(rcf) {
    var rc = new Array();
    rc[0] = rcf;
    var v = f(rc);
    (rc[0])(v);
  });
  return result;
}

/* 'f' takes argument 'pcc' which is the partial
   continuation and returns a value. */
function bshift(rc, f) {
  var result= callcc(function(k) {
    var v = f(function(v) {
      var oldrc = rc[0];
      var sv = callcc(function(kstar) {
                        rc[0] = kstar;
                        k(v);
                      });
      rc[0] = oldrc;
      return sv;
    });
    (rc[0])(v);
  });
  return result;
}

/* 'range' should be used from within a 'breset'. It executes
   the body of the beset multiple times, replacing the range call
   with each value in the range. */
function range(rc, from, to) {
 return  bshift(rc, function(pcc) {
               while(from <= to) {
                 pcc(from++);
               }
               return undefined;
             });
}


function test1() {
  breset(function(rc) { 
    print(range(rc, 1, 5)); 
    return 0; 
  });
}

function test2() {
  breset(function(rc) {
    print(range(rc, 1, 5) + range(rc, 10, 12)); 
    return 0;
  });
}

/* CLU-like iterator example. 
   See http://www.gnu.org/software/sather/docs-1.2/tutorial/iterators.html
*/
function test3() {
  var sum = 0;
  breset(function(rc) {
    var tmp = range(rc, 1, 10);
    sum += tmp;

  });
  print(sum);
}


