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).

1  
sclass VStack implements Steppable {
2  
  new L<Computable> stack;
3  
  bool justReturned;
4  
  O latestResult;
5  
  
6  
  transient bool justReturned_new;
7  
  
8  
  *() {}
9  
  *(Computable computation) { push(computation); }
10  
  
11  
  // A is what the function returns
12  
  interface Computable<A> {
13  
    public void step(VStack stack, O subComputationResult);
14  
  }
15  
  
16  
  void push(Computable computation) {
17  
    stack.add(computation);
18  
  }
19  
  
20  
  // perform a computation step. returns false iff done
21  
  public bool step() {
22  
    if (empty(stack)) false;
23  
    O latestResult = this.latestResult;
24  
    this.latestResult = null;
25  
    last(stack).step(this, latestResult);
26  
    justReturned = justReturned_new;
27  
    justReturned_new = false;
28  
    true;
29  
  }
30  
  
31  
  // called from computation to return a value
32  
  void _return(O value default null) {
33  
    latestResult = value;
34  
    set justReturned_new;
35  
    removeLast(stack);
36  
  }
37  
  
38  
  // called from computation to tail-call another routine
39  
  void tailCall(Computable computation) {
40  
    removeLast(stack);
41  
    stack.add(computation);
42  
  }
43  
  
44  
  <A> A compute(Computable<A> computation) {
45  
    if (computation == null) null;
46  
    push(computation);
47  
    stepAll(this);
48  
    ret (A) latestResult;
49  
  }
50  
  
51  
  O result() {
52  
    ret latestResult;
53  
  }
54  
}

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: 182 / 236
Referenced in: [show references]