cprint { sclass VStack implements Steppable { new L stack; O latestResult; void push(Computable computation) { stack.add(computation); } public bool step() { if (empty(stack)) false; O latestResult = this.latestResult; this.latestResult = null; last(stack).step(this, latestResult); true; } // called from computation to return a value void _return(O value default null) { latestResult = value; removeLast(stack); } // called from computation to tail-call another routine void tailCall(Computable computation) { removeLast(stack); stack.add(computation); } } // A is what the function returns asclass Computable { abstract void step(VStack stack, O subComputationResult); } sclass f_ackermann extends Computable { BigInt a, b; int currentStep; // 0 in the beginning, goes up to 1 *(BigInt *a, BigInt *b) {} void step(VStack stack, O subComputationResult) { if (currentStep == 0) { if (a.equals(BigInt.ZERO)) stack._return(plus(b, BigInt.ONE)); else if (b.equals(BigInt.ZERO)) stack.tailCall(new f_ackermann(a.subtract(BigInt.ONE), BigInt.ONE); else { stack.push(new f_ackermann(a, minus(b, BigInt.ONE))); currentStep = 1; } } else if (currentStep == 1) stack.tailCall(new f_ackermann(minus(a, BigInt.ONE), subComputationResult/BigInt)); else fail(); } } start-thread { new VStack stack; stack.push(new f_ackermann(bigInt(2), bigInt(2))); stepAllWithStats(stack); print("Result: " + stack.latestResult); } }