module examples;

behavior Fibonacci {
  int n;

  Fibonacci(int n) { this.n = n;}

  int add(int x, int y) {return x + y;}

  int compute() {
    if (n == 0)  return 0;
    else if (n <= 2)  return 1;
    else {
      Fibonacci fib1 = new Fibonacci(n-1);
      Fibonacci fib2 = new Fibonacci(n-2);
      token x = fib1<-compute();
      token y = fib2<-compute();
      //using name tokens and first-class continuations
      add(x, y) @ currentContinuation;
    }
  }

  void act(String args[]) {
    n = Integer.parseInt(args[0]);
    //using token passing continuations
    compute() @ standardOutput<-println(token);
  }
}
