sclass VStack implements Steppable { new L stack; O latestResult; // was last instruction a return? // can be used by computations to switch without a label transient bool justReturned; // the null sentinel replaces a null function result // when stored in latestResult sclass NullSentinel {} static new NullSentinel nullSentinel; *() {} *(Computable computation) { push(computation); } // A is what the function returns interface Computable { public void step(VStack stack, O subComputationResult); } void push(Computable computation) { stack.add(computation); } // perform a computation step. returns false iff done public bool step() { if (empty(stack)) false; justReturned = latestResult != null; O result = latestResult instanceof NullSentinel ? null : latestResult; latestResult = null; last(stack).step(this, result); true; } // called from computation to return a value void _return(O value default null) { latestResult = value == null ? nullSentinel : value; removeLast(stack); } // called from computation to tail-call another routine void tailCall(Computable computation) { removeLast(stack); stack.add(computation); } // all-in-one evaluation function; call on an empty stack A compute(Computable computation) { if (computation == null) null; push(computation); stepAll(this); ret (A) latestResult; } O result() { ret latestResult; } }