//
// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
//
// The contents of this file are covered under the licence agreement in the
// file "LICENCE" distributed with Cforall.
//
// fibonacci_1.cfa -- 1-state finite-state machine: precomputed first two states returning f(n - 1)
//
// Author           : Peter A. Buhr
// Created On       : Thu Apr 26 23:20:08 2018
// Last Modified By : Peter A. Buhr
// Last Modified On : Sat May 18 08:55:59 2019
// Update Count     : 36
// 

#include <fstream.hfa>
#include <coroutine.hfa>

coroutine Fibonacci { int fn1, fn; };					// used for communication

void main( Fibonacci & fib ) with( fib ) {				// called on first resume
	[fn1, fn] = [1, 0];									// precompute first two states
	for () {
		suspend();										// restart last resume
		[fn1, fn] = [fn, fn1 + fn];						// general case
	} // for
}

int ?()( Fibonacci & fib ) with( fib ) {				// function call operator
	return resume( fib ).fn1;							// restart last suspend
}

int main() {
	Fibonacci f1, f2;
	for ( 10 ) {										// print N Fibonacci values
		sout | resume( f1 ).fn | resume( f2 ).fn;
	} // for
}

// Local Variables: //
// tab-width: 4 //
// compile-command: "cfa Fib2.cfa" //
// End: //
