Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

54
LINES

< > BotCompany Repo | #1030382 // VStack - a virtual stack for reified computations [backup before NullSentinel]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2641L/17K).

sclass VStack implements Steppable {
  new L<Computable> stack;
  bool justReturned;
  O latestResult;
  
  transient bool justReturned_new;
  
  *() {}
  *(Computable computation) { push(computation); }
  
  // A is what the function returns
  interface Computable<A> {
    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;
    O latestResult = this.latestResult;
    this.latestResult = null;
    last(stack).step(this, latestResult);
    justReturned = justReturned_new;
    justReturned_new = false;
    true;
  }
  
  // called from computation to return a value
  void _return(O value default null) {
    latestResult = value;
    set justReturned_new;
    removeLast(stack);
  }
  
  // called from computation to tail-call another routine
  void tailCall(Computable computation) {
    removeLast(stack);
    stack.add(computation);
  }
  
  <A> A compute(Computable<A> computation) {
    if (computation == null) null;
    push(computation);
    stepAll(this);
    ret (A) latestResult;
  }
  
  O result() {
    ret latestResult;
  }
}

Author comment

Began life as a copy of #1030376

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030382
Snippet name: VStack - a virtual stack for reified computations [backup before NullSentinel]
Eternal ID of this version: #1030382/1
Text MD5: 14bd2c946c66c946b9288682722b92a6
Transpilation MD5: b4b746da3a2ff9897086a14c2b0de809
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-10 14:46:58
Source code size: 1291 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 104 / 142
Referenced in: [show references]