KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > standard > callcc


1 package kawa.standard;
2 import kawa.lang.*;
3 import gnu.mapping.*;
4
5 /**
6  * Implement the Scheme standard function "call-with-current-continuation".
7  * This is a restricted version, that only works for escape-like applications.
8  * @author Per Bothner
9  */

10
11 public class callcc extends MethodProc
12 {
13   public static final callcc callcc = new callcc();
14
15   public int numArgs() { return 0x1001; }
16
17   public int match1 (Object JavaDoc proc, CallContext ctx)
18   {
19     if (! (proc instanceof Procedure))
20       return NO_MATCH_BAD_TYPE;
21     return super.match1(proc, ctx);
22   }
23
24   public void apply (CallContext ctx) throws Throwable JavaDoc
25   {
26     Procedure proc = (Procedure) ctx.value1;
27     Continuation cont = new Continuation(ctx);
28     proc.check1(cont, ctx);
29     proc = ctx.proc;
30     ctx.proc = null;
31     try
32       {
33     proc.apply(ctx);
34     ctx.runUntilDone();
35       }
36     catch (CalledContinuation ex)
37       {
38     if (ex.continuation != cont)
39       throw ex;
40     Object JavaDoc[] values = ex.values;
41     int nvalues = values.length;
42     for (int i = 0; i < nvalues; i++)
43       ctx.consumer.writeObject(ex.values[i]);
44       }
45     finally
46       {
47     cont.invoked = true;
48       }
49   }
50
51   /*
52   public void apply (CallContext stack)
53   {
54     kawa.lang.Continuation cont = new Continuation ();
55     cont.frame = stack.proc;
56     cont.pc = stack.pc;
57     stack.value = cont;
58   }
59   */

60 }
61
62 /*
63 class Continuation extends MethodProc
64 {
65   Procedure frame;
66   int pc;
67
68   public void apply (CallContext stack)
69   {
70     Object result = Values.make(stack.args);
71     stack.pc = pc;
72     stack.proc = frame;
73     stack.result = result;
74   }
75 }
76 */

77
Popular Tags